mod_bosh: Don't log response XML
[prosody.git] / plugins / mod_proxy65.lua
1 -- Copyright (C) 2009 Thilo Cestonaro
2 -- 
3 -- This project is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
5 --
6 --[[
7 * to restart the proxy in the console: e.g.
8 module:unload("proxy65");
9 > server.removeserver(<proxy65_port>);
10 module:load("proxy65", <proxy65_jid>);
11 ]]--
12
13 if module:get_host_type() ~= "component" then
14         error("proxy65 should be loaded as a component, please see http://prosody.im/doc/components", 0);
15 end
16
17 local jid_split, jid_join = require "util.jid".split, require "util.jid".join;
18 local st = require "util.stanza";
19 local componentmanager = require "core.componentmanager";
20 local config_get = require "core.configmanager".get;
21 local connlisteners = require "net.connlisteners";
22 local sha1 = require "util.hashes".sha1;
23
24 local host, name = module:get_host(), "SOCKS5 Bytestreams Service";
25 local sessions, transfers, component, replies_cache = {}, {}, nil, {};
26
27 local proxy_port = config_get(host, "core", "proxy65_port") or 5000;
28 local proxy_interface = config_get(host, "core", "proxy65_interface") or "*";
29 local proxy_address = config_get(host, "core", "proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or host;
30 local proxy_acl = config_get(host, "core", "proxy65_acl");
31
32 local connlistener = { default_port = proxy_port, default_interface = proxy_interface, default_mode = "*a" };
33
34 function connlistener.onincoming(conn, data)
35         local session = sessions[conn] or {};
36         
37         if session.setup == nil and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then
38                 local nmethods = data:sub(2):byte();
39                 local methods = data:sub(3);
40                 local supported = false;
41                 for i=1, nmethods, 1 do
42                         if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH
43                                 supported = true;
44                                 break;
45                         end
46                 end
47                 if(supported) then
48                         module:log("debug", "new session found ... ")
49                         session.setup = true;
50                         sessions[conn] = session;
51                         conn:write(string.char(5, 0));
52                 end
53                 return;
54         end
55         if session.setup then
56                 if session.sha ~= nil and transfers[session.sha] ~= nil then
57                         local sha = session.sha;
58                         if transfers[sha].activated == true and transfers[sha].initiator == conn and transfers[sha].target ~= nil then
59                                 transfers[sha].target:write(data);
60                                 return;
61                         end
62                 end
63                 if data ~= nil and data:len() == 0x2F and  -- 40 == length of SHA1 HASH, and 7 other bytes => 47 => 0x2F
64                         data:sub(1):byte() == 0x05 and -- SOCKS5 has 5 in first byte
65                         data:sub(2):byte() == 0x01 and -- CMD must be 1
66                         data:sub(3):byte() == 0x00 and -- RSV must be 0
67                         data:sub(4):byte() == 0x03 and -- ATYP must be 3
68                         data:sub(5):byte() == 40 and -- SHA1 HASH length must be 40 (0x28)
69                         data:sub(-2):byte() == 0x00 and -- PORT must be 0, size 2 byte
70                         data:sub(-1):byte() == 0x00
71                 then
72                         local sha = data:sub(6, 45); -- second param is not count! it's the ending index (included!)
73                         if transfers[sha] == nil then
74                                 transfers[sha] = {};
75                                 transfers[sha].activated = false;
76                                 transfers[sha].target = conn;
77                                 session.sha = sha;
78                                 module:log("debug", "target connected ... ");
79                         elseif transfers[sha].target ~= nil then
80                                 transfers[sha].initiator = conn;
81                                 session.sha = sha;
82                                 module:log("debug", "initiator connected ... ");
83                         end
84                         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)
85                 else
86                         module:log("warn", "Neither data transfer nor initial connect of a participator of a transfer.")
87                         conn.close();
88                 end
89         else
90                 if data ~= nil then
91                         module:log("warn", "unknown connection with no authentication data -> closing it");
92                         conn.close();
93                 end
94         end
95 end
96
97 function connlistener.ondisconnect(conn, err)
98         local session = sessions[conn];
99         if session then
100                 if session.sha and transfers[session.sha] then
101                         local initiator, target = transfers[session.sha].initiator, transfers[session.sha].target;
102                         if initiator == conn and target ~= nil then
103                                 target.close();
104                         elseif target == conn and initiator ~= nil then
105                                 initiator.close();
106                         end
107                         transfers[session.sha] = nil;
108                 end
109                 -- Clean up any session-related stuff here
110                 sessions[conn] = nil;
111         end
112 end
113
114 local function get_disco_info(stanza)
115         local reply = replies_cache.disco_info;
116         if reply == nil then
117                 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#info")
118                         :tag("identity", {category='proxy', type='bytestreams', name=name}):up()
119                         :tag("feature", {var="http://jabber.org/protocol/bytestreams"});
120                 replies_cache.disco_info = reply;
121         end
122
123         reply.attr.id = stanza.attr.id;
124         reply.attr.to = stanza.attr.from;
125         return reply;
126 end
127
128 local function get_disco_items(stanza)
129         local reply = replies_cache.disco_items;
130         if reply == nil then
131                 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#items");
132                 replies_cache.disco_items = reply;
133         end
134         
135         reply.attr.id = stanza.attr.id;
136         reply.attr.to = stanza.attr.from;
137         return reply;
138 end
139
140 local function get_stream_host(origin, stanza)
141         local reply = replies_cache.stream_host;
142         local err_reply = replies_cache.stream_host_err;
143         local sid = stanza.tags[1].attr.sid;
144         local allow = false;
145         local jid_node, jid_host, jid_resource = jid_split(stanza.attr.from);
146         
147         if stanza.attr.from == nil then
148                 jid_node = origin.username;
149                 jid_host = origin.host;
150                 jid_resource = origin.resource;
151         end
152         
153         if proxy_acl and #proxy_acl > 0 then
154                 if host ~= nil then -- at least a domain is needed.
155                         for _, acl in ipairs(proxy_acl) do
156                                 local acl_node, acl_host, acl_resource = jid_split(acl);
157                                 if ((acl_node ~= nil and acl_node == jid_node) or acl_node == nil) and
158                                    ((acl_host ~= nil and acl_host == jid_host) or acl_host == nil) and
159                                    ((acl_resource ~= nil and acl_resource == jid_resource) or acl_resource == nil) then
160                                         allow = true;
161                                 end
162                         end
163                 end
164         else
165                 allow = true;
166         end
167         if allow == true then
168                 if reply == nil then
169                         reply = st.iq({type="result", from=host})
170                                 :query("http://jabber.org/protocol/bytestreams")
171                                 :tag("streamhost", {jid=host, host=proxy_address, port=proxy_port});
172                         replies_cache.stream_host = reply;
173                 end
174         else
175                 module:log("warn", "Denying use of proxy for %s", tostring(jid_join(jid_node, jid_host, jid_resource)));
176                 if err_reply == nil then
177                         err_reply = st.iq({type="error", from=host})
178                                 :query("http://jabber.org/protocol/bytestreams")
179                                 :tag("error", {code='403', type='auth'})
180                                 :tag("forbidden", {xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'});
181                         replies_cache.stream_host_err = err_reply;
182                 end
183                 reply = err_reply;
184         end
185         reply.attr.id = stanza.attr.id;
186         reply.attr.to = stanza.attr.from;
187         reply.tags[1].attr.sid = sid;
188         return reply;
189 end
190
191 module.unload = function()
192         componentmanager.deregister_component(host);
193         connlisteners.deregister(module.host .. ':proxy65');
194 end
195
196 local function set_activation(stanza)
197         local from, to, sid, reply = nil;
198         from = stanza.attr.from;
199         if stanza.tags[1] ~= nil and tostring(stanza.tags[1].name) == "query" then
200                 if stanza.tags[1].attr ~= nil then
201                         sid = stanza.tags[1].attr.sid;
202                 end
203                 if stanza.tags[1].tags[1] ~= nil and tostring(stanza.tags[1].tags[1].name) == "activate" then
204                         to = stanza.tags[1].tags[1][1];
205                 end
206         end
207         if from ~= nil and to ~= nil and sid ~= nil then
208                 reply = st.iq({type="result", from=host, to=from});
209                 reply.attr.id = stanza.attr.id;
210         end
211         return reply, from, to, sid;
212 end
213
214 function handle_to_domain(origin, stanza)
215         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
216         if to_node == nil then
217                 local type = stanza.attr.type;
218                 if type == "error" or type == "result" then return; end
219                 if stanza.name == "iq" and type == "get" then
220                         local xmlns = stanza.tags[1].attr.xmlns
221                         if xmlns == "http://jabber.org/protocol/disco#info" then
222                                 origin.send(get_disco_info(stanza));
223                                 return true;
224                         elseif xmlns == "http://jabber.org/protocol/disco#items" then
225                                 origin.send(get_disco_items(stanza));
226                                 return true;
227                         elseif xmlns == "http://jabber.org/protocol/bytestreams" then
228                                 origin.send(get_stream_host(origin, stanza));
229                                 return true;
230                         end
231                 elseif stanza.name == "iq" and type == "set" then
232                         local reply, from, to, sid = set_activation(stanza);
233                         if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then
234                                 local sha = sha1(sid .. from .. to, true);
235                                 if transfers[sha] == nil then
236                                         module:log("error", "transfers[sha]: nil");
237                                 elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then
238                                         origin.send(reply);
239                                         transfers[sha].activated = true;
240                                 end
241                         else
242                                 module:log("error", "activation failed: sid: %s, initiator: %s, target: %s", tostring(sid), tostring(from), tostring(to));
243                         end
244                 end
245         end
246         return;
247 end
248
249 if not connlisteners.register(module.host .. ':proxy65', connlistener) then
250         error("mod_proxy65: Could not establish a connection listener. Check your configuration please.");
251         error(" one possible cause for this would be that two proxy65 components share the same port.");
252 end
253
254 connlisteners.start(module.host .. ':proxy65');
255 component = componentmanager.register_component(host, handle_to_domain);