Skip to content

Commit 3a40adf

Browse files
committed
test: Add user/password authenticated SOCKS5 proxy test
1 parent 9b86155 commit 3a40adf

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

Diff for: auto_tests/invalid_udp_proxy_test.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main(void)
2424

2525
bootstrap_tox_live_network(tox, true);
2626

27-
printf("Waiting for connection...");
27+
printf("Waiting for connection...\n");
2828

2929
for (uint16_t i = 0; i < NUM_ITERATIONS; i++) {
3030
tox_iterate(tox, nullptr);

Diff for: auto_tests/proxy_test.c

+39-11
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,36 @@ static void *proxy_routine(void *arg)
1616
return nullptr;
1717
}
1818

19-
static bool try_bootstrap(Tox *tox1, Tox *tox2, Tox *tox3, Tox *tox4)
19+
static bool try_bootstrap(Tox *tox1, Tox *tox2, Tox *tox3, Tox *tox4, Tox *tox5)
2020
{
2121
for (uint32_t i = 0; i < 400; ++i) {
2222
if (tox_self_get_connection_status(tox1) != TOX_CONNECTION_NONE &&
2323
tox_self_get_connection_status(tox2) != TOX_CONNECTION_NONE &&
2424
tox_self_get_connection_status(tox3) != TOX_CONNECTION_NONE &&
25-
tox_self_get_connection_status(tox4) != TOX_CONNECTION_NONE) {
26-
printf("%d %d %d %d\n",
25+
tox_self_get_connection_status(tox4) != TOX_CONNECTION_NONE &&
26+
tox_self_get_connection_status(tox5) != TOX_CONNECTION_NONE) {
27+
printf("%d %d %d %d %d\n",
2728
tox_self_get_connection_status(tox1),
2829
tox_self_get_connection_status(tox2),
2930
tox_self_get_connection_status(tox3),
30-
tox_self_get_connection_status(tox4));
31+
tox_self_get_connection_status(tox4),
32+
tox_self_get_connection_status(tox5));
3133
return true;
3234
}
3335

3436
tox_iterate(tox1, nullptr);
3537
tox_iterate(tox2, nullptr);
3638
tox_iterate(tox3, nullptr);
3739
tox_iterate(tox4, nullptr);
40+
tox_iterate(tox5, nullptr);
3841

3942
if (i % 10 == 0) {
40-
printf("%d %d %d %d\n",
43+
printf("%d %d %d %d %d\n",
4144
tox_self_get_connection_status(tox1),
4245
tox_self_get_connection_status(tox2),
4346
tox_self_get_connection_status(tox3),
44-
tox_self_get_connection_status(tox4));
47+
tox_self_get_connection_status(tox4),
48+
tox_self_get_connection_status(tox5));
4549
}
4650

4751
c_sleep(tox_iteration_interval(tox1));
@@ -60,15 +64,16 @@ int main(int argc, char **argv)
6064
c_sleep(100);
6165
}
6266

63-
const uint16_t tcp_port = 8082;
64-
uint32_t index[] = { 1, 2, 3, 4 };
67+
const uint16_t tcp_port = 8083;
68+
uint32_t index[] = { 1, 2, 3, 4, 5 };
6569

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

6973
// tox1 is a TCP server and has UDP enabled.
7074
tox_options_set_udp_enabled(tox_options, true);
7175
tox_options_set_tcp_port(tox_options, tcp_port);
76+
tox_options_set_local_discovery_enabled(tox_options, false);
7277

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

8287
// tox2 is a regular DHT node bootstrapping against tox1.
88+
tox_options_default(tox_options);
8389
tox_options_set_udp_enabled(tox_options, true);
8490
tox_options_set_tcp_port(tox_options, 0);
91+
tox_options_set_local_discovery_enabled(tox_options, false);
8592

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

9299
// tox3 has UDP disabled and connects to tox1 via an HTTP proxy
100+
tox_options_default(tox_options);
93101
tox_options_set_udp_enabled(tox_options, false);
94102
tox_options_set_proxy_host(tox_options, "127.0.0.1");
95103
tox_options_set_proxy_port(tox_options, 8080);
96104
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_HTTP);
105+
tox_options_set_local_discovery_enabled(tox_options, false);
97106

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

