Software /
code /
verse
Annotate
plugins/proxy65.lua @ 490:6b2f31da9610
Update for new Prosody module namespace
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 20 May 2023 20:48:03 +0200 |
parent | 457:73d4eb93657b |
rev | line source |
---|---|
395 | 1 local verse = require "verse"; |
490
6b2f31da9610
Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents:
457
diff
changeset
|
2 local new_id = require "prosody.util.id".short; |
6b2f31da9610
Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents:
457
diff
changeset
|
3 local sha1 = require "prosody.util.hashes".sha1; |
56
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); |
103
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
14 stream.proxy65.available_streamhosts = {}; |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
15 local outstanding_proxies = 0; |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
16 stream:hook("disco/service-discovered/proxy", function (service) |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 -- Fill list with available proxies |
103
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
18 if service.type == "bytestreams" then |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
19 outstanding_proxies = outstanding_proxies + 1; |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
20 stream:send_iq(verse.iq({ to = service.jid, type = "get" }) |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
21 :tag("query", { xmlns = xmlns_bytestreams }), function (result) |
380 | 22 |
103
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
23 outstanding_proxies = outstanding_proxies - 1; |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
24 if result.attr.type == "result" then |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
25 local streamhost = result:get_child("query", xmlns_bytestreams) |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
26 :get_child("streamhost").attr; |
380 | 27 |
103
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
28 stream.proxy65.available_streamhosts[streamhost.jid] = { |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
29 jid = streamhost.jid; |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
30 host = streamhost.host; |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
31 port = tonumber(streamhost.port); |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
32 }; |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
33 end |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
34 if outstanding_proxies == 0 then |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
35 stream:event("proxy65/discovered-proxies", stream.proxy65.available_streamhosts); |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
36 end |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
37 end); |
6cc0ca4aa664
verse.plugins.proxy65: Add proxies discovered by disco plugin to proxy65.available_streamhosts
Matthew Wild <mwild1@gmail.com>
parents:
56
diff
changeset
|
38 end |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 end); |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 streamhosts = {}, |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 current_host = 0; |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 }); |
380 | 46 |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 -- 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
|
48 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
|
49 if tag.name == "streamhost" then |
380 | 50 table.insert(conn.streamhosts, tag.attr); |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 end |
380 | 53 |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 --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
|
55 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
|
56 -- 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
|
57 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
|
58 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
|
59 conn:connect( |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 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
|
61 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
|
62 ); |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 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
|
64 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
|
65 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 -- 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
|
67 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
|
68 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
|
69 -- 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
|
70 end |
380 | 71 |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 function conn:accept() |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 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
|
74 -- 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
|
75 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
|
76 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
|
77 -- 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
|
78 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
|
79 :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
|
80 :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
|
81 stream:send(reply); |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end, 100); |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 attempt_next_streamhost(); |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 function conn:refuse() |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 -- 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
|
87 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 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
|
89 end); |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 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
|
93 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
|
94 target_jid = target_jid; |
457
73d4eb93657b
Update to use util.id for random ids instead of counters (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
395
diff
changeset
|
95 bytestream_sid = new_id(); |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 }); |
380 | 97 |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 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
|
99 :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
|
100 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
|
101 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
|
102 end |
380 | 103 |
104 | |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 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
|
106 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
|
107 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
|
108 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
|
109 else |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 -- 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
|
111 local streamhost_used = reply.tags[1]:get_child("streamhost-used"); |
395 | 112 -- if not streamhost_used then |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 --FIXME: Emit error |
395 | 114 -- end |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 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
|
116 local host, port; |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 break; |
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 end |
395 | 123 -- if not (host and port) then |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 --FIXME: Emit error |
395 | 125 -- end |
380 | 126 |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 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
|
128 |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 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
|
130 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
|
131 -- Both of us connected, tell proxy to activate connection |
395 | 132 local activate_request = verse.iq{to = conn.streamhost_jid, type="set"} |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 :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
|
134 :tag("activate"):text(target_jid); |
395 | 135 self.stream:send_iq(activate_request, function (activated) |
136 if activated.attr.type == "result" then | |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 -- 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
|
138 conn:event("connected", conn); |
395 | 139 -- else --FIXME: Emit error |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 end |
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 return true; |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 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
|
145 |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 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
|
147 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 end); |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 return conn; |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 function negotiate_socks5(stream, conn, sid, requester_jid, target_jid) |
392
cdea6a28369e
plugins: Use util.hashes instead of util.sha1
Kim Alvefur <zash@zash.se>
parents:
380
diff
changeset
|
153 local hash = sha1(sid..requester_jid..target_jid); |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 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
|
155 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
|
156 return true; |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 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
|
159 conn:unhook("incoming-raw", receive_connection_response); |
380 | 160 |
56
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 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
|
162 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
|
163 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 conn:event("connected"); |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 return true; |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 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
|
168 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
|
169 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
|
170 -- 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
|
171 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
|
172 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
|
173 err = "authentication-failure"; |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 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
|
176 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 -- Request SOCKS5 connection |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 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
|
179 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
|
180 return true; |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 end |
014bdb4154e9
verse.plugins.proxy65: XEP-0065 plugin for file transfer through a proxy
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 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
|
183 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
|
184 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
|
185 end |