From 9751cfc407660d8efc0caa255f6d446b7b2b67ef Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 1 Nov 2015 21:34:42 -0500 Subject: [PATCH 1/2] Force name lookups through proxy set by client --- src/name_lookup.c | 53 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/name_lookup.c b/src/name_lookup.c index 4f259e2..f982946 100644 --- a/src/name_lookup.c +++ b/src/name_lookup.c @@ -243,6 +243,47 @@ static int process_response(struct Recv_Data *recv_data) return 0; } +/* Sets proxy info for given CURL handler. + * + * Returns 0 on success or if no proxy is set by the client. + * Returns -1 on failure. + */ +static int set_lookup_proxy(ToxWindow *self, CURL *c_handle, const char *proxy_address, uint16_t port, uint8_t proxy_type) +{ + if (proxy_type == TOX_PROXY_TYPE_NONE) + return 0; + + if (proxy_address == NULL || port == 0) { + lookup_error(self, "Unknkown proxy error"); + return -1; + } + + int ret = curl_easy_setopt(c_handle, CURLOPT_PROXYPORT, (long) port); + + if (ret != CURLE_OK) { + lookup_error(self, "Failed to set proxy port (libcurl error %d)", ret); + return -1; + } + + long int type = proxy_type == TOX_PROXY_TYPE_SOCKS5 ? CURLPROXY_SOCKS5 : CURLPROXY_HTTP; + + ret = curl_easy_setopt(c_handle, CURLOPT_PROXYTYPE, type); + + if (ret != CURLE_OK) { + lookup_error(self, "Failed to set proxy type (libcurl error %d)", ret); + return -1; + } + + ret = curl_easy_setopt(c_handle, CURLOPT_PROXY, proxy_address); + + if (ret != CURLE_OK) { + lookup_error(self, "Failed to set proxy (libcurl error %d)", ret); + return -1; + } + + return 0; +} + void *lookup_thread_func(void *data) { ToxWindow *self = t_data.self; @@ -292,6 +333,9 @@ void *lookup_thread_func(void *data) curl_easy_setopt(c_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); curl_easy_setopt(c_handle, CURLOPT_POSTFIELDS, post_data); + if (set_lookup_proxy(self, c_handle, arg_opts.proxy_address, arg_opts.proxy_port, arg_opts.proxy_type) == -1) + goto on_exit; + int ret = curl_easy_setopt(c_handle, CURLOPT_USE_SSL, CURLUSESSL_ALL); if (ret != CURLE_OK) { @@ -323,13 +367,13 @@ void *lookup_thread_func(void *data) } if (ret != CURLE_OK) { - lookup_error(self, "https lookup error (libcurl error %d)", ret); + lookup_error(self, "HTTPS lookup error (libcurl error %d)", ret); goto on_exit; } } if (process_response(&recv_data) == -1) { - lookup_error(self, ""); + lookup_error(self, "Bad response."); goto on_exit; } @@ -352,11 +396,6 @@ void name_lookup(ToxWindow *self, Tox *m, const char *id_bin, const char *addr, return; } - if (arg_opts.proxy_type != TOX_PROXY_TYPE_NONE && arg_opts.force_tcp) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "name lookups are disabled."); - return; - } - if (t_data.busy) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Please wait for previous name lookup to finish."); return; From e02cf1bb7dde04853d9668d99cf9c8ad6f62af9a Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 2 Nov 2015 02:01:17 -0500 Subject: [PATCH 2/2] Use proxy for DNS requests when type is SOCKS5 --- src/name_lookup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/name_lookup.c b/src/name_lookup.c index f982946..464e206 100644 --- a/src/name_lookup.c +++ b/src/name_lookup.c @@ -254,7 +254,7 @@ static int set_lookup_proxy(ToxWindow *self, CURL *c_handle, const char *proxy_a return 0; if (proxy_address == NULL || port == 0) { - lookup_error(self, "Unknkown proxy error"); + lookup_error(self, "Unknown proxy error"); return -1; } @@ -265,7 +265,7 @@ static int set_lookup_proxy(ToxWindow *self, CURL *c_handle, const char *proxy_a return -1; } - long int type = proxy_type == TOX_PROXY_TYPE_SOCKS5 ? CURLPROXY_SOCKS5 : CURLPROXY_HTTP; + long int type = proxy_type == TOX_PROXY_TYPE_SOCKS5 ? CURLPROXY_SOCKS5_HOSTNAME : CURLPROXY_HTTP; ret = curl_easy_setopt(c_handle, CURLOPT_PROXYTYPE, type);