Skip to content

Commit c3f5aa8

Browse files
committed
test: Add user/password authenticated SOCKS5 proxy test
1 parent 3baf6e7 commit c3f5aa8

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-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

+40-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <pthread.h>
55
#include <stdbool.h>
66
#include <stdint.h>
7+
#include <string.h>
78

89
#include "auto_test_support.h"
910

@@ -16,32 +17,36 @@ static void *proxy_routine(void *arg)
1617
return nullptr;
1718
}
1819

19-
static bool try_bootstrap(Tox *tox1, Tox *tox2, Tox *tox3, Tox *tox4)
20+
static bool try_bootstrap(Tox *tox1, Tox *tox2, Tox *tox3, Tox *tox4, Tox *tox5)
2021
{
2122
for (uint32_t i = 0; i < 400; ++i) {
2223
if (tox_self_get_connection_status(tox1) != TOX_CONNECTION_NONE &&
2324
tox_self_get_connection_status(tox2) != TOX_CONNECTION_NONE &&
2425
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",
26+
tox_self_get_connection_status(tox4) != TOX_CONNECTION_NONE &&
27+
tox_self_get_connection_status(tox5) != TOX_CONNECTION_NONE) {
28+
printf("%d %d %d %d %d\n",
2729
tox_self_get_connection_status(tox1),
2830
tox_self_get_connection_status(tox2),
2931
tox_self_get_connection_status(tox3),
30-
tox_self_get_connection_status(tox4));
32+
tox_self_get_connection_status(tox4),
33+
tox_self_get_connection_status(tox5));
3134
return true;
3235
}
3336

3437
tox_iterate(tox1, nullptr);
3538
tox_iterate(tox2, nullptr);
3639
tox_iterate(tox3, nullptr);
3740
tox_iterate(tox4, nullptr);
41+
tox_iterate(tox5, nullptr);
3842

3943
if (i % 10 == 0) {
40-
printf("%d %d %d %d\n",
44+
printf("%d %d %d %d %d\n",
4145
tox_self_get_connection_status(tox1),
4246
tox_self_get_connection_status(tox2),
4347
tox_self_get_connection_status(tox3),
44-
tox_self_get_connection_status(tox4));
48+
tox_self_get_connection_status(tox4),
49+
tox_self_get_connection_status(tox5));
4550
}
4651

4752
c_sleep(tox_iteration_interval(tox1));
@@ -60,15 +65,16 @@ int main(int argc, char **argv)
6065
c_sleep(100);
6166
}
6267

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

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

6974
// tox1 is a TCP server and has UDP enabled.
7075
tox_options_set_udp_enabled(tox_options, true);
7176
tox_options_set_tcp_port(tox_options, tcp_port);
77+
tox_options_set_local_discovery_enabled(tox_options, false);
7278

7379
Tox *tox1 = tox_new_log(tox_options, nullptr, &index[0]);
7480
ck_assert(tox1 != nullptr);
@@ -80,8 +86,10 @@ int main(int argc, char **argv)
8086
ck_assert(dht_port != 0);
8187

8288
// tox2 is a regular DHT node bootstrapping against tox1.
89+
tox_options_default(tox_options);
8390
tox_options_set_udp_enabled(tox_options, true);
8491
tox_options_set_tcp_port(tox_options, 0);
92+
tox_options_set_local_discovery_enabled(tox_options, false);
8593

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

92100
// tox3 has UDP disabled and connects to tox1 via an HTTP proxy
101+
tox_options_default(tox_options);
93102
tox_options_set_udp_enabled(tox_options, false);
94103
tox_options_set_proxy_host(tox_options, "127.0.0.1");
95104
tox_options_set_proxy_port(tox_options, 8080);
96105
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_HTTP);
106+
tox_options_set_local_discovery_enabled(tox_options, false);
97107

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

101-
// tox4 has UDP disabled and connects to tox1 via a SOCKS5 proxy
111+
// tox4 has UDP disabled and connects to tox1 via a SOCKS5 proxy with no auth
112+
tox_options_default(tox_options);
102113
tox_options_set_udp_enabled(tox_options, false);
103114
tox_options_set_proxy_host(tox_options, "127.0.0.1");
104115
tox_options_set_proxy_port(tox_options, 8081);
105116
tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_SOCKS5);
117+
tox_options_set_local_discovery_enabled(tox_options, false);
106118

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

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

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

142+
ck_assert(tox_bootstrap(tox5, "127.0.0.1", dht_port, dht_pk, nullptr));
143+
ck_assert(tox_add_tcp_relay(tox5, "127.0.0.1", tcp_port, dht_pk, nullptr));
144+
117145
int ret = 1;
118-
if (try_bootstrap(tox1, tox2, tox3, tox4)) {
146+
if (try_bootstrap(tox1, tox2, tox3, tox4, tox5)) {
119147
ret = 0;
120148
}
121149

122150
tox_options_free(tox_options);
151+
tox_kill(tox5);
123152
tox_kill(tox4);
124153
tox_kill(tox3);
125154
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)