Skip to content

Commit

Permalink
test: Add user/password authenticated SOCKS5 proxy test
Browse files Browse the repository at this point in the history
  • Loading branch information
nurupo committed Feb 4, 2025
1 parent 9b86155 commit 3a40adf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
2 changes: 1 addition & 1 deletion auto_tests/invalid_udp_proxy_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main(void)

bootstrap_tox_live_network(tox, true);

printf("Waiting for connection...");
printf("Waiting for connection...\n");

for (uint16_t i = 0; i < NUM_ITERATIONS; i++) {
tox_iterate(tox, nullptr);
Expand Down
50 changes: 39 additions & 11 deletions auto_tests/proxy_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,36 @@ static void *proxy_routine(void *arg)
return nullptr;
}

static bool try_bootstrap(Tox *tox1, Tox *tox2, Tox *tox3, Tox *tox4)
static bool try_bootstrap(Tox *tox1, Tox *tox2, Tox *tox3, Tox *tox4, Tox *tox5)
{
for (uint32_t i = 0; i < 400; ++i) {
if (tox_self_get_connection_status(tox1) != TOX_CONNECTION_NONE &&
tox_self_get_connection_status(tox2) != TOX_CONNECTION_NONE &&
tox_self_get_connection_status(tox3) != TOX_CONNECTION_NONE &&
tox_self_get_connection_status(tox4) != TOX_CONNECTION_NONE) {
printf("%d %d %d %d\n",
tox_self_get_connection_status(tox4) != TOX_CONNECTION_NONE &&
tox_self_get_connection_status(tox5) != TOX_CONNECTION_NONE) {
printf("%d %d %d %d %d\n",
tox_self_get_connection_status(tox1),
tox_self_get_connection_status(tox2),
tox_self_get_connection_status(tox3),
tox_self_get_connection_status(tox4));
tox_self_get_connection_status(tox4),
tox_self_get_connection_status(tox5));
return true;
}

tox_iterate(tox1, nullptr);
tox_iterate(tox2, nullptr);
tox_iterate(tox3, nullptr);
tox_iterate(tox4, nullptr);
tox_iterate(tox5, nullptr);

if (i % 10 == 0) {
printf("%d %d %d %d\n",
printf("%d %d %d %d %d\n",
tox_self_get_connection_status(tox1),
tox_self_get_connection_status(tox2),
tox_self_get_connection_status(tox3),
tox_self_get_connection_status(tox4));
tox_self_get_connection_status(tox4),
tox_self_get_connection_status(tox5));
}

c_sleep(tox_iteration_interval(tox1));
Expand All @@ -60,15 +64,16 @@ int main(int argc, char **argv)
c_sleep(100);
}

const uint16_t tcp_port = 8082;
uint32_t index[] = { 1, 2, 3, 4 };
const uint16_t tcp_port = 8083;
uint32_t index[] = { 1, 2, 3, 4, 5 };

struct Tox_Options *tox_options = tox_options_new(nullptr);
ck_assert(tox_options != nullptr);

// tox1 is a TCP server and has UDP enabled.
tox_options_set_udp_enabled(tox_options, true);
tox_options_set_tcp_port(tox_options, tcp_port);
tox_options_set_local_discovery_enabled(tox_options, false);

Tox *tox1 = tox_new_log(tox_options, nullptr, &index[0]);
ck_assert(tox1 != nullptr);
Expand All @@ -80,8 +85,10 @@ int main(int argc, char **argv)
ck_assert(dht_port != 0);

// tox2 is a regular DHT node bootstrapping against tox1.
tox_options_default(tox_options);
tox_options_set_udp_enabled(tox_options, true);
tox_options_set_tcp_port(tox_options, 0);
tox_options_set_local_discovery_enabled(tox_options, false);

Tox *tox2 = tox_new_log(tox_options, nullptr, &index[1]);
ck_assert(tox2 != nullptr);
Expand All @@ -90,36 +97,57 @@ int main(int argc, char **argv)
ck_assert(tox_bootstrap(tox2, "127.0.0.1", dht_port, dht_pk, nullptr));

// tox3 has UDP disabled and connects to tox1 via an HTTP proxy
tox_options_default(tox_options);
tox_options_set_udp_enabled(tox_options, false);
tox_options_set_proxy_host(tox_options, "127.0.0.1");
tox_options_set_proxy_port(tox_options, 8080);
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_HTTP);
tox_options_set_local_discovery_enabled(tox_options, false);

Tox *tox3 = tox_new_log(tox_options, nullptr, &index[2]);
ck_assert(tox3 != nullptr);

