Branch data Line data Source code
1 : : /* GIO - GLib Input, Output and Streaming Library
2 : : *
3 : : * Copyright (C) 2008, 2010 Collabora, Ltd.
4 : : * Copyright (C) 2008 Nokia Corporation. All rights reserved.
5 : : *
6 : : * SPDX-License-Identifier: LGPL-2.1-or-later
7 : : *
8 : : * This library is free software; you can redistribute it and/or
9 : : * modify it under the terms of the GNU Lesser General Public
10 : : * License as published by the Free Software Foundation; either
11 : : * version 2.1 of the License, or (at your option) any later version.
12 : : *
13 : : * This library is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : : * Lesser General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU Lesser General
19 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 : : *
21 : : * Author: Youness Alaoui <youness.alaoui@collabora.co.uk
22 : : *
23 : : * Contributors:
24 : : * Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
25 : : */
26 : :
27 : : #include "config.h"
28 : :
29 : : #include "gsocks5proxy.h"
30 : :
31 : : #include <string.h>
32 : :
33 : : #include "giomodule.h"
34 : : #include "giomodule-priv.h"
35 : : #include "giostream.h"
36 : : #include "ginetaddress.h"
37 : : #include "ginputstream.h"
38 : : #include "glibintl.h"
39 : : #include "goutputstream.h"
40 : : #include "gproxy.h"
41 : : #include "gproxyaddress.h"
42 : : #include "gtask.h"
43 : :
44 : : #define SOCKS5_VERSION 0x05
45 : :
46 : : #define SOCKS5_CMD_CONNECT 0x01
47 : : #define SOCKS5_CMD_BIND 0x02
48 : : #define SOCKS5_CMD_UDP_ASSOCIATE 0x03
49 : :
50 : : #define SOCKS5_ATYP_IPV4 0x01
51 : : #define SOCKS5_ATYP_DOMAINNAME 0x03
52 : : #define SOCKS5_ATYP_IPV6 0x04
53 : :
54 : : #define SOCKS5_AUTH_VERSION 0x01
55 : :
56 : : #define SOCKS5_AUTH_NONE 0x00
57 : : #define SOCKS5_AUTH_GSSAPI 0x01
58 : : #define SOCKS5_AUTH_USR_PASS 0x02
59 : : #define SOCKS5_AUTH_NO_ACCEPT 0xff
60 : :
61 : : #define SOCKS5_MAX_LEN 255
62 : : #define SOCKS5_RESERVED 0x00
63 : :
64 : : #define SOCKS5_REP_SUCCEEDED 0x00
65 : : #define SOCKS5_REP_SRV_FAILURE 0x01
66 : : #define SOCKS5_REP_NOT_ALLOWED 0x02
67 : : #define SOCKS5_REP_NET_UNREACH 0x03
68 : : #define SOCKS5_REP_HOST_UNREACH 0x04
69 : : #define SOCKS5_REP_REFUSED 0x05
70 : : #define SOCKS5_REP_TTL_EXPIRED 0x06
71 : : #define SOCKS5_REP_CMD_NOT_SUP 0x07
72 : : #define SOCKS5_REP_ATYPE_NOT_SUP 0x08
73 : :
74 : :
75 : : struct _GSocks5Proxy
76 : : {
77 : : GObject parent;
78 : : };
79 : :
80 : : struct _GSocks5ProxyClass
81 : : {
82 : : GObjectClass parent_class;
83 : : };
84 : :
85 : : static void g_socks5_proxy_iface_init (GProxyInterface *proxy_iface);
86 : :
87 : : #define g_socks5_proxy_get_type _g_socks5_proxy_get_type
88 : 481 : G_DEFINE_TYPE_WITH_CODE (GSocks5Proxy, g_socks5_proxy, G_TYPE_OBJECT,
89 : 63 : G_IMPLEMENT_INTERFACE (G_TYPE_PROXY,
90 : : g_socks5_proxy_iface_init)
91 : 63 : _g_io_modules_ensure_extension_points_registered ();
92 : 63 : g_io_extension_point_implement (G_PROXY_EXTENSION_POINT_NAME,
93 : 63 : g_define_type_id,
94 : : "socks5",
95 : : 0))
96 : :
97 : : static void
98 : 0 : g_socks5_proxy_finalize (GObject *object)
99 : : {
100 : : /* must chain up */
101 : 0 : G_OBJECT_CLASS (g_socks5_proxy_parent_class)->finalize (object);
102 : 0 : }
103 : :
104 : : static void
105 : 0 : g_socks5_proxy_init (GSocks5Proxy *proxy)
106 : : {
107 : 0 : }
108 : :
109 : : /*
110 : : * +----+----------+----------+
111 : : * |VER | NMETHODS | METHODS |
112 : : * +----+----------+----------+
113 : : * | 1 | 1 | 1 to 255 |
114 : : * +----+----------+----------+
115 : : */
116 : : #define SOCKS5_NEGO_MSG_LEN 4
117 : : static size_t
118 : 0 : set_nego_msg (guint8 *msg, gboolean has_auth)
119 : : {
120 : 0 : size_t len = 3;
121 : :
122 : 0 : msg[0] = SOCKS5_VERSION;
123 : 0 : msg[1] = 0x01; /* number of methods supported */
124 : 0 : msg[2] = SOCKS5_AUTH_NONE;
125 : :
126 : : /* add support for authentication method */
127 : 0 : if (has_auth)
128 : : {
129 : 0 : msg[1] = 0x02; /* number of methods supported */
130 : 0 : msg[3] = SOCKS5_AUTH_USR_PASS;
131 : 0 : len++;
132 : 0 : }
133 : :
134 : 0 : return len;
135 : : }
136 : :
137 : :
138 : : /*
139 : : * +----+--------+
140 : : * |VER | METHOD |
141 : : * +----+--------+
142 : : * | 1 | 1 |
143 : : * +----+--------+
144 : : */
145 : : #define SOCKS5_NEGO_REP_LEN 2
146 : : static gboolean
147 : 0 : parse_nego_reply (const guint8 *data,
148 : : gboolean has_auth,
149 : : gboolean *must_auth,
150 : : GError **error)
151 : : {
152 : 0 : if (data[0] != SOCKS5_VERSION)
153 : : {
154 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
155 : 0 : _("The server is not a SOCKSv5 proxy server."));
156 : 0 : return FALSE;
157 : : }
158 : :
159 : 0 : switch (data[1])
160 : : {
161 : 0 : case SOCKS5_AUTH_NONE:
162 : 0 : *must_auth = FALSE;
163 : 0 : break;
164 : :
165 : 0 : case SOCKS5_AUTH_USR_PASS:
166 : 0 : if (!has_auth)
167 : : {
168 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_NEED_AUTH,
169 : 0 : _("The SOCKSv5 proxy requires authentication."));
170 : 0 : return FALSE;
171 : : }
172 : 0 : *must_auth = TRUE;
173 : 0 : break;
174 : :
175 : 0 : case SOCKS5_AUTH_NO_ACCEPT:
176 : 0 : if (!has_auth)
177 : : {
178 : : /* The server has said it accepts none of our authentication methods,
179 : : * but given the slightly odd implementation of set_nego_msg(), we
180 : : * actually only gave it the choice of %SOCKS5_AUTH_NONE, since the
181 : : * caller specified no username or password.
182 : : * Return %G_IO_ERROR_PROXY_NEED_AUTH so the caller knows that if
183 : : * they specify a username and password and try again, authentication
184 : : * might succeed (since we’ll send %SOCKS5_AUTH_USR_PASS next time). */
185 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_NEED_AUTH,
186 : 0 : _("The SOCKSv5 proxy requires authentication."));
187 : 0 : return FALSE;
188 : : }
189 : : G_GNUC_FALLTHROUGH;
190 : 0 : case SOCKS5_AUTH_GSSAPI:
191 : : default:
192 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_AUTH_FAILED,
193 : 0 : _("The SOCKSv5 proxy requires an authentication "
194 : : "method that is not supported by GLib."));
195 : 0 : return FALSE;
196 : : break;
197 : : }
198 : :
199 : 0 : return TRUE;
200 : 0 : }
201 : :
202 : : #define SOCKS5_AUTH_MSG_LEN 515
203 : : static gboolean
204 : 0 : set_auth_msg (guint8 *msg,
205 : : const gchar *username,
206 : : const gchar *password,
207 : : size_t *out_len,
208 : : GError **error)
209 : : {
210 : 0 : size_t len = 0;
211 : 0 : size_t ulen = 0; /* username length */
212 : 0 : size_t plen = 0; /* Password length */
213 : :
214 : : /* Clear output first */
215 : 0 : if (out_len != NULL)
216 : 0 : *out_len = 0;
217 : :
218 : 0 : if (username)
219 : 0 : ulen = strlen (username);
220 : :
221 : 0 : if (password)
222 : 0 : plen = strlen (password);
223 : :
224 : 0 : if (ulen > SOCKS5_MAX_LEN || plen > SOCKS5_MAX_LEN)
225 : : {
226 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
227 : 0 : _("Username or password is too long for SOCKSv5 "
228 : : "protocol."));
229 : 0 : return FALSE;
230 : : }
231 : :
232 : 0 : msg[len++] = SOCKS5_AUTH_VERSION;
233 : 0 : msg[len++] = ulen;
234 : :
235 : 0 : if (ulen > 0)
236 : 0 : memcpy (msg + len, username, ulen);
237 : :
238 : 0 : len += ulen;
239 : 0 : msg[len++] = plen;
240 : :
241 : 0 : if (plen > 0)
242 : 0 : memcpy (msg + len, password, plen);
243 : :
244 : 0 : len += plen;
245 : :
246 : 0 : if (out_len != NULL)
247 : 0 : *out_len = len;
248 : :
249 : 0 : return TRUE;
250 : 0 : }
251 : :
252 : :
253 : : static gboolean
254 : 0 : check_auth_status (const guint8 *data, GError **error)
255 : : {
256 : 0 : if (data[0] != SOCKS5_AUTH_VERSION
257 : 0 : || data[1] != SOCKS5_REP_SUCCEEDED)
258 : : {
259 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_AUTH_FAILED,
260 : 0 : _("SOCKSv5 authentication failed due to wrong "
261 : : "username or password."));
262 : 0 : return FALSE;
263 : : }
264 : 0 : return TRUE;
265 : 0 : }
266 : :
267 : : /*
268 : : * +----+-----+-------+------+----------+----------+
269 : : * |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
270 : : * +----+-----+-------+------+----------+----------+
271 : : * | 1 | 1 | X'00' | 1 | Variable | 2 |
272 : : * +----+-----+-------+------+----------+----------+
273 : : * DST.ADDR is a string with first byte being the size. So DST.ADDR may not be
274 : : * longer then 256 bytes.
275 : : */
276 : : #define SOCKS5_CONN_MSG_LEN 262
277 : : static gboolean
278 : 0 : set_connect_msg (guint8 *msg,
279 : : const gchar *hostname,
280 : : guint16 port,
281 : : size_t *out_len,
282 : : GError **error)
283 : : {
284 : 0 : size_t len = 0;
285 : :
286 : : /* Clear output first */
287 : 0 : if (out_len != NULL)
288 : 0 : *out_len = 0;
289 : :
290 : 0 : msg[len++] = SOCKS5_VERSION;
291 : 0 : msg[len++] = SOCKS5_CMD_CONNECT;
292 : 0 : msg[len++] = SOCKS5_RESERVED;
293 : :
294 : 0 : if (g_hostname_is_ip_address (hostname))
295 : : {
296 : 0 : GInetAddress *addr = g_inet_address_new_from_string (hostname);
297 : : gsize addr_len;
298 : :
299 : 0 : if (addr == NULL)
300 : : {
301 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
302 : 0 : _("Invalid IP address “%s”"),
303 : 0 : hostname);
304 : 0 : return FALSE;
305 : : }
306 : :
307 : 0 : addr_len = g_inet_address_get_native_size (addr);
308 : : /* We are cheating for simplicity, here's the logic:
309 : : * 1 = IPV4 = 4 bytes / 4
310 : : * 4 = IPV6 = 16 bytes / 4 */
311 : 0 : msg[len++] = addr_len / 4;
312 : 0 : memcpy (msg + len, g_inet_address_to_bytes (addr), addr_len);
313 : 0 : len += addr_len;
314 : :
315 : 0 : g_object_unref (addr);
316 : 0 : }
317 : : else
318 : : {
319 : 0 : gsize host_len = strlen (hostname);
320 : :
321 : 0 : if (host_len > SOCKS5_MAX_LEN)
322 : : {
323 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
324 : 0 : _("Hostname “%s” is too long for SOCKSv5 protocol"),
325 : 0 : hostname);
326 : 0 : return FALSE;
327 : : }
328 : :
329 : 0 : msg[len++] = SOCKS5_ATYP_DOMAINNAME;
330 : 0 : msg[len++] = (guint8) host_len;
331 : 0 : memcpy (msg + len, hostname, host_len);
332 : 0 : len += host_len;
333 : : }
334 : :
335 : : {
336 : 0 : guint16 hp = g_htons (port);
337 : 0 : memcpy (msg + len, &hp, 2);
338 : 0 : len += 2;
339 : : }
340 : :
341 : 0 : if (out_len != NULL)
342 : 0 : *out_len = len;
343 : :
344 : 0 : return TRUE;
345 : 0 : }
346 : :
347 : : /*
348 : : * +----+-----+-------+------+----------+----------+
349 : : * |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
350 : : * +----+-----+-------+------+----------+----------+
351 : : * | 1 | 1 | X'00' | 1 | Variable | 2 |
352 : : * +----+-----+-------+------+----------+----------+
353 : : * This reply need to be read by small part to determine size. Buffer
354 : : * size is determined in function of the biggest part to read.
355 : : *
356 : : * The parser only requires 4 bytes.
357 : : */
358 : : #define SOCKS5_CONN_REP_LEN 257
359 : : static gboolean
360 : 0 : parse_connect_reply (const guint8 *data, gint *atype, GError **error)
361 : : {
362 : 0 : if (data[0] != SOCKS5_VERSION)
363 : : {
364 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
365 : 0 : _("The server is not a SOCKSv5 proxy server."));
366 : 0 : return FALSE;
367 : : }
368 : :
369 : 0 : switch (data[1])
370 : : {
371 : 0 : case SOCKS5_REP_SUCCEEDED:
372 : 0 : if (data[2] != SOCKS5_RESERVED)
373 : : {
374 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
375 : 0 : _("The server is not a SOCKSv5 proxy server."));
376 : 0 : return FALSE;
377 : : }
378 : :
379 : 0 : switch (data[3])
380 : : {
381 : 0 : case SOCKS5_ATYP_IPV4:
382 : : case SOCKS5_ATYP_IPV6:
383 : : case SOCKS5_ATYP_DOMAINNAME:
384 : 0 : *atype = data[3];
385 : 0 : break;
386 : :
387 : 0 : default:
388 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
389 : 0 : _("The SOCKSv5 proxy server uses unknown address type."));
390 : 0 : return FALSE;
391 : : }
392 : 0 : break;
393 : :
394 : 0 : case SOCKS5_REP_SRV_FAILURE:
395 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
396 : 0 : _("Internal SOCKSv5 proxy server error."));
397 : 0 : return FALSE;
398 : : break;
399 : :
400 : 0 : case SOCKS5_REP_NOT_ALLOWED:
401 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_NOT_ALLOWED,
402 : 0 : _("SOCKSv5 connection not allowed by ruleset."));
403 : 0 : return FALSE;
404 : : break;
405 : :
406 : 0 : case SOCKS5_REP_TTL_EXPIRED:
407 : : case SOCKS5_REP_HOST_UNREACH:
408 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_HOST_UNREACHABLE,
409 : 0 : _("Host unreachable through SOCKSv5 server."));
410 : 0 : return FALSE;
411 : : break;
412 : :
413 : 0 : case SOCKS5_REP_NET_UNREACH:
414 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NETWORK_UNREACHABLE,
415 : 0 : _("Network unreachable through SOCKSv5 proxy."));
416 : 0 : return FALSE;
417 : : break;
418 : :
419 : 0 : case SOCKS5_REP_REFUSED:
420 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED,
421 : 0 : _("Connection refused through SOCKSv5 proxy."));
422 : 0 : return FALSE;
423 : : break;
424 : :
425 : 0 : case SOCKS5_REP_CMD_NOT_SUP:
426 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
427 : 0 : _("SOCKSv5 proxy does not support “connect” command."));
428 : 0 : return FALSE;
429 : : break;
430 : :
431 : 0 : case SOCKS5_REP_ATYPE_NOT_SUP:
432 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
433 : 0 : _("SOCKSv5 proxy does not support provided address type."));
434 : 0 : return FALSE;
435 : : break;
436 : :
437 : 0 : default: /* Unknown error */
438 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
439 : 0 : _("Unknown SOCKSv5 proxy error."));
440 : 0 : return FALSE;
441 : : break;
442 : : }
443 : :
444 : 0 : return TRUE;
445 : 0 : }
446 : :
447 : : static GIOStream *
448 : 0 : g_socks5_proxy_connect (GProxy *proxy,
449 : : GIOStream *io_stream,
450 : : GProxyAddress *proxy_address,
451 : : GCancellable *cancellable,
452 : : GError **error)
453 : : {
454 : : gboolean has_auth;
455 : : GInputStream *in;
456 : : GOutputStream *out;
457 : : const gchar *hostname;
458 : : guint16 port;
459 : : const gchar *username;
460 : : const gchar *password;
461 : :
462 : 0 : hostname = g_proxy_address_get_destination_hostname (proxy_address);
463 : 0 : port = g_proxy_address_get_destination_port (proxy_address);
464 : 0 : username = g_proxy_address_get_username (proxy_address);
465 : 0 : password = g_proxy_address_get_password (proxy_address);
466 : :
467 : 0 : has_auth = username || password;
468 : :
469 : 0 : in = g_io_stream_get_input_stream (io_stream);
470 : 0 : out = g_io_stream_get_output_stream (io_stream);
471 : :
472 : : /* Send SOCKS5 handshake */
473 : : {
474 : : guint8 msg[SOCKS5_NEGO_MSG_LEN];
475 : : size_t len;
476 : :
477 : 0 : len = set_nego_msg (msg, has_auth);
478 : :
479 : 0 : if (!g_output_stream_write_all (out, msg, len, NULL,
480 : 0 : cancellable, error))
481 : 0 : goto error;
482 : : }
483 : :
484 : : /* Receive SOCKS5 response and reply with authentication if required */
485 : : {
486 : : guint8 data[SOCKS5_NEGO_REP_LEN];
487 : 0 : gboolean must_auth = FALSE;
488 : :
489 : 0 : if (!g_input_stream_read_all (in, data, sizeof (data), NULL,
490 : 0 : cancellable, error))
491 : 0 : goto error;
492 : :
493 : 0 : if (!parse_nego_reply (data, has_auth, &must_auth, error))
494 : 0 : goto error;
495 : :
496 : 0 : if (must_auth)
497 : : {
498 : : guint8 msg[SOCKS5_AUTH_MSG_LEN];
499 : : size_t len;
500 : :
501 : 0 : if (!set_auth_msg (msg, username, password, &len, error))
502 : 0 : goto error;
503 : :
504 : 0 : if (!g_output_stream_write_all (out, msg, len, NULL,
505 : 0 : cancellable, error))
506 : 0 : goto error;
507 : :
508 : 0 : if (!g_input_stream_read_all (in, data, sizeof (data), NULL,
509 : 0 : cancellable, error))
510 : 0 : goto error;
511 : :
512 : 0 : if (!check_auth_status (data, error))
513 : 0 : goto error;
514 : 0 : }
515 : : }
516 : :
517 : : /* Send SOCKS5 connection request */
518 : : {
519 : : guint8 msg[SOCKS5_CONN_MSG_LEN];
520 : : size_t len;
521 : :
522 : 0 : if (!set_connect_msg (msg, hostname, port, &len, error))
523 : 0 : goto error;
524 : :
525 : 0 : if (!g_output_stream_write_all (out, msg, len, NULL,
526 : 0 : cancellable, error))
527 : 0 : goto error;
528 : : }
529 : :
530 : : /* Read SOCKS5 response */
531 : : {
532 : : guint8 data[SOCKS5_CONN_REP_LEN];
533 : : gint atype;
534 : :
535 : 0 : if (!g_input_stream_read_all (in, data, 4 /* VER, REP, RSV, ATYP */, NULL,
536 : 0 : cancellable, error))
537 : 0 : goto error;
538 : :
539 : 0 : if (!parse_connect_reply (data, &atype, error))
540 : 0 : goto error;
541 : :
542 : 0 : switch (atype)
543 : : {
544 : 0 : case SOCKS5_ATYP_IPV4:
545 : 0 : if (!g_input_stream_read_all (in, data,
546 : : 4 /* IPv4 length */ + 2 /* port */,
547 : 0 : NULL, cancellable, error))
548 : 0 : goto error;
549 : 0 : break;
550 : :
551 : 0 : case SOCKS5_ATYP_IPV6:
552 : 0 : if (!g_input_stream_read_all (in, data,
553 : : 16 /* IPv6 length */ + 2 /* port */,
554 : 0 : NULL, cancellable, error))
555 : 0 : goto error;
556 : 0 : break;
557 : :
558 : 0 : case SOCKS5_ATYP_DOMAINNAME:
559 : 0 : if (!g_input_stream_read_all (in, data, 1 /* domain name length */,
560 : 0 : NULL, cancellable, error))
561 : 0 : goto error;
562 : 0 : if (!g_input_stream_read_all (in, data,
563 : 0 : data[0] /* domain name length */ + 2 /* port */,
564 : 0 : NULL, cancellable, error))
565 : 0 : goto error;
566 : 0 : break;
567 : : }
568 : : }
569 : :
570 : 0 : return g_object_ref (io_stream);
571 : :
572 : 0 : error:
573 : 0 : return NULL;
574 : 0 : }
575 : :
576 : :
577 : : typedef struct
578 : : {
579 : : GIOStream *io_stream;
580 : : gchar *hostname;
581 : : guint16 port;
582 : : gchar *username;
583 : : gchar *password;
584 : : guint8 *buffer;
585 : : size_t length;
586 : : size_t offset;
587 : : } ConnectAsyncData;
588 : :
589 : : static void nego_msg_write_cb (GObject *source,
590 : : GAsyncResult *res,
591 : : gpointer user_data);
592 : : static void nego_reply_read_cb (GObject *source,
593 : : GAsyncResult *res,
594 : : gpointer user_data);
595 : : static void auth_msg_write_cb (GObject *source,
596 : : GAsyncResult *res,
597 : : gpointer user_data);
598 : : static void auth_reply_read_cb (GObject *source,
599 : : GAsyncResult *result,
600 : : gpointer user_data);
601 : : static void send_connect_msg (GTask *task);
602 : : static void connect_msg_write_cb (GObject *source,
603 : : GAsyncResult *result,
604 : : gpointer user_data);
605 : : static void connect_reply_read_cb (GObject *source,
606 : : GAsyncResult *result,
607 : : gpointer user_data);
608 : : static void connect_addr_len_read_cb (GObject *source,
609 : : GAsyncResult *result,
610 : : gpointer user_data);
611 : : static void connect_addr_read_cb (GObject *source,
612 : : GAsyncResult *result,
613 : : gpointer user_data);
614 : :
615 : : static void
616 : 0 : free_connect_data (ConnectAsyncData *data)
617 : : {
618 : 0 : g_object_unref (data->io_stream);
619 : :
620 : 0 : g_free (data->hostname);
621 : 0 : g_free (data->username);
622 : 0 : g_free (data->password);
623 : 0 : g_free (data->buffer);
624 : :
625 : 0 : g_slice_free (ConnectAsyncData, data);
626 : 0 : }
627 : :
628 : : static void
629 : 0 : do_read (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
630 : : {
631 : : GInputStream *in;
632 : 0 : in = g_io_stream_get_input_stream (data->io_stream);
633 : 0 : g_input_stream_read_async (in,
634 : 0 : data->buffer + data->offset,
635 : 0 : data->length - data->offset,
636 : 0 : g_task_get_priority (task),
637 : 0 : g_task_get_cancellable (task),
638 : 0 : callback, task);
639 : 0 : }
640 : :
641 : : static void
642 : 0 : do_write (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
643 : : {
644 : : GOutputStream *out;
645 : 0 : out = g_io_stream_get_output_stream (data->io_stream);
646 : 0 : g_output_stream_write_async (out,
647 : 0 : data->buffer + data->offset,
648 : 0 : data->length - data->offset,
649 : 0 : g_task_get_priority (task),
650 : 0 : g_task_get_cancellable (task),
651 : 0 : callback, task);
652 : 0 : }
653 : :
654 : : static void
655 : 0 : g_socks5_proxy_connect_async (GProxy *proxy,
656 : : GIOStream *io_stream,
657 : : GProxyAddress *proxy_address,
658 : : GCancellable *cancellable,
659 : : GAsyncReadyCallback callback,
660 : : gpointer user_data)
661 : : {
662 : : GTask *task;
663 : : ConnectAsyncData *data;
664 : :
665 : 0 : data = g_slice_new0 (ConnectAsyncData);
666 : 0 : data->io_stream = g_object_ref (io_stream);
667 : :
668 : 0 : task = g_task_new (proxy, cancellable, callback, user_data);
669 : 0 : g_task_set_source_tag (task, g_socks5_proxy_connect_async);
670 : 0 : g_task_set_task_data (task, data, (GDestroyNotify) free_connect_data);
671 : :
672 : 0 : g_object_get (G_OBJECT (proxy_address),
673 : 0 : "destination-hostname", &data->hostname,
674 : 0 : "destination-port", &data->port,
675 : 0 : "username", &data->username,
676 : 0 : "password", &data->password,
677 : : NULL);
678 : :
679 : 0 : data->buffer = g_malloc0 (SOCKS5_NEGO_MSG_LEN);
680 : 0 : data->length = set_nego_msg (data->buffer,
681 : 0 : data->username || data->password);
682 : 0 : data->offset = 0;
683 : :
684 : 0 : do_write (nego_msg_write_cb, task, data);
685 : 0 : }
686 : :
687 : :
688 : : static void
689 : 0 : nego_msg_write_cb (GObject *source,
690 : : GAsyncResult *res,
691 : : gpointer user_data)
692 : : {
693 : 0 : GTask *task = user_data;
694 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
695 : 0 : GError *error = NULL;
696 : : gssize written;
697 : :
698 : 0 : written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
699 : 0 : res, &error);
700 : :
701 : 0 : if (written < 0)
702 : : {
703 : 0 : g_task_return_error (task, error);
704 : 0 : g_object_unref (task);
705 : 0 : return;
706 : : }
707 : :
708 : 0 : data->offset += written;
709 : :
710 : 0 : if (data->offset == data->length)
711 : : {
712 : 0 : g_free (data->buffer);
713 : :
714 : 0 : data->buffer = g_malloc0 (SOCKS5_NEGO_REP_LEN);
715 : 0 : data->length = SOCKS5_NEGO_REP_LEN;
716 : 0 : data->offset = 0;
717 : :
718 : 0 : do_read (nego_reply_read_cb, task, data);
719 : 0 : }
720 : : else
721 : : {
722 : 0 : do_write (nego_msg_write_cb, task, data);
723 : : }
724 : 0 : }
725 : :
726 : : static void
727 : 0 : nego_reply_read_cb (GObject *source,
728 : : GAsyncResult *res,
729 : : gpointer user_data)
730 : : {
731 : 0 : GTask *task = user_data;
732 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
733 : 0 : GError *error = NULL;
734 : : gssize read;
735 : :
736 : 0 : read = g_input_stream_read_finish (G_INPUT_STREAM (source),
737 : 0 : res, &error);
738 : :
739 : 0 : if (read < 0)
740 : : {
741 : 0 : g_task_return_error (task, error);
742 : 0 : g_object_unref (task);
743 : 0 : return;
744 : : }
745 : :
746 : 0 : if (read == 0)
747 : : {
748 : 0 : g_task_return_new_error_literal (task,
749 : 0 : G_IO_ERROR,
750 : : G_IO_ERROR_CONNECTION_CLOSED,
751 : : "Connection to SOCKSv5 proxy server lost");
752 : 0 : g_object_unref (task);
753 : 0 : return;
754 : : }
755 : :
756 : 0 : data->offset += read;
757 : :
758 : 0 : if (data->offset == data->length)
759 : : {
760 : 0 : gboolean must_auth = FALSE;
761 : 0 : gboolean has_auth = data->username || data->password;
762 : :
763 : 0 : if (!parse_nego_reply (data->buffer, has_auth, &must_auth, &error))
764 : : {
765 : 0 : g_task_return_error (task, error);
766 : 0 : g_object_unref (task);
767 : 0 : return;
768 : : }
769 : :
770 : 0 : if (must_auth)
771 : : {
772 : 0 : g_free (data->buffer);
773 : :
774 : 0 : data->buffer = g_malloc0 (SOCKS5_AUTH_MSG_LEN);
775 : 0 : data->offset = 0;
776 : :
777 : 0 : if (!set_auth_msg (data->buffer,
778 : 0 : data->username,
779 : 0 : data->password,
780 : 0 : &data->length,
781 : : &error))
782 : : {
783 : 0 : g_task_return_error (task, error);
784 : 0 : g_object_unref (task);
785 : 0 : return;
786 : : }
787 : :
788 : 0 : do_write (auth_msg_write_cb, task, data);
789 : 0 : }
790 : : else
791 : : {
792 : 0 : send_connect_msg (task);
793 : : }
794 : 0 : }
795 : : else
796 : : {
797 : 0 : do_read (nego_reply_read_cb, task, data);
798 : : }
799 : 0 : }
800 : :
801 : : static void
802 : 0 : auth_msg_write_cb (GObject *source,
803 : : GAsyncResult *result,
804 : : gpointer user_data)
805 : : {
806 : 0 : GTask *task = user_data;
807 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
808 : 0 : GError *error = NULL;
809 : : gssize written;
810 : :
811 : 0 : written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
812 : 0 : result, &error);
813 : :
814 : 0 : if (written < 0)
815 : : {
816 : 0 : g_task_return_error (task, error);
817 : 0 : g_object_unref (task);
818 : 0 : return;
819 : : }
820 : :
821 : 0 : data->offset += written;
822 : :
823 : 0 : if (data->offset == data->length)
824 : : {
825 : 0 : g_free (data->buffer);
826 : :
827 : 0 : data->buffer = g_malloc0 (SOCKS5_NEGO_REP_LEN);
828 : 0 : data->length = SOCKS5_NEGO_REP_LEN;
829 : 0 : data->offset = 0;
830 : :
831 : 0 : do_read (auth_reply_read_cb, task, data);
832 : 0 : }
833 : : else
834 : : {
835 : 0 : do_write (auth_msg_write_cb, task, data);
836 : : }
837 : 0 : }
838 : :
839 : : static void
840 : 0 : auth_reply_read_cb (GObject *source,
841 : : GAsyncResult *result,
842 : : gpointer user_data)
843 : : {
844 : 0 : GTask *task = user_data;
845 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
846 : 0 : GError *error = NULL;
847 : : gssize read;
848 : :
849 : 0 : read = g_input_stream_read_finish (G_INPUT_STREAM (source),
850 : 0 : result, &error);
851 : :
852 : 0 : if (read < 0)
853 : : {
854 : 0 : g_task_return_error (task, error);
855 : 0 : g_object_unref (task);
856 : 0 : return;
857 : : }
858 : :
859 : 0 : if (read == 0)
860 : : {
861 : 0 : g_task_return_new_error_literal (task,
862 : 0 : G_IO_ERROR,
863 : : G_IO_ERROR_CONNECTION_CLOSED,
864 : : "Connection to SOCKSv5 proxy server lost");
865 : 0 : g_object_unref (task);
866 : 0 : return;
867 : : }
868 : :
869 : 0 : data->offset += read;
870 : :
871 : 0 : if (data->offset == data->length)
872 : : {
873 : 0 : if (!check_auth_status (data->buffer, &error))
874 : : {
875 : 0 : g_task_return_error (task, error);
876 : 0 : g_object_unref (task);
877 : 0 : return;
878 : : }
879 : :
880 : 0 : send_connect_msg (task);
881 : 0 : }
882 : : else
883 : : {
884 : 0 : do_read (auth_reply_read_cb, task, data);
885 : : }
886 : 0 : }
887 : :
888 : : static void
889 : 0 : send_connect_msg (GTask *task)
890 : : {
891 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
892 : 0 : GError *error = NULL;
893 : :
894 : 0 : g_free (data->buffer);
895 : :
896 : 0 : data->buffer = g_malloc0 (SOCKS5_CONN_MSG_LEN);
897 : 0 : data->offset = 0;
898 : :
899 : 0 : if (!set_connect_msg (data->buffer,
900 : 0 : data->hostname,
901 : 0 : data->port,
902 : 0 : &data->length,
903 : : &error))
904 : : {
905 : 0 : g_task_return_error (task, error);
906 : 0 : g_object_unref (task);
907 : 0 : return;
908 : : }
909 : :
910 : 0 : do_write (connect_msg_write_cb, task, data);
911 : 0 : }
912 : :
913 : : static void
914 : 0 : connect_msg_write_cb (GObject *source,
915 : : GAsyncResult *result,
916 : : gpointer user_data)
917 : : {
918 : 0 : GTask *task = user_data;
919 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
920 : 0 : GError *error = NULL;
921 : : gssize written;
922 : :
923 : 0 : written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
924 : 0 : result, &error);
925 : :
926 : 0 : if (written < 0)
927 : : {
928 : 0 : g_task_return_error (task, error);
929 : 0 : g_object_unref (task);
930 : 0 : return;
931 : : }
932 : :
933 : 0 : data->offset += written;
934 : :
935 : 0 : if (data->offset == data->length)
936 : : {
937 : 0 : g_free (data->buffer);
938 : :
939 : 0 : data->buffer = g_malloc0 (SOCKS5_CONN_REP_LEN);
940 : 0 : data->length = 4;
941 : 0 : data->offset = 0;
942 : :
943 : 0 : do_read (connect_reply_read_cb, task, data);
944 : 0 : }
945 : : else
946 : : {
947 : 0 : do_write (connect_msg_write_cb, task, data);
948 : : }
949 : 0 : }
950 : :
951 : : static void
952 : 0 : connect_reply_read_cb (GObject *source,
953 : : GAsyncResult *result,
954 : : gpointer user_data)
955 : : {
956 : 0 : GTask *task = user_data;
957 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
958 : 0 : GError *error = NULL;
959 : : gssize read;
960 : :
961 : 0 : read = g_input_stream_read_finish (G_INPUT_STREAM (source),
962 : 0 : result, &error);
963 : :
964 : 0 : if (read < 0)
965 : : {
966 : 0 : g_task_return_error (task, error);
967 : 0 : g_object_unref (task);
968 : 0 : return;
969 : : }
970 : :
971 : 0 : if (read == 0)
972 : : {
973 : 0 : g_task_return_new_error_literal (task,
974 : 0 : G_IO_ERROR,
975 : : G_IO_ERROR_CONNECTION_CLOSED,
976 : : "Connection to SOCKSv5 proxy server lost");
977 : 0 : g_object_unref (task);
978 : 0 : return;
979 : : }
980 : :
981 : 0 : data->offset += read;
982 : :
983 : 0 : if (data->offset == data->length)
984 : : {
985 : : gint atype;
986 : :
987 : 0 : if (!parse_connect_reply (data->buffer, &atype, &error))
988 : : {
989 : 0 : g_task_return_error (task, error);
990 : 0 : g_object_unref (task);
991 : 0 : return;
992 : : }
993 : :
994 : 0 : switch (atype)
995 : : {
996 : 0 : case SOCKS5_ATYP_IPV4:
997 : 0 : data->length = 6;
998 : 0 : data->offset = 0;
999 : 0 : do_read (connect_addr_read_cb, task, data);
1000 : 0 : break;
1001 : :
1002 : 0 : case SOCKS5_ATYP_IPV6:
1003 : 0 : data->length = 18;
1004 : 0 : data->offset = 0;
1005 : 0 : do_read (connect_addr_read_cb, task, data);
1006 : 0 : break;
1007 : :
1008 : 0 : case SOCKS5_ATYP_DOMAINNAME:
1009 : 0 : data->length = 1;
1010 : 0 : data->offset = 0;
1011 : 0 : do_read (connect_addr_len_read_cb, task, data);
1012 : 0 : break;
1013 : : }
1014 : 0 : }
1015 : : else
1016 : : {
1017 : 0 : do_read (connect_reply_read_cb, task, data);
1018 : : }
1019 : 0 : }
1020 : :
1021 : : static void
1022 : 0 : connect_addr_len_read_cb (GObject *source,
1023 : : GAsyncResult *result,
1024 : : gpointer user_data)
1025 : : {
1026 : 0 : GTask *task = user_data;
1027 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
1028 : 0 : GError *error = NULL;
1029 : : gssize read;
1030 : :
1031 : 0 : read = g_input_stream_read_finish (G_INPUT_STREAM (source),
1032 : 0 : result, &error);
1033 : :
1034 : 0 : if (read < 0)
1035 : : {
1036 : 0 : g_task_return_error (task, error);
1037 : 0 : g_object_unref (task);
1038 : 0 : return;
1039 : : }
1040 : :
1041 : 0 : if (read == 0)
1042 : : {
1043 : 0 : g_task_return_new_error_literal (task,
1044 : 0 : G_IO_ERROR,
1045 : : G_IO_ERROR_CONNECTION_CLOSED,
1046 : : "Connection to SOCKSv5 proxy server lost");
1047 : 0 : g_object_unref (task);
1048 : 0 : return;
1049 : : }
1050 : :
1051 : 0 : data->length = data->buffer[0] + 2;
1052 : 0 : data->offset = 0;
1053 : :
1054 : 0 : do_read (connect_addr_read_cb, task, data);
1055 : 0 : }
1056 : :
1057 : : static void
1058 : 0 : connect_addr_read_cb (GObject *source,
1059 : : GAsyncResult *result,
1060 : : gpointer user_data)
1061 : : {
1062 : 0 : GTask *task = user_data;
1063 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
1064 : 0 : GError *error = NULL;
1065 : : gssize read;
1066 : :
1067 : 0 : read = g_input_stream_read_finish (G_INPUT_STREAM (source),
1068 : 0 : result, &error);
1069 : :
1070 : 0 : if (read < 0)
1071 : : {
1072 : 0 : g_task_return_error (task, error);
1073 : 0 : g_object_unref (task);
1074 : 0 : return;
1075 : : }
1076 : :
1077 : 0 : if (read == 0)
1078 : : {
1079 : 0 : g_task_return_new_error_literal (task,
1080 : 0 : G_IO_ERROR,
1081 : : G_IO_ERROR_CONNECTION_CLOSED,
1082 : : "Connection to SOCKSv5 proxy server lost");
1083 : 0 : g_object_unref (task);
1084 : 0 : return;
1085 : : }
1086 : :
1087 : 0 : data->offset += read;
1088 : :
1089 : 0 : if (data->offset == data->length)
1090 : : {
1091 : 0 : g_task_return_pointer (task, g_object_ref (data->io_stream), g_object_unref);
1092 : 0 : g_object_unref (task);
1093 : 0 : return;
1094 : : }
1095 : : else
1096 : : {
1097 : 0 : do_read (connect_reply_read_cb, task, data);
1098 : : }
1099 : 0 : }
1100 : :
1101 : : static GIOStream *
1102 : 0 : g_socks5_proxy_connect_finish (GProxy *proxy,
1103 : : GAsyncResult *result,
1104 : : GError **error)
1105 : : {
1106 : 0 : return g_task_propagate_pointer (G_TASK (result), error);
1107 : : }
1108 : :
1109 : : static gboolean
1110 : 0 : g_socks5_proxy_supports_hostname (GProxy *proxy)
1111 : : {
1112 : 0 : return TRUE;
1113 : : }
1114 : :
1115 : : static void
1116 : 0 : g_socks5_proxy_class_init (GSocks5ProxyClass *class)
1117 : : {
1118 : : GObjectClass *object_class;
1119 : :
1120 : 0 : object_class = (GObjectClass *) class;
1121 : 0 : object_class->finalize = g_socks5_proxy_finalize;
1122 : 0 : }
1123 : :
1124 : : static void
1125 : 0 : g_socks5_proxy_iface_init (GProxyInterface *proxy_iface)
1126 : : {
1127 : 0 : proxy_iface->connect = g_socks5_proxy_connect;
1128 : 0 : proxy_iface->connect_async = g_socks5_proxy_connect_async;
1129 : 0 : proxy_iface->connect_finish = g_socks5_proxy_connect_finish;
1130 : 0 : proxy_iface->supports_hostname = g_socks5_proxy_supports_hostname;
1131 : 0 : }
|