Annotate

plugins/jingle_s5b.lua @ 472:864c9dc27c60

buildscripts: Update included squish (now supports Lua 5.2)
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Mar 2023 11:07:02 +0000
parent 457:73d4eb93657b
child 490:6b2f31da9610
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
288
22544dcf0579 plugins.jingle_s5b: Add local verse
Kim Alvefur <zash@zash.se>
parents: 140
diff changeset
1 local verse = require "verse";
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local xmlns_s5b = "urn:xmpp:jingle:transports:s5b:1";
289
8bbe0d6dfd11 plugins.jingle_s5b: Add missing xmlns
Kim Alvefur <zash@zash.se>
parents: 288
diff changeset
4 local xmlns_bytestreams = "http://jabber.org/protocol/bytestreams";
392
cdea6a28369e plugins: Use util.hashes instead of util.sha1
Kim Alvefur <zash@zash.se>
parents: 380
diff changeset
5 local sha1 = require "util.hashes".sha1;
457
73d4eb93657b Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents: 392
diff changeset
6 local new_id = require "util.id".short;
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local function negotiate_socks5(conn, hash)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local function suppress_connected()
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 conn:unhook("connected", suppress_connected);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 return true;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local function receive_connection_response(data)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 conn:unhook("incoming-raw", receive_connection_response);
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
15
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 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
17 return conn:event("error", "connection-failure");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 conn:event("connected");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return true;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local function receive_auth_response(data)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 conn:unhook("incoming-raw", receive_auth_response);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 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
25 -- 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
26 local err = "version-mismatch";
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if data:sub(1,1) == "\005" then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 err = "authentication-failure";
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 return conn:event("error", err);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 -- Request SOCKS5 connection
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 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
34 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
35 return true;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 conn:hook("connected", suppress_connected, 200);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 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
39 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
40 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 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
43 local conn = verse.new(nil, {
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 streamhosts = streamhosts,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 current_host = 0;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 });
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 --Attempt to connect to the next host
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local function attempt_next_streamhost(event)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if event then
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
50 return callback(nil, event.reason);
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 -- First connect, or the last connect failed
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if conn.current_host < #conn.streamhosts then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 conn.current_host = conn.current_host + 1;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 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
56 local ok, err = conn:connect(
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 conn.streamhosts[conn.current_host].host,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 conn.streamhosts[conn.current_host].port
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 );
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if not ok then
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
61 conn:debug("Error connecting to proxy (%s:%s): %s",
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 conn.streamhosts[conn.current_host].host,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 conn.streamhosts[conn.current_host].port,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 err
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 );
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 else
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 conn:debug("Connecting...");
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 negotiate_socks5(conn, auth_token);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 return true; -- Halt processing of disconnected event
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 -- All streamhosts tried, none successful
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 conn:unhook("disconnected", attempt_next_streamhost);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return callback(nil);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 -- 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
76 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 conn:hook("disconnected", attempt_next_streamhost, 100);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 -- 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
79 conn:hook("connected", function ()
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 conn:unhook("disconnected", attempt_next_streamhost);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 callback(conn.streamhosts[conn.current_host], conn);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end, 100);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 attempt_next_streamhost(); -- Set it in motion
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return conn;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 function verse.plugins.jingle_s5b(stream)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 stream:hook("ready", function ()
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 stream:add_disco_feature(xmlns_s5b);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end, 10);
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 local s5b = {};
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
93
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 function s5b:generate_initiate()
457
73d4eb93657b Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents: 392
diff changeset
95 self.s5b_sid = new_id();
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 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
97 mode = "tcp", sid = self.s5b_sid });
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local p = 0;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 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
100 p = p + 1;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 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
102 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
103 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 stream:debug("Have %d proxies", p)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 return transport;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
107
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 function s5b:generate_accept(initiate_transport)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local candidates = {};
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 self.s5b_peer_candidates = candidates;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 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
112 self.s5b_sid = initiate_transport.attr.sid or self.jingle.sid;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
113
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 -- 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
115 for candidate in initiate_transport:childtags() do
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 --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
117 --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
118 candidates[candidate.attr.cid] = {
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 type = candidate.attr.type;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 jid = candidate.attr.jid;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 host = candidate.attr.host;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 port = tonumber(candidate.attr.port) or 0;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 priority = tonumber(candidate.attr.priority) or 0;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 cid = candidate.attr.cid;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 };
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 --end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
128
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 -- Import our own candidates
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 -- TODO ^
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 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
132 return transport;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
134
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 function s5b:connect(callback)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 stream:warn("Connecting!");
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
137
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 local streamhost_array = {};
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 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
140 streamhost_array[#streamhost_array+1] = streamhost;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
142
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 if #streamhost_array > 0 then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 self.connecting_peer_candidates = true;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 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
146 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
147 :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
148 :tag("candidate-used", { cid = streamhost.cid }));
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 self.onconnect_callback = callback;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 self.conn = conn;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 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
153 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
154 else
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 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
156 self.onconnect_callback = callback;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
159
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 function s5b:info_received(jingle_tag)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 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
162 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
163 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
164 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
165 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
166 if candidate_used then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 -- 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
168 local function onconnect(streamhost, conn)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 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
170 -- Activate the stream
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 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
172 :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
173 :tag("activate"):text(self.jingle.peer), function (result)
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
174
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 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
176 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
177 :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
178 :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
179 self.conn = conn;
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 self.onconnect_callback(conn);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 else
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 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
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 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
187
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 -- 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
189 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
190 local streamhost_array = {
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 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
192 };
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 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
195 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
196 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 elseif transport_tag:get_child("activated") then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 self.onconnect_callback(self.conn);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
201
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 function s5b:disconnect()
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 if self.conn then
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 self.conn:close();
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 289
diff changeset
207
101
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 function s5b:handle_accepted(jingle_tag)
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 end
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 local s5b_mt = { __index = s5b };
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 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
213 return setmetatable({
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 role = jingle.role,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 peer = jingle.peer,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 stream = jingle.stream,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 jingle = jingle,
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 }, s5b_mt);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 end);
9c5362d393f0 verse.plugins.jingle_s5b: Jingle SOCKS5 Bytestreams transport
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 end