Annotate

plugins/jingle_s5b.lua @ 271:c9d8628a61cc

plugins.roster: Change variable name to what's used in XMPP-IM
author Kim Alvefur <zash@zash.se>
date Sun, 01 Jan 2012 22:17:39 +0100
parent 140:97bf22d6ff96
child 288:22544dcf0579
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local xmlns_s5b = "urn:xmpp:jingle:transports:s5b:1";
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local sha1 = require "util.sha1".sha1;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local uuid_generate = require "util.uuid".generate;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local function negotiate_socks5(conn, hash)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local function suppress_connected()
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 conn:unhook("connected", suppress_connected);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 return true;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local function receive_connection_response(data)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 conn:unhook("incoming-raw", receive_connection_response);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if data:sub(1, 2) ~= "\005\000" then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 return conn:event("error", "connection-failure");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 conn:event("connected");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 return true;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local function receive_auth_response(data)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 conn:unhook("incoming-raw", receive_auth_response);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 if data ~= "\005\000" then -- SOCKSv5; "NO AUTHENTICATION"
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- Server is not SOCKSv5, or does not allow no auth
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local err = "version-mismatch";
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 if data:sub(1,1) == "\005" then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 err = "authentication-failure";
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return conn:event("error", err);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- Request SOCKS5 connection
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 conn:send(string.char(0x05, 0x01, 0x00, 0x03, #hash)..hash.."\0\0"); --FIXME: Move to "connected"?
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 conn:hook("incoming-raw", receive_connection_response, 100);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 return true;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 conn:hook("connected", suppress_connected, 200);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 conn:hook("incoming-raw", receive_auth_response, 100);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 conn:send("\005\001\000"); -- SOCKSv5; 1 mechanism; "NO AUTHENTICATION"
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local function connect_to_usable_streamhost(callback, streamhosts, auth_token)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local conn = verse.new(nil, {
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 streamhosts = streamhosts,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 current_host = 0;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 });
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 --Attempt to connect to the next host
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local function attempt_next_streamhost(event)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if event then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 return callback(nil, event.reason);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 -- First connect, or the last connect failed
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if conn.current_host < #conn.streamhosts then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 conn.current_host = conn.current_host + 1;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 conn:debug("Attempting to connect to "..conn.streamhosts[conn.current_host].host..":"..conn.streamhosts[conn.current_host].port.."...");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local ok, err = conn:connect(
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 conn.streamhosts[conn.current_host].host,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 conn.streamhosts[conn.current_host].port
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 );
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 if not ok then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 conn:debug("Error connecting to proxy (%s:%s): %s",
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 conn.streamhosts[conn.current_host].host,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 conn.streamhosts[conn.current_host].port,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 err
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 );
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 else
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 conn:debug("Connecting...");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 negotiate_socks5(conn, auth_token);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 return true; -- Halt processing of disconnected event
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 -- All streamhosts tried, none successful
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 conn:unhook("disconnected", attempt_next_streamhost);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return callback(nil);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 -- Let disconnected event fall through to user handlers...
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 conn:hook("disconnected", attempt_next_streamhost, 100);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 -- When this event fires, we're connected to a streamhost
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 conn:hook("connected", function ()
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 conn:unhook("disconnected", attempt_next_streamhost);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 callback(conn.streamhosts[conn.current_host], conn);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end, 100);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 attempt_next_streamhost(); -- Set it in motion
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 return conn;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 function verse.plugins.jingle_s5b(stream)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 stream:hook("ready", function ()
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 stream:add_disco_feature(xmlns_s5b);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end, 10);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local s5b = {};
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 function s5b:generate_initiate()
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 self.s5b_sid = uuid_generate();
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local transport = verse.stanza("transport", { xmlns = xmlns_s5b,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 mode = "tcp", sid = self.s5b_sid });
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 local p = 0;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 for jid, streamhost in pairs(stream.proxy65.available_streamhosts) do
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 p = p + 1;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 transport:tag("candidate", { jid = jid, host = streamhost.host,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 port = streamhost.port, cid=jid, priority = p, type = "proxy" }):up();
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 stream:debug("Have %d proxies", p)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 return transport;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 function s5b:generate_accept(initiate_transport)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 local candidates = {};
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 self.s5b_peer_candidates = candidates;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 self.s5b_mode = initiate_transport.attr.mode or "tcp";
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 self.s5b_sid = initiate_transport.attr.sid or self.jingle.sid;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 -- Import the list of candidates the initiator offered us
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 for candidate in initiate_transport:childtags() do
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 --if candidate.attr.jid == "asterix4@jabber.lagaule.org/Gajim"
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 --and candidate.attr.host == "82.246.25.239" then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 candidates[candidate.attr.cid] = {
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 type = candidate.attr.type;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 jid = candidate.attr.jid;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 host = candidate.attr.host;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 port = tonumber(candidate.attr.port) or 0;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 priority = tonumber(candidate.attr.priority) or 0;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 cid = candidate.attr.cid;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 };
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 --end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 -- Import our own candidates
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 -- TODO ^
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 local transport = verse.stanza("transport", { xmlns = xmlns_s5b });
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 return transport;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 function s5b:connect(callback)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 stream:warn("Connecting!");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local streamhost_array = {};
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 for cid, streamhost in pairs(self.s5b_peer_candidates or {}) do
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 streamhost_array[#streamhost_array+1] = streamhost;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 if #streamhost_array > 0 then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 self.connecting_peer_candidates = true;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 local function onconnect(streamhost, conn)
140
97bf22d6ff96 plugins.jingle, plugins.jingle_s5b: API changes to allow not sending <reason> inside <content> when transfer succeeds
Matthew Wild <mwild1@gmail.com>
parents: 101
diff changeset
144 self.jingle:send_command("transport-info", verse.stanza("content", { creator = self.creator, name = self.name })
97bf22d6ff96 plugins.jingle, plugins.jingle_s5b: API changes to allow not sending <reason> inside <content> when transfer succeeds
Matthew Wild <mwild1@gmail.com>
parents: 101
diff changeset
145 :tag("transport", { xmlns = xmlns_s5b, sid = self.s5b_sid })
97bf22d6ff96 plugins.jingle, plugins.jingle_s5b: API changes to allow not sending <reason> inside <content> when transfer succeeds
Matthew Wild <mwild1@gmail.com>
parents: 101
diff changeset
146 :tag("candidate-used", { cid = streamhost.cid }));
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 self.onconnect_callback = callback;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 self.conn = conn;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 local auth_token = sha1(self.s5b_sid..self.peer..stream.jid, true);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 connect_to_usable_streamhost(onconnect, streamhost_array, auth_token);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 else
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 stream:warn("Actually, I'm going to wait for my peer to tell me its streamhost...");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 self.onconnect_callback = callback;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 function s5b:info_received(jingle_tag)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 stream:warn("Info received");
140
97bf22d6ff96 plugins.jingle, plugins.jingle_s5b: API changes to allow not sending <reason> inside <content> when transfer succeeds
Matthew Wild <mwild1@gmail.com>
parents: 101
diff changeset
160 local content_tag = jingle_tag:child_with_name("content");
97bf22d6ff96 plugins.jingle, plugins.jingle_s5b: API changes to allow not sending <reason> inside <content> when transfer succeeds
Matthew Wild <mwild1@gmail.com>
parents: 101
diff changeset
161 local transport_tag = content_tag:child_with_name("transport");
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 if transport_tag:get_child("candidate-used") and not self.connecting_peer_candidates then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 local candidate_used = transport_tag:child_with_name("candidate-used");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 if candidate_used then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 -- Connect straight away to candidate used, we weren't trying any anyway
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 local function onconnect(streamhost, conn)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 if self.jingle.role == "initiator" then -- More correct would be - "is this a candidate we offered?"
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 -- Activate the stream
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 self.jingle.stream:send_iq(verse.iq({ to = streamhost.jid, type = "set" })
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 :tag("query", { xmlns = xmlns_bytestreams, sid = self.s5b_sid })
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 :tag("activate"):text(self.jingle.peer), function (result)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 if result.attr.type == "result" then
140
97bf22d6ff96 plugins.jingle, plugins.jingle_s5b: API changes to allow not sending <reason> inside <content> when transfer succeeds
Matthew Wild <mwild1@gmail.com>
parents: 101
diff changeset
174 self.jingle:send_command("transport-info", verse.stanza("content", content_tag.attr)
97bf22d6ff96 plugins.jingle, plugins.jingle_s5b: API changes to allow not sending <reason> inside <content> when transfer succeeds
Matthew Wild <mwild1@gmail.com>
parents: 101
diff changeset
175 :tag("transport", { xmlns = xmlns_s5b, sid = self.s5b_sid })
97bf22d6ff96 plugins.jingle, plugins.jingle_s5b: API changes to allow not sending <reason> inside <content> when transfer succeeds
Matthew Wild <mwild1@gmail.com>
parents: 101
diff changeset
176 :tag("activated", { cid = candidate_used.attr.cid }));
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 self.conn = conn;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 self.onconnect_callback(conn);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 else
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 self.jingle.stream:error("Failed to activate bytestream");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 end);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 -- FIXME: Another assumption that cid==jid, and that it was our candidate
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 self.jingle.stream:debug("CID: %s", self.jingle.stream.proxy65.available_streamhosts[candidate_used.attr.cid]);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 local streamhost_array = {
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 self.jingle.stream.proxy65.available_streamhosts[candidate_used.attr.cid];
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 };
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 local auth_token = sha1(self.s5b_sid..stream.jid..self.peer, true);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 connect_to_usable_streamhost(onconnect, streamhost_array, auth_token);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 elseif transport_tag:get_child("activated") then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 self.onconnect_callback(self.conn);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 function s5b:disconnect()
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 if self.conn then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 self.conn:close();
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 function s5b:handle_accepted(jingle_tag)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 local s5b_mt = { __index = s5b };
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 stream:hook("jingle/transport/"..xmlns_s5b, function (jingle)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 return setmetatable({
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 role = jingle.role,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 peer = jingle.peer,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 stream = jingle.stream,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 jingle = jingle,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 }, s5b_mt);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 end);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 end