| 1 |
using Gee; |
| 2 |
using Xmpp; |
| 3 |
using Xmpp.Xep; |
| 4 |
|
| 5 |
namespace Xmpp.Xep.JingleSocks5Bytestreams { |
| 6 |
|
| 7 |
private const string NS_URI = "urn:xmpp:jingle:transports:s5b:1"; |
| 8 |
private const int NEGOTIATION_TIMEOUT = 3; |
| 9 |
|
| 10 |
public class Module : Jingle.Transport, XmppStreamModule { |
| 11 |
public static Xmpp.ModuleIdentity<Module> IDENTITY = new Xmpp.ModuleIdentity<Module>(NS_URI, "0260_jingle_socks5_bytestreams"); |
| 12 |
|
| 13 |
public override void attach(XmppStream stream) { |
| 14 |
stream.get_module(Jingle.Module.IDENTITY).register_transport(this); |
| 15 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); |
| 16 |
} |
| 17 |
public override void detach(XmppStream stream) { |
| 18 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); |
| 19 |
} |
| 20 |
|
| 21 |
public override string get_ns() { return NS_URI; } |
| 22 |
public override string get_id() { return IDENTITY.id; } |
| 23 |
|
| 24 |
public async bool is_transport_available(XmppStream stream, uint8 components, Jid full_jid) { |
| 25 |
return components == 1 && yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, full_jid, NS_URI); |
| 26 |
} |
| 27 |
|
| 28 |
public string ns_uri { get { return NS_URI; } } |
| 29 |
public Jingle.TransportType type_ { get { return Jingle.TransportType.STREAMING; } } |
| 30 |
public int priority { get { return 1; } } |
| 31 |
|
| 32 |
private Gee.List<Candidate> get_proxies(XmppStream stream) { |
| 33 |
Gee.List<Candidate> result = new ArrayList<Candidate>(); |
| 34 |
int i = 1 << 15; |
| 35 |
foreach (Socks5Bytestreams.Proxy proxy in stream.get_module(Socks5Bytestreams.Module.IDENTITY).get_proxies(stream)) { |
| 36 |
result.add(new Candidate.proxy(random_uuid(), proxy, i)); |
| 37 |
i -= 1; |
| 38 |
} |
| 39 |
return result; |
| 40 |
} |
| 41 |
|
| 42 |
private Gee.List<Candidate> start_local_listeners(XmppStream stream, Jid local_full_jid, string dstaddr, out LocalListener? local_listener) { |
| 43 |
Gee.List<Candidate> result = new ArrayList<Candidate>(); |
| 44 |
SocketListener listener = new SocketListener(); |
| 45 |
int i = 1 << 15; |
| 46 |
foreach (string ip_address in stream.get_module(Socks5Bytestreams.Module.IDENTITY).get_local_ip_addresses()) { |
| 47 |
InetSocketAddress addr = new InetSocketAddress.from_string(ip_address, 0); |
| 48 |
SocketAddress effective_any; |
| 49 |
string cid = random_uuid(); |
| 50 |
try { |
| 51 |
listener.add_address(addr, SocketType.STREAM, SocketProtocol.DEFAULT, new StringWrapper(cid), out effective_any); |
| 52 |
} catch (Error e) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
InetSocketAddress effective = (InetSocketAddress)effective_any; |
| 56 |
result.add(new Candidate.build(cid, ip_address, local_full_jid, (int)effective.port, i, CandidateType.DIRECT)); |
| 57 |
i -= 1; |
| 58 |
} |
| 59 |
if (!result.is_empty) { |
| 60 |
local_listener = new LocalListener(listener, dstaddr); |
| 61 |
local_listener.start(); |
| 62 |
} else { |
| 63 |
local_listener = new LocalListener.empty(); |
| 64 |
} |
| 65 |
return result; |
| 66 |
} |
| 67 |
|
| 68 |
private void select_candidates(XmppStream stream, Jid local_full_jid, string dstaddr, Parameters result) { |
| 69 |
result.local_candidates.add_all(get_proxies(stream)); |
| 70 |
result.local_candidates.add_all(start_local_listeners(stream, local_full_jid, dstaddr, out result.listener)); |
| 71 |
result.local_candidates.sort((c1, c2) => { |
| 72 |
if (c1.priority < c2.priority) { return 1; } |
| 73 |
if (c1.priority > c2.priority) { return -1; } |
| 74 |
return 0; |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
public Jingle.TransportParameters create_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid) { |
| 79 |
assert(components == 1); |
| 80 |
Parameters result = new Parameters.create(local_full_jid, peer_full_jid, random_uuid()); |
| 81 |
string dstaddr = calculate_dstaddr(result.sid, local_full_jid, peer_full_jid); |
| 82 |
select_candidates(stream, local_full_jid, dstaddr, result); |
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
public Jingle.TransportParameters parse_transport_parameters(XmppStream stream, uint8 components, Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { |
| 87 |
Parameters result = Parameters.parse(local_full_jid, peer_full_jid, transport); |
| 88 |
string dstaddr = calculate_dstaddr(result.sid, local_full_jid, peer_full_jid); |
| 89 |
select_candidates(stream, local_full_jid, dstaddr, result); |
| 90 |
return result; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
private string calculate_dstaddr(string sid, Jid first_jid, Jid second_jid) { |
| 95 |
string hashed = sid + first_jid.to_string() + second_jid.to_string(); |
| 96 |
return Checksum.compute_for_string(ChecksumType.SHA1, hashed); |
| 97 |
} |
| 98 |
|
| 99 |
public enum CandidateType { |
| 100 |
ASSISTED, |
| 101 |
DIRECT, |
| 102 |
PROXY, |
| 103 |
TUNNEL; |
| 104 |
|
| 105 |
public static CandidateType parse(string type) throws Jingle.IqError { |
| 106 |
switch (type) { |
| 107 |
case "assisted": return CandidateType.ASSISTED; |
| 108 |
case "direct": return CandidateType.DIRECT; |
| 109 |
case "proxy": return CandidateType.PROXY; |
| 110 |
case "tunnel": return CandidateType.TUNNEL; |
| 111 |
} |
| 112 |
throw new Jingle.IqError.BAD_REQUEST(@"unknown candidate type $(type)"); |
| 113 |
} |
| 114 |
|
| 115 |
public string to_string() { |
| 116 |
switch (this) { |
| 117 |
case ASSISTED: return "assisted"; |
| 118 |
case DIRECT: return "direct"; |
| 119 |
case PROXY: return "proxy"; |
| 120 |
case TUNNEL: return "tunnel"; |
| 121 |
} |
| 122 |
assert_not_reached(); |
| 123 |
} |
| 124 |
|
| 125 |
private int type_preference_impl() { |
| 126 |
switch (this) { |
| 127 |
case ASSISTED: return 120; |
| 128 |
case DIRECT: return 126; |
| 129 |
case PROXY: return 10; |
| 130 |
case TUNNEL: return 110; |
| 131 |
} |
| 132 |
assert_not_reached(); |
| 133 |
} |
| 134 |
public int type_preference() { |
| 135 |
return type_preference_impl() << 16; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public class Candidate : Socks5Bytestreams.Proxy { |
| 140 |
public string cid { get; private set; } |
| 141 |
public int priority { get; private set; } |
| 142 |
public CandidateType type_ { get; private set; } |
| 143 |
|
| 144 |
private Candidate(string cid, string host, Jid jid, int port, int priority, CandidateType type) { |
| 145 |
base(host, jid, port); |
| 146 |
this.cid = cid; |
| 147 |
this.priority = priority; |
| 148 |
this.type_ = type; |
| 149 |
} |
| 150 |
|
| 151 |
public Candidate.build(string cid, string host, Jid jid, int port, int local_priority, CandidateType type) { |
| 152 |
this(cid, host, jid, port, type.type_preference() + local_priority, type); |
| 153 |
} |
| 154 |
|
| 155 |
public Candidate.proxy(string cid, Socks5Bytestreams.Proxy proxy, int local_priority) { |
| 156 |
this.build(cid, proxy.host, proxy.jid, proxy.port, local_priority, CandidateType.PROXY); |
| 157 |
} |
| 158 |
|
| 159 |
public static Candidate parse(StanzaNode candidate) throws Jingle.IqError { |
| 160 |
string? cid = candidate.get_attribute("cid"); |
| 161 |
string? host = candidate.get_attribute("host"); |
| 162 |
string? jid_str = candidate.get_attribute("jid"); |
| 163 |
Jid? jid = null; |
| 164 |
try { |
| 165 |
jid = new Jid(jid_str); |
| 166 |
} catch (InvalidJidError ignored) { |
| 167 |
} |
| 168 |
int port = candidate.get_attribute("port") != null ? candidate.get_attribute_int("port") : 1080; |
| 169 |
int priority = candidate.get_attribute_int("priority"); |
| 170 |
string? type_str = candidate.get_attribute("type"); |
| 171 |
CandidateType type = type_str != null ? CandidateType.parse(type_str) : CandidateType.DIRECT; |
| 172 |
|
| 173 |
if (cid == null || host == null || jid == null || port <= 0 || priority <= 0) { |
| 174 |
throw new Jingle.IqError.BAD_REQUEST("missing or invalid cid, host, jid or port"); |
| 175 |
} |
| 176 |
|
| 177 |
return new Candidate(cid, host, jid, port, priority, type); |
| 178 |
} |
| 179 |
|
| 180 |
public StanzaNode to_xml() { |
| 181 |
return new StanzaNode.build("candidate", NS_URI) |
| 182 |
.put_attribute("cid", cid) |
| 183 |
.put_attribute("host", host) |
| 184 |
.put_attribute("jid", jid.to_string()) |
| 185 |
.put_attribute("port", port.to_string()) |
| 186 |
.put_attribute("priority", priority.to_string()) |
| 187 |
.put_attribute("type", type_.to_string()); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
bool bytes_equal(uint8[] a, uint8[] b) { |
| 192 |
if (a.length != b.length) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
for (int i = 0; i < a.length; i++) { |
| 196 |
if (a[i] != b[i]) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
} |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
class StringWrapper : GLib.Object { |
| 204 |
public string str { get; set; } |
| 205 |
|
| 206 |
public StringWrapper(string str) { |
| 207 |
this.str = str; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
class LocalListener { |
| 212 |
SocketListener? inner; |
| 213 |
string dstaddr; |
| 214 |
HashMap<string, SocketConnection> connections = new HashMap<string, SocketConnection>(); |
| 215 |
|
| 216 |
public LocalListener(SocketListener inner, string dstaddr) { |
| 217 |
this.inner = inner; |
| 218 |
this.dstaddr = dstaddr; |
| 219 |
} |
| 220 |
|
| 221 |
public LocalListener.empty() { |
| 222 |
this.inner = null; |
| 223 |
this.dstaddr = ""; |
| 224 |
} |
| 225 |
|
| 226 |
public void start() { |
| 227 |
if (inner == null) { |
| 228 |
return; |
| 229 |
} |
| 230 |
run.begin(); |
| 231 |
} |
| 232 |
async void run() { |
| 233 |
while (true) { |
| 234 |
Object cid; |
| 235 |
SocketConnection conn; |
| 236 |
try { |
| 237 |
conn = yield inner.accept_async(null, out cid); |
| 238 |
} catch (Error e) { |
| 239 |
break; |
| 240 |
} |
| 241 |
handle_conn.begin(((StringWrapper)cid).str, conn); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
async void handle_conn(string cid, SocketConnection conn) { |
| 246 |
conn.socket.timeout = NEGOTIATION_TIMEOUT; |
| 247 |
size_t read; |
| 248 |
size_t written; |
| 249 |
uint8[] read_buffer = new uint8[1024]; |
| 250 |
ByteArray write_buffer = new ByteArray(); |
| 251 |
|
| 252 |
try { |
| 253 |
// 05 SOCKS version 5 |
| 254 |
// ?? number of authentication methods |
| 255 |
yield conn.input_stream.read_all_async(read_buffer[0:2], GLib.Priority.DEFAULT, null, out read); |
| 256 |
if (read != 2) { |
| 257 |
throw new IOError.PROXY_FAILED("wanted client hello message consisting of 2 bytes, only got %d bytes".printf((int)read)); |
| 258 |
} |
| 259 |
if (read_buffer[0] != 0x05 || read_buffer[1] == 0) { |
| 260 |
throw new IOError.PROXY_FAILED("wanted 05 xx, got %02x %02x".printf(read_buffer[0], read_buffer[1])); |
| 261 |
} |
| 262 |
int num_auth_methods = read_buffer[1]; |
| 263 |
// ?? authentication method (num_auth_methods times) |
| 264 |
yield conn.input_stream.read_all_async(read_buffer[0:num_auth_methods], GLib.Priority.DEFAULT, null, out read); |
| 265 |
bool found_null_auth = false; |
| 266 |
for (int i = 0; i < read; i++) { |
| 267 |
if (read_buffer[i] == 0x00) { |
| 268 |
found_null_auth = true; |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
if (read != num_auth_methods || !found_null_auth) { |
| 273 |
throw new IOError.PROXY_FAILED("peer didn't offer null auth"); |
| 274 |
} |
| 275 |
// 05 SOCKS version 5 |
| 276 |
// 00 nop authentication |
| 277 |
yield conn.output_stream.write_all_async({0x05, 0x00}, GLib.Priority.DEFAULT, null, out written); |
| 278 |
|
| 279 |
// 05 SOCKS version 5 |
| 280 |
// 01 connect |
| 281 |
// 00 reserved |
| 282 |
// 03 address type: domain name |
| 283 |
// ?? length of the domain |
| 284 |
// .. domain |
| 285 |
// 00 port 0 (upper half) |
| 286 |
// 00 port 0 (lower half) |
| 287 |
yield conn.input_stream.read_all_async(read_buffer[0:4], GLib.Priority.DEFAULT, null, out read); |
| 288 |
if (read != 4) { |
| 289 |
throw new IOError.PROXY_FAILED("wanted connect message consisting of 4 bytes, only got %d bytes".printf((int)read)); |
| 290 |
} |
| 291 |
if (read_buffer[0] != 0x05 || read_buffer[1] != 0x01 || read_buffer[3] != 0x03) { |
| 292 |
throw new IOError.PROXY_FAILED("wanted 05 00 ?? 03, got %02x %02x %02x %02x".printf(read_buffer[0], read_buffer[1], read_buffer[2], read_buffer[3])); |
| 293 |
} |
| 294 |
yield conn.input_stream.read_all_async(read_buffer[0:1], GLib.Priority.DEFAULT, null, out read); |
| 295 |
if (read != 1) { |
| 296 |
throw new IOError.PROXY_FAILED("wanted length of dstaddr consisting of 1 byte, only got %d bytes".printf((int)read)); |
| 297 |
} |
| 298 |
int dstaddr_len = read_buffer[0]; |
| 299 |
yield conn.input_stream.read_all_async(read_buffer[0:dstaddr_len+2], GLib.Priority.DEFAULT, null, out read); |
| 300 |
if (read != dstaddr_len + 2) { |
| 301 |
throw new IOError.PROXY_FAILED("wanted dstaddr and port consisting of %d bytes, got %d bytes".printf(dstaddr_len + 2, (int)read)); |
| 302 |
} |
| 303 |
if (!bytes_equal(read_buffer[0:dstaddr_len], dstaddr.data)) { |
| 304 |
string repr = ((string)read_buffer[0:dstaddr.length]).make_valid().escape(); |
| 305 |
throw new IOError.PROXY_FAILED(@"wanted dstaddr $(dstaddr), got $(repr)"); |
| 306 |
} |
| 307 |
if (read_buffer[dstaddr_len] != 0x00 || read_buffer[dstaddr_len + 1] != 0x00) { |
| 308 |
throw new IOError.PROXY_FAILED("wanted 00 00, got %02x %02x".printf(read_buffer[dstaddr_len], read_buffer[dstaddr_len + 1])); |
| 309 |
} |
| 310 |
|
| 311 |
// 05 SOCKS version 5 |
| 312 |
// 00 success |
| 313 |
// 00 reserved |
| 314 |
// 03 address type: domain name |
| 315 |
// ?? length of the domain |
| 316 |
// .. domain |
| 317 |
// 00 port 0 (upper half) |
| 318 |
// 00 port 0 (lower half) |
| 319 |
write_buffer.append({0x05, 0x00, 0x00, 0x03}); |
| 320 |
write_buffer.append({(uint8)dstaddr.length}); |
| 321 |
write_buffer.append(dstaddr.data); |
| 322 |
write_buffer.append({0x00, 0x00}); |
| 323 |
yield conn.output_stream.write_all_async(write_buffer.data, GLib.Priority.DEFAULT, null, out written); |
| 324 |
|
| 325 |
conn.socket.timeout = 0; |
| 326 |
if (!connections.has_key(cid)) { |
| 327 |
connections[cid] = conn; |
| 328 |
} |
| 329 |
} catch (Error e) { |
| 330 |
warning("Jingle SOCKS5 bytestream establish error cid %s: %s", cid, e.message); |
| 331 |
// Send a "general SOCKS server failure" (0x01) |
| 332 |
yield conn.output_stream.write_all_async({0x05, 0x01}, GLib.Priority.DEFAULT, null, out written); |
| 333 |
yield conn.output_stream.close_async(); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
public SocketConnection? get_connection(string cid) { |
| 338 |
if (!connections.has_key(cid)) { |
| 339 |
return null; |
| 340 |
} |
| 341 |
return connections[cid]; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
class Parameters : Jingle.TransportParameters, Object { |
| 346 |
public string ns_uri { get { return NS_URI; } } |
| 347 |
public uint8 components { get { return 1; } } |
| 348 |
public Jingle.Role role { get; private set; } |
| 349 |
public string sid { get; private set; } |
| 350 |
public string remote_dstaddr { get; private set; } |
| 351 |
public string local_dstaddr { get; private set; } |
| 352 |
public Gee.List<Candidate> local_candidates = new ArrayList<Candidate>(); |
| 353 |
public Gee.List<Candidate> remote_candidates = new ArrayList<Candidate>(); |
| 354 |
public LocalListener? listener = null; |
| 355 |
|
| 356 |
Jid local_full_jid; |
| 357 |
Jid peer_full_jid; |
| 358 |
|
| 359 |
bool remote_sent_selected_candidate = false; |
| 360 |
Candidate? remote_selected_candidate = null; |
| 361 |
bool local_determined_selected_candidate = false; |
| 362 |
Candidate? local_selected_candidate = null; |
| 363 |
SocketConnection? local_selected_candidate_conn = null; |
| 364 |
weak Jingle.Session? session = null; |
| 365 |
weak Jingle.Content? content = null; |
| 366 |
XmppStream? hack = null; |
| 367 |
|
| 368 |
string? waiting_for_activation_cid = null; |
| 369 |
SourceFunc waiting_for_activation_callback; |
| 370 |
bool waiting_for_activation_error = false; |
| 371 |
|
| 372 |
private Parameters(Jingle.Role role, string sid, Jid local_full_jid, Jid peer_full_jid, string? remote_dstaddr) { |
| 373 |
this.role = role; |
| 374 |
this.sid = sid; |
| 375 |
this.local_dstaddr = calculate_dstaddr(sid, local_full_jid, peer_full_jid); |
| 376 |
this.remote_dstaddr = remote_dstaddr ?? calculate_dstaddr(sid, peer_full_jid, local_full_jid); |
| 377 |
|
| 378 |
this.local_full_jid = local_full_jid; |
| 379 |
this.peer_full_jid = peer_full_jid; |
| 380 |
} |
| 381 |
|
| 382 |
public Parameters.create(Jid local_full_jid, Jid peer_full_jid, string sid) { |
| 383 |
this(Jingle.Role.INITIATOR, sid, local_full_jid, peer_full_jid, null); |
| 384 |
} |
| 385 |
|
| 386 |
public static Parameters parse(Jid local_full_jid, Jid peer_full_jid, StanzaNode transport) throws Jingle.IqError { |
| 387 |
string? dstaddr = transport.get_attribute("dstaddr"); |
| 388 |
string? mode = transport.get_attribute("mode"); |
| 389 |
string? sid = transport.get_attribute("sid"); |
| 390 |
if (mode != null && mode != "tcp") { |
| 391 |
throw new Jingle.IqError.BAD_REQUEST(@"unknown transport method $(mode)"); |
| 392 |
} |
| 393 |
if (dstaddr != null && dstaddr.length > 255) { |
| 394 |
throw new Jingle.IqError.BAD_REQUEST("too long dstaddr"); |
| 395 |
} |
| 396 |
Parameters result = new Parameters(Jingle.Role.RESPONDER, sid, local_full_jid, peer_full_jid, dstaddr); |
| 397 |
foreach (StanzaNode candidate in transport.get_subnodes("candidate", NS_URI)) { |
| 398 |
result.remote_candidates.add(Candidate.parse(candidate)); |
| 399 |
} |
| 400 |
return result; |
| 401 |
} |
| 402 |
|
| 403 |
public void set_content(Jingle.Content content) { |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
public StanzaNode to_transport_stanza_node(string action_type) { |
| 408 |
StanzaNode transport = new StanzaNode.build("transport", NS_URI) |
| 409 |
.add_self_xmlns() |
| 410 |
.put_attribute("dstaddr", local_dstaddr); |
| 411 |
|
| 412 |
if (role == Jingle.Role.INITIATOR) { |
| 413 |
// Must not be included by the responder according to XEP-0260. |
| 414 |
transport.put_attribute("mode", "tcp"); |
| 415 |
} |
| 416 |
|
| 417 |
transport.put_attribute("sid", sid); |
| 418 |
foreach (Candidate candidate in local_candidates) { |
| 419 |
transport.put_node(candidate.to_xml()); |
| 420 |
} |
| 421 |
return transport; |
| 422 |
} |
| 423 |
|
| 424 |
public void handle_transport_accept(StanzaNode transport) throws Jingle.IqError { |
| 425 |
Parameters other = Parameters.parse(local_full_jid, peer_full_jid, transport); |
| 426 |
if (other.sid != sid) { |
| 427 |
throw new Jingle.IqError.BAD_REQUEST("invalid sid"); |
| 428 |
} |
| 429 |
remote_candidates = other.remote_candidates; |
| 430 |
remote_dstaddr = other.remote_dstaddr; |
| 431 |
} |
| 432 |
|
| 433 |
public void handle_transport_info(StanzaNode transport) throws Jingle.IqError { |
| 434 |
ArrayList<StanzaNode> socks5_nodes = new ArrayList<StanzaNode>(); |
| 435 |
foreach (StanzaNode node in transport.sub_nodes) { |
| 436 |
if (node.ns_uri == NS_URI) socks5_nodes.add(node); |
| 437 |
} |
| 438 |
if (socks5_nodes.is_empty) { warning("No socks5 subnodes in transport node"); return; } |
| 439 |
if (socks5_nodes.size > 1) { warning("Too many socks5 subnodes in transport node"); return; } |
| 440 |
|
| 441 |
StanzaNode node = socks5_nodes[0]; |
| 442 |
|
| 443 |
switch (node.name) { |
| 444 |
case "activated": |
| 445 |
string? cid = node.get_attribute("cid"); |
| 446 |
if (cid == null) { |
| 447 |
throw new Jingle.IqError.BAD_REQUEST("missing cid"); |
| 448 |
} |
| 449 |
handle_activated(cid); |
| 450 |
break; |
| 451 |
case "candidate-used": |
| 452 |
string? cid = node.get_attribute("cid"); |
| 453 |
if (cid == null) { |
| 454 |
throw new Jingle.IqError.BAD_REQUEST("missing cid"); |
| 455 |
} |
| 456 |
handle_remote_candidate(cid); |
| 457 |
break; |
| 458 |
case "candidate-error": |
| 459 |
handle_remote_candidate(null); |
| 460 |
break; |
| 461 |
case "proxy-error": |
| 462 |
handle_proxy_error(); |
| 463 |
break; |
| 464 |
default: |
| 465 |
warning("Unknown transport-info: %s", transport.to_string()); |
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
private void handle_remote_candidate(string? cid) throws Jingle.IqError { |
| 471 |
if (remote_sent_selected_candidate) { |
| 472 |
throw new Jingle.IqError.BAD_REQUEST("remote candidate already specified"); |
| 473 |
} |
| 474 |
Candidate? candidate = null; |
| 475 |
if (cid != null) { |
| 476 |
foreach (Candidate c in local_candidates) { |
| 477 |
if (c.cid == cid) { |
| 478 |
candidate = c; |
| 479 |
break; |
| 480 |
} |
| 481 |
} |
| 482 |
if (candidate == null) { |
| 483 |
throw new Jingle.IqError.BAD_REQUEST("unknown cid"); |
| 484 |
} |
| 485 |
} |
| 486 |
remote_sent_selected_candidate = true; |
| 487 |
remote_selected_candidate = candidate; |
| 488 |
debug("Remote selected candidate %s", candidate != null ? candidate.cid : "(null)"); |
| 489 |
try_completing_negotiation(); |
| 490 |
} |
| 491 |
|
| 492 |
private void handle_activated(string cid) throws Jingle.IqError { |
| 493 |
if (waiting_for_activation_cid == null || cid != waiting_for_activation_cid) { |
| 494 |
throw new Jingle.IqError.BAD_REQUEST("unexpected proxy activation message"); |
| 495 |
} |
| 496 |
Idle.add((owned)waiting_for_activation_callback); |
| 497 |
waiting_for_activation_cid = null; |
| 498 |
} |
| 499 |
|
| 500 |
private void handle_proxy_error() throws Jingle.IqError { |
| 501 |
if (waiting_for_activation_cid == null) { |
| 502 |
throw new Jingle.IqError.BAD_REQUEST("unexpected proxy error message"); |
| 503 |
} |
| 504 |
Idle.add((owned)waiting_for_activation_callback); |
| 505 |
waiting_for_activation_cid = null; |
| 506 |
waiting_for_activation_error = true; |
| 507 |
|
| 508 |
} |
| 509 |
|
| 510 |
private void try_completing_negotiation() { |
| 511 |
if (!remote_sent_selected_candidate || !local_determined_selected_candidate) { |
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
if (remote_selected_candidate == null && local_selected_candidate == null) { |
| 516 |
content_set_transport_connection_error(new IOError.FAILED("No candidates")); |
| 517 |
return; |
| 518 |
} |
| 519 |
|
| 520 |
bool remote_wins; |
| 521 |
if (remote_selected_candidate != null && local_selected_candidate != null) { |
| 522 |
if (local_selected_candidate.priority == remote_selected_candidate.priority) { |
| 523 |
// equal priority -> XEP-0260 says that the candidate offered |
| 524 |
// by the initiator wins, so the one that the remote chose |
| 525 |
remote_wins = role == Jingle.Role.INITIATOR; |
| 526 |
} else { |
| 527 |
remote_wins = local_selected_candidate.priority < remote_selected_candidate.priority; |
| 528 |
} |
| 529 |
} else { |
| 530 |
remote_wins = remote_selected_candidate != null; |
| 531 |
} |
| 532 |
|
| 533 |
if (!remote_wins) { |
| 534 |
if (local_selected_candidate.type_ != CandidateType.PROXY) { |
| 535 |
Jingle.Session? strong = session; |
| 536 |
if (strong == null) { |
| 537 |
return; |
| 538 |
} |
| 539 |
content_set_transport_connection(local_selected_candidate_conn); |
| 540 |
} else { |
| 541 |
wait_for_remote_activation.begin(local_selected_candidate, local_selected_candidate_conn); |
| 542 |
} |
| 543 |
} else { |
| 544 |
if (remote_selected_candidate.type_ == CandidateType.DIRECT) { |
| 545 |
Jingle.Session? strong = session; |
| 546 |
if (strong == null) { |
| 547 |
return; |
| 548 |
} |
| 549 |
SocketConnection? conn = listener.get_connection(remote_selected_candidate.cid); |
| 550 |
if (conn == null) { |
| 551 |
content_set_transport_connection_error(new IOError.FAILED("Remote hasn't actually connected to us?!")); |
| 552 |
return; |
| 553 |
} |
| 554 |
content_set_transport_connection(conn); |
| 555 |
} else { |
| 556 |
connect_to_local_candidate.begin(remote_selected_candidate); |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
public async void wait_for_remote_activation(Candidate candidate, SocketConnection conn) { |
| 562 |
debug("Waiting for remote activation of %s", candidate.cid); |
| 563 |
waiting_for_activation_cid = candidate.cid; |
| 564 |
waiting_for_activation_callback = wait_for_remote_activation.callback; |
| 565 |
yield; |
| 566 |
|
| 567 |
Jingle.Session? strong = session; |
| 568 |
if (strong == null) { |
| 569 |
return; |
| 570 |
} |
| 571 |
if (!waiting_for_activation_error) { |
| 572 |
content_set_transport_connection(conn); |
| 573 |
} else { |
| 574 |
content_set_transport_connection_error(new IOError.FAILED("waiting_for_activation_error")); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
public async void connect_to_local_candidate(Candidate candidate) { |
| 579 |
debug("Connecting to candidate %s", candidate.cid); |
| 580 |
try { |
| 581 |
SocketConnection conn = yield connect_to_socks5(candidate, local_dstaddr); |
| 582 |
|
| 583 |
bool activation_error = false; |
| 584 |
SourceFunc callback = connect_to_local_candidate.callback; |
| 585 |
StanzaNode query = new StanzaNode.build("query", Socks5Bytestreams.NS_URI) |
| 586 |
.add_self_xmlns() |
| 587 |
.put_attribute("sid", sid) |
| 588 |
.put_node(new StanzaNode.build("activate", Socks5Bytestreams.NS_URI) |
| 589 |
.put_node(new StanzaNode.text(peer_full_jid.to_string())) |
| 590 |
); |
| 591 |
Iq.Stanza iq = new Iq.Stanza.set(query) { to=candidate.jid }; |
| 592 |
hack.get_module(Iq.Module.IDENTITY).send_iq(hack, iq, (stream, iq) => { |
| 593 |
activation_error = iq.is_error(); |
| 594 |
Idle.add((owned)callback); |
| 595 |
}); |
| 596 |
yield; |
| 597 |
|
| 598 |
if (activation_error) { |
| 599 |
throw new IOError.PROXY_FAILED("activation iq error"); |
| 600 |
} |
| 601 |
|
| 602 |
Jingle.Content? strong_content = content; |
| 603 |
if (strong_content == null) { |
| 604 |
return; |
| 605 |
} |
| 606 |
strong_content.send_transport_info(new StanzaNode.build("transport", NS_URI) |
| 607 |
.add_self_xmlns() |
| 608 |
.put_attribute("sid", sid) |
| 609 |
.put_node(new StanzaNode.build("activated", NS_URI) |
| 610 |
.put_attribute("cid", candidate.cid) |
| 611 |
) |
| 612 |
); |
| 613 |
|
| 614 |
content_set_transport_connection(conn); |
| 615 |
} catch (Error e) { |
| 616 |
Jingle.Content? strong_content = content; |
| 617 |
if (strong_content == null) { |
| 618 |
return; |
| 619 |
} |
| 620 |
strong_content.send_transport_info(new StanzaNode.build("transport", NS_URI) |
| 621 |
.add_self_xmlns() |
| 622 |
.put_attribute("sid", sid) |
| 623 |
.put_node(new StanzaNode.build("proxy-error", NS_URI)) |
| 624 |
); |
| 625 |
content_set_transport_connection_error(new IOError.FAILED("Connect to local candidate error: %s", e.message)); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
public async SocketConnection connect_to_socks5(Candidate candidate, string dstaddr) throws Error { |
| 630 |
SocketClient socket_client = new SocketClient() { timeout=NEGOTIATION_TIMEOUT }; |
| 631 |
|
| 632 |
string address = @"[$(candidate.host)]:$(candidate.port)"; |
| 633 |
debug("Connecting to SOCKS5 server at %s", address); |
| 634 |
|
| 635 |
size_t written; |
| 636 |
size_t read; |
| 637 |
uint8[] read_buffer = new uint8[1024]; |
| 638 |
ByteArray write_buffer = new ByteArray(); |
| 639 |
|
| 640 |
SocketConnection conn = yield socket_client.connect_to_host_async(address, 0); |
| 641 |
|
| 642 |
// 05 SOCKS version 5 |
| 643 |
// 01 number of authentication methods: 1 |
| 644 |
// 00 nop authentication |
| 645 |
yield conn.output_stream.write_all_async({0x05, 0x01, 0x00}, GLib.Priority.DEFAULT, null, out written); |
| 646 |
|
| 647 |
yield conn.input_stream.read_all_async(read_buffer[0:2], GLib.Priority.DEFAULT, null, out read); |
| 648 |
// 05 SOCKS version 5 |
| 649 |
// 00 nop authentication |
| 650 |
if (read != 2) { |
| 651 |
throw new IOError.PROXY_FAILED("wanted 05 00, only got %d bytes".printf((int)read)); |
| 652 |
} |
| 653 |
if (read_buffer[0] != 0x05 || read_buffer[1] != 0x00) { |
| 654 |
throw new IOError.PROXY_FAILED("wanted 05 00, got %02x %02x".printf(read_buffer[0], read_buffer[1])); |
| 655 |
} |
| 656 |
|
| 657 |
// 05 SOCKS version 5 |
| 658 |
// 01 connect |
| 659 |
// 00 reserved |
| 660 |
// 03 address type: domain name |
| 661 |
// ?? length of the domain |
| 662 |
// .. domain |
| 663 |
// 00 port 0 (upper half) |
| 664 |
// 00 port 0 (lower half) |
| 665 |
write_buffer.append({0x05, 0x01, 0x00, 0x03}); |
| 666 |
write_buffer.append({(uint8)dstaddr.length}); |
| 667 |
write_buffer.append(dstaddr.data); |
| 668 |
write_buffer.append({0x00, 0x00}); |
| 669 |
yield conn.output_stream.write_all_async(write_buffer.data, GLib.Priority.DEFAULT, null, out written); |
| 670 |
|
| 671 |
yield conn.input_stream.read_all_async(read_buffer[0:write_buffer.len], GLib.Priority.DEFAULT, null, out read); |
| 672 |
// 05 SOCKS version 5 |
| 673 |
// 00 success |
| 674 |
// 00 reserved |
| 675 |
// 03 address type: domain name |
| 676 |
// ?? length of the domain |
| 677 |
// .. domain |
| 678 |
// 00 port 0 (upper half) |
| 679 |
// 00 port 0 (lower half) |
| 680 |
if (read != write_buffer.len) { |
| 681 |
throw new IOError.PROXY_FAILED("wanted server success response consisting of %d bytes, only got %d bytes".printf((int)write_buffer.len, (int)read)); |
| 682 |
} |
| 683 |
if (read_buffer[0] != 0x05 || read_buffer[1] != 0x00 || read_buffer[3] != 0x03) { |
| 684 |
throw new IOError.PROXY_FAILED("wanted 05 00 ?? 03, got %02x %02x %02x %02x".printf(read_buffer[0], read_buffer[1], read_buffer[2], read_buffer[3])); |
| 685 |
} |
| 686 |
if (read_buffer[4] != (uint8)dstaddr.length) { |
| 687 |
throw new IOError.PROXY_FAILED("wanted %02x for length, got %02x".printf(dstaddr.length, read_buffer[4])); |
| 688 |
} |
| 689 |
if (!bytes_equal(read_buffer[5:5+dstaddr.length], dstaddr.data)) { |
| 690 |
string repr = ((string)read_buffer[5:5+dstaddr.length]).escape(); // TODO call make_valid() once glib>=2.52 becomes widespread |
| 691 |
throw new IOError.PROXY_FAILED(@"wanted dstaddr $(dstaddr), got $(repr)"); |
| 692 |
} |
| 693 |
if (read_buffer[5+dstaddr.length] != 0x00 || read_buffer[5+dstaddr.length+1] != 0x00) { |
| 694 |
throw new IOError.PROXY_FAILED("wanted port 00 00, got %02x %02x".printf(read_buffer[5+dstaddr.length], read_buffer[5+dstaddr.length+1])); |
| 695 |
} |
| 696 |
|
| 697 |
conn.socket.timeout = 0; |
| 698 |
|
| 699 |
return conn; |
| 700 |
} |
| 701 |
|
| 702 |
public async void try_connecting_to_candidates(XmppStream stream, Jingle.Session session) throws Error { |
| 703 |
remote_candidates.sort((c1, c2) => { |
| 704 |
// sort from priorities from high to low |
| 705 |
if (c1.priority < c2.priority) { return 1; } |
| 706 |
if (c1.priority > c2.priority) { return -1; } |
| 707 |
return 0; |
| 708 |
}); |
| 709 |
foreach (Candidate candidate in remote_candidates) { |
| 710 |
if (remote_selected_candidate != null && remote_selected_candidate.priority > candidate.priority) { |
| 711 |
// Don't try candidates with lower priority than the one the |
| 712 |
// peer already selected. |
| 713 |
break; |
| 714 |
} |
| 715 |
try { |
| 716 |
SocketConnection conn = yield connect_to_socks5(candidate, remote_dstaddr); |
| 717 |
|
| 718 |
local_determined_selected_candidate = true; |
| 719 |
local_selected_candidate = candidate; |
| 720 |
local_selected_candidate_conn = conn; |
| 721 |
debug("Selected candidate %s", candidate.cid); |
| 722 |
content.send_transport_info(new StanzaNode.build("transport", NS_URI) |
| 723 |
.add_self_xmlns() |
| 724 |
.put_attribute("sid", sid) |
| 725 |
.put_node(new StanzaNode.build("candidate-used", NS_URI) |
| 726 |
.put_attribute("cid", candidate.cid) |
| 727 |
) |
| 728 |
); |
| 729 |
try_completing_negotiation(); |
| 730 |
return; |
| 731 |
} catch (Error e) { |
| 732 |
// An error in the connection establishment isn't fatal, just |
| 733 |
// try the next candidate or respond that none of the |
| 734 |
// candidates work. |
| 735 |
} |
| 736 |
} |
| 737 |
local_determined_selected_candidate = true; |
| 738 |
local_selected_candidate = null; |
| 739 |
content.send_transport_info(new StanzaNode.build("transport", NS_URI) |
| 740 |
.add_self_xmlns() |
| 741 |
.put_attribute("sid", sid) |
| 742 |
.put_node(new StanzaNode.build("candidate-error", NS_URI)) |
| 743 |
); |
| 744 |
// Try remote candidates |
| 745 |
try_completing_negotiation(); |
| 746 |
} |
| 747 |
|
| 748 |
private Jingle.StreamingConnection connection = new Jingle.StreamingConnection(); |
| 749 |
|
| 750 |
private void content_set_transport_connection(IOStream ios) { |
| 751 |
IOStream iostream = ios; |
| 752 |
Jingle.Content? strong_content = content; |
| 753 |
if (strong_content == null) return; |
| 754 |
|
| 755 |
if (strong_content.security_params != null) { |
| 756 |
iostream = strong_content.security_params.wrap_stream(iostream); |
| 757 |
} |
| 758 |
connection.set_stream.begin(iostream); |
| 759 |
} |
| 760 |
|
| 761 |
private void content_set_transport_connection_error(Error e) { |
| 762 |
Jingle.Content? strong_content = content; |
| 763 |
if (strong_content == null) return; |
| 764 |
|
| 765 |
strong_content.select_new_transport.begin(); |
| 766 |
connection.set_error(e); |
| 767 |
} |
| 768 |
|
| 769 |
public void create_transport_connection(XmppStream stream, Jingle.Content content) { |
| 770 |
this.session = content.session; |
| 771 |
this.content = content; |
| 772 |
this.hack = stream; |
| 773 |
try_connecting_to_candidates.begin(stream, session); |
| 774 |
this.content.set_transport_connection(connection, 1); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
} |