comparison mod_onions/mod_onions.lua @ 1071:8f59b45fe6a7

mod_onions: Copy the code to find the bit module from mod_websockets.
author Thijs Alkemade <me@thijsalkema.de>
date Sat, 15 Jun 2013 12:48:56 +0200
parents f4031e7ccec1
children a7d6c6d2c7b5
comparison
equal deleted inserted replaced
1070:dee31eeae7ae 1071:8f59b45fe6a7
1 local wrapclient = require "net.server".wrapclient; 1 local wrapclient = require "net.server".wrapclient;
2 local s2s_new_outgoing = require "core.s2smanager".new_outgoing; 2 local s2s_new_outgoing = require "core.s2smanager".new_outgoing;
3 local initialize_filters = require "util.filters".initialize; 3 local initialize_filters = require "util.filters".initialize;
4 local bit = require "bit32";
5 local st = require "util.stanza"; 4 local st = require "util.stanza";
5
6 local portmanager = require "core.portmanager"; 6 local portmanager = require "core.portmanager";
7
8 local softreq = require "util.dependencies".softreq;
9
10 local bit;
11 pcall(function() bit = require"bit"; end);
12 bit = bit or softreq"bit32"
13 if not bit then module:log("error", "No bit module found. Either LuaJIT 2, lua-bitop or Lua 5.2 is required"); end
14
15 local band = bit.band;
16 local rshift = bit.rshift;
17 local lshift = bit.lshift;
18
7 local byte = string.byte; 19 local byte = string.byte;
8 local c = string.char; 20 local c = string.char;
9 21
10 local proxy_ip = module:get_option("onions_socks5_host") or "127.0.0.1"; 22 local proxy_ip = module:get_option("onions_socks5_host") or "127.0.0.1";
11 local proxy_port = module:get_option("onions_socks5_port") or 9050; 23 local proxy_port = module:get_option("onions_socks5_port") or 9050;
49 -- this means the server tells us to connect on an IPv4 address 61 -- this means the server tells us to connect on an IPv4 address
50 local ip1 = byte(data, 5); 62 local ip1 = byte(data, 5);
51 local ip2 = byte(data, 6); 63 local ip2 = byte(data, 6);
52 local ip3 = byte(data, 7); 64 local ip3 = byte(data, 7);
53 local ip4 = byte(data, 8); 65 local ip4 = byte(data, 8);
54 local port = bit.band(byte(data, 9), bit.lshift(byte(data, 10), 8)); 66 local port = band(byte(data, 9), lshift(byte(data, 10), 8));
55 module:log("debug", "Should connect to: "..ip1.."."..ip2.."."..ip3.."."..ip4..":"..port); 67 module:log("debug", "Should connect to: "..ip1.."."..ip2.."."..ip3.."."..ip4..":"..port);
56 68
57 if not (ip1 == 0 and ip2 == 0 and ip3 == 0 and ip4 == 0 and port == 0) then 69 if not (ip1 == 0 and ip2 == 0 and ip3 == 0 and ip4 == 0 and port == 0) then
58 module:log("debug", "The SOCKS5 proxy tells us to connect to a different IP, don't know how. :("); 70 module:log("debug", "The SOCKS5 proxy tells us to connect to a different IP, don't know how. :(");
59 session:close(false); 71 session:close(false);
123 135
124 module:log("debug", "Sending connect message."); 136 module:log("debug", "Sending connect message.");
125 137
126 -- version 5, connect, (reserved), type: domainname, (length, hostname), port 138 -- version 5, connect, (reserved), type: domainname, (length, hostname), port
127 conn:send(c(5) .. c(1) .. c(0) .. c(3) .. c(#session.socks5_to) .. session.socks5_to); 139 conn:send(c(5) .. c(1) .. c(0) .. c(3) .. c(#session.socks5_to) .. session.socks5_to);
128 conn:send(c(bit.rshift(session.socks5_port, 8)) .. c(bit.band(session.socks5_port, 0xff))); 140 conn:send(c(rshift(session.socks5_port, 8)) .. c(band(session.socks5_port, 0xff)));
129 141
130 session.socks5_handler = socks5_connect_sent; 142 session.socks5_handler = socks5_connect_sent;
131 end 143 end
132 144
133 function socks5listener.onconnect(conn) 145 function socks5listener.onconnect(conn)