circuit: Refactor connection_proxy_connect

Since connection_proxy_connect is too long now, it's better to create
new functions (connection_https_proxy_connect,
connection_socks4_proxy_connect, and connection_socks5_proxy_connect) to
make connection_proxy_connect shorter.
This commit is contained in:
Suphanat Chunhapanya 2019-08-23 10:35:50 +08:00
parent 101bdeb02d
commit 52e59640f9

View File

@ -2326,29 +2326,21 @@ conn_get_proxy_type(const connection_t *conn)
username NUL: */
#define SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1)
/** Write a proxy request of <b>type</b> (socks4, socks5, https) to conn
* for conn->addr:conn->port, authenticating with the auth details given
* in the configuration (if available). SOCKS 5 and HTTP CONNECT proxies
* support authentication.
/** Write a proxy request of https to conn for conn->addr:conn->port,
* authenticating with the auth details given in the configuration
* (if available).
*
* Returns -1 if conn->addr is incompatible with the proxy protocol, and
* 0 otherwise.
*
* Use connection_read_proxy_handshake() to complete the handshake.
*/
int
connection_proxy_connect(connection_t *conn, int type)
static int
connection_https_proxy_connect(connection_t *conn)
{
const or_options_t *options;
tor_assert(conn);
options = get_options();
switch (type) {
case PROXY_CONNECT: {
const or_options_t *options = get_options();
char buf[1024];
char *base64_authenticator=NULL;
char *base64_authenticator = NULL;
const char *authenticator = options->HTTPSProxyAuthenticator;
/* Send HTTP CONNECT and authentication (if available) in
@ -2376,10 +2368,20 @@ connection_proxy_connect(connection_t *conn, int type)
connection_buf_add(buf, strlen(buf), conn);
conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
break;
}
case PROXY_SOCKS4: {
return 0;
}
/** Write a proxy request of socks4 to conn for conn->addr:conn->port.
*
* Returns -1 if conn->addr is incompatible with the proxy protocol, and
* 0 otherwise.
*/
static int
connection_socks4_proxy_connect(connection_t *conn)
{
tor_assert(conn);
unsigned char *buf;
uint16_t portn;
uint32_t ip4addr;
@ -2444,10 +2446,22 @@ connection_proxy_connect(connection_t *conn, int type)
tor_free(buf);
conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
break;
}
return 0;
}
case PROXY_SOCKS5: {
/** Write a proxy request of socks5 to conn for conn->addr:conn->port,
* authenticating with the auth details given in the configuration
* (if available).
*
* Returns -1 if conn->addr is incompatible with the proxy protocol, and
* 0 otherwise.
*/
static int
connection_socks5_proxy_connect(connection_t *conn)
{
tor_assert(conn);
const or_options_t *options = get_options();
unsigned char buf[4]; /* fields: vers, num methods, method list */
/* Send a SOCKS5 greeting (connect request must wait) */
@ -2472,19 +2486,52 @@ connection_proxy_connect(connection_t *conn, int type)
}
connection_buf_add((char *)buf, 2 + buf[1], conn);
return 0;
}
/** Write a proxy request of <b>type</b> (socks4, socks5, https, haproxy)
* to conn for conn->addr:conn->port, authenticating with the auth details
* given in the configuration (if available). SOCKS 5 and HTTP CONNECT
* proxies support authentication.
*
* Returns -1 if conn->addr is incompatible with the proxy protocol, and
* 0 otherwise.
*
* Use connection_read_proxy_handshake() to complete the handshake.
*/
int
connection_proxy_connect(connection_t *conn, int type)
{
int ret = 0;
tor_assert(conn);
switch (type) {
case PROXY_CONNECT:
ret = connection_https_proxy_connect(conn);
break;
case PROXY_SOCKS4:
ret = connection_socks4_proxy_connect(conn);
break;
case PROXY_SOCKS5:
ret = connection_socks5_proxy_connect(conn);
break;
}
default:
log_err(LD_BUG, "Invalid proxy protocol, %d", type);
tor_fragile_assert();
return -1;
ret = -1;
break;
}
if (ret == 0) {
log_debug(LD_NET, "set state %s",
connection_proxy_state_to_string(conn->proxy_state));
}
return 0;
return ret;
}
/** Read conn's inbuf. If the http response from the proxy is all