plugins/proxy65.lua

Thu, 06 May 2010 10:34:27 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 06 May 2010 10:34:27 +0100
changeset 56
014bdb4154e9
child 103
6cc0ca4aa664
permissions
-rw-r--r--

verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy

56
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local events = require "util.events";
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local uuid = require "util.uuid";
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local sha1 = require "util.sha1";
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local proxy65_mt = {};
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 proxy65_mt.__index = proxy65_mt;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local xmlns_bytestreams = "http://jabber.org/protocol/bytestreams";
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local negotiate_socks5;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 function verse.plugins.proxy65(stream)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 stream.proxy65 = setmetatable({ stream = stream }, proxy65_mt);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 stream:hook("disco-result", function (result)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 -- Fill list with available proxies
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 stream:hook("iq/"..xmlns_bytestreams, function (request)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local conn = verse.new(nil, {
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 initiator_jid = request.attr.from,
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 streamhosts = {},
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 current_host = 0;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 });
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 -- Parse hosts from request
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 for tag in request.tags[1]:childtags() do
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if tag.name == "streamhost" then
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 table.insert(conn.streamhosts, tag.attr);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 --Attempt to connect to the next host
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local function attempt_next_streamhost()
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 -- First connect, or the last connect failed
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if conn.current_host < #conn.streamhosts then
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 conn.current_host = conn.current_host + 1;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 conn:connect(
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 conn.streamhosts[conn.current_host].host,
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 conn.streamhosts[conn.current_host].port
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 );
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 negotiate_socks5(stream, conn, request.tags[1].attr.sid, request.attr.from, stream.jid);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return true; -- Halt processing of disconnected event
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 -- All streamhosts tried, none successful
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 conn:unhook("disconnected", attempt_next_streamhost);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 stream:send(verse.error_reply(request, "cancel", "item-not-found"));
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 -- Let disconnected event fall through to user handlers...
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 function conn:accept()
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 conn:hook("disconnected", attempt_next_streamhost, 100);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 -- When this event fires, we're connected to a streamhost
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 conn:hook("connected", function ()
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 conn:unhook("disconnected", attempt_next_streamhost);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 -- Send XMPP success notification
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local reply = verse.reply(request)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 :tag("query", request.tags[1].attr)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 :tag("streamhost-used", { jid = conn.streamhosts[conn.current_host].jid });
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 stream:send(reply);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end, 100);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 attempt_next_streamhost();
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 function conn:refuse()
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 -- FIXME: XMPP refused reply
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 stream:event("proxy65/request", conn);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 function proxy65_mt:new(target_jid, proxies)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local conn = verse.new(nil, {
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 target_jid = target_jid;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 bytestream_sid = uuid.generate();
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 });
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local request = verse.iq{type="set", to = target_jid}
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 :tag("query", { xmlns = xmlns_bytestreams, mode = "tcp", sid = conn.bytestream_sid });
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 for _, proxy in ipairs(proxies or self.proxies) do
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 request:tag("streamhost", proxy):up();
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 self.stream:send_iq(request, function (reply)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if reply.attr.type == "error" then
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 local type, condition, text = reply:get_error();
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 conn:event("connection-failed", { conn = conn, type = type, condition = condition, text = text });
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 else
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 -- Target connected to streamhost, connect ourselves
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local streamhost_used = reply.tags[1]:get_child("streamhost-used");
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if not streamhost_used then
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 --FIXME: Emit error
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 conn.streamhost_jid = streamhost_used.attr.jid;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 local host, port;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 for _, proxy in ipairs(proxies or self.proxies) do
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 if proxy.jid == conn.streamhost_jid then
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 host, port = proxy.host, proxy.port;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 break;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 if not (host and port) then
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 --FIXME: Emit error
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 conn:connect(host, port);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 local function handle_proxy_connected()
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 conn:unhook("connected", handle_proxy_connected);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 -- Both of us connected, tell proxy to activate connection
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local request = verse.iq{to = conn.streamhost_jid, type="set"}
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 :tag("query", { xmlns = xmlns_bytestreams, sid = conn.bytestream_sid })
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 :tag("activate"):text(target_jid);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 self.stream:send_iq(request, function (reply)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 if reply.attr.type == "result" then
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 -- Connection activated, ready to use
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 conn:event("connected", conn);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 else
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 --FIXME: Emit error
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 return true;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 conn:hook("connected", handle_proxy_connected, 100);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 negotiate_socks5(self.stream, conn, conn.bytestream_sid, self.stream.jid, target_jid);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 return conn;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 function negotiate_socks5(stream, conn, sid, requester_jid, target_jid)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 local hash = sha1.sha1(sid..requester_jid..target_jid);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local function suppress_connected()
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 conn:unhook("connected", suppress_connected);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 return true;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local function receive_connection_response(data)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 conn:unhook("incoming-raw", receive_connection_response);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 if data:sub(1, 2) ~= "\005\000" then
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 return conn:event("error", "connection-failure");
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 conn:event("connected");
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 return true;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 local function receive_auth_response(data)
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 conn:unhook("incoming-raw", receive_auth_response);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 if data ~= "\005\000" then -- SOCKSv5; "NO AUTHENTICATION"
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 -- Server is not SOCKSv5, or does not allow no auth
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 local err = "version-mismatch";
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 if data:sub(1,1) == "\005" then
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 err = "authentication-failure";
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 return conn:event("error", err);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 -- Request SOCKS5 connection
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 conn:send(string.char(0x05, 0x01, 0x00, 0x03, #hash)..hash.."\0\0"); --FIXME: Move to "connected"?
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 conn:hook("incoming-raw", receive_connection_response, 100);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 return true;
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 conn:hook("connected", suppress_connected, 200);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 conn:hook("incoming-raw", receive_auth_response, 100);
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 conn:send("\005\001\000"); -- SOCKSv5; 1 mechanism; "NO AUTHENTICATION"
014bdb4154e9 verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end

mercurial