101-
// tox4 has UDP disabled and connects to tox1 via a SOCKS5 proxy
110+
// tox4 has UDP disabled and connects to tox1 via a SOCKS5 proxy with no auth
111+
tox_options_default(tox_options);
102112
tox_options_set_udp_enabled(tox_options, false);
103113
tox_options_set_proxy_host(tox_options, "127.0.0.1");
104114
tox_options_set_proxy_port(tox_options, 8081);
105115
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_SOCKS5);
116+
tox_options_set_local_discovery_enabled(tox_options, false);
106117

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

110-
// tox3 and tox4 bootstrap against tox1 and add it as a TCP relay
121+
// tox5 has UDP disabled and connects to tox1 via a SOCKS5 proxy with username/password auth
122+
tox_options_default(tox_options);
123+
tox_options_set_udp_enabled(tox_options, false);
124+
tox_options_set_proxy_host(tox_options, "127.0.0.1");
125+
tox_options_set_proxy_port(tox_options, 8082);
126+
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_SOCKS5);
127+
tox_options_set_proxy_socks5_username(tox_options, (const uint8_t *) "nurupo", strlen("nurupo"));
128+
tox_options_set_proxy_socks5_password(tox_options, (const uint8_t *) "hunter2", strlen("hunter2"));
129+
tox_options_set_local_discovery_enabled(tox_options, false);
130+
131+
Tox *tox5 = tox_new_log(tox_options, nullptr, &index[4]);
132+
ck_assert(tox5 != nullptr);
133+
134+
// tox3, tox4 and tox5 bootstrap against tox1 and add it as a TCP relay
111135
ck_assert(tox_bootstrap(tox3, "127.0.0.1", dht_port, dht_pk, nullptr));
112136
ck_assert(tox_add_tcp_relay(tox3, "127.0.0.1", tcp_port, dht_pk, nullptr));
113137

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

141+
ck_assert(tox_bootstrap(tox5, "127.0.0.1", dht_port, dht_pk, nullptr));
142+
ck_assert(tox_add_tcp_relay(tox5, "127.0.0.1", tcp_port, dht_pk, nullptr));
143+
117144
int ret = 1;
118-
if (try_bootstrap(tox1, tox2, tox3, tox4)) {
145+
if (try_bootstrap(tox1, tox2, tox3, tox4, tox5)) {
119146
ret = 0;
120147
}
121148

122149
tox_options_free(tox_options);
150+
tox_kill(tox5);
123151
tox_kill(tox4);
124152
tox_kill(tox3);
125153
tox_kill(tox2);

Diff for: other/proxy/proxy_server.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import (
1414
)
1515

1616
const (
17-
debug = false
18-
httpAddr = ":8080"
19-
socks5Addr = ":8081"
17+
debug = false
18+
httpAddr = "127.0.0.1:8080"
19+
socks5NoAuthAddr = "127.0.0.1:8081"
20+
socks5UsernamePasswordAuthAddr = "127.0.0.1:8082"
2021
)
2122

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

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

83-
go socks5Server.ListenAndServe("tcp", socks5Addr)
84+
log.Printf("starting SOCKS5 username/password auth proxy server on %s", socks5UsernamePasswordAuthAddr)
85+
authenticator := socks5.UserPassAuthenticator{socks5.StaticCredentials{"nurupo": "hunter2"}}
86+
socks5UsernamePasswordAuthServer := socks5.NewServer(
87+
socks5.WithAuthMethods([]socks5.Authenticator{authenticator}),
88+
socks5.WithLogger(socks5.NewLogger(log.New(os.Stdout, "socks5 username/password auth: ", log.LstdFlags))),
89+
)
90+
91+
go socks5NoAuthServer.ListenAndServe("tcp", socks5NoAuthAddr)
92+
go socks5UsernamePasswordAuthServer.ListenAndServe("tcp", socks5UsernamePasswordAuthAddr)
8493
log.Fatal(httpServer.ListenAndServe())
8594
}

0 commit comments

Comments
 (0)