Software /
code /
prosody-modules
Annotate
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 |
rev | line source |
---|---|
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
1 -- Copyright (C) 2009 Thilo Cestonaro |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
2 -- |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
3 -- This project is MIT/X11 licensed. Please see the |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
4 -- COPYING file in the source package for more information. |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
5 -- |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
6 |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
7 if module:get_host_type() ~= "component" then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
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); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
9 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
10 |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
11 |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
12 local jid_split = require "util.jid".split; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
13 local st = require "util.stanza"; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
14 local component_register = require "core.componentmanager".register_component; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
15 local component_deregister = require "core.componentmanager".deregister_component; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
16 local configmanager = require "core.configmanager"; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
17 local config_get = require "core.configmanager".get; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
18 local connlisteners_register = require "net.connlisteners".register; |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
19 local connlisteners_start = require "net.connlisteners".start; |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
20 local connlisteners_deregister = require "net.connlisteners".deregister; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
21 local adns, dns = require "net.adns", require "net.dns"; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
22 local add_task = require "util.timer".add_task; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
23 local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
24 local dns_timeout = config.get("*", "core", "dns_timeout") or 60; |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
25 local serialize = require "util.serialization".serialize; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
26 local sha1 = require "util.hashes".sha1; |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
27 |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
28 local replies_cache = {}; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
29 local _host = module:get_host(); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
30 local _name = "SOCKS5 Bytestreams Service"; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
31 local connlistener = {registered=false}; |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
32 local _config = {}; |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
33 local sessions = {}; |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
34 local transfers = {}; |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
35 local component; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
36 |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
37 _config.port = config_get(_host, "core", "port"); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
38 _config.interface = config_get(_host, "core", "interface"); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
39 |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
40 if _config.port == nil then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
41 _config.port = 5000; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
42 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
43 |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
44 local function bin2hex(bin) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
45 return bin:gsub(".", function (c) return ("%02x"):format(c:byte()); end) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
46 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
47 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
48 function new_session(conn) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
49 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
50 local session = { conn = conn; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
51 send = function (t) w(tostring(t)); end; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
52 print = function (t) w("| "..tostring(t).."\n"); end; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
53 disconnect = function () conn.close(); end; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
54 }; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
55 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
56 return session; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
57 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
58 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
59 function connlistener.listener(conn, data) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
60 local session = sessions[conn]; |
66
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
61 |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
62 if session == nil and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
63 local nmethods = data:sub(2):byte(); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
64 local methods = data:sub(3); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
65 local supported = false; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
66 for i=1, nmethods, 1 do |
66
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
67 if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
68 supported = true; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
69 break; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
70 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
71 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
72 if(supported) then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
73 module:log("debug", "new session found ... ") |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
74 session = new_session(conn); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
75 sessions[conn] = session; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
76 session.send(string.char(5, 0)); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
77 end |
66
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
78 return; |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
79 end |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
80 if session ~= nil then |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
81 if session.sha ~= nil and transfers[session.sha] ~= nil then |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
82 local sha = session.sha; |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
83 if transfers[sha].activated == true and transfers[sha].initiator == conn and transfers[sha].target ~= nil then |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
84 transfers[sha].target.write(data); |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
85 return; |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
86 end |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
87 end |
66
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
88 if data ~= nil and data:len() == 0x2F and -- 40 == length of SHA1 HASH, and 7 other bytes => 47 => 0x2F |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
89 data:sub(1):byte() == 0x05 and -- SOCKS5 has 5 in first byte |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
90 data:sub(2):byte() == 0x01 and -- CMD must be 1 |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
91 data:sub(3):byte() == 0x00 and -- RSV must be 0 |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
92 data:sub(4):byte() == 0x03 and -- ATYP must be 3 |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
93 data:sub(5):byte() == 40 and -- SHA1 HASH length must be 64 (0x40) |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
94 data:sub(-2):byte() == 0x00 and -- PORT must be 0, size 2 byte |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
95 data:sub(-1):byte() == 0x00 |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
96 then |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
97 local sha = data:sub(6, 45); -- second param is not count! it's the ending index (included!) |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
98 if transfers[sha] == nil then |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
99 transfers[sha] = {}; |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
100 transfers[sha].activated = false; |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
101 transfers[sha].target = conn; |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
102 session.sha = sha; |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
103 module:log("debug", "target connected ... "); |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
104 elseif transfers[sha].target ~= nil then |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
105 transfers[sha].initiator = conn; |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
106 session.sha = sha; |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
107 module:log("debug", "initiator connected ... "); |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
108 end |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
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) |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
110 end |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
111 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
112 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
113 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
114 function connlistener.disconnect(conn, err) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
115 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
116 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
117 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
118 local function get_disco_info(stanza) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
119 local reply = replies_cache.disco_info; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
120 if reply == nil then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
121 reply = st.iq({type='result', from=_host}):query("http://jabber.org/protocol/disco#info") |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
122 :tag("identity", {category='proxy', type='bytestreams', name=_name}):up() |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
123 :tag("feature", {var="http://jabber.org/protocol/bytestreams"}); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
124 replies_cache.disco_info = reply; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
125 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
126 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
127 reply.attr.id = stanza.attr.id; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
128 reply.attr.to = stanza.attr.from; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
129 return reply; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
130 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
131 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
132 local function get_disco_items(stanza) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
133 local reply = replies_cache.disco_items; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
134 if reply == nil then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
135 reply = st.iq({type='result', from=_host}):query("http://jabber.org/protocol/disco#items"); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
136 replies_cache.disco_info = reply; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
137 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
138 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
139 reply.attr.id = stanza.attr.id; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
140 reply.attr.to = stanza.attr.from; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
141 return reply; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
142 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
143 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
144 local function get_stream_host(stanza) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
145 local reply = replies_cache.stream_host; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
146 local sid = stanza.tags[1].attr.sid; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
147 if reply == nil then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
148 reply = st.iq({type="result", from=_host}) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
149 :query("http://jabber.org/protocol/bytestreams") |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
150 :tag("streamhost", {jid=_host, host=_config.interface, port=_config.port}); -- TODO get the correct data |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
151 replies_cache.stream_host = reply; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
152 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
153 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
154 reply.attr.id = stanza.attr.id; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
155 reply.attr.to = stanza.attr.from; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
156 reply.tags[1].attr.sid = sid; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
157 return reply; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
158 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
159 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
160 module.unload = function() |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
161 component_deregister(_host); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
162 connlisteners_deregister("proxy65"); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
163 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
164 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
165 local function set_activation(stanza) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
166 local from = nil; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
167 local to = nil; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
168 local sid = nil; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
169 local reply = nil; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
170 if stanza.attr ~= nil then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
171 from = stanza.attr.from; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
172 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
173 if stanza.tags[1] ~= nil and tostring(stanza.tags[1].name) == "query" then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
174 if stanza.tags[1].attr ~= nil then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
175 sid = stanza.tags[1].attr.sid; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
176 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
177 if stanza.tags[1].tags[1] ~= nil and tostring(stanza.tags[1].tags[1].name) == "activate" then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
178 to = stanza.tags[1].tags[1][1]; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
179 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
180 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
181 if from ~= nil and to ~= nil and sid ~= nil then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
182 reply = st.iq({type="result", from=_host}); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
183 reply.attr.id = stanza.attr.id; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
184 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
185 return reply, from, to, sid; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
186 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
187 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
188 local function forward(initiator, target) |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
189 module:log("debug", "forward it ...."); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
190 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
191 |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
192 |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
193 local function register() |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
194 connlistener.default_port = _config.port; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
195 connlistener.default_interface = "*"; |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
196 connlistener.default_mode = "*a"; |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
197 connlistener.registered = connlisteners_register('proxy65', connlistener); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
198 if(connlistener.registered == false) then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
199 error("Proxy65: Could not establish a connection listener. Check your configuration please."); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
200 else |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
201 connlistener.handler = connlisteners_start('proxy65'); |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
202 module:add_item("proxy65", {jid=_host, name=_name}) |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
203 component = component_register(_host, function(origin, stanza) |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
204 local to_node, to_host, to_resource = jid_split(stanza.attr.to); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
205 if to_node == nil then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
206 local type = stanza.attr.type; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
207 if type == "error" or type == "result" then return; end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
208 if stanza.name == "iq" and type == "get" then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
209 local xmlns = stanza.tags[1].attr.xmlns |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
210 if xmlns == "http://jabber.org/protocol/disco#info" then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
211 origin.send(get_disco_info(stanza)); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
212 return true; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
213 elseif xmlns == "http://jabber.org/protocol/disco#items" then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
214 origin.send(get_disco_items(stanza)); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
215 return true; |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
216 elseif xmlns == "http://jabber.org/protocol/bytestreams" then |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
217 origin.send(get_stream_host(stanza)); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
218 return true; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
219 end |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
220 elseif stanza.name == "iq" and type == "set" then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
221 local reply, from, to, sid = set_activation(stanza); |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
222 if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
223 local sha = sha1(sid .. from .. to, true); |
66
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
224 if transfers[sha] == nil then |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
225 module:log("error", "transfers[sha]: nil"); |
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
226 elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
227 origin.send(reply); |
66
b86ae5e21a56
mod_proxy65: done! Who wants to test? :)
Thilo Cestonaro <thilo@cestona.ro>
parents:
65
diff
changeset
|
228 transfers[sha].activated = true; |
65
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
229 end |
a35eb0764ac6
mod_proxy65: tcp connection of initiator and target are established
Thilo Cestonaro <thilo@cestona.ro>
parents:
64
diff
changeset
|
230 end |
64
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
231 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
232 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
233 return; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
234 end); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
235 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
236 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
237 |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
238 local function getDefaultIP(host) |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
239 local handle; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
240 handle = adns.lookup(function (reply) |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
241 handle = nil; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
242 |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
243 -- COMPAT: This is a compromise for all you CNAME-(ab)users :) |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
244 if not (reply and reply[#reply] and reply[#reply].a) then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
245 local count = max_dns_depth; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
246 reply = dns.peek(host, "CNAME", "IN"); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
247 while count > 0 and reply and reply[#reply] and not reply[#reply].a and reply[#reply].cname do |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
248 module:log("debug", "Looking up %s (DNS depth is %d)", tostring(reply[#reply].cname), count); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
249 reply = dns.peek(reply[#reply].cname, "A", "IN") or dns.peek(reply[#reply].cname, "CNAME", "IN"); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
250 count = count - 1; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
251 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
252 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
253 -- end of CNAME resolving |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
254 |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
255 if reply and reply[#reply] and reply[#reply].a then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
256 module:log("debug", "DNS reply for %s gives us %s", host, reply[#reply].a); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
257 _config.interface = reply[#reply].a |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
258 return register(); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
259 else |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
260 module:log("debug", "DNS lookup failed to get a response for %s", host); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
261 if host:find(".") ~= nil then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
262 host = host:gsub("^[^%.]*%.", ""); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
263 if host:find(".") ~= nil then -- still one dot left? |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
264 return getDefaultIP(host); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
265 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
266 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
267 error("Proxy65: Could not get an interface to bind to. Please configure one."); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
268 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
269 end, host, "A", "IN"); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
270 |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
271 -- Set handler for DNS timeout |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
272 add_task(dns_timeout, function () |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
273 if handle then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
274 adns.cancel(handle, true); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
275 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
276 end); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
277 return true; |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
278 end |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
279 |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
280 if _config.interface ~= nil then |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
281 register(); |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
282 else |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
283 getDefaultIP(_host); -- try to DNS lookup module:host() |
853c3c7e9936
mod_proxy65: remove the proxy65 folder
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff
changeset
|
284 end |