Comparison

mod_proxy65/mod_proxy65.lua @ 66:b86ae5e21a56

mod_proxy65: done! Who wants to test? :)
author Thilo Cestonaro <thilo@cestona.ro>
date Wed, 28 Oct 2009 23:29:37 +0100
parent 65:a35eb0764ac6
child 68:0df3e4d1f1a3
comparison
equal deleted inserted replaced
65:a35eb0764ac6 66:b86ae5e21a56
55 55
56 return session; 56 return session;
57 end 57 end
58 58
59 function connlistener.listener(conn, data) 59 function connlistener.listener(conn, data)
60 module:log("debug", "listener called....")
61 local session = sessions[conn]; 60 local session = sessions[conn];
62 61
63 if data ~= nil then module:log("debug", bin2hex(data)); end 62 if session == nil and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then
64 if not session and data ~= nil and data:byte() == string.char(5):byte() and data:len() > 2 then
65 local nmethods = data:sub(2):byte(); 63 local nmethods = data:sub(2):byte();
66 local methods = data:sub(3); 64 local methods = data:sub(3);
67 local supported = false; 65 local supported = false;
68 for i=1, nmethods, 1 do 66 for i=1, nmethods, 1 do
69 if(methods:sub(i):byte() == string.char(0):byte()) then 67 if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH
70 supported = true; 68 supported = true;
71 break; 69 break;
72 end 70 end
73 end 71 end
74 if(supported) then 72 if(supported) then
75 module:log("debug", "new session found ... ") 73 module:log("debug", "new session found ... ")
76 session = new_session(conn); 74 session = new_session(conn);
77 sessions[conn] = session; 75 sessions[conn] = session;
78 session.send(string.char(5, 0)); 76 session.send(string.char(5, 0));
79 end 77 end
80 elseif data ~= nil and data:len() > 6 and 78 return;
81 data:sub(1):byte() == string.char(5):byte() and -- SOCKS5 has 5 in first byte 79 end
82 data:sub(2):byte() == string.char(1):byte() and -- CMD must be 1 80 if session ~= nil then
83 data:sub(3):byte() == string.char(0):byte() and -- RSV must be 0 81 if session.sha ~= nil and transfers[session.sha] ~= nil then
84 data:sub(4):byte() == string.char(3):byte() and -- ATYP must be 3 82 local sha = session.sha;
85 data:sub(-2):byte() == string.char(0):byte() and data:sub(-1):byte() == string.char(0):byte() -- PORT must be 0, size 2 byte 83 if transfers[sha].activated == true and transfers[sha].initiator == conn and transfers[sha].target ~= nil then
86 then 84 transfers[sha].target.write(data);
87 local sha = data:sub(6, data:len() - 2); 85 return;
88 module:log("debug", "gotten sha: >%s<", sha); 86 end
89 if transfers[sha] == nil then 87 end
90 transfers[sha] = {}; 88 if data ~= nil and data:len() == 0x2F and -- 40 == length of SHA1 HASH, and 7 other bytes => 47 => 0x2F
91 transfers[sha].target = conn; 89 data:sub(1):byte() == 0x05 and -- SOCKS5 has 5 in first byte
92 module:log("debug", "target connected ... "); 90 data:sub(2):byte() == 0x01 and -- CMD must be 1
93 elseif transfers[sha].target ~= nil then 91 data:sub(3):byte() == 0x00 and -- RSV must be 0
94 transfers[sha].initiator = conn; 92 data:sub(4):byte() == 0x03 and -- ATYP must be 3
95 module:log("debug", "initiator connected ... "); 93 data:sub(5):byte() == 40 and -- SHA1 HASH length must be 64 (0x40)
96 end 94 data:sub(-2):byte() == 0x00 and -- PORT must be 0, size 2 byte
97 session.send(string.char(5, 0, 0, 3, 40) .. sha .. string.char(0, 0)); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte) 95 data:sub(-1):byte() == 0x00
96 then
97 local sha = data:sub(6, 45); -- second param is not count! it's the ending index (included!)
98 if transfers[sha] == nil then
99 transfers[sha] = {};
100 transfers[sha].activated = false;
101 transfers[sha].target = conn;
102 session.sha = sha;
103 module:log("debug", "target connected ... ");
104 elseif transfers[sha].target ~= nil then
105 transfers[sha].initiator = conn;
106 session.sha = sha;
107 module:log("debug", "initiator connected ... ");
108 end
109 session.send(string.char(5, 0, 0, 3, sha:len()) .. sha .. string.char(0, 0)); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte)
110 end
98 end 111 end
99 end 112 end
100 113
101 function connlistener.disconnect(conn, err) 114 function connlistener.disconnect(conn, err)
102 115
184 connlistener.registered = connlisteners_register('proxy65', connlistener); 197 connlistener.registered = connlisteners_register('proxy65', connlistener);
185 if(connlistener.registered == false) then 198 if(connlistener.registered == false) then
186 error("Proxy65: Could not establish a connection listener. Check your configuration please."); 199 error("Proxy65: Could not establish a connection listener. Check your configuration please.");
187 else 200 else
188 connlistener.handler = connlisteners_start('proxy65'); 201 connlistener.handler = connlisteners_start('proxy65');
189 module:log("debug", "Connection listener registered ... ")
190 module:add_item("proxy65", {jid=_host, name=_name}) 202 module:add_item("proxy65", {jid=_host, name=_name})
191 component = component_register(_host, function(origin, stanza) 203 component = component_register(_host, function(origin, stanza)
192 local to_node, to_host, to_resource = jid_split(stanza.attr.to); 204 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
193 if to_node == nil then 205 if to_node == nil then
194 local type = stanza.attr.type; 206 local type = stanza.attr.type;
206 return true; 218 return true;
207 end 219 end
208 elseif stanza.name == "iq" and type == "set" then 220 elseif stanza.name == "iq" and type == "set" then
209 local reply, from, to, sid = set_activation(stanza); 221 local reply, from, to, sid = set_activation(stanza);
210 if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then 222 if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then
211 module:log("debug", "need to build sha1 of data: from: %s, to: %s, sid: %s", from, to, sid);
212 local sha = sha1(sid .. from .. to, true); 223 local sha = sha1(sid .. from .. to, true);
213 module:log("debug", "generated sha: %s", sha); 224 if transfers[sha] == nil then
214 if(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then 225 module:log("error", "transfers[sha]: nil");
226 elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then
215 origin.send(reply); 227 origin.send(reply);
216 forward(transfers[sha].initiator, transfers[sha].target); 228 transfers[sha].activated = true;
217 transfers[sha] = nil;
218 end 229 end
219 end 230 end
220 end 231 end
221 end 232 end
222 return; 233 return;