mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Further refactoring of proxy code
This commit is contained in:
parent
e9bf38499e
commit
35602e12c0
|
@ -37,15 +37,8 @@
|
|||
*/
|
||||
static int connect_sock_to(sock_t sock, IP_Port ip_port, TCP_Proxy_Info *proxy_info)
|
||||
{
|
||||
switch (proxy_info->proxy_type) {
|
||||
case TCP_PROXY_HTTP:
|
||||
ip_port = ((TCP_Proxy_HTTP*)proxy_info->proxy)->ip_port;
|
||||
break;
|
||||
case TCP_PROXY_SOCKS5:
|
||||
ip_port = ((TCP_Proxy_SOCKS5*)proxy_info->proxy)->ip_port;
|
||||
break;
|
||||
case TCP_PROXY_NONE:
|
||||
break;
|
||||
if (proxy_info->proxy_type != TCP_PROXY_NONE) {
|
||||
ip_port =proxy_info->ip_port;
|
||||
}
|
||||
|
||||
struct sockaddr_storage addr = {0};
|
||||
|
@ -613,15 +606,8 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public
|
|||
|
||||
uint8_t family = ip_port.ip.family;
|
||||
|
||||
switch (proxy_info->proxy_type) {
|
||||
case TCP_PROXY_HTTP:
|
||||
family = ((TCP_Proxy_HTTP*)proxy_info->proxy)->ip_port.ip.family;
|
||||
break;
|
||||
case TCP_PROXY_SOCKS5:
|
||||
family = ((TCP_Proxy_SOCKS5*)proxy_info->proxy)->ip_port.ip.family;
|
||||
break;
|
||||
case TCP_PROXY_NONE:
|
||||
break;
|
||||
if (proxy_info->proxy_type != TCP_PROXY_NONE) {
|
||||
family = proxy_info->ip_port.ip.family;
|
||||
}
|
||||
|
||||
sock_t sock = socket(family, SOCK_STREAM, IPPROTO_TCP);
|
||||
|
|
|
@ -37,17 +37,7 @@ typedef enum {
|
|||
|
||||
typedef struct {
|
||||
IP_Port ip_port;
|
||||
} TCP_Proxy_HTTP;
|
||||
|
||||
typedef struct {
|
||||
IP_Port ip_port;
|
||||
} TCP_Proxy_SOCKS5;
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t proxy_type; // a value from TCP_PROXY_TYPE
|
||||
void* proxy; // pointer to the corresponding proxy type struct
|
||||
} TCP_Proxy_Info;
|
||||
|
||||
enum {
|
||||
|
|
|
@ -1022,61 +1022,27 @@ Tox *tox_new(Tox_Options *options)
|
|||
m_options.udp_disabled = options->udp_disabled;
|
||||
|
||||
switch (options->proxy_type) {
|
||||
case TOX_PROXY_HTTP: {
|
||||
case TOX_PROXY_HTTP:
|
||||
m_options.proxy_info.proxy_type = TCP_PROXY_HTTP;
|
||||
m_options.proxy_info.proxy = malloc(sizeof(TCP_Proxy_HTTP));
|
||||
if (!m_options.proxy_info.proxy) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TCP_Proxy_HTTP* m_proxy = (TCP_Proxy_HTTP*)m_options.proxy_info.proxy;
|
||||
Tox_Proxy_HTTP* tox_proxy = (Tox_Proxy_HTTP*)options->proxy;
|
||||
|
||||
ip_init(&m_proxy->ip_port.ip, m_options.ipv6enabled);
|
||||
|
||||
if (m_options.ipv6enabled) {
|
||||
m_proxy->ip_port.ip.family = AF_UNSPEC;
|
||||
}
|
||||
|
||||
if (!addr_resolve_or_parse_ip(tox_proxy->address, &m_proxy->ip_port.ip, NULL)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
m_proxy->ip_port.port = htons(tox_proxy->port);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case TOX_PROXY_SOCKS5: {
|
||||
case TOX_PROXY_SOCKS5:
|
||||
m_options.proxy_info.proxy_type = TCP_PROXY_SOCKS5;
|
||||
m_options.proxy_info.proxy = malloc(sizeof(TCP_Proxy_SOCKS5));
|
||||
if (!m_options.proxy_info.proxy) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TCP_Proxy_SOCKS5* m_proxy = (TCP_Proxy_SOCKS5*)m_options.proxy_info.proxy;
|
||||
Tox_Proxy_SOCKS5* tox_proxy = (Tox_Proxy_SOCKS5*)options->proxy;
|
||||
|
||||
ip_init(&m_proxy->ip_port.ip, m_options.ipv6enabled);
|
||||
|
||||
if (m_options.ipv6enabled) {
|
||||
m_proxy->ip_port.ip.family = AF_UNSPEC;
|
||||
}
|
||||
|
||||
if (!addr_resolve_or_parse_ip(tox_proxy->address, &m_proxy->ip_port.ip, NULL)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
m_proxy->ip_port.port = htons(tox_proxy->port);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case TOX_PROXY_NONE: {
|
||||
case TOX_PROXY_NONE:
|
||||
m_options.proxy_info.proxy_type = TCP_PROXY_NONE;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) {
|
||||
ip_init(&m_options.proxy_info.ip_port.ip, m_options.ipv6enabled);
|
||||
|
||||
if (m_options.ipv6enabled)
|
||||
m_options.proxy_info.ip_port.ip.family = AF_UNSPEC;
|
||||
|
||||
if (!addr_resolve_or_parse_ip(options->proxy_address, &m_options.proxy_info.ip_port.ip, NULL))
|
||||
return NULL;
|
||||
|
||||
m_options.proxy_info.ip_port.port = htons(options->proxy_port);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -879,16 +879,6 @@ typedef enum {
|
|||
TOX_PROXY_SOCKS5
|
||||
} TOX_PROXY_TYPE;
|
||||
|
||||
typedef struct {
|
||||
char address[256]; /* Proxy ip or domain in NULL terminated string format. */
|
||||
uint16_t port; /* Proxy port: in host byte order. */
|
||||
} Tox_Proxy_HTTP;
|
||||
|
||||
typedef struct {
|
||||
char address[256]; /* Proxy ip or domain in NULL terminated string format. */
|
||||
uint16_t port; /* Proxy port: in host byte order. */
|
||||
} Tox_Proxy_SOCKS5;
|
||||
|
||||
typedef struct {
|
||||
/*
|
||||
* The type of UDP socket created depends on ipv6enabled:
|
||||
|
@ -903,8 +893,9 @@ typedef struct {
|
|||
This will force Tox to use TCP only which may slow things down.
|
||||
Disabling udp support is necessary when using proxies or Tor.*/
|
||||
uint8_t udp_disabled;
|
||||
uint8_t proxy_type; // a value from TOX_PROXY_TYPE
|
||||
void* proxy; // pointer to the corresponding proxy type struct
|
||||
uint8_t proxy_type; /* a value from TOX_PROXY_TYPE */
|
||||
char proxy_address[256]; /* Proxy ip or domain in NULL terminated string format. */
|
||||
uint16_t proxy_port; /* Proxy port in host byte order. */
|
||||
} Tox_Options;
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue
Block a user