Annotate

plugins/mod_proxy65.lua @ 2751:1d7746c3a8c6

sessionmanager: Return stream error when incoming stream header is missing 'to' attribute
author Matthew Wild <mwild1@gmail.com>
date Sun, 14 Mar 2010 02:59:16 +0000
parent 2729:7e0c35713bf5
child 3004:c20b9fe1624b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Copyright (C) 2009 Thilo Cestonaro
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 --
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- This project is MIT/X11 licensed. Please see the
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 --
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --[[
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 * to restart the proxy in the console: e.g.
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 module:unload("proxy65");
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 > server.removeserver(<proxy65_port>);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 module:load("proxy65", <proxy65_jid>);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 ]]--
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 if module:get_host_type() ~= "component" then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 error("proxy65 should be loaded as a component, please see http://prosody.im/doc/components", 0);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
2249
6e0c861dc61f mod_proxy65: Use new jid.join() from util.jid
Matthew Wild <mwild1@gmail.com>
parents: 2244
diff changeset
17 local jid_split, jid_join = require "util.jid".split, require "util.jid".join;
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local st = require "util.stanza";
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local componentmanager = require "core.componentmanager";
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local config_get = require "core.configmanager".get;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local connlisteners = require "net.connlisteners";
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local sha1 = require "util.hashes".sha1;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local host, name = module:get_host(), "SOCKS5 Bytestreams Service";
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local sessions, transfers, component, replies_cache = {}, {}, nil, {};
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local proxy_port = config_get(host, "core", "proxy65_port") or 5000;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local proxy_interface = config_get(host, "core", "proxy65_interface") or "*";
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local proxy_address = config_get(host, "core", "proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or host;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local proxy_acl = config_get(host, "core", "proxy65_acl");
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local connlistener = { default_port = proxy_port, default_interface = proxy_interface, default_mode = "*a" };
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
2244
730038d3e9e3 mod_proxy65: Update listener callback names for new server API
sjoerd.simons@collabora.co.uk
parents: 2138
diff changeset
34 function connlistener.onincoming(conn, data)
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local session = sessions[conn] or {};
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if session.setup == nil and data ~= nil and data:sub(1):byte() == 0x05 and data:len() > 2 then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local nmethods = data:sub(2):byte();
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local methods = data:sub(3);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local supported = false;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 for i=1, nmethods, 1 do
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if(methods:sub(i):byte() == 0x00) then -- 0x00 == method: NO AUTH
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 supported = true;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 break;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if(supported) then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 module:log("debug", "new session found ... ")
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 session.setup = true;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 sessions[conn] = session;
2138
8bb1a2d82896 mod_proxy65: Update for new net.server API, untested
Matthew Wild <mwild1@gmail.com>
parents: 2137
diff changeset
51 conn:write(string.char(5, 0));
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 if session.setup then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if session.sha ~= nil and transfers[session.sha] ~= nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 local sha = session.sha;
2310
e74c6740a42b mod_proxy65: Make the proxying bidirectional
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2309
diff changeset
58 if transfers[sha].activated == true and transfers[sha].target ~= nil then
e74c6740a42b mod_proxy65: Make the proxying bidirectional
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2309
diff changeset
59 if transfers[sha].initiator == conn then
e74c6740a42b mod_proxy65: Make the proxying bidirectional
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2309
diff changeset
60 transfers[sha].target:write(data);
e74c6740a42b mod_proxy65: Make the proxying bidirectional
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2309
diff changeset
61 else
e74c6740a42b mod_proxy65: Make the proxying bidirectional
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2309
diff changeset
62 transfers[sha].initiator:write(data);
e74c6740a42b mod_proxy65: Make the proxying bidirectional
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2309
diff changeset
63 end
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 return;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if data ~= nil and data:len() == 0x2F and -- 40 == length of SHA1 HASH, and 7 other bytes => 47 => 0x2F
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 data:sub(1):byte() == 0x05 and -- SOCKS5 has 5 in first byte
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 data:sub(2):byte() == 0x01 and -- CMD must be 1
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 data:sub(3):byte() == 0x00 and -- RSV must be 0
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 data:sub(4):byte() == 0x03 and -- ATYP must be 3
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 data:sub(5):byte() == 40 and -- SHA1 HASH length must be 40 (0x28)
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 data:sub(-2):byte() == 0x00 and -- PORT must be 0, size 2 byte
2273
b98b29f614ae mod_proxy65: Remove trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2272
diff changeset
74 data:sub(-1):byte() == 0x00
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local sha = data:sub(6, 45); -- second param is not count! it's the ending index (included!)
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 if transfers[sha] == nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 transfers[sha] = {};
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 transfers[sha].activated = false;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 transfers[sha].target = conn;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 session.sha = sha;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 module:log("debug", "target connected ... ");
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 elseif transfers[sha].target ~= nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 transfers[sha].initiator = conn;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 session.sha = sha;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 module:log("debug", "initiator connected ... ");
2320
2ca7445b882a mod_proxy65: Strip trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2311
diff changeset
87 throttle_sending(conn, transfers[sha].target);
2ca7445b882a mod_proxy65: Strip trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2311
diff changeset
88 throttle_sending(transfers[sha].target, conn);
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
2138
8bb1a2d82896 mod_proxy65: Update for new net.server API, untested
Matthew Wild <mwild1@gmail.com>
parents: 2137
diff changeset
90 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)
2311
5fe837ebe542 mod_proxy65: Don't read data from the connection untill the proxying is activated
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2310
diff changeset
91 conn:lock_read(true)
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 else
2272
9c3564117b24 mod_proxy65: Fix log:module -> module:log :)
Matthew Wild <mwild1@gmail.com>
parents: 2249
diff changeset
93 module:log("warn", "Neither data transfer nor initial connect of a participator of a transfer.")
2729
7e0c35713bf5 mod_proxy65: Fix for old-style closing of connections (another source of tracebacks with libevent enabled)
Matthew Wild <mwild1@gmail.com>
parents: 2320
diff changeset
94 conn:close();
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 else
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 if data ~= nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 module:log("warn", "unknown connection with no authentication data -> closing it");
2729
7e0c35713bf5 mod_proxy65: Fix for old-style closing of connections (another source of tracebacks with libevent enabled)
Matthew Wild <mwild1@gmail.com>
parents: 2320
diff changeset
99 conn:close();
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
2244
730038d3e9e3 mod_proxy65: Update listener callback names for new server API
sjoerd.simons@collabora.co.uk
parents: 2138
diff changeset
104 function connlistener.ondisconnect(conn, err)
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 local session = sessions[conn];
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 if session then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 if session.sha and transfers[session.sha] then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 local initiator, target = transfers[session.sha].initiator, transfers[session.sha].target;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 if initiator == conn and target ~= nil then
2729
7e0c35713bf5 mod_proxy65: Fix for old-style closing of connections (another source of tracebacks with libevent enabled)
Matthew Wild <mwild1@gmail.com>
parents: 2320
diff changeset
110 target:close();
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 elseif target == conn and initiator ~= nil then
2729
7e0c35713bf5 mod_proxy65: Fix for old-style closing of connections (another source of tracebacks with libevent enabled)
Matthew Wild <mwild1@gmail.com>
parents: 2320
diff changeset
112 initiator:close();
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 transfers[session.sha] = nil;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 -- Clean up any session-related stuff here
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 sessions[conn] = nil;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local function get_disco_info(stanza)
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 local reply = replies_cache.disco_info;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 if reply == nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#info")
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 :tag("identity", {category='proxy', type='bytestreams', name=name}):up()
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 :tag("feature", {var="http://jabber.org/protocol/bytestreams"});
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 replies_cache.disco_info = reply;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 reply.attr.id = stanza.attr.id;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 reply.attr.to = stanza.attr.from;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 return reply;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 local function get_disco_items(stanza)
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local reply = replies_cache.disco_items;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 if reply == nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 reply = st.iq({type='result', from=host}):query("http://jabber.org/protocol/disco#items");
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 replies_cache.disco_items = reply;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 reply.attr.id = stanza.attr.id;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 reply.attr.to = stanza.attr.from;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 return reply;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 local function get_stream_host(origin, stanza)
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 local reply = replies_cache.stream_host;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 local err_reply = replies_cache.stream_host_err;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 local sid = stanza.tags[1].attr.sid;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 local allow = false;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 local jid_node, jid_host, jid_resource = jid_split(stanza.attr.from);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 if stanza.attr.from == nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 jid_node = origin.username;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 jid_host = origin.host;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 jid_resource = origin.resource;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 if proxy_acl and #proxy_acl > 0 then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 if host ~= nil then -- at least a domain is needed.
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 for _, acl in ipairs(proxy_acl) do
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 local acl_node, acl_host, acl_resource = jid_split(acl);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 if ((acl_node ~= nil and acl_node == jid_node) or acl_node == nil) and
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 ((acl_host ~= nil and acl_host == jid_host) or acl_host == nil) and
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 ((acl_resource ~= nil and acl_resource == jid_resource) or acl_resource == nil) then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 allow = true;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 else
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 allow = true;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 if allow == true then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 if reply == nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 reply = st.iq({type="result", from=host})
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 :query("http://jabber.org/protocol/bytestreams")
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 :tag("streamhost", {jid=host, host=proxy_address, port=proxy_port});
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 replies_cache.stream_host = reply;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 else
2249
6e0c861dc61f mod_proxy65: Use new jid.join() from util.jid
Matthew Wild <mwild1@gmail.com>
parents: 2244
diff changeset
182 module:log("warn", "Denying use of proxy for %s", tostring(jid_join(jid_node, jid_host, jid_resource)));
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 if err_reply == nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 err_reply = st.iq({type="error", from=host})
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 :query("http://jabber.org/protocol/bytestreams")
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 :tag("error", {code='403', type='auth'})
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 :tag("forbidden", {xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'});
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 replies_cache.stream_host_err = err_reply;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 reply = err_reply;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 reply.attr.id = stanza.attr.id;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 reply.attr.to = stanza.attr.from;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 reply.tags[1].attr.sid = sid;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 return reply;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 module.unload = function()
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 componentmanager.deregister_component(host);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 connlisteners.deregister(module.host .. ':proxy65');
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 local function set_activation(stanza)
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 local from, to, sid, reply = nil;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 from = stanza.attr.from;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 if stanza.tags[1] ~= nil and tostring(stanza.tags[1].name) == "query" then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 if stanza.tags[1].attr ~= nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 sid = stanza.tags[1].attr.sid;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 if stanza.tags[1].tags[1] ~= nil and tostring(stanza.tags[1].tags[1].name) == "activate" then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 to = stanza.tags[1].tags[1][1];
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 if from ~= nil and to ~= nil and sid ~= nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 reply = st.iq({type="result", from=host, to=from});
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 reply.attr.id = stanza.attr.id;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 return reply, from, to, sid;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 function handle_to_domain(origin, stanza)
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 if to_node == nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 local type = stanza.attr.type;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 if type == "error" or type == "result" then return; end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 if stanza.name == "iq" and type == "get" then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 local xmlns = stanza.tags[1].attr.xmlns
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 if xmlns == "http://jabber.org/protocol/disco#info" then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 origin.send(get_disco_info(stanza));
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 return true;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 elseif xmlns == "http://jabber.org/protocol/disco#items" then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 origin.send(get_disco_items(stanza));
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 return true;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 elseif xmlns == "http://jabber.org/protocol/bytestreams" then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 origin.send(get_stream_host(origin, stanza));
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 return true;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 elseif stanza.name == "iq" and type == "set" then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 local reply, from, to, sid = set_activation(stanza);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 if reply ~= nil and from ~= nil and to ~= nil and sid ~= nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 local sha = sha1(sid .. from .. to, true);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 if transfers[sha] == nil then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 module:log("error", "transfers[sha]: nil");
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 elseif(transfers[sha] ~= nil and transfers[sha].initiator ~= nil and transfers[sha].target ~= nil) then
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 origin.send(reply);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 transfers[sha].activated = true;
2311
5fe837ebe542 mod_proxy65: Don't read data from the connection untill the proxying is activated
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2310
diff changeset
247 transfers[sha].target:lock_read(false);
5fe837ebe542 mod_proxy65: Don't read data from the connection untill the proxying is activated
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2310
diff changeset
248 transfers[sha].initiator:lock_read(false);
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 else
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 module:log("error", "activation failed: sid: %s, initiator: %s, target: %s", tostring(sid), tostring(from), tostring(to));
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 return;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 if not connlisteners.register(module.host .. ':proxy65', connlistener) then
2305
7ddd00260808 mod_proxy65: Replace error() calls with module:log("error", ...)
Matthew Wild <mwild1@gmail.com>
parents: 2273
diff changeset
259 module:log("error", "mod_proxy65: Could not establish a connection listener. Check your configuration please.");
7ddd00260808 mod_proxy65: Replace error() calls with module:log("error", ...)
Matthew Wild <mwild1@gmail.com>
parents: 2273
diff changeset
260 module:log("error", "Possibly two proxy65 components are configured to share the same port.");
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 connlisteners.start(module.host .. ':proxy65');
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 component = componentmanager.register_component(host, handle_to_domain);
2309
7dc6049a69e8 mod_proxy65: Use a bigger buffer for data we're proxying
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2308
diff changeset
265 local sender_lock_threshold = 4096;
2306
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
266 function throttle_sending(sender, receiver)
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
267 sender:pattern(sender_lock_threshold);
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
268 local sender_locked;
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
269 local _sendbuffer = receiver.sendbuffer;
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
270 function receiver.sendbuffer()
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
271 _sendbuffer();
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
272 if sender_locked and receiver.bufferlen() < sender_lock_threshold then
2308
600ac8992a4c mod_proxy65: Only lock the reading side when throttling
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2306
diff changeset
273 sender:lock_read(false); -- Unlock now
2306
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
274 sender_locked = nil;
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
275 end
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
276 end
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
277
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
278 local _readbuffer = sender.readbuffer;
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
279 function sender.readbuffer()
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
280 _readbuffer();
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
281 if not sender_locked and receiver.bufferlen() >= sender_lock_threshold then
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
282 sender_locked = true;
2308
600ac8992a4c mod_proxy65: Only lock the reading side when throttling
Sjoerd Simons <sjoerd.simons@collabora.co.uk>
parents: 2306
diff changeset
283 sender:lock_read(true);
2306
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
284 end
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
285 end
21f0d80f244a mod_proxy65: Throttle connections to prevent senders flooding the server's buffers if the receiver doesn't receive fast enough
Matthew Wild <mwild1@gmail.com>
parents: 2305
diff changeset
286 end