Debian Codegraph @

xmpp-vala/src/module/xep/0065_socks5_bytestreams.vala

raw
1 using Gee;
2 using Xmpp;
3 using Xmpp.Xep;
4
5 namespace Xmpp.Xep.Socks5Bytestreams {
6
7 internal const string NS_URI = "http://jabber.org/protocol/bytestreams";
8
9 public class Proxy : Object {
10 public string host { get; private set; }
11 public Jid jid { get; private set; }
12 public int port { get; private set; }
13
14 public Proxy(string host, Jid jid, int port) {
15 this.host = host;
16 this.jid = jid;
17 this.port = port;
18 }
19 }
20
21 public delegate Gee.List<string> GetLocalIpAddresses();
22
23 public class Module : XmppStreamModule, Iq.Handler {
24 public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0065_socks5_bytestreams");
25
26 private GetLocalIpAddresses? get_local_ip_addresses_impl = null;
27
28 public override void attach(XmppStream stream) {
29 stream.add_flag(new Flag());
30 query_availability.begin(stream);
31 }
32 public override void detach(XmppStream stream) { }
33
34 public Gee.List<Proxy> get_proxies(XmppStream stream) {
35 return stream.get_flag(Flag.IDENTITY).proxies;
36 }
37
38 public void set_local_ip_address_handler(owned GetLocalIpAddresses get_local_ip_addresses) {
39 get_local_ip_addresses_impl = (owned)get_local_ip_addresses;
40 }
41
42 public Gee.List<string> get_local_ip_addresses() {
43 if (get_local_ip_addresses_impl == null) {
44 return Gee.List.empty();
45 }
46 return get_local_ip_addresses_impl();
47 }
48
49 private async void query_availability(XmppStream stream) {
50 ServiceDiscovery.ItemsResult? items_result = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).request_items(stream, stream.remote_name);
51 if (items_result == null) return;
52
53 foreach (Xep.ServiceDiscovery.Item item in items_result.items) {
54 bool has_feature = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, item.jid, NS_URI);
55 if (!has_feature) continue;
56
57 StanzaNode query_node = new StanzaNode.build("query", NS_URI).add_self_xmlns();
58 Iq.Stanza iq = new Iq.Stanza.get(query_node) { to=item.jid };
59
60 Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq);
61 if (iq_result.is_error()) continue;
62
63 StanzaNode? query_result_node = iq_result.stanza.get_subnode("query", NS_URI);
64 StanzaNode? stream_host = query_result_node != null ? query_result_node.get_subnode("streamhost", NS_URI) : null;
65 if (query_result_node == null || stream_host == null) {
66 return;
67 }
68 string? host = stream_host.get_attribute("host");
69 string? jid_str = stream_host.get_attribute("jid");
70 Jid? jid = null;
71 try {
72 jid = jid_str != null ? new Jid(jid_str) : null;
73 } catch (InvalidJidError ignored) { }
74 int port = stream_host.get_attribute_int("port");
75 if (host == null || jid == null || port <= 0 || port > 65535) {
76 continue;
77 }
78 stream.get_flag(Flag.IDENTITY).proxies.add(new Proxy(host, jid, port));
79 }
80 }
81
82 public override string get_ns() { return NS_URI; }
83 public override string get_id() { return IDENTITY.id; }
84 }
85
86 public class Flag : XmppStreamFlag {
87 public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "socks5_bytestreams");
88
89 public Gee.List<Proxy> proxies = new ArrayList<Proxy>();
90
91 public override string get_ns() { return NS_URI; }
92 public override string get_id() { return IDENTITY.id; }
93 }
94
95
96 }