Annotate

mod_proxy65/proxy65/mod_proxy65.lua @ 63:d0f5a16e7a66

mod_proxy65: create connection listener, interface a. port are configureable but not needed
author Thilo Cestonaro <thilo@cestona.ro>
date Mon, 26 Oct 2009 23:25:29 +0100
parent 31:a0dfa3e5883c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
1 -- Copyright (C) 2009 Thilo Cestonaro
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
2 --
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
3 -- This project is MIT/X11 licensed. Please see the
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
5 --
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
6
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
7 if module:get_host_type() ~= "component" then
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
8 error("proxy65 should be loaded as a component, please see http://prosody.im/doc/components", 0);
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
9 end
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
10
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
11
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
12 local jid_split = require "util.jid".split;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
13 local st = require "util.stanza";
63
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
14 local component_register = require "core.componentmanager".register_component;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
15 local component_deregister = require "core.componentmanager".deregister_component;
31
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
16 local configmanager = require "core.configmanager";
63
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
17 local config_get = require "core.configmanager".get;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
18 local connlisteners_register = require "net.connlisteners".register;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
19 local connlisteners_deregister = require "net.connlisteners".deregister;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
20 local adns, dns = require "net.adns", require "net.dns";
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
21 local add_task = require "util.timer".add_task;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
22 local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
23 local dns_timeout = config.get("*", "core", "dns_timeout") or 60;
31
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
24
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
25 local replies_cache = {};
63
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
26 local _host = module:get_host();
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
27 local _name = "SOCKS5 Bytestreams Service";
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
28 local _config = config_get(_host, "core", "proxy65");
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
29 local connlistener = {registered=false};
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
30 local sessions = {};
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
31 local component;
31
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
32
63
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
33 if _config == nil then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
34 _config = {};
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
35 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
36 if _config.port == nil then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
37 _config.port = 5000;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
38 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
39
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
40 local function register()
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
41 connlistener = { default_port = _config.port; default_interface = _config.interface };
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
42 connlistener.registered = connlisteners_register('proxy65', connlistener);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
43 if(connlistener.registered == false) then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
44 error("Proxy65: Could not establish a connection listener. Check your configuration please.");
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
45 else
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
46 module:add_item("proxy65", {jid=_host, name=_name})
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
47 component = component_register(_host, function(origin, stanza)
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
48 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
49 if to_node == nil then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
50 local type = stanza.attr.type;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
51 if type == "error" or type == "result" then return; end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
52 if stanza.name == "iq" and type == "get" then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
53 local xmlns = stanza.tags[1].attr.xmlns
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
54 if xmlns == "http://jabber.org/protocol/disco#info" then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
55 origin.send(get_disco_info(stanza));
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
56 return true;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
57 elseif xmlns == "http://jabber.org/protocol/disco#items" then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
58 origin.send(get_disco_items(stanza));
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
59 return true;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
60 elseif xmlns == "http://jabber.org/protocol/bytestreams" and stanza.tags[1].attr.sid ~= nil then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
61 origin.send(get_stream_host(stanza));
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
62 return true;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
63 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
64 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
65 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
66 return;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
67 end);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
68 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
69 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
70
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
71 local function getDefaultIP(host)
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
72 local handle;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
73 handle = adns.lookup(function (reply)
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
74 handle = nil;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
75
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
76 -- COMPAT: This is a compromise for all you CNAME-(ab)users :)
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
77 if not (reply and reply[#reply] and reply[#reply].a) then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
78 local count = max_dns_depth;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
79 reply = dns.peek(host, "CNAME", "IN");
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
80 while count > 0 and reply and reply[#reply] and not reply[#reply].a and reply[#reply].cname do
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
81 module:log("debug", "Looking up %s (DNS depth is %d)", tostring(reply[#reply].cname), count);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
82 reply = dns.peek(reply[#reply].cname, "A", "IN") or dns.peek(reply[#reply].cname, "CNAME", "IN");
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
83 count = count - 1;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
84 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
85 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
86 -- end of CNAME resolving
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
87
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
88 if reply and reply[#reply] and reply[#reply].a then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
89 module:log("debug", "DNS reply for %s gives us %s", host, reply[#reply].a);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
90 _config.interface = reply[#reply].a
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
91 return register();
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
92 else
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
93 module:log("debug", "DNS lookup failed to get a response for %s", host);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
94 if host:find(".") ~= nil then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
95 host = host:gsub("^[^%.]*%.", "");
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
96 if host:find(".") ~= nil then -- still one dot left?
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
97 return getDefaultIP(host);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
98 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
99 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
100 error("Proxy65: Could not get an interface to bind to. Please configure one.");
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
101 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
102 end, host, "A", "IN");
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
103
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
104 -- Set handler for DNS timeout
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
105 add_task(dns_timeout, function ()
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
106 if handle then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
107 adns.cancel(handle, true);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
108 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
109 end);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
110 return true;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
111 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
112
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
113 if _config.interface ~= nil then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
114 register();
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
115 else
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
116 getDefaultIP(_host); -- try to DNS lookup module:host()
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
117 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
118
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
119 function new_session(conn)
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
120 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
121 local session = { conn = conn;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
122 send = function (t) w(tostring(t)); end;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
123 print = function (t) w("| "..tostring(t).."\n"); end;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
124 disconnect = function () conn.close(); end;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
125 };
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
126
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
127 return session;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
128 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
129
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
130 function connlistener.listener(conn, data)
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
131 local session = sessions[conn];
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
132
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
133 if not session then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
134 session = new_session(conn);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
135 sessions[conn] = session;
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
136 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
137 if data then
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
138 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
139 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
140
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
141 function connlistener.disconnect(conn, err)
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
142
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
143 end
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
144
31
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
145 local function get_disco_info(stanza)
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
146 local reply = replies_cache.disco_info;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
147 if reply == nil then
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
148 reply = st.iq({type='result', from=_host}):query("http://jabber.org/protocol/disco#info")
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
149 :tag("identity", {category='proxy', type='bytestreams', name=_name}):up()
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
150 :tag("feature", {var="http://jabber.org/protocol/bytestreams"});
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
151 replies_cache.disco_info = reply;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
152 end
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
153
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
154 reply.attr.id = stanza.attr.id;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
155 reply.attr.to = stanza.attr.from;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
156 return reply;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
157 end
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
158
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
159 local function get_disco_items(stanza)
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
160 local reply = replies_cache.disco_items;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
161 if reply == nil then
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
162 reply = st.iq({type='result', from=_host}):query("http://jabber.org/protocol/disco#items");
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
163 replies_cache.disco_info = reply;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
164 end
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
165
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
166 reply.attr.id = stanza.attr.id;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
167 reply.attr.to = stanza.attr.from;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
168 return reply;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
169 end
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
170
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
171 local function get_stream_host(stanza)
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
172 local reply = replies_cache.stream_host;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
173 if reply == nil then
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
174 reply = st.iq({type="result", from=_host})
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
175 :query("http://jabber.org/protocol/bytestreams")
63
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
176 :tag("streamhost", {jid=_host, host=_config.interface, port=_config.port}); -- TODO get the correct data
31
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
177 replies_cache.stream_host = reply;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
178 end
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
179
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
180 reply.attr.id = stanza.attr.id;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
181 reply.attr.to = stanza.attr.from;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
182 reply.tags[1].attr.sid = stanza.tags[1].attr.sid;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
183 return reply;
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
184 end
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
185
a0dfa3e5883c mod_proxy65: new component proxy65; currently only disco#info and disco#items done
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
186 module.unload = function()
63
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
187 component_deregister(_host);
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
188 connlisteners_deregister("proxy65");
d0f5a16e7a66 mod_proxy65: create connection listener, interface a. port are configureable but not needed
Thilo Cestonaro <thilo@cestona.ro>
parents: 31
diff changeset
189 end