24#include <openssl/err.h>
28#include <freerdp/settings.h>
29#include <freerdp/utils/proxy_utils.h>
30#include <freerdp/crypto/crypto.h>
33#include <winpr/assert.h>
34#include <winpr/sysinfo.h>
35#include <winpr/environment.h>
39#include <freerdp/log.h>
40#define TAG FREERDP_TAG("core.proxy")
52 SOCKS_CMD_CONNECT = 1,
54 SOCKS_CMD_UDP_ASSOCIATE = 3
64static const char logprefix[] =
"SOCKS Proxy:";
67static const char* rplstat[] = {
"succeeded",
68 "general SOCKS server failure",
69 "connection not allowed by ruleset",
70 "Network unreachable",
74 "Command not supported",
75 "Address type not supported" };
77static BOOL http_proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
78 const char* proxyPassword,
const char* hostname, UINT16 port);
79static BOOL socks_proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
80 const char* proxyPassword,
const char* hostname, UINT16 port);
81static void proxy_read_environment(rdpSettings* settings,
char* envname);
83BOOL proxy_prepare(rdpSettings* settings,
const char** lpPeerHostname, UINT16* lpPeerPort,
84 const char** lpProxyUsername,
const char** lpProxyPassword)
91 proxy_read_environment(settings,
"https_proxy");
94 proxy_read_environment(settings,
"HTTPS_PROXY");
97 proxy_read_environment(settings,
"no_proxy");
100 proxy_read_environment(settings,
"NO_PROXY");
114static BOOL value_to_int(
const char* value, LONGLONG* result, LONGLONG min, LONGLONG max)
118 if (!value || !result)
122 rc = _strtoi64(value, NULL, 0);
127 if ((rc < min) || (rc > max))
134static BOOL cidr4_match(
const struct in_addr* addr,
const struct in_addr* net, BYTE bits)
139 const uint32_t mask = htonl(0xFFFFFFFFu << (32 - bits));
140 const uint32_t amask = addr->s_addr & mask;
141 const uint32_t nmask = net->s_addr & mask;
142 return amask == nmask;
145static BOOL cidr6_match(
const struct in6_addr* address,
const struct in6_addr* network,
148 const uint32_t* a = (
const uint32_t*)address;
149 const uint32_t* n = (
const uint32_t*)network;
150 const size_t bits_whole = bits >> 5;
151 const size_t bits_incomplete = bits & 0x1F;
155 if (memcmp(a, n, bits_whole << 2) != 0)
161 uint32_t mask = htonl((0xFFFFFFFFu) << (32 - bits_incomplete));
163 if ((a[bits_whole] ^ n[bits_whole]) & mask)
170static BOOL option_ends_with(
const char* str,
const char* ext)
174 const size_t strLen = strlen(str);
175 const size_t extLen = strlen(ext);
180 return _strnicmp(&str[strLen - extLen], ext, extLen) == 0;
186static BOOL no_proxy_match_host(
const char* val,
const char* hostname)
189 WINPR_ASSERT(hostname);
192 if (_stricmp(
"*", val) == 0)
200 return option_ends_with(hostname, val);
203static BOOL starts_with(
const char* val,
const char* prefix)
205 const size_t plen = strlen(prefix);
206 const size_t vlen = strlen(val);
209 return _strnicmp(val, prefix, plen) == 0;
212static BOOL no_proxy_match_ip(
const char* val,
const char* hostname)
215 WINPR_ASSERT(hostname);
217 struct sockaddr_in sa4 = { 0 };
218 struct sockaddr_in6 sa6 = { 0 };
220 if (inet_pton(AF_INET, hostname, &sa4.sin_addr) == 1)
223 if (starts_with(hostname, val))
226 char* sub = strchr(val,
'/');
230 struct sockaddr_in mask = { 0 };
231 if (inet_pton(AF_INET, val, &mask.sin_addr) == 0)
235 if (memcmp(&mask, &sa4,
sizeof(mask)) == 0)
240 const unsigned long usub = strtoul(sub, NULL, 0);
241 if ((errno == 0) && (usub <= UINT8_MAX))
242 return cidr4_match(&sa4.sin_addr, &mask.sin_addr, (UINT8)usub);
245 else if (inet_pton(AF_INET6, hostname, &sa6.sin6_addr) == 1)
250 char str[INET6_ADDRSTRLEN + 1] = { 0 };
251 strncpy(str, val, INET6_ADDRSTRLEN);
253 const size_t len = strnlen(str, INET6_ADDRSTRLEN);
256 if (str[len - 1] ==
']')
261 if (starts_with(hostname, str))
264 char* sub = strchr(str,
'/');
268 struct sockaddr_in6 mask = { 0 };
269 if (inet_pton(AF_INET6, str, &mask.sin6_addr) == 0)
273 if (memcmp(&mask, &sa6,
sizeof(mask)) == 0)
278 const unsigned long usub = strtoul(sub, NULL, 0);
279 if ((errno == 0) && (usub <= UINT8_MAX))
280 return cidr6_match(&sa6.sin6_addr, &mask.sin6_addr, (UINT8)usub);
287static BOOL check_no_proxy(rdpSettings* settings,
const char* no_proxy)
289 const char* delimiter =
", ";
291 char* context = NULL;
293 if (!no_proxy || !settings)
296 char* copy = _strdup(no_proxy);
301 char* current = strtok_s(copy, delimiter, &context);
303 while (current && !result)
305 const size_t currentlen = strlen(current);
309 WLog_DBG(TAG,
"%s => %s (%" PRIdz
")", settings->ServerHostname, current, currentlen);
311 if (no_proxy_match_host(current, settings->ServerHostname))
313 else if (no_proxy_match_ip(current, settings->ServerHostname))
317 current = strtok_s(NULL, delimiter, &context);
324void proxy_read_environment(rdpSettings* settings,
char* envname)
326 const DWORD envlen = GetEnvironmentVariableA(envname, NULL, 0);
328 if (!envlen || (envlen <= 1))
331 char* env = calloc(1, envlen);
335 WLog_ERR(TAG,
"Not enough memory");
339 if (GetEnvironmentVariableA(envname, env, envlen) == envlen - 1)
341 if (_strnicmp(
"NO_PROXY", envname, 9) == 0)
343 if (check_no_proxy(settings, env))
345 WLog_INFO(TAG,
"deactivating proxy: %s [%s=%s]",
349 WLog_WARN(TAG,
"failed to set FreeRDP_ProxyType=PROXY_TYPE_NONE");
354 if (!proxy_parse_uri(settings, env))
357 TAG,
"Error while parsing proxy URI from environment variable; ignoring proxy");
365BOOL proxy_parse_uri(rdpSettings* settings,
const char* uri_in)
368 const char* protocol =
"";
371 if (!settings || !uri_in)
374 char* uri_copy = _strdup(uri_in);
375 char* uri = uri_copy;
380 char* p = strstr(uri,
"://");
385 if (_stricmp(
"no_proxy", uri) == 0)
390 if (_stricmp(
"http", uri) == 0)
396 else if (_stricmp(
"socks5", uri) == 0)
404 WLog_ERR(TAG,
"Only HTTP and SOCKS5 proxies supported by now");
421 char* atPtr = strrchr(uri,
'@');
432 char* colonPtr = strchr(uri,
':');
434 if (!colonPtr || (colonPtr > atPtr))
436 WLog_ERR(TAG,
"invalid syntax for proxy (contains no password)");
443 WLog_ERR(TAG,
"unable to allocate proxy username");
451 WLog_ERR(TAG,
"unable to allocate proxy password");
460 char* p = strchr(uri,
':');
466 if (!value_to_int(&p[1], &val, 0, UINT16_MAX))
468 WLog_ERR(TAG,
"invalid syntax for proxy (invalid port)");
474 WLog_ERR(TAG,
"invalid syntax for proxy (port missing)");
483 if (_stricmp(
"http", protocol) == 0)
493 WLog_DBG(TAG,
"setting default proxy port: %" PRIu16, port);
500 char* p = strchr(uri,
'/');
507 if (_stricmp(
"", uri) == 0)
509 WLog_ERR(TAG,
"invalid syntax for proxy (hostname missing)");
515 WLog_INFO(TAG,
"Parsed proxy configuration: %s://%s:%s@%s:%" PRIu16, protocol,
522 WLog_INFO(TAG,
"Parsed proxy configuration: %s://%s:%" PRIu16, protocol,
530 WLog_WARN(TAG,
"Failed to parse proxy configuration: %s://%s:%" PRIu16, protocol, uri,
536BOOL proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
537 const char* proxyPassword,
const char* hostname, UINT16 port)
539 WINPR_ASSERT(context);
540 rdpSettings* settings = context->settings;
544 case PROXY_TYPE_NONE:
545 case PROXY_TYPE_IGNORE:
548 case PROXY_TYPE_HTTP:
549 return http_proxy_connect(context, bufferedBio, proxyUsername, proxyPassword, hostname,
552 case PROXY_TYPE_SOCKS:
553 return socks_proxy_connect(context, bufferedBio, proxyUsername, proxyPassword, hostname,
557 WLog_ERR(TAG,
"Invalid internal proxy configuration");
562static const char* get_response_header(
char* response)
564 char* current_pos = strchr(response,
'\r');
566 current_pos = strchr(response,
'\n');
574static BOOL http_proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
575 const char* proxyPassword,
const char* hostname, UINT16 port)
580 char port_str[10] = { 0 };
581 char recv_buf[256] = { 0 };
583 size_t resultsize = 0;
584 size_t reserveSize = 0;
587 const char connect[] =
"CONNECT ";
588 const char httpheader[] =
" HTTP/1.1" CRLF
"Host: ";
590 WINPR_ASSERT(context);
591 WINPR_ASSERT(bufferedBio);
592 WINPR_ASSERT(hostname);
593 const UINT32 timeout =
596 _itoa_s(port, port_str,
sizeof(port_str), 10);
598 hostLen = strlen(hostname);
599 portLen = strnlen(port_str,
sizeof(port_str));
600 reserveSize = strlen(connect) + (hostLen + 1 + portLen) * 2 + strlen(httpheader);
601 s = Stream_New(NULL, reserveSize);
605 Stream_Write(s, connect, strlen(connect));
606 Stream_Write(s, hostname, hostLen);
607 Stream_Write_UINT8(s,
':');
608 Stream_Write(s, port_str, portLen);
609 Stream_Write(s, httpheader, strlen(httpheader));
610 Stream_Write(s, hostname, hostLen);
611 Stream_Write_UINT8(s,
':');
612 Stream_Write(s, port_str, portLen);
614 if (proxyUsername && proxyPassword)
616 const int length = _scprintf(
"%s:%s", proxyUsername, proxyPassword);
619 const size_t size = (size_t)length + 1ull;
620 char* creds = (
char*)malloc(size);
626 const char basic[] = CRLF
"Proxy-Authorization: Basic ";
629 (void)sprintf_s(creds, size,
"%s:%s", proxyUsername, proxyPassword);
630 base64 = crypto_base64_encode((
const BYTE*)creds, size - 1);
632 if (!base64 || !Stream_EnsureRemainingCapacity(s, strlen(basic) + strlen(base64)))
638 Stream_Write(s, basic, strlen(basic));
639 Stream_Write(s, base64, strlen(base64));
647 if (!Stream_EnsureRemainingCapacity(s, 4))
650 Stream_Write(s, CRLF CRLF, 4);
654 const size_t pos = Stream_GetPosition(s);
658 status = BIO_write(bufferedBio, Stream_Buffer(s), WINPR_ASSERTING_INT_CAST(
int, pos));
661 if ((status < 0) || ((
size_t)status != Stream_GetPosition(s)))
663 WLog_ERR(TAG,
"HTTP proxy: failed to write CONNECT request");
670 const UINT64 start = GetTickCount64();
671 while (strstr(recv_buf, CRLF CRLF) == NULL)
673 if (resultsize >=
sizeof(recv_buf) - 1)
675 WLog_ERR(TAG,
"HTTP Reply headers too long: %s", get_response_header(recv_buf));
678 const size_t rdsize =
sizeof(recv_buf) - resultsize - 1ULL;
682 WINPR_ASSERT(rdsize <= INT32_MAX);
683 status = BIO_read(bufferedBio, (BYTE*)recv_buf + resultsize, (
int)rdsize);
688 if (!freerdp_shall_disconnect_context(context) && BIO_should_retry(bufferedBio))
694 WLog_ERR(TAG,
"Failed reading reply from HTTP proxy (Status %d)", status);
697 else if (status == 0)
699 const UINT64 now = GetTickCount64();
700 const UINT64 diff = now - start;
701 if (freerdp_shall_disconnect_context(context) || (now < start) || (diff > timeout))
704 WLog_ERR(TAG,
"Failed reading reply from HTTP proxy (BIO_read returned zero)");
710 resultsize += WINPR_ASSERTING_INT_CAST(
size_t, status);
715 eol = strchr(recv_buf,
'\r');
724 WLog_INFO(TAG,
"HTTP Proxy: %s", recv_buf);
726 if (strnlen(recv_buf,
sizeof(recv_buf)) < 12)
731 if (strncmp(recv_buf,
"HTTP/1.X 200", 12) != 0)
736 Stream_Free(s, TRUE);
740static int recv_socks_reply(rdpContext* context, BIO* bufferedBio, BYTE* buf,
int len,
char* reason,
745 WINPR_ASSERT(context);
747 const UINT32 timeout =
749 const UINT64 start = GetTickCount64();
753 status = BIO_read(bufferedBio, buf, len);
762 if (!freerdp_shall_disconnect_context(context) && BIO_should_retry(bufferedBio))
768 WLog_ERR(TAG,
"Failed reading %s reply from SOCKS proxy (Status %d)", reason, status);
771 else if (status == 0)
773 const UINT64 now = GetTickCount64();
774 const UINT64 diff = now - start;
775 if (freerdp_shall_disconnect_context(context) || (now < start) || (diff > timeout))
778 WLog_ERR(TAG,
"Failed reading %s reply from SOCKS proxy (BIO_read returned zero)",
787 WLog_ERR(TAG,
"Failed reading %s reply from SOCKS proxy (BIO_read returned zero)",
795 WLog_ERR(TAG,
"SOCKS Proxy reply packet too short (%s)", reason);
799 if (buf[0] != checkVer)
801 WLog_ERR(TAG,
"SOCKS Proxy version is not 5 (%s)", reason);
808static BOOL socks_proxy_userpass(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
809 const char* proxyPassword)
811 WINPR_ASSERT(context);
812 WINPR_ASSERT(bufferedBio);
814 if (!proxyUsername || !proxyPassword)
816 WLog_ERR(TAG,
"%s invalid username (%p) or password (%p)", logprefix, proxyUsername,
821 const size_t usernameLen = (BYTE)strnlen(proxyUsername, 256);
822 if (usernameLen > 255)
824 WLog_ERR(TAG,
"%s username too long (%" PRIuz
", max=255)", logprefix, usernameLen);
828 const size_t userpassLen = (BYTE)strnlen(proxyPassword, 256);
829 if (userpassLen > 255)
831 WLog_ERR(TAG,
"%s password too long (%" PRIuz
", max=255)", logprefix, userpassLen);
837 BYTE buf[2 * 255 + 3] = { 0 };
841 buf[offset++] = WINPR_ASSERTING_INT_CAST(uint8_t, usernameLen);
842 memcpy(&buf[offset], proxyUsername, usernameLen);
843 offset += usernameLen;
845 buf[offset++] = WINPR_ASSERTING_INT_CAST(uint8_t, userpassLen);
846 memcpy(&buf[offset], proxyPassword, userpassLen);
847 offset += userpassLen;
850 const int ioffset = WINPR_ASSERTING_INT_CAST(
int, offset);
851 const int status = BIO_write(bufferedBio, buf, ioffset);
853 if (status != ioffset)
855 WLog_ERR(TAG,
"%s error writing user/password request", logprefix);
861 const int status = recv_socks_reply(context, bufferedBio, buf,
sizeof(buf),
"AUTH REQ", 1);
868 WLog_ERR(TAG,
"%s invalid user/password", logprefix);
874static BOOL socks_proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
875 const char* proxyPassword,
const char* hostname, UINT16 port)
877 BYTE nauthMethods = 1;
878 const size_t hostnlen = strnlen(hostname, 255);
880 if (proxyUsername || proxyPassword)
885 const BYTE buf[] = { 5,
887 AUTH_M_NO_AUTH, AUTH_M_USR_PASS };
889 size_t writeLen =
sizeof(buf);
890 if (nauthMethods <= 1)
894 const int iwriteLen = WINPR_ASSERTING_INT_CAST(
int, writeLen);
895 const int status = BIO_write(bufferedBio, buf, iwriteLen);
897 if (status != iwriteLen)
899 WLog_ERR(TAG,
"SOCKS proxy: failed to write AUTH METHOD request", logprefix);
906 const int status = recv_socks_reply(context, bufferedBio, buf,
sizeof(buf),
"AUTH REQ", 5);
914 WLog_DBG(TAG,
"%s (NO AUTH) method was selected", logprefix);
917 case AUTH_M_USR_PASS:
918 if (nauthMethods < 2)
920 WLog_ERR(TAG,
"%s USER/PASS method was not proposed to server", logprefix);
923 if (!socks_proxy_userpass(context, bufferedBio, proxyUsername, proxyPassword))
928 WLog_ERR(TAG,
"%s unknown method 0x%x was selected by proxy", logprefix, buf[1]);
934 BYTE buf[262] = { 0 };
937 buf[offset++] = SOCKS_CMD_CONNECT;
940 if (inet_pton(AF_INET6, hostname, &buf[offset + 1]) == 1)
942 buf[offset++] = SOCKS_ADDR_IPV6;
945 else if (inet_pton(AF_INET, hostname, &buf[offset + 1]) == 1)
947 buf[offset++] = SOCKS_ADDR_IPV4;
952 buf[offset++] = SOCKS_ADDR_FQDN;
953 buf[offset++] = WINPR_ASSERTING_INT_CAST(uint8_t, hostnlen);
954 memcpy(&buf[offset], hostname, hostnlen);
958 if (offset >
sizeof(buf) - 2)
962 buf[offset++] = (port >> 8) & 0xff;
963 buf[offset++] = port & 0xff;
966 const int ioffset = WINPR_ASSERTING_INT_CAST(
int, offset);
967 const int status = BIO_write(bufferedBio, buf, ioffset);
969 if ((status < 0) || (status != ioffset))
971 WLog_ERR(TAG,
"SOCKS proxy: failed to write CONN REQ", logprefix);
976 BYTE buf[255] = { 0 };
977 const int status = recv_socks_reply(context, bufferedBio, buf,
sizeof(buf),
"CONN REQ", 5);
984 WLog_INFO(TAG,
"Successfully connected to %s:%" PRIu16, hostname, port);
988 if ((buf[1] > 0) && (buf[1] < 9))
989 WLog_INFO(TAG,
"SOCKS Proxy replied: %s", rplstat[buf[1]]);
991 WLog_INFO(TAG,
"SOCKS Proxy replied: %" PRIu8
" status not listed in rfc1928", buf[1]);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *param)
Sets a string settings value. The param is copied.
FREERDP_API UINT16 freerdp_settings_get_uint16(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt16 id)
Returns a UINT16 settings value.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API BOOL freerdp_settings_set_uint16(rdpSettings *settings, FreeRDP_Settings_Keys_UInt16 id, UINT16 param)
Sets a UINT16 settings value.