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,
nullptr, 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 = WINPR_C_ARRAY_INIT;
218 struct sockaddr_in6 sa6 = WINPR_C_ARRAY_INIT;
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 = WINPR_C_ARRAY_INIT;
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,
nullptr, 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] = WINPR_C_ARRAY_INIT;
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 = WINPR_C_ARRAY_INIT;
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,
nullptr, 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 =
nullptr;
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 const char* ServerHostname =
311 WLog_DBG(TAG,
"%s => %s (%" PRIuz
")", ServerHostname, current, currentlen);
313 if (no_proxy_match_host(current, ServerHostname))
315 else if (no_proxy_match_ip(current, ServerHostname))
319 current = strtok_s(
nullptr, delimiter, &context);
326void proxy_read_environment(rdpSettings* settings,
char* envname)
328 const DWORD envlen = GetEnvironmentVariableA(envname,
nullptr, 0);
330 if (!envlen || (envlen <= 1))
333 char* env = calloc(1, envlen);
337 WLog_ERR(TAG,
"Not enough memory");
341 if (GetEnvironmentVariableA(envname, env, envlen) == envlen - 1)
343 if (_strnicmp(
"NO_PROXY", envname, 9) == 0)
345 if (check_no_proxy(settings, env))
347 WLog_INFO(TAG,
"deactivating proxy: %s [%s=%s]",
351 WLog_WARN(TAG,
"failed to set FreeRDP_ProxyType=PROXY_TYPE_NONE");
356 if (!proxy_parse_uri(settings, env))
359 TAG,
"Error while parsing proxy URI from environment variable; ignoring proxy");
367BOOL proxy_parse_uri(rdpSettings* settings,
const char* uri_in)
370 const char* protocol =
"";
373 if (!settings || !uri_in)
376 char* uri_copy = _strdup(uri_in);
377 char* uri = uri_copy;
382 char* p = strstr(uri,
"://");
387 if (_stricmp(
"no_proxy", uri) == 0)
392 if (_stricmp(
"http", uri) == 0)
398 else if (_stricmp(
"socks5", uri) == 0)
406 WLog_ERR(TAG,
"Only HTTP and SOCKS5 proxies supported by now");
423 char* atPtr = strrchr(uri,
'@');
434 char* colonPtr = strchr(uri,
':');
436 if (!colonPtr || (colonPtr > atPtr))
438 WLog_ERR(TAG,
"invalid syntax for proxy (contains no password)");
445 WLog_ERR(TAG,
"unable to allocate proxy username");
453 WLog_ERR(TAG,
"unable to allocate proxy password");
462 char* p = strchr(uri,
':');
468 if (!value_to_int(&p[1], &val, 0, UINT16_MAX))
470 WLog_ERR(TAG,
"invalid syntax for proxy (invalid port)");
476 WLog_ERR(TAG,
"invalid syntax for proxy (port missing)");
485 if (_stricmp(
"http", protocol) == 0)
495 WLog_DBG(TAG,
"setting default proxy port: %" PRIu16, port);
502 char* p = strchr(uri,
'/');
509 if (_stricmp(
"", uri) == 0)
511 WLog_ERR(TAG,
"invalid syntax for proxy (hostname missing)");
517 WLog_INFO(TAG,
"Parsed proxy configuration: %s://%s:%s@%s:%" PRIu16, protocol,
524 WLog_INFO(TAG,
"Parsed proxy configuration: %s://%s:%" PRIu16, protocol,
532 WLog_WARN(TAG,
"Failed to parse proxy configuration: %s://%s:%" PRIu16, protocol, uri,
538BOOL proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
539 const char* proxyPassword,
const char* hostname, UINT16 port)
541 WINPR_ASSERT(context);
542 rdpSettings* settings = context->settings;
546 case PROXY_TYPE_NONE:
547 case PROXY_TYPE_IGNORE:
550 case PROXY_TYPE_HTTP:
551 return http_proxy_connect(context, bufferedBio, proxyUsername, proxyPassword, hostname,
554 case PROXY_TYPE_SOCKS:
555 return socks_proxy_connect(context, bufferedBio, proxyUsername, proxyPassword, hostname,
559 WLog_ERR(TAG,
"Invalid internal proxy configuration");
564static const char* get_response_header(
char* response)
566 char* current_pos = strchr(response,
'\r');
568 current_pos = strchr(response,
'\n');
576static BOOL http_proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
577 const char* proxyPassword,
const char* hostname, UINT16 port)
582 char port_str[10] = WINPR_C_ARRAY_INIT;
583 char recv_buf[256] = WINPR_C_ARRAY_INIT;
585 size_t resultsize = 0;
586 size_t reserveSize = 0;
589 const char connect[] =
"CONNECT ";
590 const char httpheader[] =
" HTTP/1.1" CRLF
"Host: ";
592 WINPR_ASSERT(context);
593 WINPR_ASSERT(bufferedBio);
594 WINPR_ASSERT(hostname);
595 const UINT32 timeout =
598 if (_itoa_s(port, port_str,
sizeof(port_str), 10) < 0)
601 hostLen = strlen(hostname);
602 portLen = strnlen(port_str,
sizeof(port_str));
603 reserveSize = strlen(connect) + (hostLen + 1ull + portLen) * 2ull + strlen(httpheader);
604 s = Stream_New(
nullptr, reserveSize);
608 Stream_Write(s, connect, strlen(connect));
609 Stream_Write(s, hostname, hostLen);
610 Stream_Write_UINT8(s,
':');
611 Stream_Write(s, port_str, portLen);
612 Stream_Write(s, httpheader, strlen(httpheader));
613 Stream_Write(s, hostname, hostLen);
614 Stream_Write_UINT8(s,
':');
615 Stream_Write(s, port_str, portLen);
617 if (proxyUsername && proxyPassword)
619 const int length = _scprintf(
"%s:%s", proxyUsername, proxyPassword);
622 const size_t size = (size_t)length + 1ull;
623 char* creds = (
char*)malloc(size);
629 const char basic[] = CRLF
"Proxy-Authorization: Basic ";
630 char* base64 =
nullptr;
632 (void)sprintf_s(creds, size,
"%s:%s", proxyUsername, proxyPassword);
633 base64 = crypto_base64_encode((
const BYTE*)creds, size - 1);
635 if (!base64 || !Stream_EnsureRemainingCapacity(s, strlen(basic) + strlen(base64)))
641 Stream_Write(s, basic, strlen(basic));
642 Stream_Write(s, base64, strlen(base64));
650 if (!Stream_EnsureRemainingCapacity(s, 4))
653 Stream_Write(s, CRLF CRLF, 4);
657 const size_t pos = Stream_GetPosition(s);
661 status = BIO_write(bufferedBio, Stream_Buffer(s), WINPR_ASSERTING_INT_CAST(
int, pos));
664 if ((status < 0) || ((
size_t)status != Stream_GetPosition(s)))
666 WLog_ERR(TAG,
"HTTP proxy: failed to write CONNECT request");
673 const UINT64 start = GetTickCount64();
674 while (strstr(recv_buf, CRLF CRLF) ==
nullptr)
676 if (resultsize >=
sizeof(recv_buf) - 1)
678 WLog_ERR(TAG,
"HTTP Reply headers too long: %s", get_response_header(recv_buf));
681 const size_t rdsize =
sizeof(recv_buf) - resultsize - 1ULL;
685 WINPR_ASSERT(rdsize <= INT32_MAX);
686 status = BIO_read(bufferedBio, (BYTE*)recv_buf + resultsize, (
int)rdsize);
691 if (!freerdp_shall_disconnect_context(context) && BIO_should_retry(bufferedBio))
697 WLog_ERR(TAG,
"Failed reading reply from HTTP proxy (Status %d)", status);
700 else if (status == 0)
702 const UINT64 now = GetTickCount64();
703 const UINT64 diff = now - start;
704 if (freerdp_shall_disconnect_context(context) || (now < start) || (diff > timeout))
707 WLog_ERR(TAG,
"Failed reading reply from HTTP proxy (BIO_read returned zero)");
713 resultsize += WINPR_ASSERTING_INT_CAST(
size_t, status);
718 eol = strchr(recv_buf,
'\r');
727 WLog_INFO(TAG,
"HTTP Proxy: %s", recv_buf);
729 if (strnlen(recv_buf,
sizeof(recv_buf)) < 12)
734 if (strncmp(recv_buf,
"HTTP/1.X 200", 12) != 0)
739 Stream_Free(s, TRUE);
743static int recv_socks_reply(rdpContext* context, BIO* bufferedBio, BYTE* buf,
int len,
char* reason,
748 WINPR_ASSERT(context);
750 const UINT32 timeout =
752 const UINT64 start = GetTickCount64();
756 status = BIO_read(bufferedBio, buf, len);
765 if (!freerdp_shall_disconnect_context(context) && BIO_should_retry(bufferedBio))
771 WLog_ERR(TAG,
"Failed reading %s reply from SOCKS proxy (Status %d)", reason, status);
774 else if (status == 0)
776 const UINT64 now = GetTickCount64();
777 const UINT64 diff = now - start;
778 if (freerdp_shall_disconnect_context(context) || (now < start) || (diff > timeout))
781 WLog_ERR(TAG,
"Failed reading %s reply from SOCKS proxy (BIO_read returned zero)",
790 WLog_ERR(TAG,
"Failed reading %s reply from SOCKS proxy (BIO_read returned zero)",
798 WLog_ERR(TAG,
"SOCKS Proxy reply packet too short (%s)", reason);
802 if (buf[0] != checkVer)
804 WLog_ERR(TAG,
"SOCKS Proxy version is not 5 (%s)", reason);
811static BOOL socks_proxy_userpass(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
812 const char* proxyPassword)
814 WINPR_ASSERT(context);
815 WINPR_ASSERT(bufferedBio);
817 if (!proxyUsername || !proxyPassword)
819 WLog_ERR(TAG,
"%s invalid username (%p) or password (%p)", logprefix,
820 WINPR_CXX_COMPAT_CAST(
const void*, proxyUsername),
821 WINPR_CXX_COMPAT_CAST(
const void*, proxyPassword));
825 const size_t usernameLen = (BYTE)strnlen(proxyUsername, 256);
826 if (usernameLen > 255)
828 WLog_ERR(TAG,
"%s username too long (%" PRIuz
", max=255)", logprefix, usernameLen);
832 const size_t userpassLen = (BYTE)strnlen(proxyPassword, 256);
833 if (userpassLen > 255)
835 WLog_ERR(TAG,
"%s password too long (%" PRIuz
", max=255)", logprefix, userpassLen);
841 BYTE buf[2 * 255 + 3] = WINPR_C_ARRAY_INIT;
845 buf[offset++] = WINPR_ASSERTING_INT_CAST(uint8_t, usernameLen);
846 memcpy(&buf[offset], proxyUsername, usernameLen);
847 offset += usernameLen;
849 buf[offset++] = WINPR_ASSERTING_INT_CAST(uint8_t, userpassLen);
850 memcpy(&buf[offset], proxyPassword, userpassLen);
851 offset += userpassLen;
854 const int ioffset = WINPR_ASSERTING_INT_CAST(
int, offset);
855 const int status = BIO_write(bufferedBio, buf, ioffset);
857 if (status != ioffset)
859 WLog_ERR(TAG,
"%s error writing user/password request", logprefix);
864 BYTE buf[2] = WINPR_C_ARRAY_INIT;
865 const int status = recv_socks_reply(context, bufferedBio, buf,
sizeof(buf),
"AUTH REQ", 1);
872 WLog_ERR(TAG,
"%s invalid user/password", logprefix);
878static BOOL socks_proxy_connect(rdpContext* context, BIO* bufferedBio,
const char* proxyUsername,
879 const char* proxyPassword,
const char* hostname, UINT16 port)
881 BYTE nauthMethods = 1;
882 const size_t hostnlen = strnlen(hostname, 255);
884 if (proxyUsername || proxyPassword)
889 const BYTE buf[] = { 5,
891 AUTH_M_NO_AUTH, AUTH_M_USR_PASS };
893 size_t writeLen =
sizeof(buf);
894 if (nauthMethods <= 1)
898 const int iwriteLen = WINPR_ASSERTING_INT_CAST(
int, writeLen);
899 const int status = BIO_write(bufferedBio, buf, iwriteLen);
901 if (status != iwriteLen)
903 WLog_ERR(TAG,
"%s SOCKS proxy: failed to write AUTH METHOD request", logprefix);
909 BYTE buf[2] = WINPR_C_ARRAY_INIT;
910 const int status = recv_socks_reply(context, bufferedBio, buf,
sizeof(buf),
"AUTH REQ", 5);
918 WLog_DBG(TAG,
"%s (NO AUTH) method was selected", logprefix);
921 case AUTH_M_USR_PASS:
922 if (nauthMethods < 2)
924 WLog_ERR(TAG,
"%s USER/PASS method was not proposed to server", logprefix);
927 if (!socks_proxy_userpass(context, bufferedBio, proxyUsername, proxyPassword))
932 WLog_ERR(TAG,
"%s unknown method 0x%x was selected by proxy", logprefix, buf[1]);
938 BYTE buf[262] = WINPR_C_ARRAY_INIT;
941 buf[offset++] = SOCKS_CMD_CONNECT;
944 if (inet_pton(AF_INET6, hostname, &buf[offset + 1]) == 1)
946 buf[offset++] = SOCKS_ADDR_IPV6;
949 else if (inet_pton(AF_INET, hostname, &buf[offset + 1]) == 1)
951 buf[offset++] = SOCKS_ADDR_IPV4;
956 buf[offset++] = SOCKS_ADDR_FQDN;
957 buf[offset++] = WINPR_ASSERTING_INT_CAST(uint8_t, hostnlen);
958 memcpy(&buf[offset], hostname, hostnlen);
962 if (offset >
sizeof(buf) - 2)
966 buf[offset++] = (port >> 8) & 0xff;
967 buf[offset++] = port & 0xff;
970 const int ioffset = WINPR_ASSERTING_INT_CAST(
int, offset);
971 const int status = BIO_write(bufferedBio, buf, ioffset);
973 if ((status < 0) || (status != ioffset))
975 WLog_ERR(TAG,
"%s SOCKS proxy: failed to write CONN REQ", logprefix);
980 BYTE buf[255] = WINPR_C_ARRAY_INIT;
981 const int status = recv_socks_reply(context, bufferedBio, buf,
sizeof(buf),
"CONN REQ", 5);
988 WLog_INFO(TAG,
"Successfully connected to %s:%" PRIu16, hostname, port);
992 if ((buf[1] > 0) && (buf[1] < 9))
993 WLog_INFO(TAG,
"SOCKS Proxy replied: %s", rplstat[buf[1]]);
995 WLog_INFO(TAG,
"SOCKS Proxy replied: %" PRIu8
" status not listed in rfc1928", buf[1]);
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT16 freerdp_settings_get_uint16(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt16 id)
Returns a UINT16 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint16(rdpSettings *settings, FreeRDP_Settings_Keys_UInt16 id, UINT16 val)
Sets a UINT16 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.