Annotate

plugins/mod_proxy65.lua @ 4414:aa2e79f20962

mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
author Waqas Hussain <waqas20@gmail.com>
date Sun, 06 Nov 2011 00:51:39 +0500
parent 4376:99277a1abe58
child 4679:5b52b5eaa03d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
1 -- Prosody IM
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
2 -- Copyright (C) 2008-2011 Matthew Wild
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
3 -- Copyright (C) 2008-2011 Waqas Hussain
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- Copyright (C) 2009 Thilo Cestonaro
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 -- 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
7 -- 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
8 --
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 --[[
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 * 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
11 module:unload("proxy65");
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 > server.removeserver(<proxy65_port>);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 module:load("proxy65", <proxy65_jid>);
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 ]]--
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
3691
07f789ac7e3c mod_proxy65: Make some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3690
diff changeset
17 local module = module;
07f789ac7e3c mod_proxy65: Make some globals local.
Waqas Hussain <waqas20@gmail.com>
parents: 3690
diff changeset
18 local tostring = tostring;
4376
99277a1abe58 mod_proxy65: Apply stringprep to activation target JID.
Waqas Hussain <waqas20@gmail.com>
parents: 4375
diff changeset
19 local jid_compare, jid_prep = require "util.jid".compare, require "util.jid".prep;
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local st = require "util.stanza";
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;
3004
c20b9fe1624b mod_proxy65: Use new server.link to link proxied connections, now works with either connection backend
Matthew Wild <mwild1@gmail.com>
parents: 2729
diff changeset
23 local server = require "net.server";
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
24 local b64 = require "util.encodings".base64.encode;
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local host, name = module:get_host(), "SOCKS5 Bytestreams Service";
4374
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
27 local sessions, transfers = {}, {};
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
3607
e1259b6d7560 mod_proxy65: Use module:get_option() instead of configmanager.
Waqas Hussain <waqas20@gmail.com>
parents: 3604
diff changeset
29 local proxy_port = module:get_option("proxy65_port") or 5000;
e1259b6d7560 mod_proxy65: Use module:get_option() instead of configmanager.
Waqas Hussain <waqas20@gmail.com>
parents: 3604
diff changeset
30 local proxy_interface = module:get_option("proxy65_interface") or "*";
e1259b6d7560 mod_proxy65: Use module:get_option() instead of configmanager.
Waqas Hussain <waqas20@gmail.com>
parents: 3604
diff changeset
31 local proxy_address = module:get_option("proxy65_address") or (proxy_interface ~= "*" and proxy_interface) or host;
e1259b6d7560 mod_proxy65: Use module:get_option() instead of configmanager.
Waqas Hussain <waqas20@gmail.com>
parents: 3604
diff changeset
32 local proxy_acl = module:get_option("proxy65_acl");
3004
c20b9fe1624b mod_proxy65: Use new server.link to link proxied connections, now works with either connection backend
Matthew Wild <mwild1@gmail.com>
parents: 2729
diff changeset
33 local max_buffer_size = 4096;
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 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
36
2244
730038d3e9e3 mod_proxy65: Update listener callback names for new server API
sjoerd.simons@collabora.co.uk
parents: 2138
diff changeset
37 function connlistener.onincoming(conn, data)
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local session = sessions[conn] or {};
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
39
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
40 local transfer = transfers[session.sha];
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
41 if transfer and transfer.activated then -- copy data between initiator and target
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
42 local initiator, target = transfer.initiator, transfer.target;
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
43 (conn == initiator and target or initiator):write(data);
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
44 return;
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
45 end -- FIXME server.link should be doing this?
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
47 if not session.greeting_done then
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
48 local nmethods = data:byte(2) or 0;
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
49 if data:byte(1) == 0x05 and nmethods > 0 and #data == 2 + nmethods then -- check if we have all the data
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
50 if data:find("%z") then -- 0x00 = 'No authentication' is supported
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
51 session.greeting_done = true;
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
52 sessions[conn] = session;
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
53 conn:write("\5\0"); -- send (SOCKS version 5, No authentication)
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
54 module:log("debug", "SOCKS5 greeting complete");
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 return;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
57 end -- else error, unexpected input
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
58 conn:write("\5\255"); -- send (SOCKS version 5, no acceptable method)
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
59 conn:close();
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
60 module:log("debug", "Invalid SOCKS5 greeting recieved: '%s'", b64(data));
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
61 else -- connection request
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
62 --local head = string.char( 0x05, 0x01, 0x00, 0x03, 40 ); -- ( VER=5=SOCKS5, CMD=1=CONNECT, RSV=0=RESERVED, ATYP=3=DOMAIMNAME, SHA-1 size )
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
63 if #data == 47 and data:sub(1,5) == "\5\1\0\3\40" and data:sub(-2) == "\0\0" then
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
64 local sha = data:sub(6, 45);
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
65 conn:pause();
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
66 conn:write("\5\0\0\3\40" .. sha .. "\0\0"); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte)
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
67 if not transfers[sha] then
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 transfers[sha] = {};
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 transfers[sha].target = conn;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 session.sha = sha;
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
71 module:log("debug", "SOCKS5 target connected for session %s", sha);
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
72 else -- transfers[sha].target ~= nil
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 transfers[sha].initiator = conn;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 session.sha = sha;
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
75 module:log("debug", "SOCKS5 initiator connected for session %s", sha);
3004
c20b9fe1624b mod_proxy65: Use new server.link to link proxied connections, now works with either connection backend
Matthew Wild <mwild1@gmail.com>
parents: 2729
diff changeset
76 server.link(conn, transfers[sha].target, max_buffer_size);
c20b9fe1624b mod_proxy65: Use new server.link to link proxied connections, now works with either connection backend
Matthew Wild <mwild1@gmail.com>
parents: 2729
diff changeset
77 server.link(transfers[sha].target, conn, max_buffer_size);
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
79 else -- error, unexpected input
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
80 conn:write("\5\1\0\3\0\0\0"); -- VER, REP, RSV, ATYP, BND.ADDR (sha), BND.PORT (2 Byte)
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
81 conn:close();
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
82 module:log("debug", "Invalid SOCKS5 negotiation recieved: '%s'", b64(data));
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
2244
730038d3e9e3 mod_proxy65: Update listener callback names for new server API
sjoerd.simons@collabora.co.uk
parents: 2138
diff changeset
87 function connlistener.ondisconnect(conn, err)
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local session = sessions[conn];
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if session then
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
90 if transfers[session.sha] then
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 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
92 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
93 target:close();
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 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
95 initiator:close();
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 transfers[session.sha] = nil;
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 -- Clean up any session-related stuff here
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 sessions[conn] = nil;
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
3694
a7d88f58abbb mod_proxy65: Add service discovery identity and feature, to help out mod_disco when loaded on a normal host.
Waqas Hussain <waqas20@gmail.com>
parents: 3693
diff changeset
104 module:add_identity("proxy", "bytestreams", name);
a7d88f58abbb mod_proxy65: Add service discovery identity and feature, to help out mod_disco when loaded on a normal host.
Waqas Hussain <waqas20@gmail.com>
parents: 3693
diff changeset
105 module:add_feature("http://jabber.org/protocol/bytestreams");
a7d88f58abbb mod_proxy65: Add service discovery identity and feature, to help out mod_disco when loaded on a normal host.
Waqas Hussain <waqas20@gmail.com>
parents: 3693
diff changeset
106
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
107 module:hook("iq-get/host/http://jabber.org/protocol/disco#info:query", function(event)
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
108 local origin, stanza = event.origin, event.stanza;
4374
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
109 origin.send(st.reply(stanza):query("http://jabber.org/protocol/disco#info")
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
110 :tag("identity", {category='proxy', type='bytestreams', name=name}):up()
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
111 :tag("feature", {var="http://jabber.org/protocol/bytestreams"}) );
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
112 return true;
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
113 end, -1);
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
115 module:hook("iq-get/host/http://jabber.org/protocol/disco#items:query", function(event)
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
116 local origin, stanza = event.origin, event.stanza;
4374
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
117 origin.send(st.reply(stanza):query("http://jabber.org/protocol/disco#items"));
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
118 return true;
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
119 end, -1);
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
121 module:hook("iq-get/host/http://jabber.org/protocol/bytestreams:query", function(event)
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
122 local origin, stanza = event.origin, event.stanza;
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
4374
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
124 -- check ACL
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
125 while proxy_acl and #proxy_acl > 0 do -- using 'while' instead of 'if' so we can break out of it
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
126 local jid = stanza.attr.from;
3377
9328179c9c76 mod_proxy65: Use util.jid.compare() and remove some clutter
Kim Alvefur <zash@zash.se>
parents: 3006
diff changeset
127 for _, acl in ipairs(proxy_acl) do
4374
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
128 if jid_compare(jid, acl) then break; end
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
4374
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
130 module:log("warn", "Denying use of proxy for %s", tostring(stanza.attr.from));
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
131 origin.send(st.error_reply(stanza, "auth", "forbidden"));
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
132 return true;
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
4374
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
134
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
135 local sid = stanza.tags[1].attr.sid;
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
136 origin.send(st.reply(stanza):tag("query", {xmlns="http://jabber.org/protocol/bytestreams", sid=sid})
c38f20f172b3 mod_proxy65: Cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3694
diff changeset
137 :tag("streamhost", {jid=host, host=proxy_address, port=proxy_port}));
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
138 return true;
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
139 end);
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 module.unload = function()
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 connlisteners.deregister(module.host .. ':proxy65');
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
145 module:hook("iq-set/host/http://jabber.org/protocol/bytestreams:query", function(event)
3558
f1201ff060b7 mod_proxy65: Use "iq/host" event for hooking stanzas instead of the component stanza handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3377
diff changeset
146 local origin, stanza = event.origin, event.stanza;
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
147
4375
81f5e83211dd mod_proxy65: Some more cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4374
diff changeset
148 local query = stanza.tags[1];
81f5e83211dd mod_proxy65: Some more cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4374
diff changeset
149 local sid = query.attr.sid;
81f5e83211dd mod_proxy65: Some more cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4374
diff changeset
150 local from = stanza.attr.from;
81f5e83211dd mod_proxy65: Some more cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4374
diff changeset
151 local to = query:get_child_text("activate");
4376
99277a1abe58 mod_proxy65: Apply stringprep to activation target JID.
Waqas Hussain <waqas20@gmail.com>
parents: 4375
diff changeset
152 local prepped_to = jid_prep(to);
4375
81f5e83211dd mod_proxy65: Some more cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4374
diff changeset
153
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
154 local info = "sid: "..tostring(sid)..", initiator: "..tostring(from)..", target: "..tostring(prepped_to or to);
4376
99277a1abe58 mod_proxy65: Apply stringprep to activation target JID.
Waqas Hussain <waqas20@gmail.com>
parents: 4375
diff changeset
155 if prepped_to and sid then
99277a1abe58 mod_proxy65: Apply stringprep to activation target JID.
Waqas Hussain <waqas20@gmail.com>
parents: 4375
diff changeset
156 local sha = sha1(sid .. from .. prepped_to, true);
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
157 if not transfers[sha] then
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
158 module:log("debug", "Activation request has unknown session id; activation failed (%s)", info);
4375
81f5e83211dd mod_proxy65: Some more cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4374
diff changeset
159 origin.send(st.error_reply(stanza, "modify", "item-not-found"));
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
160 elseif not transfers[sha].initiator then
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
161 module:log("debug", "The sender was not connected to the proxy; activation failed (%s)", info);
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
162 origin.send(st.error_reply(stanza, "cancel", "not-allowed", "The sender (you) is not connected to the proxy"));
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
163 --elseif not transfers[sha].target then -- can't happen, as target is set when a transfer object is created
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
164 -- module:log("debug", "The recipient was not connected to the proxy; activation failed (%s)", info);
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
165 -- origin.send(st.error_reply(stanza, "cancel", "not-allowed", "The recipient is not connected to the proxy"));
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
166 else -- if transfers[sha].initiator ~= nil and transfers[sha].target ~= nil then
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
167 module:log("debug", "Transfer activated (%s)", info);
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
168 transfers[sha].activated = true;
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
169 transfers[sha].target:resume();
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
170 transfers[sha].initiator:resume();
4375
81f5e83211dd mod_proxy65: Some more cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4374
diff changeset
171 origin.send(st.reply(stanza));
3559
0708d42ef0d4 mod_proxy65: Removed useless checks from the event handler.
Waqas Hussain <waqas20@gmail.com>
parents: 3558
diff changeset
172 end
4376
99277a1abe58 mod_proxy65: Apply stringprep to activation target JID.
Waqas Hussain <waqas20@gmail.com>
parents: 4375
diff changeset
173 elseif to and sid then
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
174 module:log("debug", "Malformed activation jid; activation failed (%s)", info);
4376
99277a1abe58 mod_proxy65: Apply stringprep to activation target JID.
Waqas Hussain <waqas20@gmail.com>
parents: 4375
diff changeset
175 origin.send(st.error_reply(stanza, "modify", "jid-malformed"));
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
176 else
4414
aa2e79f20962 mod_proxy65: Major cleanup, better logging, handling of all error cases, less code, and other goodness.
Waqas Hussain <waqas20@gmail.com>
parents: 4376
diff changeset
177 module:log("debug", "Bad request; activation failed (%s)", info);
4375
81f5e83211dd mod_proxy65: Some more cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4374
diff changeset
178 origin.send(st.error_reply(stanza, "modify", "bad-request"));
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 end
4375
81f5e83211dd mod_proxy65: Some more cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 4374
diff changeset
180 return true;
3688
f7de887ea0be mod_proxy65: Updated to use sub-events. Now only hooks what it needs to.
Waqas Hussain <waqas20@gmail.com>
parents: 3676
diff changeset
181 end);
2137
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 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
184 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
185 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
186 end
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187
c5d87a3316f8 mod_proxy65: Import from prosody-modules, thanks Ephraim :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 connlisteners.start(module.host .. ':proxy65');