2014-09-22 18:58:01 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* HTTP Proxy support
|
|
|
|
*
|
2016-12-08 20:51:26 +03:00
|
|
|
* Copyright 2016 Christian Plattner <ccpp@gmx.at>
|
2014-09-22 18:58:01 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-12-11 01:13:35 +03:00
|
|
|
#include <ctype.h>
|
2017-11-14 18:10:52 +03:00
|
|
|
#include <errno.h>
|
2014-09-22 18:58:01 +04:00
|
|
|
|
2015-01-20 02:47:16 +03:00
|
|
|
#include "proxy.h"
|
2014-09-22 18:58:01 +04:00
|
|
|
#include "freerdp/settings.h"
|
|
|
|
#include "tcp.h"
|
|
|
|
|
2015-01-22 15:19:25 +03:00
|
|
|
#include "winpr/environment.h" /* For GetEnvironmentVariableA */
|
|
|
|
|
|
|
|
#define CRLF "\r\n"
|
2016-12-10 20:59:58 +03:00
|
|
|
#define TAG FREERDP_TAG("core.proxy")
|
2014-09-22 18:58:01 +04:00
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
/* SOCKS Proxy auth methods by rfc1928 */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
AUTH_M_NO_AUTH = 0,
|
|
|
|
AUTH_M_GSSAPI = 1,
|
|
|
|
AUTH_M_USR_PASS = 2
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SOCKS_CMD_CONNECT = 1,
|
|
|
|
SOCKS_CMD_BIND = 2,
|
|
|
|
SOCKS_CMD_UDP_ASSOCIATE = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SOCKS_ADDR_IPV4 = 1,
|
|
|
|
SOCKS_ADDR_FQDN = 3,
|
|
|
|
SOCKS_ADDR_IPV6 = 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* CONN REQ replies in enum. order */
|
2018-05-04 13:35:51 +03:00
|
|
|
static const char* rplstat[] =
|
2018-04-29 22:25:28 +03:00
|
|
|
{
|
|
|
|
"succeeded",
|
|
|
|
"general SOCKS server failure",
|
|
|
|
"connection not allowed by ruleset",
|
|
|
|
"Network unreachable",
|
|
|
|
"Host unreachable",
|
|
|
|
"Connection refused",
|
|
|
|
"TTL expired",
|
|
|
|
"Command not supported",
|
|
|
|
"Address type not supported"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-18 20:17:17 +03:00
|
|
|
static BOOL http_proxy_connect(BIO* bufferedBio, const char* hostname, UINT16 port);
|
2018-05-04 13:35:51 +03:00
|
|
|
static BOOL socks_proxy_connect(BIO* bufferedBio, const char* proxyUsername,
|
|
|
|
const char* proxyPassword, const char* hostname, UINT16 port);
|
2016-12-13 14:10:47 +03:00
|
|
|
void proxy_read_environment(rdpSettings* settings, char* envname);
|
2016-12-11 01:13:35 +03:00
|
|
|
|
2016-12-13 14:10:47 +03:00
|
|
|
BOOL proxy_prepare(rdpSettings* settings, const char** lpPeerHostname, UINT16* lpPeerPort,
|
2018-05-04 13:35:51 +03:00
|
|
|
const char** lpProxyUsername, const char** lpProxyPassword)
|
2016-12-11 01:13:35 +03:00
|
|
|
{
|
2018-08-21 12:01:54 +03:00
|
|
|
if (settings->ProxyType == PROXY_TYPE_IGNORE)
|
|
|
|
return FALSE;
|
|
|
|
|
2016-12-11 01:13:35 +03:00
|
|
|
/* For TSGateway, find the system HTTPS proxy automatically */
|
2018-08-21 12:01:54 +03:00
|
|
|
if (settings->ProxyType == PROXY_TYPE_NONE)
|
2016-12-11 01:13:35 +03:00
|
|
|
proxy_read_environment(settings, "https_proxy");
|
|
|
|
|
2018-08-21 12:01:54 +03:00
|
|
|
if (settings->ProxyType == PROXY_TYPE_NONE)
|
2016-12-11 01:13:35 +03:00
|
|
|
proxy_read_environment(settings, "HTTPS_PROXY");
|
|
|
|
|
2018-08-21 12:01:54 +03:00
|
|
|
if (settings->ProxyType != PROXY_TYPE_NONE)
|
2018-08-21 11:53:33 +03:00
|
|
|
proxy_read_environment(settings, "no_proxy");
|
|
|
|
|
2018-08-21 12:01:54 +03:00
|
|
|
if (settings->ProxyType != PROXY_TYPE_NONE)
|
2018-08-21 11:53:33 +03:00
|
|
|
proxy_read_environment(settings, "NO_PROXY");
|
|
|
|
|
2018-08-21 12:01:54 +03:00
|
|
|
if (settings->ProxyType != PROXY_TYPE_NONE)
|
2016-12-13 14:10:47 +03:00
|
|
|
{
|
2016-12-11 01:13:35 +03:00
|
|
|
*lpPeerHostname = settings->ProxyHostname;
|
|
|
|
*lpPeerPort = settings->ProxyPort;
|
2018-04-29 22:25:28 +03:00
|
|
|
*lpProxyUsername = settings->ProxyUsername;
|
|
|
|
*lpProxyPassword = settings->ProxyPassword;
|
2016-12-11 01:13:35 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-09-24 12:42:23 +03:00
|
|
|
static BOOL cidr4_match(const struct in_addr* addr, const struct in_addr* net, BYTE bits)
|
|
|
|
{
|
|
|
|
uint32_t mask, amask, nmask;
|
|
|
|
|
|
|
|
if (bits == 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
mask = htonl(0xFFFFFFFFu << (32 - bits));
|
|
|
|
amask = addr->s_addr & mask;
|
|
|
|
nmask = net->s_addr & mask;
|
|
|
|
return amask == nmask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL cidr6_match(const struct in6_addr* address, const struct in6_addr* network,
|
|
|
|
uint8_t bits)
|
|
|
|
{
|
|
|
|
const uint32_t* a = (const uint32_t*)address;
|
|
|
|
const uint32_t* n = (const uint32_t*)network;
|
|
|
|
size_t bits_whole, bits_incomplete;
|
|
|
|
bits_whole = bits >> 5;
|
|
|
|
bits_incomplete = bits & 0x1F;
|
|
|
|
|
|
|
|
if (bits_whole)
|
|
|
|
{
|
|
|
|
if (memcmp(a, n, bits_whole << 2) != 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bits_incomplete)
|
|
|
|
{
|
|
|
|
uint32_t mask = htonl((0xFFFFFFFFu) << (32 - bits_incomplete));
|
|
|
|
|
|
|
|
if ((a[bits_whole] ^ n[bits_whole]) & mask)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-08-21 11:53:33 +03:00
|
|
|
static BOOL check_no_proxy(rdpSettings* settings, const char* no_proxy)
|
|
|
|
{
|
|
|
|
const char* delimiter = ",";
|
|
|
|
BOOL result = FALSE;
|
|
|
|
char* current;
|
|
|
|
char* copy;
|
|
|
|
size_t host_len;
|
2018-09-24 13:24:15 +03:00
|
|
|
struct sockaddr_in sa4;
|
|
|
|
struct sockaddr_in6 sa6;
|
|
|
|
BOOL is_ipv4 = FALSE;
|
|
|
|
BOOL is_ipv6 = FALSE;
|
2018-08-21 11:53:33 +03:00
|
|
|
|
|
|
|
if (!no_proxy || !settings)
|
|
|
|
return FALSE;
|
|
|
|
|
2018-09-24 13:24:15 +03:00
|
|
|
if (inet_pton(AF_INET, settings->ServerHostname, &sa4.sin_addr) == 1)
|
|
|
|
is_ipv4 = TRUE;
|
|
|
|
else if (inet_pton(AF_INET6, settings->ServerHostname, &sa6.sin6_addr) == 1)
|
|
|
|
is_ipv6 = TRUE;
|
|
|
|
|
2018-08-21 11:53:33 +03:00
|
|
|
host_len = strlen(settings->ServerHostname);
|
|
|
|
copy = _strdup(no_proxy);
|
|
|
|
|
|
|
|
if (!copy)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
current = strtok(copy, delimiter);
|
|
|
|
|
|
|
|
while (current && !result)
|
|
|
|
{
|
|
|
|
const size_t currentlen = strlen(current);
|
|
|
|
|
|
|
|
if (currentlen > 0)
|
|
|
|
{
|
|
|
|
WLog_DBG(TAG, "%s => %s (%"PRIdz")", settings->ServerHostname, current, currentlen);
|
|
|
|
|
|
|
|
/* detect left and right "*" wildcard */
|
|
|
|
if (current[0] == '*')
|
|
|
|
{
|
|
|
|
if (host_len >= currentlen)
|
|
|
|
{
|
|
|
|
const size_t offset = host_len + 1 - currentlen;
|
2018-09-24 13:24:15 +03:00
|
|
|
const char* name = settings->ServerHostname + offset;
|
2018-08-21 11:53:33 +03:00
|
|
|
|
2018-09-24 13:24:15 +03:00
|
|
|
if (strncmp(current + 1, name, currentlen - 1) == 0)
|
2018-08-21 11:53:33 +03:00
|
|
|
result = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (current[currentlen - 1] == '*')
|
|
|
|
{
|
|
|
|
if (strncmp(current, settings->ServerHostname, currentlen - 1) == 0)
|
|
|
|
result = TRUE;
|
|
|
|
}
|
2018-09-24 13:24:15 +03:00
|
|
|
else if (current[0] == '.') /* Only compare if the no_proxy variable contains a whole domain. */
|
2018-08-21 11:53:33 +03:00
|
|
|
{
|
2018-09-24 13:24:15 +03:00
|
|
|
if (host_len > currentlen)
|
2018-08-21 11:53:33 +03:00
|
|
|
{
|
2018-09-24 13:24:15 +03:00
|
|
|
const size_t offset = host_len - currentlen;
|
|
|
|
const char* name = settings->ServerHostname + offset;
|
2018-08-21 11:53:33 +03:00
|
|
|
|
2018-09-24 13:24:15 +03:00
|
|
|
if (strncmp(current, name, currentlen) == 0)
|
|
|
|
result = TRUE; /* right-aligned match for host names */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(current, settings->ServerHostname) == 0)
|
|
|
|
result = TRUE; /* exact match */
|
|
|
|
else if (is_ipv4 || is_ipv6)
|
|
|
|
{
|
|
|
|
char* rangedelim = strchr(current, '/');
|
2018-08-21 11:53:33 +03:00
|
|
|
|
2018-09-24 13:24:15 +03:00
|
|
|
/* Check for IP ranges */
|
|
|
|
if (rangedelim != NULL)
|
|
|
|
{
|
|
|
|
const char* range = rangedelim + 1;
|
|
|
|
int sub;
|
|
|
|
int rc = sscanf(range, "%u", &sub);
|
|
|
|
|
|
|
|
if ((rc == 1) && (rc >= 0))
|
2018-08-21 11:53:33 +03:00
|
|
|
{
|
2018-09-24 13:24:15 +03:00
|
|
|
*rangedelim = '\0';
|
2018-09-24 12:42:23 +03:00
|
|
|
|
2018-09-24 13:24:15 +03:00
|
|
|
if (is_ipv4)
|
2018-09-24 12:42:23 +03:00
|
|
|
{
|
2018-09-24 13:24:15 +03:00
|
|
|
struct sockaddr_in mask;
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, current, &mask.sin_addr))
|
|
|
|
result = cidr4_match(&sa4.sin_addr, &mask.sin_addr, sub);
|
2018-09-24 12:42:23 +03:00
|
|
|
}
|
2018-09-24 13:24:15 +03:00
|
|
|
else if (is_ipv6)
|
2018-08-21 11:53:33 +03:00
|
|
|
{
|
2018-09-24 13:24:15 +03:00
|
|
|
struct sockaddr_in6 mask;
|
2018-08-21 11:53:33 +03:00
|
|
|
|
2018-09-24 13:24:15 +03:00
|
|
|
if (inet_pton(AF_INET6, current, &mask.sin6_addr))
|
|
|
|
result = cidr6_match(&sa6.sin6_addr, &mask.sin6_addr, sub);
|
2018-08-21 11:53:33 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 13:24:15 +03:00
|
|
|
else
|
|
|
|
WLog_WARN(TAG, "NO_PROXY invalid entry %s", current);
|
2018-08-21 11:53:33 +03:00
|
|
|
}
|
2018-09-24 13:24:15 +03:00
|
|
|
else if (strncmp(current, settings->ServerHostname, currentlen) == 0)
|
|
|
|
result = TRUE; /* left-aligned match for IPs */
|
2018-08-21 11:53:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
current = strtok(NULL, delimiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(copy);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:10:47 +03:00
|
|
|
void proxy_read_environment(rdpSettings* settings, char* envname)
|
2014-09-22 18:58:01 +04:00
|
|
|
{
|
|
|
|
DWORD envlen;
|
2016-12-13 14:10:47 +03:00
|
|
|
char* env;
|
2016-12-12 19:16:12 +03:00
|
|
|
envlen = GetEnvironmentVariableA(envname, NULL, 0);
|
2016-12-13 14:10:47 +03:00
|
|
|
|
|
|
|
if (!envlen)
|
2014-09-22 18:58:01 +04:00
|
|
|
return;
|
|
|
|
|
2017-03-03 14:37:27 +03:00
|
|
|
env = calloc(1, envlen);
|
2016-12-13 14:10:47 +03:00
|
|
|
|
|
|
|
if (!env)
|
|
|
|
{
|
2016-12-12 19:16:12 +03:00
|
|
|
WLog_ERR(TAG, "Not enough memory");
|
|
|
|
return;
|
|
|
|
}
|
2016-12-11 01:13:35 +03:00
|
|
|
|
2017-03-03 14:37:27 +03:00
|
|
|
if (GetEnvironmentVariableA(envname, env, envlen) == envlen - 1)
|
2018-08-21 11:53:33 +03:00
|
|
|
{
|
2018-08-21 12:01:54 +03:00
|
|
|
if (_strnicmp("NO_PROXY", envname, 9) == 0)
|
2018-08-21 11:53:33 +03:00
|
|
|
{
|
|
|
|
if (check_no_proxy(settings, env))
|
|
|
|
{
|
|
|
|
WLog_INFO(TAG, "deactivating proxy: %s [%s=%s]", settings->ServerHostname, envname, env);
|
|
|
|
settings->ProxyType = PROXY_TYPE_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
proxy_parse_uri(settings, env);
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 20:31:58 +03:00
|
|
|
|
2016-12-12 19:16:12 +03:00
|
|
|
free(env);
|
2016-12-11 01:13:35 +03:00
|
|
|
}
|
2014-09-22 18:58:01 +04:00
|
|
|
|
2016-12-13 14:10:47 +03:00
|
|
|
BOOL proxy_parse_uri(rdpSettings* settings, const char* uri)
|
2016-12-11 01:13:35 +03:00
|
|
|
{
|
2016-12-13 14:10:47 +03:00
|
|
|
const char* hostname, *pport;
|
|
|
|
const char* protocol;
|
|
|
|
const char* p;
|
2016-12-12 18:56:52 +03:00
|
|
|
UINT16 port;
|
2016-12-11 01:13:35 +03:00
|
|
|
int hostnamelen;
|
|
|
|
p = strstr(uri, "://");
|
2016-12-13 14:10:47 +03:00
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
if (p == uri + 4 && !strncmp("http", uri, 4))
|
|
|
|
{
|
2016-12-11 01:13:35 +03:00
|
|
|
settings->ProxyType = PROXY_TYPE_HTTP;
|
2016-12-12 18:56:52 +03:00
|
|
|
protocol = "http";
|
2016-12-13 14:10:47 +03:00
|
|
|
}
|
2019-08-12 17:57:51 +03:00
|
|
|
else if (p == uri + 6 && !strncmp("socks5", uri, 6))
|
|
|
|
{
|
|
|
|
settings->ProxyType = PROXY_TYPE_SOCKS;
|
|
|
|
protocol = "socks5";
|
|
|
|
}
|
2016-12-13 14:10:47 +03:00
|
|
|
else
|
|
|
|
{
|
2019-08-12 17:57:51 +03:00
|
|
|
WLog_ERR(TAG, "Only HTTP and SOCKS5 proxies supported by now");
|
2016-12-11 01:13:35 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-12-13 14:10:47 +03:00
|
|
|
|
2016-12-11 01:13:35 +03:00
|
|
|
uri = p + 3;
|
2016-12-13 14:10:47 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-11 01:13:35 +03:00
|
|
|
WLog_ERR(TAG, "No scheme in proxy URI");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-09-22 18:58:01 +04:00
|
|
|
|
2016-12-11 01:13:35 +03:00
|
|
|
hostname = uri;
|
2014-09-22 18:58:01 +04:00
|
|
|
pport = strchr(hostname, ':');
|
2016-12-13 14:10:47 +03:00
|
|
|
|
|
|
|
if (pport)
|
|
|
|
{
|
2017-11-14 18:10:52 +03:00
|
|
|
long val;
|
|
|
|
errno = 0;
|
|
|
|
val = strtol(pport + 1, NULL, 0);
|
|
|
|
|
|
|
|
if ((errno != 0) || (val <= 0) || (val > UINT16_MAX))
|
2016-12-11 01:13:35 +03:00
|
|
|
return FALSE;
|
2016-12-13 14:10:47 +03:00
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
port = val;
|
2014-09-22 18:58:01 +04:00
|
|
|
}
|
2016-12-13 14:10:47 +03:00
|
|
|
else
|
|
|
|
{
|
2014-09-22 18:58:01 +04:00
|
|
|
/* The default is 80. Also for Proxys. */
|
2016-12-12 18:56:52 +03:00
|
|
|
port = 80;
|
2014-09-22 18:58:01 +04:00
|
|
|
pport = strchr(hostname, '/');
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:10:47 +03:00
|
|
|
if (pport)
|
|
|
|
{
|
2016-12-11 01:13:35 +03:00
|
|
|
hostnamelen = pport - hostname;
|
2016-12-13 14:10:47 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-11 01:13:35 +03:00
|
|
|
hostnamelen = strlen(hostname);
|
|
|
|
}
|
|
|
|
|
2017-05-30 11:46:43 +03:00
|
|
|
settings->ProxyHostname = calloc(hostnamelen + 1, 1);
|
2016-12-13 14:10:47 +03:00
|
|
|
|
|
|
|
if (!settings->ProxyHostname)
|
|
|
|
{
|
2016-12-12 18:56:52 +03:00
|
|
|
WLog_ERR(TAG, "Not enough memory");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2016-12-13 14:10:47 +03:00
|
|
|
|
2016-12-12 18:56:52 +03:00
|
|
|
memcpy(settings->ProxyHostname, hostname, hostnamelen);
|
|
|
|
settings->ProxyPort = port;
|
2016-12-13 14:10:47 +03:00
|
|
|
WLog_INFO(TAG, "Parsed proxy configuration: %s://%s:%d", protocol, settings->ProxyHostname,
|
|
|
|
settings->ProxyPort);
|
2016-12-11 01:13:35 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-05-04 13:35:51 +03:00
|
|
|
BOOL proxy_connect(rdpSettings* settings, BIO* bufferedBio, const char* proxyUsername,
|
|
|
|
const char* proxyPassword,
|
|
|
|
const char* hostname, UINT16 port)
|
2016-12-11 01:13:35 +03:00
|
|
|
{
|
2016-12-13 14:10:47 +03:00
|
|
|
switch (settings->ProxyType)
|
|
|
|
{
|
|
|
|
case PROXY_TYPE_NONE:
|
2018-08-21 12:01:54 +03:00
|
|
|
case PROXY_TYPE_IGNORE:
|
2016-12-13 14:10:47 +03:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case PROXY_TYPE_HTTP:
|
|
|
|
return http_proxy_connect(bufferedBio, hostname, port);
|
|
|
|
|
2017-08-18 20:17:17 +03:00
|
|
|
case PROXY_TYPE_SOCKS:
|
2018-04-29 22:25:28 +03:00
|
|
|
return socks_proxy_connect(bufferedBio, proxyUsername, proxyPassword, hostname, port);
|
2017-08-18 20:17:17 +03:00
|
|
|
|
2016-12-13 14:10:47 +03:00
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "Invalid internal proxy configuration");
|
|
|
|
return FALSE;
|
2016-12-11 01:13:35 +03:00
|
|
|
}
|
2014-09-22 18:58:01 +04:00
|
|
|
}
|
|
|
|
|
2017-08-18 20:17:17 +03:00
|
|
|
static BOOL http_proxy_connect(BIO* bufferedBio, const char* hostname, UINT16 port)
|
2014-09-22 18:58:01 +04:00
|
|
|
{
|
|
|
|
int status;
|
2016-12-08 20:51:26 +03:00
|
|
|
wStream* s;
|
|
|
|
char port_str[10], recv_buf[256], *eol;
|
2019-02-07 16:22:28 +03:00
|
|
|
size_t resultsize;
|
2016-12-08 20:51:26 +03:00
|
|
|
_itoa_s(port, port_str, sizeof(port_str), 10);
|
|
|
|
s = Stream_New(NULL, 200);
|
|
|
|
Stream_Write(s, "CONNECT ", 8);
|
|
|
|
Stream_Write(s, hostname, strlen(hostname));
|
|
|
|
Stream_Write_UINT8(s, ':');
|
|
|
|
Stream_Write(s, port_str, strlen(port_str));
|
|
|
|
Stream_Write(s, " HTTP/1.1" CRLF "Host: ", 17);
|
|
|
|
Stream_Write(s, hostname, strlen(hostname));
|
|
|
|
Stream_Write_UINT8(s, ':');
|
2019-10-29 12:18:09 +03:00
|
|
|
Stream_Write(s, port_str, strnlen(port_str, sizeof (port_str)));
|
2016-12-08 20:51:26 +03:00
|
|
|
Stream_Write(s, CRLF CRLF, 4);
|
|
|
|
status = BIO_write(bufferedBio, Stream_Buffer(s), Stream_GetPosition(s));
|
2015-01-22 15:19:25 +03:00
|
|
|
|
2019-02-07 16:22:28 +03:00
|
|
|
if ((status < 0) || ((size_t)status != Stream_GetPosition(s)))
|
2016-12-13 14:10:47 +03:00
|
|
|
{
|
2018-08-17 17:22:56 +03:00
|
|
|
Stream_Free(s, TRUE);
|
2016-12-10 20:59:58 +03:00
|
|
|
WLog_ERR(TAG, "HTTP proxy: failed to write CONNECT request");
|
2015-01-22 15:19:25 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-09-22 18:58:01 +04:00
|
|
|
|
2016-12-08 20:51:26 +03:00
|
|
|
Stream_Free(s, TRUE);
|
|
|
|
s = NULL;
|
2014-09-22 18:58:01 +04:00
|
|
|
/* Read result until CR-LF-CR-LF.
|
2015-01-22 15:19:25 +03:00
|
|
|
* Keep recv_buf a null-terminated string. */
|
|
|
|
memset(recv_buf, '\0', sizeof(recv_buf));
|
2014-09-22 18:58:01 +04:00
|
|
|
resultsize = 0;
|
2016-12-13 14:10:47 +03:00
|
|
|
|
|
|
|
while (strstr(recv_buf, CRLF CRLF) == NULL)
|
|
|
|
{
|
|
|
|
if (resultsize >= sizeof(recv_buf) - 1)
|
|
|
|
{
|
2016-12-10 20:59:58 +03:00
|
|
|
WLog_ERR(TAG, "HTTP Reply headers too long.");
|
2014-09-22 18:58:01 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-12-13 14:10:47 +03:00
|
|
|
status = BIO_read(bufferedBio, (BYTE*)recv_buf + resultsize, sizeof(recv_buf) - resultsize - 1);
|
|
|
|
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2014-09-22 18:58:01 +04:00
|
|
|
/* Error? */
|
2016-12-13 14:10:47 +03:00
|
|
|
if (BIO_should_retry(bufferedBio))
|
|
|
|
{
|
2016-12-09 22:43:02 +03:00
|
|
|
USleep(100);
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-13 14:10:47 +03:00
|
|
|
|
2016-12-10 20:59:58 +03:00
|
|
|
WLog_ERR(TAG, "Failed reading reply from HTTP proxy (Status %d)", status);
|
2014-09-22 18:58:01 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-12-13 14:10:47 +03:00
|
|
|
else if (status == 0)
|
|
|
|
{
|
2014-09-22 18:58:01 +04:00
|
|
|
/* Error? */
|
2016-12-10 20:59:58 +03:00
|
|
|
WLog_ERR(TAG, "Failed reading reply from HTTP proxy (BIO_read returned zero)");
|
2014-09-22 18:58:01 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-12-13 14:10:47 +03:00
|
|
|
|
2014-09-22 18:58:01 +04:00
|
|
|
resultsize += status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract HTTP status line */
|
2015-01-22 15:19:25 +03:00
|
|
|
eol = strchr(recv_buf, '\r');
|
2016-12-13 14:10:47 +03:00
|
|
|
|
|
|
|
if (!eol)
|
|
|
|
{
|
2014-09-22 18:58:01 +04:00
|
|
|
/* should never happen */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*eol = '\0';
|
2016-12-10 20:59:58 +03:00
|
|
|
WLog_INFO(TAG, "HTTP Proxy: %s", recv_buf);
|
2014-09-22 18:58:01 +04:00
|
|
|
|
2019-10-29 12:18:09 +03:00
|
|
|
if (strnlen(recv_buf, sizeof(recv_buf)) < 12)
|
2016-12-13 14:10:47 +03:00
|
|
|
{
|
2014-09-22 18:58:01 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-01-22 15:19:25 +03:00
|
|
|
recv_buf[7] = 'X';
|
2016-12-13 14:10:47 +03:00
|
|
|
|
2015-01-22 15:19:25 +03:00
|
|
|
if (strncmp(recv_buf, "HTTP/1.X 200", 12))
|
2014-09-22 18:58:01 +04:00
|
|
|
return FALSE;
|
2016-12-13 14:10:47 +03:00
|
|
|
|
2014-09-22 18:58:01 +04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
static int recv_socks_reply(BIO* bufferedBio, BYTE* buf, int len, char* reason, BYTE checkVer)
|
2018-04-23 21:30:08 +03:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2018-05-04 13:35:51 +03:00
|
|
|
for (;;)
|
2018-04-29 22:25:28 +03:00
|
|
|
{
|
|
|
|
status = BIO_read(bufferedBio, buf, len);
|
2018-05-04 13:35:51 +03:00
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
if (status > 0)
|
2019-10-16 19:49:24 +03:00
|
|
|
{
|
2018-04-29 22:25:28 +03:00
|
|
|
break;
|
2019-10-16 19:49:24 +03:00
|
|
|
}
|
|
|
|
else if (status < 0)
|
2018-04-29 22:25:28 +03:00
|
|
|
{
|
|
|
|
/* Error? */
|
|
|
|
if (BIO_should_retry(bufferedBio))
|
|
|
|
{
|
|
|
|
USleep(100);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
WLog_ERR(TAG, "Failed reading %s reply from SOCKS proxy (Status %d)", reason, status);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-10-16 19:49:24 +03:00
|
|
|
else // if (status == 0)
|
2018-04-29 22:25:28 +03:00
|
|
|
{
|
|
|
|
/* Error? */
|
|
|
|
WLog_ERR(TAG, "Failed reading %s reply from SOCKS proxy (BIO_read returned zero)", reason);
|
|
|
|
return -1;
|
|
|
|
}
|
2018-04-23 21:30:08 +03:00
|
|
|
}
|
2018-05-04 13:35:51 +03:00
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
if (status < 2)
|
2018-04-23 21:30:08 +03:00
|
|
|
{
|
2018-04-29 22:25:28 +03:00
|
|
|
WLog_ERR(TAG, "SOCKS Proxy reply packet too short (%s)", reason);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[0] != checkVer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "SOCKS Proxy version is not 5 (%s)", reason);
|
|
|
|
return -1;
|
2018-04-23 21:30:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-05-04 13:35:51 +03:00
|
|
|
static BOOL socks_proxy_connect(BIO* bufferedBio, const char* proxyUsername,
|
|
|
|
const char* proxyPassword,
|
|
|
|
const char* hostname, UINT16 port)
|
2017-08-18 20:17:17 +03:00
|
|
|
{
|
|
|
|
int status;
|
2018-04-29 22:25:28 +03:00
|
|
|
int nauthMethods = 1, writeLen = 3;
|
|
|
|
BYTE buf[3 + 255 + 255]; /* biggest packet is user/pass auth */
|
|
|
|
size_t hostnlen = strnlen(hostname, 255);
|
|
|
|
|
|
|
|
if (proxyUsername && proxyPassword)
|
|
|
|
{
|
|
|
|
nauthMethods++;
|
|
|
|
writeLen++;
|
|
|
|
}
|
2017-08-18 20:17:17 +03:00
|
|
|
|
|
|
|
/* select auth. method */
|
|
|
|
buf[0] = 5; /* SOCKS version */
|
2018-04-29 22:25:28 +03:00
|
|
|
buf[1] = nauthMethods; /* #of methods offered */
|
2017-08-18 20:17:17 +03:00
|
|
|
buf[2] = AUTH_M_NO_AUTH;
|
2018-05-04 13:35:51 +03:00
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
if (nauthMethods > 1)
|
|
|
|
buf[3] = AUTH_M_USR_PASS;
|
|
|
|
|
|
|
|
status = BIO_write(bufferedBio, buf, writeLen);
|
2018-05-04 13:35:51 +03:00
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
if (status != writeLen)
|
2017-08-18 20:17:17 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "SOCKS proxy: failed to write AUTH METHOD request");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
status = recv_socks_reply(bufferedBio, buf, 2, "AUTH REQ", 5);
|
2018-05-04 13:35:51 +03:00
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
if (status <= 0)
|
|
|
|
return FALSE;
|
2017-08-18 20:17:17 +03:00
|
|
|
|
2018-05-04 13:35:51 +03:00
|
|
|
switch (buf[1])
|
2017-08-18 20:17:17 +03:00
|
|
|
{
|
2018-05-04 13:35:51 +03:00
|
|
|
case AUTH_M_NO_AUTH:
|
|
|
|
WLog_DBG(TAG, "SOCKS Proxy: (NO AUTH) method was selected");
|
|
|
|
break;
|
2018-04-29 22:25:28 +03:00
|
|
|
|
2018-05-04 13:35:51 +03:00
|
|
|
case AUTH_M_USR_PASS:
|
|
|
|
if (!proxyUsername || !proxyPassword)
|
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int usernameLen = strnlen(proxyUsername, 255);
|
|
|
|
int userpassLen = strnlen(proxyPassword, 255);
|
|
|
|
BYTE* ptr;
|
|
|
|
|
|
|
|
if (nauthMethods < 2)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "SOCKS Proxy: USER/PASS method was not proposed to server");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* user/password v1 method */
|
|
|
|
ptr = buf + 2;
|
|
|
|
buf[0] = 1;
|
|
|
|
buf[1] = usernameLen;
|
|
|
|
memcpy(ptr, proxyUsername, usernameLen);
|
|
|
|
ptr += usernameLen;
|
|
|
|
*ptr = userpassLen;
|
|
|
|
ptr++;
|
|
|
|
memcpy(ptr, proxyPassword, userpassLen);
|
|
|
|
status = BIO_write(bufferedBio, buf, 3 + usernameLen + userpassLen);
|
|
|
|
|
|
|
|
if (status != 3 + usernameLen + userpassLen)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "SOCKS Proxy: error writing user/password request");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = recv_socks_reply(bufferedBio, buf, 2, "AUTH REQ", 1);
|
|
|
|
|
|
|
|
if (status < 2)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (buf[1] != 0x00)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "SOCKS Proxy: invalid user/password");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2018-04-29 22:25:28 +03:00
|
|
|
|
2018-05-04 13:35:51 +03:00
|
|
|
break;
|
2018-04-29 22:25:28 +03:00
|
|
|
|
2018-05-04 13:35:51 +03:00
|
|
|
default:
|
|
|
|
WLog_ERR(TAG, "SOCKS Proxy: unknown method 0x%x was selected by proxy", buf[1]);
|
2018-04-29 22:25:28 +03:00
|
|
|
return FALSE;
|
2017-08-18 20:17:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CONN request */
|
|
|
|
buf[0] = 5; /* SOCKS version */
|
2018-04-29 22:25:28 +03:00
|
|
|
buf[1] = SOCKS_CMD_CONNECT; /* command */
|
|
|
|
buf[2] = 0; /* 3rd octet is reserved x00 */
|
|
|
|
buf[3] = SOCKS_ADDR_FQDN; /* addr.type */
|
2017-08-18 20:17:17 +03:00
|
|
|
buf[4] = hostnlen; /* DST.ADDR */
|
2018-04-29 22:25:28 +03:00
|
|
|
memcpy(buf + 5, hostname, hostnlen);
|
2017-08-18 20:17:17 +03:00
|
|
|
/* follows DST.PORT in netw. format */
|
2018-04-29 22:25:28 +03:00
|
|
|
buf[hostnlen + 5] = (port >> 8) & 0xff;
|
|
|
|
buf[hostnlen + 6] = port & 0xff;
|
2019-02-07 16:22:28 +03:00
|
|
|
status = BIO_write(bufferedBio, buf, hostnlen + 7U);
|
2018-05-04 13:35:51 +03:00
|
|
|
|
2019-02-07 16:22:28 +03:00
|
|
|
if ((status < 0) || ((size_t)status != (hostnlen + 7U)))
|
2017-08-18 20:17:17 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "SOCKS proxy: failed to write CONN REQ");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
status = recv_socks_reply(bufferedBio, buf, sizeof(buf), "CONN REQ", 5);
|
2018-05-04 13:35:51 +03:00
|
|
|
|
2018-04-29 22:25:28 +03:00
|
|
|
if (status < 4)
|
|
|
|
return FALSE;
|
2017-08-18 20:17:17 +03:00
|
|
|
|
|
|
|
if (buf[1] == 0)
|
|
|
|
{
|
2018-04-29 22:25:28 +03:00
|
|
|
WLog_INFO(TAG, "Successfully connected to %s:%d", hostname, port);
|
|
|
|
return TRUE;
|
2017-08-18 20:17:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buf[1] > 0 && buf[1] < 9)
|
2018-05-04 13:35:51 +03:00
|
|
|
WLog_INFO(TAG, "SOCKS Proxy replied: %s", rplstat[buf[1]]);
|
2017-08-18 20:17:17 +03:00
|
|
|
else
|
2018-05-04 13:35:51 +03:00
|
|
|
WLog_INFO(TAG, "SOCKS Proxy replied: %d status not listed in rfc1928", buf[1]);
|
2017-08-18 20:17:17 +03:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|