--- a/mod_onions/mod_onions.lua Tue Jan 20 11:02:14 2015 +0000
+++ b/mod_onions/mod_onions.lua Sun Jan 25 13:04:02 2015 +0100
@@ -1,15 +1,17 @@
-local wrapclient = require "net.server".wrapclient;
+local addclient = require "net.server".addclient;
local s2s_new_outgoing = require "core.s2smanager".new_outgoing;
local initialize_filters = require "util.filters".initialize;
local st = require "util.stanza";
+local log = module._log;
+local core_process_stanza = prosody.core_process_stanza;
+local hosts = prosody.hosts;
local portmanager = require "core.portmanager";
local softreq = require "util.dependencies".softreq;
-local bit;
-pcall(function() bit = require"bit"; end);
-bit = bit or softreq"bit32"
+local socket = require"socket";
+local bit = softreq"bit" or softreq"bit32"
if not bit then module:log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end
local band = bit.band;
@@ -59,14 +61,11 @@
end
-- this means the server tells us to connect on an IPv4 address
- local ip1 = byte(data, 5);
- local ip2 = byte(data, 6);
- local ip3 = byte(data, 7);
- local ip4 = byte(data, 8);
+ local ip = ("%d.%d.%d.%d"):format(byte(data, 5, 8));
local port = band(byte(data, 9), lshift(byte(data, 10), 8));
- module:log("debug", "Should connect to: "..ip1.."."..ip2.."."..ip3.."."..ip4..":"..port);
+ module:log("debug", "Should connect to: %s:%d", ip, port);
- if not (ip1 == 0 and ip2 == 0 and ip3 == 0 and ip4 == 0 and port == 0) then
+ if not (ip == "0.0.0.0" and port == 0) then
module:log("debug", "The SOCKS5 proxy tells us to connect to a different IP, don't know how. :(");
session:close(false);
return;
@@ -94,7 +93,7 @@
if t then
t = filter("bytes/out", tostring(t));
if t then
- return conn:write(tostring(t));
+ return w(conn, t);
end
end
end
@@ -138,7 +137,7 @@
module:log("debug", "Sending connect message.");
-- version 5, connect, (reserved), type: domainname, (length, hostname), port
- conn:write(c(5) .. c(1) .. c(0) .. c(3) .. c(#session.socks5_to) .. session.socks5_to);
+ conn:write("\5\1\0\3" .. c(#session.socks5_to) .. session.socks5_to);
conn:write(c(rshift(session.socks5_port, 8)) .. c(band(session.socks5_port, 0xff)));
session.socks5_handler = socks5_connect_sent;
@@ -148,7 +147,7 @@
module:log("debug", "Connected to SOCKS5 proxy, sending SOCKS5 handshake.");
-- Socks version 5, 1 method, no auth
- conn:write(c(5) .. c(1) .. c(0));
+ conn:write("\5\1\0");
sessions[conn].socks5_handler = socks5_handshake_sent;
end
@@ -176,19 +175,13 @@
local function connect_socks5(host_session, connect_host, connect_port)
- local conn, handler = socket.tcp();
-
module:log("debug", "Connecting to " .. connect_host .. ":" .. connect_port);
-- this is not necessarily the same as .to_host (it can be that this is a SRV record)
host_session.socks5_to = connect_host;
host_session.socks5_port = connect_port;
- conn:settimeout(0);
-
- local success, err = conn:connect(proxy_ip, proxy_port);
-
- conn = wrapclient(conn, connect_host, connect_port, socks5listener, "*a");
+ local conn, err = addclient( proxy_ip, proxy_port, socks5listener, '*a', nil );
socks5listener.register_outgoing(conn, host_session);
@@ -229,7 +222,7 @@
if not event.to_host:find(".onion(.?)$") then
if forbid_else then
- module:log("debug", event.to_host .. " is not an onion. Blocking it.");
+ module:log("debug", event.to_host .. " is not an onion. Blocking it.");
return false;
elseif not torify_all then
return;
@@ -256,4 +249,4 @@
module:log("debug", "Onions ready and loaded");
-hosts[module.host].events.add_handler("route/remote", route_to_onion, 200);
+module:hook("route/remote", route_to_onion, 200);