3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 /* this is a little piece of code to handle proxy connection */
24 /* it is intended to : 1st handle http proxy, using the CONNECT command
25 , 2nd provide an easy way to add socks support
26 , 3rd draw women to it like flies to honey */
33 #include "purple-gio.h"
37 #include <libsoup/soup.h>
39 struct _PurpleProxyInfo
41 PurpleProxyType type
; /* The proxy type. */
43 char *host
; /* The host. */
44 int port
; /* The port number. */
45 char *username
; /* The username. */
46 char *password
; /* The password. */
49 struct _PurpleProxyConnectData
{
51 PurpleProxyConnectFunction connect_cb
;
58 GCancellable
*cancellable
;
61 static PurpleProxyInfo
*global_proxy_info
= NULL
;
63 static GSList
*handles
= NULL
;
66 * TODO: Eventually (GObjectification) this bad boy will be removed, because it is
67 * a gross fix for a crashy problem.
69 #define PURPLE_PROXY_CONNECT_DATA_IS_VALID(connect_data) g_slist_find(handles, connect_data)
71 /**************************************************************************
73 **************************************************************************/
75 purple_proxy_info_new(void)
77 return g_new0(PurpleProxyInfo
, 1);
80 static PurpleProxyInfo
*
81 purple_proxy_info_copy(PurpleProxyInfo
*info
)
83 PurpleProxyInfo
*copy
;
85 g_return_val_if_fail(info
!= NULL
, NULL
);
87 copy
= purple_proxy_info_new();
88 copy
->type
= info
->type
;
89 copy
->host
= g_strdup(info
->host
);
90 copy
->port
= info
->port
;
91 copy
->username
= g_strdup(info
->username
);
92 copy
->password
= g_strdup(info
->password
);
98 purple_proxy_info_destroy(PurpleProxyInfo
*info
)
100 g_return_if_fail(info
!= NULL
);
103 g_free(info
->username
);
104 g_free(info
->password
);
110 purple_proxy_info_set_proxy_type(PurpleProxyInfo
*info
, PurpleProxyType type
)
112 g_return_if_fail(info
!= NULL
);
118 purple_proxy_info_set_host(PurpleProxyInfo
*info
, const char *host
)
120 g_return_if_fail(info
!= NULL
);
123 info
->host
= g_strdup(host
);
127 purple_proxy_info_set_port(PurpleProxyInfo
*info
, int port
)
129 g_return_if_fail(info
!= NULL
);
135 purple_proxy_info_set_username(PurpleProxyInfo
*info
, const char *username
)
137 g_return_if_fail(info
!= NULL
);
139 g_free(info
->username
);
140 info
->username
= g_strdup(username
);
144 purple_proxy_info_set_password(PurpleProxyInfo
*info
, const char *password
)
146 g_return_if_fail(info
!= NULL
);
148 g_free(info
->password
);
149 info
->password
= g_strdup(password
);
153 purple_proxy_info_get_proxy_type(const PurpleProxyInfo
*info
)
155 g_return_val_if_fail(info
!= NULL
, PURPLE_PROXY_NONE
);
161 purple_proxy_info_get_host(const PurpleProxyInfo
*info
)
163 g_return_val_if_fail(info
!= NULL
, NULL
);
169 purple_proxy_info_get_port(const PurpleProxyInfo
*info
)
171 g_return_val_if_fail(info
!= NULL
, 0);
177 purple_proxy_info_get_username(const PurpleProxyInfo
*info
)
179 g_return_val_if_fail(info
!= NULL
, NULL
);
181 return info
->username
;
185 purple_proxy_info_get_password(const PurpleProxyInfo
*info
)
187 g_return_val_if_fail(info
!= NULL
, NULL
);
189 return info
->password
;
192 G_DEFINE_BOXED_TYPE(PurpleProxyInfo
, purple_proxy_info
,
193 purple_proxy_info_copy
, purple_proxy_info_destroy
);
195 /**************************************************************************
197 **************************************************************************/
199 purple_global_proxy_get_info(void)
201 return global_proxy_info
;
205 purple_global_proxy_set_info(PurpleProxyInfo
*info
)
207 g_return_if_fail(info
!= NULL
);
209 purple_proxy_info_destroy(global_proxy_info
);
211 global_proxy_info
= info
;
215 /* index in gproxycmds below, keep them in sync */
216 #define GNOME_PROXY_MODE 0
217 #define GNOME_PROXY_USE_SAME_PROXY 1
218 #define GNOME_PROXY_SOCKS_HOST 2
219 #define GNOME_PROXY_SOCKS_PORT 3
220 #define GNOME_PROXY_HTTP_HOST 4
221 #define GNOME_PROXY_HTTP_PORT 5
222 #define GNOME_PROXY_HTTP_USER 6
223 #define GNOME_PROXY_HTTP_PASS 7
224 #define GNOME2_CMDS 0
225 #define GNOME3_CMDS 1
227 /* detect proxy settings for gnome2/gnome3 */
228 static const char* gproxycmds
[][2] = {
229 { "gconftool-2 -g /system/proxy/mode" , "gsettings get org.gnome.system.proxy mode" },
230 { "gconftool-2 -g /system/http_proxy/use_same_proxy", "gsettings get org.gnome.system.proxy use-same-proxy" },
231 { "gconftool-2 -g /system/proxy/socks_host", "gsettings get org.gnome.system.proxy.socks host" },
232 { "gconftool-2 -g /system/proxy/socks_port", "gsettings get org.gnome.system.proxy.socks port" },
233 { "gconftool-2 -g /system/http_proxy/host", "gsettings get org.gnome.system.proxy.http host" },
234 { "gconftool-2 -g /system/http_proxy/port", "gsettings get org.gnome.system.proxy.http port"},
235 { "gconftool-2 -g /system/http_proxy/authentication_user", "gsettings get org.gnome.system.proxy.http authentication-user" },
236 { "gconftool-2 -g /system/http_proxy/authentication_password", "gsettings get org.gnome.system.proxy.http authentication-password" },
240 * purple_gnome_proxy_get_parameter:
241 * @parameter: One of the GNOME_PROXY_x constants defined above
242 * @gnome_version: GNOME2_CMDS or GNOME3_CMDS
244 * This is a utility function used to retrieve proxy parameter values from
245 * GNOME 2/3 environment.
247 * Returns: The value of requested proxy parameter
250 purple_gnome_proxy_get_parameter(guint8 parameter
, guint8 gnome_version
)
255 if (parameter
> GNOME_PROXY_HTTP_PASS
)
257 if (gnome_version
> GNOME3_CMDS
)
260 if (!g_spawn_command_line_sync(gproxycmds
[parameter
][gnome_version
],
261 ¶m
, &err
, NULL
, NULL
))
266 if (param
[0] == '\'' || param
[0] == '\"') {
267 param_len
= strlen(param
);
268 memmove(param
, param
+ 1, param_len
); /* copy last \0 too */
270 if (param_len
> 0 && (param
[param_len
- 1] == '\'' || param
[param_len
- 1] == '\"'))
271 param
[param_len
- 1] = '\0';
278 static PurpleProxyInfo
*
279 purple_gnome_proxy_get_info(void)
281 static PurpleProxyInfo info
= {0, NULL
, 0, NULL
, NULL
};
282 gboolean use_same_proxy
= FALSE
;
284 guint8 gnome_version
= GNOME3_CMDS
;
286 tmp
= g_find_program_in_path("gsettings");
288 tmp
= g_find_program_in_path("gconftool-2");
289 gnome_version
= GNOME2_CMDS
;
292 return purple_global_proxy_get_info();
296 /* Check whether to use a proxy. */
297 tmp
= purple_gnome_proxy_get_parameter(GNOME_PROXY_MODE
, gnome_version
);
299 return purple_global_proxy_get_info();
301 if (purple_strequal(tmp
, "none")) {
302 info
.type
= PURPLE_PROXY_NONE
;
307 if (!purple_strequal(tmp
, "manual")) {
308 /* Unknown setting. Fallback to using our global proxy settings. */
310 return purple_global_proxy_get_info();
315 /* Free the old fields */
318 g_free(info
.username
);
319 info
.username
= NULL
;
320 g_free(info
.password
);
321 info
.password
= NULL
;
323 tmp
= purple_gnome_proxy_get_parameter(GNOME_PROXY_USE_SAME_PROXY
, gnome_version
);
325 return purple_global_proxy_get_info();
327 if (purple_strequal(tmp
, "true"))
328 use_same_proxy
= TRUE
;
332 if (!use_same_proxy
) {
333 info
.host
= purple_gnome_proxy_get_parameter(GNOME_PROXY_SOCKS_HOST
, gnome_version
);
335 return purple_global_proxy_get_info();
338 if (!use_same_proxy
&& (info
.host
!= NULL
) && (*info
.host
!= '\0')) {
339 info
.type
= PURPLE_PROXY_SOCKS5
;
340 tmp
= purple_gnome_proxy_get_parameter(GNOME_PROXY_SOCKS_PORT
, gnome_version
);
344 return purple_global_proxy_get_info();
346 info
.port
= atoi(tmp
);
350 info
.host
= purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_HOST
, gnome_version
);
352 return purple_global_proxy_get_info();
354 /* If we get this far then we know we're using an HTTP proxy */
355 info
.type
= PURPLE_PROXY_HTTP
;
357 if (*info
.host
== '\0')
359 purple_debug_info("proxy", "Gnome proxy settings are set to "
360 "'manual' but no suitable proxy server is specified. Using "
361 "Pidgin's proxy settings instead.\n");
364 return purple_global_proxy_get_info();
367 info
.username
= purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_USER
, gnome_version
);
372 return purple_global_proxy_get_info();
375 info
.password
= purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_PASS
, gnome_version
);
380 g_free(info
.username
);
381 info
.username
= NULL
;
382 return purple_global_proxy_get_info();
385 tmp
= purple_gnome_proxy_get_parameter(GNOME_PROXY_HTTP_PORT
, gnome_version
);
390 g_free(info
.username
);
391 info
.username
= NULL
;
392 g_free(info
.password
);
393 info
.password
= NULL
;
394 return purple_global_proxy_get_info();
396 info
.port
= atoi(tmp
);
405 typedef BOOL (CALLBACK
* LPFNWINHTTPGETIEPROXYCONFIG
)(/*IN OUT*/ WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
* pProxyConfig
);
407 /* This modifies "host" in-place evilly */
409 _proxy_fill_hostinfo(PurpleProxyInfo
*info
, char *host
, int default_port
)
411 int port
= default_port
;
414 d
= g_strrstr(host
, ":");
420 sscanf(d
, "%d", &port
);
426 purple_proxy_info_set_host(info
, host
);
427 purple_proxy_info_set_port(info
, port
);
430 static PurpleProxyInfo
*
431 purple_win32_proxy_get_info(void)
433 static LPFNWINHTTPGETIEPROXYCONFIG MyWinHttpGetIEProxyConfig
= NULL
;
434 static gboolean loaded
= FALSE
;
435 static PurpleProxyInfo info
= {0, NULL
, 0, NULL
, NULL
};
437 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_proxy_config
;
441 MyWinHttpGetIEProxyConfig
= (LPFNWINHTTPGETIEPROXYCONFIG
)
442 wpurple_find_and_loadproc("winhttp.dll", "WinHttpGetIEProxyConfigForCurrentUser");
443 if (!MyWinHttpGetIEProxyConfig
)
444 purple_debug_warning("proxy", "Unable to read Windows Proxy Settings.\n");
447 if (!MyWinHttpGetIEProxyConfig
)
450 ZeroMemory(&ie_proxy_config
, sizeof(ie_proxy_config
));
451 if (!MyWinHttpGetIEProxyConfig(&ie_proxy_config
)) {
452 purple_debug_error("proxy", "Error reading Windows Proxy Settings(%lu).\n", GetLastError());
456 /* We can't do much if it is autodetect*/
457 if (ie_proxy_config
.fAutoDetect
) {
458 purple_debug_error("proxy", "Windows Proxy Settings set to autodetect (not supported).\n");
460 /* TODO: For 3.0.0 we'll revisit this (maybe)*/
464 } else if (ie_proxy_config
.lpszProxy
) {
465 gchar
*proxy_list
= g_utf16_to_utf8(ie_proxy_config
.lpszProxy
, -1,
468 /* We can't do anything about the bypass list, as we don't have the url */
469 /* TODO: For 3.0.0 we'll revisit this*/
471 /* There are proxy settings for several protocols */
472 if (proxy_list
&& *proxy_list
) {
473 char *specific
= NULL
, *tmp
;
475 /* If there is only a global proxy, which means "HTTP" */
476 if (!strchr(proxy_list
, ';') || (specific
= g_strstr_len(proxy_list
, -1, "http=")) != NULL
) {
479 specific
+= strlen("http=");
480 tmp
= strchr(specific
, ';');
483 /* specific now points the proxy server (and port) */
485 specific
= proxy_list
;
487 purple_proxy_info_set_proxy_type(&info
, PURPLE_PROXY_HTTP
);
488 _proxy_fill_hostinfo(&info
, specific
, 80);
489 /* TODO: is there a way to set the username/password? */
490 purple_proxy_info_set_username(&info
, NULL
);
491 purple_proxy_info_set_password(&info
, NULL
);
493 purple_debug_info("proxy", "Windows Proxy Settings: HTTP proxy: '%s:%d'.\n",
494 purple_proxy_info_get_host(&info
),
495 purple_proxy_info_get_port(&info
));
497 } else if ((specific
= g_strstr_len(proxy_list
, -1, "socks=")) != NULL
) {
499 specific
+= strlen("socks=");
500 tmp
= strchr(specific
, ';');
503 /* specific now points the proxy server (and port) */
505 purple_proxy_info_set_proxy_type(&info
, PURPLE_PROXY_SOCKS5
);
506 _proxy_fill_hostinfo(&info
, specific
, 1080);
507 /* TODO: is there a way to set the username/password? */
508 purple_proxy_info_set_username(&info
, NULL
);
509 purple_proxy_info_set_password(&info
, NULL
);
511 purple_debug_info("proxy", "Windows Proxy Settings: SOCKS5 proxy: '%s:%d'.\n",
512 purple_proxy_info_get_host(&info
),
513 purple_proxy_info_get_port(&info
));
517 purple_debug_info("proxy", "Windows Proxy Settings: No supported proxy specified.\n");
519 purple_proxy_info_set_proxy_type(&info
, PURPLE_PROXY_NONE
);
524 /* TODO: Fix API to be able look at proxy bypass settings */
528 purple_debug_info("proxy", "No Windows proxy set.\n");
529 purple_proxy_info_set_proxy_type(&info
, PURPLE_PROXY_NONE
);
532 if (ie_proxy_config
.lpszAutoConfigUrl
)
533 GlobalFree(ie_proxy_config
.lpszAutoConfigUrl
);
534 if (ie_proxy_config
.lpszProxy
)
535 GlobalFree(ie_proxy_config
.lpszProxy
);
536 if (ie_proxy_config
.lpszProxyBypass
)
537 GlobalFree(ie_proxy_config
.lpszProxyBypass
);
544 /**************************************************************************
546 **************************************************************************/
549 * Whoever calls this needs to have called
550 * purple_proxy_connect_data_disconnect() beforehand.
553 purple_proxy_connect_data_destroy(PurpleProxyConnectData
*connect_data
)
555 if (!PURPLE_PROXY_CONNECT_DATA_IS_VALID(connect_data
))
558 handles
= g_slist_remove(handles
, connect_data
);
560 if(G_IS_CANCELLABLE(connect_data
->cancellable
)) {
561 g_cancellable_cancel(connect_data
->cancellable
);
563 g_object_unref(G_OBJECT(connect_data
->cancellable
));
565 connect_data
->cancellable
= NULL
;
568 g_free(connect_data
->host
);
569 g_free(connect_data
);
573 * purple_proxy_connect_data_disconnect:
574 * @error_message: An error message explaining why the connection
575 * failed. This will be passed to the callback function
576 * specified in the call to purple_proxy_connect(). If the
577 * connection was successful then pass in null.
579 * Free all information dealing with a connection attempt and
580 * reset the connect_data to prepare for it to try to connect
581 * to another IP address.
583 * If an error message is passed in, then we know the connection
584 * attempt failed. If so, we call the callback with the given
585 * error message, then destroy the connect_data.
588 purple_proxy_connect_data_disconnect(PurpleProxyConnectData
*connect_data
, const gchar
*error_message
)
590 if (connect_data
->fd
>= 0)
592 close(connect_data
->fd
);
593 connect_data
->fd
= -1;
596 if (error_message
!= NULL
)
598 purple_debug_error("proxy", "Connection attempt failed: %s\n",
601 /* Everything failed! Tell the originator of the request. */
602 connect_data
->connect_cb(connect_data
->data
, -1, error_message
);
603 purple_proxy_connect_data_destroy(connect_data
);
608 purple_proxy_connect_data_connected(PurpleProxyConnectData
*connect_data
)
610 purple_debug_info("proxy", "Connected to %s:%d.\n",
611 connect_data
->host
, connect_data
->port
);
613 connect_data
->connect_cb(connect_data
->data
, connect_data
->fd
, NULL
);
616 * We've passed the file descriptor to the protocol, so it's no longer
617 * our responsibility, and we should be careful not to free it when
618 * we destroy the connect_data.
620 connect_data
->fd
= -1;
622 purple_proxy_connect_data_disconnect(connect_data
, NULL
);
623 purple_proxy_connect_data_destroy(connect_data
);
627 purple_proxy_get_setup(PurpleAccount
*account
)
629 PurpleProxyInfo
*gpi
= NULL
;
632 /* This is used as a fallback so we don't overwrite the selected proxy type */
633 static PurpleProxyInfo
*tmp_none_proxy_info
= NULL
;
634 if (!tmp_none_proxy_info
) {
635 tmp_none_proxy_info
= purple_proxy_info_new();
636 purple_proxy_info_set_proxy_type(tmp_none_proxy_info
, PURPLE_PROXY_NONE
);
639 if (account
&& purple_account_get_proxy_info(account
) != NULL
) {
640 gpi
= purple_account_get_proxy_info(account
);
641 if (purple_proxy_info_get_proxy_type(gpi
) == PURPLE_PROXY_USE_GLOBAL
)
645 if (purple_running_gnome())
646 gpi
= purple_gnome_proxy_get_info();
648 gpi
= purple_global_proxy_get_info();
651 if (purple_proxy_info_get_proxy_type(gpi
) == PURPLE_PROXY_USE_ENVVAR
) {
652 if ((tmp
= g_getenv("HTTP_PROXY")) != NULL
||
653 (tmp
= g_getenv("http_proxy")) != NULL
||
654 (tmp
= g_getenv("HTTPPROXY")) != NULL
)
658 /* http_proxy-format:
659 * export http_proxy="http://user:passwd@your.proxy.server:port/"
661 url
= soup_uri_new(tmp
);
662 if (!SOUP_URI_VALID_FOR_HTTP(url
)) {
663 purple_debug_warning("proxy", "Couldn't parse URL: %s", tmp
);
667 purple_proxy_info_set_host(gpi
, url
->host
);
668 purple_proxy_info_set_username(gpi
, url
->user
);
669 purple_proxy_info_set_password(gpi
, url
->password
);
670 purple_proxy_info_set_port(gpi
, url
->port
);
674 /* XXX: Do we want to skip this step if user/password/port were part of url? */
675 if ((tmp
= g_getenv("HTTP_PROXY_USER")) != NULL
||
676 (tmp
= g_getenv("http_proxy_user")) != NULL
||
677 (tmp
= g_getenv("HTTPPROXYUSER")) != NULL
)
678 purple_proxy_info_set_username(gpi
, tmp
);
680 if ((tmp
= g_getenv("HTTP_PROXY_PASS")) != NULL
||
681 (tmp
= g_getenv("http_proxy_pass")) != NULL
||
682 (tmp
= g_getenv("HTTPPROXYPASS")) != NULL
)
683 purple_proxy_info_set_password(gpi
, tmp
);
685 if ((tmp
= g_getenv("HTTP_PROXY_PORT")) != NULL
||
686 (tmp
= g_getenv("http_proxy_port")) != NULL
||
687 (tmp
= g_getenv("HTTPPROXYPORT")) != NULL
)
688 purple_proxy_info_set_port(gpi
, atoi(tmp
));
691 PurpleProxyInfo
*wgpi
;
692 if ((wgpi
= purple_win32_proxy_get_info()) != NULL
)
695 /* no proxy environment variable found, don't use a proxy */
696 purple_debug_info("proxy", "No environment settings found, not using a proxy\n");
697 gpi
= tmp_none_proxy_info
;
705 /* Grabbed duplicate_fd() from GLib's testcases (gio/tests/socket.c).
706 * Can be dropped once this API has been converted to Gio.
709 duplicate_fd (int fd
)
714 if (!DuplicateHandle (GetCurrentProcess (),
716 GetCurrentProcess (),
720 DUPLICATE_SAME_ACCESS
))
730 /* End function grabbed from GLib */
733 connect_to_host_cb(GObject
*source
, GAsyncResult
*res
, gpointer user_data
)
735 PurpleProxyConnectData
*connect_data
= user_data
;
736 GSocketConnection
*conn
;
737 GError
*error
= NULL
;
740 conn
= g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(source
),
743 /* Ignore cancelled error as that signifies connect_data has
746 if (!g_error_matches(error
, G_IO_ERROR
,
747 G_IO_ERROR_CANCELLED
)) {
748 purple_debug_error("proxy", "Unable to connect to "
749 "destination host: %s\n",
751 purple_proxy_connect_data_disconnect(connect_data
,
752 "Unable to connect to destination "
756 g_clear_error(&error
);
760 socket
= g_socket_connection_get_socket(conn
);
761 g_assert(socket
!= NULL
);
763 /* Duplicate the file descriptor, and then free the connection.
764 * libpurple's proxy code doesn't keep an object around for the
765 * lifetime of the connection. Therefore, in order to not leak
766 * memory, the GSocketConnection must be freed here. In order
767 * to avoid the double close/free of the file descriptor, the
768 * file descriptor is duplicated.
770 connect_data
->fd
= duplicate_fd(g_socket_get_fd(socket
));
771 g_object_unref(conn
);
773 purple_proxy_connect_data_connected(connect_data
);
776 PurpleProxyConnectData
*
777 purple_proxy_connect(void *handle
, PurpleAccount
*account
,
778 const char *host
, int port
,
779 PurpleProxyConnectFunction connect_cb
, gpointer data
)
781 PurpleProxyConnectData
*connect_data
;
782 GSocketClient
*client
;
783 GError
*error
= NULL
;
785 g_return_val_if_fail(host
!= NULL
, NULL
);
786 g_return_val_if_fail(port
> 0, NULL
);
787 g_return_val_if_fail(connect_cb
!= NULL
, NULL
);
789 client
= purple_gio_socket_client_new(account
, &error
);
791 if (client
== NULL
) {
792 /* Assume it's a proxy error */
793 purple_notify_error(NULL
, NULL
, _("Invalid proxy settings"),
795 purple_request_cpar_from_account(account
));
796 g_clear_error(&error
);
800 connect_data
= g_new0(PurpleProxyConnectData
, 1);
801 connect_data
->fd
= -1;
802 connect_data
->handle
= handle
;
803 connect_data
->connect_cb
= connect_cb
;
804 connect_data
->data
= data
;
805 connect_data
->host
= g_strdup(host
);
806 connect_data
->port
= port
;
807 connect_data
->gpi
= purple_proxy_get_setup(account
);
808 connect_data
->cancellable
= g_cancellable_new();
810 purple_debug_info("proxy", "Attempting connection to %s:%u\n",
813 g_socket_client_connect_to_host_async(client
, host
, port
,
814 connect_data
->cancellable
, connect_to_host_cb
,
816 g_object_unref(client
);
818 handles
= g_slist_prepend(handles
, connect_data
);
824 socks5_proxy_connect_cb(GObject
*source
, GAsyncResult
*res
, gpointer user_data
)
826 PurpleProxyConnectData
*connect_data
= user_data
;
828 GError
*error
= NULL
;
831 stream
= g_proxy_connect_finish(G_PROXY(source
), res
, &error
);
833 if (stream
== NULL
) {
834 /* Ignore cancelled error as that signifies connect_data has
837 if (!g_error_matches(error
, G_IO_ERROR
,
838 G_IO_ERROR_CANCELLED
)) {
839 purple_debug_error("proxy", "Unable to connect to "
840 "destination host: %s\n",
842 purple_proxy_connect_data_disconnect(connect_data
,
843 "Unable to connect to destination "
847 g_clear_error(&error
);
851 if (!G_IS_SOCKET_CONNECTION(stream
)) {
852 purple_debug_error("proxy",
853 "GProxy didn't return a GSocketConnection.\n");
854 purple_proxy_connect_data_disconnect(connect_data
,
855 "GProxy didn't return a GSocketConnection.\n");
856 g_object_unref(stream
);
860 socket
= g_socket_connection_get_socket(G_SOCKET_CONNECTION(stream
));
862 /* Duplicate the file descriptor, and then free the connection.
863 * libpurple's proxy code doesn't keep an object around for the
864 * lifetime of the connection. Therefore, in order to not leak
865 * memory, the GSocketConnection (aka GIOStream here) must be
866 * freed here. In order to avoid the double close/free of the
867 * file descriptor, the file descriptor is duplicated.
869 connect_data
->fd
= duplicate_fd(g_socket_get_fd(socket
));
870 g_object_unref(stream
);
872 purple_proxy_connect_data_connected(connect_data
);
875 /* This is called when we connect to the SOCKS5 proxy server (through any
876 * relevant account proxy)
879 socks5_connect_to_host_cb(GObject
*source
, GAsyncResult
*res
,
882 PurpleProxyConnectData
*connect_data
= user_data
;
883 GSocketConnection
*conn
;
884 GError
*error
= NULL
;
886 PurpleProxyInfo
*info
;
887 GSocketAddress
*addr
;
888 GInetSocketAddress
*inet_addr
;
889 GSocketAddress
*proxy_addr
;
891 conn
= g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(source
),
894 /* Ignore cancelled error as that signifies connect_data has
897 if (!g_error_matches(error
, G_IO_ERROR
,
898 G_IO_ERROR_CANCELLED
)) {
899 purple_debug_error("proxy", "Unable to connect to "
900 "SOCKS5 host: %s\n", error
->message
);
901 purple_proxy_connect_data_disconnect(connect_data
,
902 "Unable to connect to SOCKS5 host.\n");
905 g_clear_error(&error
);
909 proxy
= g_proxy_get_default_for_protocol("socks5");
911 purple_debug_error("proxy", "SOCKS5 proxy backend missing.\n");
912 purple_proxy_connect_data_disconnect(connect_data
,
913 "SOCKS5 proxy backend missing.\n");
914 g_object_unref(conn
);
918 info
= connect_data
->gpi
;
920 addr
= g_socket_connection_get_remote_address(conn
, &error
);
922 purple_debug_error("proxy", "Unable to retrieve SOCKS5 host "
923 "address from connection: %s\n",
925 purple_proxy_connect_data_disconnect(connect_data
,
926 "Unable to retrieve SOCKS5 host address from "
928 g_object_unref(conn
);
929 g_object_unref(proxy
);
930 g_clear_error(&error
);
934 inet_addr
= G_INET_SOCKET_ADDRESS(addr
);
936 proxy_addr
= g_proxy_address_new(
937 g_inet_socket_address_get_address(inet_addr
),
938 g_inet_socket_address_get_port(inet_addr
),
939 "socks5", connect_data
->host
, connect_data
->port
,
940 purple_proxy_info_get_username(info
),
941 purple_proxy_info_get_password(info
));
942 g_object_unref(inet_addr
);
944 purple_debug_info("proxy", "Initiating SOCKS5 negotiation.\n");
946 purple_debug_info("proxy",
947 "Connecting to %s:%d via %s:%d using SOCKS5\n",
948 connect_data
->host
, connect_data
->port
,
949 purple_proxy_info_get_host(connect_data
->gpi
),
950 purple_proxy_info_get_port(connect_data
->gpi
));
952 g_proxy_connect_async(proxy
, G_IO_STREAM(conn
),
953 G_PROXY_ADDRESS(proxy_addr
),
954 connect_data
->cancellable
,
955 socks5_proxy_connect_cb
, connect_data
);
956 g_object_unref(proxy_addr
);
957 g_object_unref(conn
);
958 g_object_unref(proxy
);
962 * Combine some of this code with purple_proxy_connect()
964 PurpleProxyConnectData
*
965 purple_proxy_connect_socks5_account(void *handle
, PurpleAccount
*account
,
966 PurpleProxyInfo
*gpi
,
967 const char *host
, int port
,
968 PurpleProxyConnectFunction connect_cb
,
971 PurpleProxyConnectData
*connect_data
;
972 GSocketClient
*client
;
973 GError
*error
= NULL
;
975 g_return_val_if_fail(host
!= NULL
, NULL
);
976 g_return_val_if_fail(port
>= 0, NULL
);
977 g_return_val_if_fail(connect_cb
!= NULL
, NULL
);
979 client
= purple_gio_socket_client_new(account
, &error
);
981 if (client
== NULL
) {
982 /* Assume it's a proxy error */
983 purple_notify_error(NULL
, NULL
, _("Invalid proxy settings"),
985 purple_request_cpar_from_account(account
));
986 g_clear_error(&error
);
990 connect_data
= g_new0(PurpleProxyConnectData
, 1);
991 connect_data
->fd
= -1;
992 connect_data
->handle
= handle
;
993 connect_data
->connect_cb
= connect_cb
;
994 connect_data
->data
= data
;
995 connect_data
->host
= g_strdup(host
);
996 connect_data
->port
= port
;
997 connect_data
->gpi
= gpi
;
998 connect_data
->cancellable
= g_cancellable_new();
1000 purple_debug_info("proxy",
1001 "Connecting to %s:%d via %s:%d using SOCKS5\n",
1002 connect_data
->host
, connect_data
->port
,
1003 purple_proxy_info_get_host(connect_data
->gpi
),
1004 purple_proxy_info_get_port(connect_data
->gpi
));
1006 g_socket_client_connect_to_host_async(client
,
1007 purple_proxy_info_get_host(connect_data
->gpi
),
1008 purple_proxy_info_get_port(connect_data
->gpi
),
1009 connect_data
->cancellable
, socks5_connect_to_host_cb
,
1011 g_object_unref(client
);
1013 handles
= g_slist_prepend(handles
, connect_data
);
1015 return connect_data
;
1019 purple_proxy_connect_cancel(PurpleProxyConnectData
*connect_data
)
1021 g_return_if_fail(connect_data
!= NULL
);
1023 purple_proxy_connect_data_disconnect(connect_data
, NULL
);
1024 purple_proxy_connect_data_destroy(connect_data
);
1028 purple_proxy_connect_cancel_with_handle(void *handle
)
1032 for (l
= handles
; l
!= NULL
; l
= l_next
) {
1033 PurpleProxyConnectData
*connect_data
= l
->data
;
1037 if (connect_data
->handle
== handle
)
1038 purple_proxy_connect_cancel(connect_data
);
1043 purple_proxy_get_proxy_resolver(PurpleAccount
*account
, GError
**error
)
1045 PurpleProxyInfo
*info
= purple_proxy_get_setup(account
);
1046 const gchar
*protocol
;
1047 const gchar
*username
;
1048 const gchar
*password
;
1051 GProxyResolver
*resolver
;
1053 if (purple_proxy_info_get_proxy_type(info
) == PURPLE_PROXY_NONE
) {
1054 /* Return an empty simple resolver, which will resolve on direct
1056 return g_simple_proxy_resolver_new(NULL
, NULL
);
1059 switch (purple_proxy_info_get_proxy_type(info
))
1061 /* PURPLE_PROXY_NONE already handled above */
1063 case PURPLE_PROXY_USE_ENVVAR
:
1064 /* Intentional passthrough */
1065 case PURPLE_PROXY_HTTP
:
1068 case PURPLE_PROXY_SOCKS4
:
1069 protocol
= "socks4";
1071 case PURPLE_PROXY_SOCKS5
:
1072 /* Intentional passthrough */
1073 case PURPLE_PROXY_TOR
:
1074 protocol
= "socks5";
1078 g_set_error(error
, PURPLE_CONNECTION_ERROR
,
1079 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS
,
1080 _("Invalid Proxy type (%d) specified"),
1081 purple_proxy_info_get_proxy_type(info
));
1086 if (purple_proxy_info_get_host(info
) == NULL
||
1087 purple_proxy_info_get_port(info
) <= 0) {
1088 g_set_error_literal(error
, PURPLE_CONNECTION_ERROR
,
1089 PURPLE_CONNECTION_ERROR_INVALID_SETTINGS
,
1090 _("Either the host name or port number "
1091 "specified for your given proxy type is "
1096 /* Everything checks out. Create and return the GProxyResolver */
1098 username
= purple_proxy_info_get_username(info
);
1099 password
= purple_proxy_info_get_password(info
);
1101 /* Username and password are optional */
1102 if (username
!= NULL
&& password
!= NULL
) {
1103 auth
= g_strdup_printf("%s:%s@", username
, password
);
1104 } else if (username
!= NULL
) {
1105 auth
= g_strdup_printf("%s@", username
);
1110 proxy
= g_strdup_printf("%s://%s%s:%i", protocol
,
1111 auth
!= NULL
? auth
: "",
1112 purple_proxy_info_get_host(info
),
1113 purple_proxy_info_get_port(info
));
1116 resolver
= g_simple_proxy_resolver_new(proxy
, NULL
);
1123 proxy_pref_cb(const char *name
, PurplePrefType type
,
1124 gconstpointer value
, gpointer data
)
1126 PurpleProxyInfo
*info
= purple_global_proxy_get_info();
1128 if (purple_strequal(name
, "/purple/proxy/type")) {
1130 const char *type
= value
;
1132 if (purple_strequal(type
, "none"))
1133 proxytype
= PURPLE_PROXY_NONE
;
1134 else if (purple_strequal(type
, "http"))
1135 proxytype
= PURPLE_PROXY_HTTP
;
1136 else if (purple_strequal(type
, "socks4"))
1137 proxytype
= PURPLE_PROXY_SOCKS4
;
1138 else if (purple_strequal(type
, "socks5"))
1139 proxytype
= PURPLE_PROXY_SOCKS5
;
1140 else if (purple_strequal(type
, "tor"))
1141 proxytype
= PURPLE_PROXY_TOR
;
1142 else if (purple_strequal(type
, "envvar"))
1143 proxytype
= PURPLE_PROXY_USE_ENVVAR
;
1147 purple_proxy_info_set_proxy_type(info
, proxytype
);
1148 } else if (purple_strequal(name
, "/purple/proxy/host"))
1149 purple_proxy_info_set_host(info
, value
);
1150 else if (purple_strequal(name
, "/purple/proxy/port"))
1151 purple_proxy_info_set_port(info
, GPOINTER_TO_INT(value
));
1152 else if (purple_strequal(name
, "/purple/proxy/username"))
1153 purple_proxy_info_set_username(info
, value
);
1154 else if (purple_strequal(name
, "/purple/proxy/password"))
1155 purple_proxy_info_set_password(info
, value
);
1159 purple_proxy_get_handle()
1167 purple_proxy_init(void)
1171 /* Initialize a default proxy info struct. */
1172 global_proxy_info
= purple_proxy_info_new();
1175 purple_prefs_add_none("/purple/proxy");
1176 purple_prefs_add_string("/purple/proxy/type", "none");
1177 purple_prefs_add_string("/purple/proxy/host", "");
1178 purple_prefs_add_int("/purple/proxy/port", 0);
1179 purple_prefs_add_string("/purple/proxy/username", "");
1180 purple_prefs_add_string("/purple/proxy/password", "");
1181 purple_prefs_add_bool("/purple/proxy/socks4_remotedns", FALSE
);
1183 /* Setup callbacks for the preferences. */
1184 handle
= purple_proxy_get_handle();
1185 purple_prefs_connect_callback(handle
, "/purple/proxy/type", proxy_pref_cb
,
1187 purple_prefs_connect_callback(handle
, "/purple/proxy/host", proxy_pref_cb
,
1189 purple_prefs_connect_callback(handle
, "/purple/proxy/port", proxy_pref_cb
,
1191 purple_prefs_connect_callback(handle
, "/purple/proxy/username",
1192 proxy_pref_cb
, NULL
);
1193 purple_prefs_connect_callback(handle
, "/purple/proxy/password",
1194 proxy_pref_cb
, NULL
);
1196 /* Load the initial proxy settings */
1197 purple_prefs_trigger_callback("/purple/proxy/type");
1198 purple_prefs_trigger_callback("/purple/proxy/host");
1199 purple_prefs_trigger_callback("/purple/proxy/port");
1200 purple_prefs_trigger_callback("/purple/proxy/username");
1201 purple_prefs_trigger_callback("/purple/proxy/password");
1205 purple_proxy_uninit(void)
1207 while (handles
!= NULL
)
1209 purple_proxy_connect_data_disconnect(handles
->data
, NULL
);
1210 purple_proxy_connect_data_destroy(handles
->data
);
1213 purple_prefs_disconnect_by_handle(purple_proxy_get_handle());
1215 purple_proxy_info_destroy(global_proxy_info
);
1216 global_proxy_info
= NULL
;