Comparison

mod_proxy65/mod_proxy65.lua @ 70:d3afb1403aa7

mod_proxy65: Remove unused bin2hex function, and compact more code. Finally down <200 lines :)
author Matthew Wild <mwild1@gmail.com>
date Sat, 31 Oct 2009 13:47:34 +0000
parent 69:87dfd34dceb2
child 72:2bf6c7c590a1
comparison
equal deleted inserted replaced
69:87dfd34dceb2 70:d3afb1403aa7
9 end 9 end
10 10
11 11
12 local jid_split = require "util.jid".split; 12 local jid_split = require "util.jid".split;
13 local st = require "util.stanza"; 13 local st = require "util.stanza";
14 local component_register = require "core.componentmanager".register_component; 14 local componentmanager = require "core.componentmanager";
15 local component_deregister = require "core.componentmanager".deregister_component;
16 local configmanager = require "core.configmanager";
17 local config_get = require "core.configmanager".get; 15 local config_get = require "core.configmanager".get;
18 local connlisteners_register = require "net.connlisteners".register; 16 local connlisteners = require "net.connlisteners";
19 local connlisteners_start = require "net.connlisteners".start;
20 local connlisteners_deregister = require "net.connlisteners".deregister;
21 local adns, dns = require "net.adns", require "net.dns"; 17 local adns, dns = require "net.adns", require "net.dns";
22 local add_task = require "util.timer".add_task; 18 local add_task = require "util.timer".add_task;
23 local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3; 19 local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3;
24 local dns_timeout = config.get("*", "core", "dns_timeout") or 60; 20 local dns_timeout = config.get("*", "core", "dns_timeout") or 60;
25 local sha1 = require "util.hashes".sha1; 21 local sha1 = require "util.hashes".sha1;
26 22
27 local replies_cache = {}; 23 local host, name = module:get_host(), "SOCKS5 Bytestreams Service";
28 local host = module:get_host(); 24 local sessions, transfers, component, replies_cache = {}, {}, {}, nil;
29 local name = "SOCKS5 Bytestreams Service";
30 local sessions, transfers, component = {}, {}, nil;
31 25
32 local proxy_port = config_get(host, "core", "proxy65_port") or 5000; 26 local proxy_port = config_get(host, "core", "proxy65_port") or 5000;
33 local proxy_interface = config_get(host, "core", "proxy65_interface") or "*"; 27 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; 28 local proxy_address = config_get(host, "core", "proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or module.host;
35 29
36 local connlistener = { 30 local connlistener = { default_port = proxy_port,
37 default_port = proxy_port, 31 default_interface = proxy_interface,
38 default_interface = proxy_interface, default_mode = "*a" 32 default_mode = "*a" };
39 };
40
41 local function bin2hex(bin)
42 return bin:gsub(".", function (c) return ("%02x"):format(c:byte()); end)
43 end
44 33
45 function connlistener.listener(conn, data) 34 function connlistener.listener(conn, data)
46 local session = sessions[conn] or { alreadySeen = false, sha = nil }; 35 local session = sessions[conn] or {};
47 36
48 if session.alreadySeen == false and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then 37 if session.setup == false and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then
49 local nmethods = data:sub(2):byte(); 38 local nmethods = data:sub(2):byte();
50 local methods = data:sub(3); 39 local methods = data:sub(3);
51 local supported = false; 40 local supported = false;
52 for i=1, nmethods, 1 do 41 for i=1, nmethods, 1 do
53 if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH 42 if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH
55 break; 44 break;
56 end 45 end
57 end 46 end
58 if(supported) then 47 if(supported) then
59 module:log("debug", "new session found ... ") 48 module:log("debug", "new session found ... ")
60 session.alreadySeen = true; 49 session.setup = true;
61 sessions[conn] = session; 50 sessions[conn] = session;
62 conn.write(string.char(5, 0)); 51 conn.write(string.char(5, 0));
63 end 52 end
64 return; 53 return;
65 end 54 end
66 if session.alreadySeen == true then 55 if session.setup then
67 if session.sha ~= nil and transfers[session.sha] ~= nil then 56 if session.sha ~= nil and transfers[session.sha] ~= nil then
68 local sha = session.sha; 57 local sha = session.sha;
69 if transfers[sha].activated == true and transfers[sha].initiator == conn and transfers[sha].target ~= nil then 58 if transfers[sha].activated == true and transfers[sha].initiator == conn and transfers[sha].target ~= nil then
70 transfers[sha].target.write(data); 59 transfers[sha].target.write(data);
71 return; 60 return;
145 reply.tags[1].attr.sid = sid; 134 reply.tags[1].attr.sid = sid;
146 return reply; 135 return reply;
147 end 136 end
148 137
149 module.unload = function() 138 module.unload = function()
150 component_deregister(host); 139 componentmanager.deregister(host);
151 connlisteners_deregister("proxy65"); 140 connlisteners.deregister("proxy65");
152 end 141 end
153 142
154 local function set_activation(stanza) 143 local function set_activation(stanza)
155 local from, to, sid, reply = nil; 144 local from, to, sid, reply = nil;
156 from = stanza.attr.from; 145 from = stanza.attr.from;
200 end 189 end
201 end 190 end
202 return; 191 return;
203 end 192 end
204 193
205 if not connlisteners_register('proxy65', connlistener) then 194 if not connlisteners.register('proxy65', connlistener) then
206 error("mod_proxy65: Could not establish a connection listener. Check your configuration please."); 195 error("mod_proxy65: Could not establish a connection listener. Check your configuration please.");
207 end 196 end
208 197
209 connlisteners_start('proxy65'); 198 connlisteners.start('proxy65');
210 component = component_register(host, handle_to_domain); 199 component = componentmanager.register(host, handle_to_domain);