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;
379 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");
419 char* atPtr = strrchr(uri,
'@');
430 char* colonPtr = strchr(uri,
':');
432 if (!colonPtr || (colonPtr > atPtr))
434 WLog_ERR(TAG,
"invalid syntax for proxy (contains no password)");
441 WLog_ERR(TAG,
"unable to allocate proxy username");
449 WLog_ERR(TAG,
"unable to allocate proxy password");
456 p = strchr(uri,
':');
462 if (!value_to_int(&p[1], &val, 0, UINT16_MAX))
464 WLog_ERR(TAG,
"invalid syntax for proxy (invalid port)");
470 WLog_ERR(TAG,
"invalid syntax for proxy (port missing)");
479 if (_stricmp(
"http", protocol) == 0)
489 WLog_DBG(TAG,
"setting default proxy port: %" PRIu16, port);
495 p = strchr(uri,
'/');
501 if (_stricmp(
"", uri) == 0)
503 WLog_ERR(TAG,
"invalid syntax for proxy (hostname missing)");
509 WLog_INFO(TAG,
"Parsed proxy configuration: %s://%s:%s@%s:%" PRIu16, protocol,
516 WLog_INFO(TAG,
"Parsed proxy configuration: %s://%s:%" PRIu16, protocol,
524 WLog_WARN(TAG,
"Failed to parse proxy configuration: %s://%s:%" PRIu16, protocol, uri,
530BOOL proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
531 const char* proxyPassword,
const char* hostname, UINT16 port)
533 WINPR_ASSERT(context);
534 rdpSettings* settings = context->settings;
538 case PROXY_TYPE_NONE:
539 case PROXY_TYPE_IGNORE:
542 case PROXY_TYPE_HTTP:
543 return http_proxy_connect(context, bufferedBio, proxyUsername, proxyPassword, hostname,
546 case PROXY_TYPE_SOCKS:
547 return socks_proxy_connect(context, bufferedBio, proxyUsername, proxyPassword, hostname,
551 WLog_ERR(TAG,
"Invalid internal proxy configuration");
556static const char* get_response_header(
char* response)
558 char* current_pos = strchr(response,
'\r');
560 current_pos = strchr(response,
'\n');
568static BOOL http_proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
569 const char* proxyPassword,
const char* hostname, UINT16 port)
574 char port_str[10] = { 0 };
575 char recv_buf[256] = { 0 };
577 size_t resultsize = 0;
578 size_t reserveSize = 0;
581 const char connect[] =
"CONNECT ";
582 const char httpheader[] =
" HTTP/1.1" CRLF
"Host: ";
584 WINPR_ASSERT(context);
585 WINPR_ASSERT(bufferedBio);
586 WINPR_ASSERT(hostname);
587 const UINT32 timeout =
590 _itoa_s(port, port_str,
sizeof(port_str), 10);
592 hostLen = strlen(hostname);
593 portLen = strnlen(port_str,
sizeof(port_str));
594 reserveSize = strlen(connect) + (hostLen + 1 + portLen) * 2 + strlen(httpheader);
595 s = Stream_New(NULL, reserveSize);
599 Stream_Write(s, connect, strlen(connect));
600 Stream_Write(s, hostname, hostLen);
601 Stream_Write_UINT8(s,
':');
602 Stream_Write(s, port_str, portLen);
603 Stream_Write(s, httpheader, strlen(httpheader));
604 Stream_Write(s, hostname, hostLen);
605 Stream_Write_UINT8(s,
':');
606 Stream_Write(s, port_str, portLen);
608 if (proxyUsername && proxyPassword)
610 const int length = _scprintf(
"%s:%s", proxyUsername, proxyPassword);
613 const size_t size = (size_t)length + 1ull;
614 char* creds = (
char*)malloc(size);
620 const char basic[] = CRLF
"Proxy-Authorization: Basic ";
623 (void)sprintf_s(creds, size,
"%s:%s", proxyUsername, proxyPassword);
624 base64 = crypto_base64_encode((
const BYTE*)creds, size - 1);
626 if (!base64 || !Stream_EnsureRemainingCapacity(s, strlen(basic) + strlen(base64)))
632 Stream_Write(s, basic, strlen(basic));
633 Stream_Write(s, base64, strlen(base64));
641 if (!Stream_EnsureRemainingCapacity(s, 4))
644 Stream_Write(s, CRLF CRLF, 4);
646 const size_t pos = Stream_GetPosition(s);
650 status = BIO_write(bufferedBio, Stream_Buffer(s), (
int)pos);
652 if ((status < 0) || ((
size_t)status != Stream_GetPosition(s)))
654 WLog_ERR(TAG,
"HTTP proxy: failed to write CONNECT request");
660 const UINT64 start = GetTickCount64();
661 while (strstr(recv_buf, CRLF CRLF) == NULL)
663 if (resultsize >=
sizeof(recv_buf) - 1)
665 WLog_ERR(TAG,
"HTTP Reply headers too long: %s", get_response_header(recv_buf));
668 const size_t rdsize =
sizeof(recv_buf) - resultsize - 1ULL;
672 WINPR_ASSERT(rdsize <= INT32_MAX);
673 status = BIO_read(bufferedBio, (BYTE*)recv_buf + resultsize, (
int)rdsize);
678 if (!freerdp_shall_disconnect_context(context) && BIO_should_retry(bufferedBio))
684 WLog_ERR(TAG,
"Failed reading reply from HTTP proxy (Status %d)", status);
687 else if (status == 0)
689 const UINT64 now = GetTickCount64();
690 const UINT64 diff = now - start;
691 if (freerdp_shall_disconnect_context(context) || (now < start) || (diff > timeout))
694 WLog_ERR(TAG,
"Failed reading reply from HTTP proxy (BIO_read returned zero)");
702 WLog_ERR(TAG,
"Failed reading reply from HTTP proxy (BIO_read returned zero)");
706 resultsize += WINPR_ASSERTING_INT_CAST(
size_t, status);
710 eol = strchr(recv_buf,
'\r');
719 WLog_INFO(TAG,
"HTTP Proxy: %s", recv_buf);
721 if (strnlen(recv_buf,
sizeof(recv_buf)) < 12)
726 if (strncmp(recv_buf,
"HTTP/1.X 200", 12) != 0)
731 Stream_Free(s, TRUE);
735static int recv_socks_reply(rdpContext* context, BIO* bufferedBio, BYTE* buf,
int len,
char* reason,
740 WINPR_ASSERT(context);
742 const UINT32 timeout =
744 const UINT64 start = GetTickCount64();
748 status = BIO_read(bufferedBio, buf, len);
757 if (!freerdp_shall_disconnect_context(context) && BIO_should_retry(bufferedBio))
763 WLog_ERR(TAG,
"Failed reading %s reply from SOCKS proxy (Status %d)", reason, status);
766 else if (status == 0)
768 const UINT64 now = GetTickCount64();
769 const UINT64 diff = now - start;
770 if (freerdp_shall_disconnect_context(context) || (now < start) || (diff > timeout))
773 WLog_ERR(TAG,
"Failed reading %s reply from SOCKS proxy (BIO_read returned zero)",
782 WLog_ERR(TAG,
"Failed reading %s reply from SOCKS proxy (BIO_read returned zero)",
790 WLog_ERR(TAG,
"SOCKS Proxy reply packet too short (%s)", reason);
794 if (buf[0] != checkVer)
796 WLog_ERR(TAG,
"SOCKS Proxy version is not 5 (%s)", reason);
803static BOOL socks_proxy_userpass(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
804 const char* proxyPassword)
806 WINPR_ASSERT(context);
807 WINPR_ASSERT(bufferedBio);
809 if (!proxyUsername || !proxyPassword)
811 WLog_ERR(TAG,
"%s invalid username (%p) or password (%p)", logprefix, proxyUsername,
816 const size_t usernameLen = (BYTE)strnlen(proxyUsername, 256);
817 if (usernameLen > 255)
819 WLog_ERR(TAG,
"%s username too long (%" PRIuz
", max=255)", logprefix, usernameLen);
823 const size_t userpassLen = (BYTE)strnlen(proxyPassword, 256);
824 if (userpassLen > 255)
826 WLog_ERR(TAG,
"%s password too long (%" PRIuz
", max=255)", logprefix, userpassLen);
832 BYTE buf[2 * 255 + 3] = { 0 };
836 buf[offset++] = WINPR_ASSERTING_INT_CAST(uint8_t, usernameLen);
837 memcpy(&buf[offset], proxyUsername, usernameLen);
838 offset += usernameLen;
840 buf[offset++] = WINPR_ASSERTING_INT_CAST(uint8_t, userpassLen);
841 memcpy(&buf[offset], proxyPassword, userpassLen);
842 offset += userpassLen;
845 const int ioffset = WINPR_ASSERTING_INT_CAST(
int, offset);
846 const int status = BIO_write(bufferedBio, buf, ioffset);
848 if (status != ioffset)
850 WLog_ERR(TAG,
"%s error writing user/password request", logprefix);
856 const int status = recv_socks_reply(context, bufferedBio, buf,
sizeof(buf),
"AUTH REQ", 1);
863 WLog_ERR(TAG,
"%s invalid user/password", logprefix);
869static BOOL socks_proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
870 const char* proxyPassword,
const char* hostname, UINT16 port)
872 BYTE nauthMethods = 1;
873 const size_t hostnlen = strnlen(hostname, 255);
875 if (proxyUsername || proxyPassword)
880 const BYTE buf[] = { 5,
882 AUTH_M_NO_AUTH, AUTH_M_USR_PASS };
884 size_t writeLen =
sizeof(buf);
885 if (nauthMethods <= 1)
889 const int iwriteLen = WINPR_ASSERTING_INT_CAST(
int, writeLen);
890 const int status = BIO_write(bufferedBio, buf, iwriteLen);
892 if (status != iwriteLen)
894 WLog_ERR(TAG,
"SOCKS proxy: failed to write AUTH METHOD request", logprefix);
901 const int status = recv_socks_reply(context, bufferedBio, buf,
sizeof(buf),
"AUTH REQ", 5);
909 WLog_DBG(TAG,
"%s (NO AUTH) method was selected", logprefix);
912 case AUTH_M_USR_PASS:
913 if (nauthMethods < 2)
915 WLog_ERR(TAG,
"%s USER/PASS method was not proposed to server", logprefix);
918 if (!socks_proxy_userpass(context, bufferedBio, proxyUsername, proxyPassword))
923 WLog_ERR(TAG,
"%s unknown method 0x%x was selected by proxy", logprefix, buf[1]);
929 BYTE buf[262] = { 0 };
932 buf[offset++] = SOCKS_CMD_CONNECT;
935 if (inet_pton(AF_INET6, hostname, &buf[offset + 1]) == 1)
937 buf[offset++] = SOCKS_ADDR_IPV6;
940 else if (inet_pton(AF_INET, hostname, &buf[offset + 1]) == 1)
942 buf[offset++] = SOCKS_ADDR_IPV4;
947 buf[offset++] = SOCKS_ADDR_FQDN;
948 buf[offset++] = WINPR_ASSERTING_INT_CAST(uint8_t, hostnlen);
949 memcpy(&buf[offset], hostname, hostnlen);
953 if (offset >
sizeof(buf) - 2)
957 buf[offset++] = (port >> 8) & 0xff;
958 buf[offset++] = port & 0xff;
961 const int ioffset = WINPR_ASSERTING_INT_CAST(
int, offset);
962 const int status = BIO_write(bufferedBio, buf, ioffset);
964 if ((status < 0) || (status != ioffset))
966 WLog_ERR(TAG,
"SOCKS proxy: failed to write CONN REQ", logprefix);
971 BYTE buf[255] = { 0 };
972 const int status = recv_socks_reply(context, bufferedBio, buf,
sizeof(buf),
"CONN REQ", 5);
979 WLog_INFO(TAG,
"Successfully connected to %s:%" PRIu16, hostname, port);
983 if ((buf[1] > 0) && (buf[1] < 9))
984 WLog_INFO(TAG,
"SOCKS Proxy replied: %s", rplstat[buf[1]]);
986 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.