// tox4 has UDP disabled and connects to tox1 via a SOCKS5 proxy
// tox4 has UDP disabled and connects to tox1 via a SOCKS5 proxy with no auth
tox_options_default(tox_options);
tox_options_set_udp_enabled(tox_options, false);
tox_options_set_proxy_host(tox_options, "127.0.0.1");
tox_options_set_proxy_port(tox_options, 8081);
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_SOCKS5);
tox_options_set_local_discovery_enabled(tox_options, false);

Tox *tox4 = tox_new_log(tox_options, nullptr, &index[3]);
ck_assert(tox4 != nullptr);

// tox3 and tox4 bootstrap against tox1 and add it as a TCP relay
// tox5 has UDP disabled and connects to tox1 via a SOCKS5 proxy with username/password auth
tox_options_default(tox_options);
tox_options_set_udp_enabled(tox_options, false);
tox_options_set_proxy_host(tox_options, "127.0.0.1");
tox_options_set_proxy_port(tox_options, 8082);
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_SOCKS5);
tox_options_set_proxy_socks5_username(tox_options, (const uint8_t *) "nurupo", strlen("nurupo"));
tox_options_set_proxy_socks5_password(tox_options, (const uint8_t *) "hunter2", strlen("hunter2"));
tox_options_set_local_discovery_enabled(tox_options, false);

Tox *tox5 = tox_new_log(tox_options, nullptr, &index[4]);
ck_assert(tox5 != nullptr);

// tox3, tox4 and tox5 bootstrap against tox1 and add it as a TCP relay
ck_assert(tox_bootstrap(tox3, "127.0.0.1", dht_port, dht_pk, nullptr));
ck_assert(tox_add_tcp_relay(tox3, "127.0.0.1", tcp_port, dht_pk, nullptr));

ck_assert(tox_bootstrap(tox4, "127.0.0.1", dht_port, dht_pk, nullptr));
ck_assert(tox_add_tcp_relay(tox4, "127.0.0.1", tcp_port, dht_pk, nullptr));

ck_assert(tox_bootstrap(tox5, "127.0.0.1", dht_port, dht_pk, nullptr));
ck_assert(tox_add_tcp_relay(tox5, "127.0.0.1", tcp_port, dht_pk, nullptr));

int ret = 1;
if (try_bootstrap(tox1, tox2, tox3, tox4)) {
if (try_bootstrap(tox1, tox2, tox3, tox4, tox5)) {
ret = 0;
}

tox_options_free(tox_options);
tox_kill(tox5);
tox_kill(tox4);
tox_kill(tox3);
tox_kill(tox2);
Expand Down
23 changes: 16 additions & 7 deletions other/proxy/proxy_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
)

const (
debug = false
httpAddr = ":8080"
socks5Addr = ":8081"
debug = false
httpAddr = "127.0.0.1:8080"
socks5NoAuthAddr = "127.0.0.1:8081"
socks5UsernamePasswordAuthAddr = "127.0.0.1:8082"
)

func handleTunneling(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -75,11 +76,19 @@ func main() {
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}

log.Printf("starting SOCKS5 proxy server on %s", socks5Addr)
socks5Server := socks5.NewServer(
socks5.WithLogger(socks5.NewLogger(log.New(os.Stdout, "socks5: ", log.LstdFlags))),
log.Printf("starting SOCKS5 no-auth proxy server on %s", socks5NoAuthAddr)
socks5NoAuthServer := socks5.NewServer(
socks5.WithLogger(socks5.NewLogger(log.New(os.Stdout, "socks5 no-auth: ", log.LstdFlags))),
)

go socks5Server.ListenAndServe("tcp", socks5Addr)
log.Printf("starting SOCKS5 username/password auth proxy server on %s", socks5UsernamePasswordAuthAddr)
authenticator := socks5.UserPassAuthenticator{socks5.StaticCredentials{"nurupo": "hunter2"}}
socks5UsernamePasswordAuthServer := socks5.NewServer(
socks5.WithAuthMethods([]socks5.Authenticator{authenticator}),
socks5.WithLogger(socks5.NewLogger(log.New(os.Stdout, "socks5 username/password auth: ", log.LstdFlags))),
)

go socks5NoAuthServer.ListenAndServe("tcp", socks5NoAuthAddr)
go socks5UsernamePasswordAuthServer.ListenAndServe("tcp", socks5UsernamePasswordAuthAddr)
log.Fatal(httpServer.ListenAndServe())
}

0 comments on commit 3a40adf

Please sign in to comment.