示例#1
0
文件: torsocks.c 项目: Shrie/MiniTor
/*
 * Initiate a SOCK5 connection to the Tor network using the given connection.
 * The socks5 API will use the torsocks configuration object to find the tor
 * daemon. If a username/password has been set use that method for the SOCKS5
 * connection.
 *
 * Return 0 on success or else a negative value being the errno value that
 * needs to be sent back.
 */
int tsocks_connect_to_tor(struct connection *conn)
{
	int ret;
	uint8_t socks5_method;

	assert(conn);

	DBG("Connecting to the Tor network on fd %d", conn->fd);

	/* Is this configuration is set to use SOCKS5 authentication. */
	if (tsocks_config.socks5_use_auth) {
		socks5_method = SOCKS5_USER_PASS_METHOD;
	} else {
		socks5_method = SOCKS5_NO_AUTH_METHOD;
	}

	ret = setup_tor_connection(conn, socks5_method);
	if (ret < 0) {
		goto error;
	}

	/* For the user/pass method, send the request before connect. */
	if (socks5_method == SOCKS5_USER_PASS_METHOD) {
		ret = socks5_send_user_pass_request(conn,
				tsocks_config.conf_file.socks5_username,
				tsocks_config.conf_file.socks5_password);
		if (ret < 0) {
			goto error;
		}

		ret = socks5_recv_user_pass_reply(conn);
		if (ret < 0) {
			goto error;
		}
	}

	ret = socks5_send_connect_request(conn);
	if (ret < 0) {
		goto error;
	}

	ret = socks5_recv_connect_reply(conn);
	if (ret < 0) {
		goto error;
	}

error:
	return ret;
}
示例#2
0
/*
 * Using the given connection, do a SOCKS5 authentication with the
 * username/password in the global configuration.
 *
 * Return 0 on success else a negative value on error.
 */
static int
auth_socks5(struct connection *conn)
{
	int ret;

	assert(conn);

	ret = socks5_send_user_pass_request(conn,
			tsocks_config.conf_file.socks5_username,
			tsocks_config.conf_file.socks5_password);
	if (ret < 0) {
		goto error;
	}

	ret = socks5_recv_user_pass_reply(conn);
	if (ret < 0) {
		goto error;
	}

error:
	return ret;
}