comparison mod_proxy65/mod_proxy65.lua @ 69:87dfd34dceb2

mod_proxy65: removed unneeded var and session stuff
author Thilo Cestonaro <thilo@cestona.ro>
date Sat, 31 Oct 2009 09:26:38 +0100
parents 0df3e4d1f1a3
children d3afb1403aa7
comparison
equal deleted inserted replaced
68:0df3e4d1f1a3 69:87dfd34dceb2
32 local proxy_port = config_get(host, "core", "proxy65_port") or 5000; 32 local proxy_port = config_get(host, "core", "proxy65_port") or 5000;
33 local proxy_interface = config_get(host, "core", "proxy65_interface") or "*"; 33 local proxy_interface = config_get(host, "core", "proxy65_interface") or "*";
34 local proxy_address = config_get(host, "core", "proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or module.host; 34 local proxy_address = config_get(host, "core", "proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or module.host;
35 35
36 local connlistener = { 36 local connlistener = {
37 registered = false, default_port = proxy_port, 37 default_port = proxy_port,
38 default_interface = proxy_interface, default_mode = "*a" 38 default_interface = proxy_interface, default_mode = "*a"
39 }; 39 };
40 40
41 local function bin2hex(bin) 41 local function bin2hex(bin)
42 return bin:gsub(".", function (c) return ("%02x"):format(c:byte()); end) 42 return bin:gsub(".", function (c) return ("%02x"):format(c:byte()); end)
43 end 43 end
44 44
45 function new_session(conn)
46 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
47 local session = { conn = conn;
48 send = function (t) w(tostring(t)); end;
49 disconnect = function () conn.close(); end;
50 };
51 return session;
52 end
53
54 function connlistener.listener(conn, data) 45 function connlistener.listener(conn, data)
55 local session = sessions[conn]; 46 local session = sessions[conn] or { alreadySeen = false, sha = nil };
56 47
57 if session == nil and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then 48 if session.alreadySeen == false and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then
58 local nmethods = data:sub(2):byte(); 49 local nmethods = data:sub(2):byte();
59 local methods = data:sub(3); 50 local methods = data:sub(3);
60 local supported = false; 51 local supported = false;
61 for i=1, nmethods, 1 do 52 for i=1, nmethods, 1 do
62 if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH 53 if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH
64 break; 55 break;
65 end 56 end
66 end 57 end
67 if(supported) then 58 if(supported) then
68 module:log("debug", "new session found ... ") 59 module:log("debug", "new session found ... ")
69 session = new_session(conn); 60 session.alreadySeen = true;
70 sessions[conn] = session; 61 sessions[conn] = session;
71 session.send(string.char(5, 0)); 62 conn.write(string.char(5, 0));
72 end 63 end
73 return; 64 return;
74 end 65 end
75 if session ~= nil then 66 if session.alreadySeen == true then
76 if session.sha ~= nil and transfers[session.sha] ~= nil then 67 if session.sha ~= nil and transfers[session.sha] ~= nil then
77 local sha = session.sha; 68 local sha = session.sha;
78 if transfers[sha].activated == true and transfers[sha].initiator == conn and transfers[sha].target ~= nil then 69 if transfers[sha].activated == true and transfers[sha].initiator == conn and transfers[sha].target ~= nil then
79 transfers[sha].target.write(data); 70 transfers[sha].target.write(data);
80 return; 71 return;
99 elseif transfers[sha].target ~= nil then 90 elseif transfers[sha].target ~= nil then
100 transfers[sha].initiator = conn; 91 transfers[sha].initiator = conn;
101 session.sha = sha; 92 session.sha = sha;
102 module:log("debug", "initiator connected ... "); 93 module:log("debug", "initiator connected ... ");
103 end 94 end
104 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) 95 conn.write(string.char(5, 0, 0, 3, sha:len()) .. sha .. string.char(0, 0)); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte)
105 end 96 end
106 end 97 end
107 end 98 end
108 99
109 function connlistener.disconnect(conn, err) 100 function connlistener.disconnect(conn, err)