-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_tor_isolation_test.go
More file actions
328 lines (265 loc) · 7.24 KB
/
client_tor_isolation_test.go
File metadata and controls
328 lines (265 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package socksgo_test
import (
"strings"
"testing"
socksgo "github.com/asciimoth/socksgo"
"github.com/asciimoth/socksgo/protocol"
)
func TestClientWithTorIsolation(t *testing.T) {
t.Parallel()
t.Run("nil client returns nil", func(t *testing.T) {
t.Parallel()
var client *socksgo.Client
result := client.WithTorIsolation(nil)
if result != nil {
t.Errorf(
"WithTorIsolation on nil client should return nil, got %v",
result,
)
}
})
t.Run("random isolation ID generation", func(t *testing.T) {
t.Parallel()
client := &socksgo.Client{
SocksVersion: "5",
ProxyAddr: "127.0.0.1:9050",
}
// Create two isolated clients with random IDs
isolated1 := client.WithTorIsolation(nil)
isolated2 := client.WithTorIsolation(nil)
if isolated1 == nil || isolated2 == nil {
t.Fatal("WithTorIsolation returned nil")
}
// Verify original client is unchanged
if client.Auth != nil && client.Auth.User() != "" {
t.Error("Original client Auth should be unchanged")
}
// Verify isolated clients have different random IDs
pass1, ok1 := getTorIsolationPassword(isolated1.Auth)
pass2, ok2 := getTorIsolationPassword(isolated2.Auth)
if !ok1 || !ok2 {
t.Fatal("Failed to get isolation password")
}
if pass1 == pass2 {
t.Error("Random isolation IDs should be different")
}
// Verify ID length (32 hex chars for 16 bytes)
if len(pass1) != 32 {
t.Errorf(
"Random isolation ID length should be 32, got %d",
len(pass1),
)
}
})
t.Run("specific isolation ID", func(t *testing.T) {
t.Parallel()
client := &socksgo.Client{
SocksVersion: "5",
ProxyAddr: "127.0.0.1:9050",
}
sessionID := "my-session-123"
isolated := client.WithTorIsolation(&sessionID)
if isolated == nil {
t.Fatal("WithTorIsolation returned nil")
}
pass, ok := getTorIsolationPassword(isolated.Auth)
if !ok {
t.Fatal("Failed to get isolation password")
}
if pass != sessionID {
t.Errorf("Isolation password should be %q, got %q", sessionID, pass)
}
})
t.Run("long ID is trimmed", func(t *testing.T) {
t.Parallel()
client := &socksgo.Client{
SocksVersion: "5",
ProxyAddr: "127.0.0.1:9050",
}
// Create ID longer than max password length
longID := strings.Repeat("x", 300)
isolated := client.WithTorIsolation(&longID)
if isolated == nil {
t.Fatal("WithTorIsolation returned nil")
}
pass, ok := getTorIsolationPassword(isolated.Auth)
if !ok {
t.Fatal("Failed to get isolation password")
}
if len(pass) != 255 {
t.Errorf("Long ID should be trimmed to 255, got %d", len(pass))
}
// Verify it's the prefix of the original
if pass != longID[:255] {
t.Error("Trimmed ID should be prefix of original")
}
})
t.Run("username has correct format", func(t *testing.T) {
t.Parallel()
client := &socksgo.Client{
SocksVersion: "5",
ProxyAddr: "127.0.0.1:9050",
}
isolationID := "test-id"
isolated := client.WithTorIsolation(&isolationID)
if isolated == nil {
t.Fatal("WithTorIsolation returned nil")
}
user, ok := getTorIsolationUsername(isolated.Auth)
if !ok {
t.Fatal("Failed to get isolation username")
}
// Expected: <torS0X>0 (magic prefix + format type 0)
expectedUser := "\x3c\x74\x6f\x72\x53\x30\x58\x3e" + "0"
if user != expectedUser {
t.Errorf("Username should be %q, got %q", expectedUser, user)
}
})
t.Run("other auth methods are removed", func(t *testing.T) {
t.Parallel()
client := &socksgo.Client{
SocksVersion: "5",
ProxyAddr: "127.0.0.1:9050",
Auth: (&protocol.AuthMethods{}).
Add(&protocol.PassAuthMethod{
User: "original-user",
Pass: "original-pass",
}),
}
isolationID := "new-isolation"
isolated := client.WithTorIsolation(&isolationID)
if isolated == nil {
t.Fatal("WithTorIsolation returned nil")
}
// Verify original client is unchanged
if client.Auth.User() != "original-user" {
t.Error("Original client Auth should be unchanged")
}
// Verify isolated client has only Tor isolation credentials
user, userOk := getTorIsolationUsername(isolated.Auth)
pass, passOk := getTorIsolationPassword(isolated.Auth)
if !userOk || !passOk {
t.Fatal("Failed to get isolation credentials")
}
expectedUser := "\x3c\x74\x6f\x72\x53\x30\x58\x3e" + "0"
if user != expectedUser {
t.Errorf("Username should be Tor magic prefix, got %q", user)
}
if pass != isolationID {
t.Errorf("Password should be isolation ID, got %q", pass)
}
})
t.Run("client fields are copied", func(t *testing.T) {
t.Parallel()
client := &socksgo.Client{
SocksVersion: "5",
ProxyAddr: "127.0.0.1:9050",
TLS: true,
GostMbind: true,
TorLookup: true,
}
isolated := client.WithTorIsolation(nil)
if isolated == nil {
t.Fatal("WithTorIsolation returned nil")
}
// Verify fields are copied
if isolated.SocksVersion != client.SocksVersion {
t.Errorf(
"SocksVersion should be copied, got %v",
isolated.SocksVersion,
)
}
if isolated.ProxyAddr != client.ProxyAddr {
t.Errorf(
"ProxyAddr should be copied, got %v",
isolated.ProxyAddr,
)
}
if isolated.TLS != client.TLS {
t.Errorf("TLS should be copied, got %v", isolated.TLS)
}
if isolated.GostMbind != client.GostMbind {
t.Errorf(
"GostMbind should be copied, got %v",
isolated.GostMbind,
)
}
if isolated.TorLookup != client.TorLookup {
t.Errorf(
"TorLookup should be copied, got %v",
isolated.TorLookup,
)
}
})
t.Run("empty isolation ID", func(t *testing.T) {
t.Parallel()
client := &socksgo.Client{
SocksVersion: "5",
ProxyAddr: "127.0.0.1:9050",
}
emptyID := ""
isolated := client.WithTorIsolation(&emptyID)
if isolated == nil {
t.Fatal("WithTorIsolation returned nil")
}
pass, ok := getTorIsolationPassword(isolated.Auth)
if !ok {
t.Fatal("Failed to get isolation password")
}
if pass != "" {
t.Errorf("Empty isolation ID should remain empty, got %q", pass)
}
})
t.Run("exact max length ID", func(t *testing.T) {
t.Parallel()
client := &socksgo.Client{
SocksVersion: "5",
ProxyAddr: "127.0.0.1:9050",
}
// Create ID exactly at max length
exactMaxID := strings.Repeat("y", 255)
isolated := client.WithTorIsolation(&exactMaxID)
if isolated == nil {
t.Fatal("WithTorIsolation returned nil")
}
pass, ok := getTorIsolationPassword(isolated.Auth)
if !ok {
t.Fatal("Failed to get isolation password")
}
if len(pass) != 255 {
t.Errorf("ID at max length should remain 255, got %d", len(pass))
}
if pass != exactMaxID {
t.Error("ID at max length should not be modified")
}
})
}
// Helper functions to extract Tor isolation credentials
func getTorIsolationPassword(auth *protocol.AuthMethods) (string, bool) {
if auth == nil {
return "", false
}
method := auth.Get(protocol.PassAuthCode)
if method == nil {
return "", false
}
passMethod, ok := method.(*protocol.PassAuthMethod)
if !ok {
return "", false
}
return passMethod.Pass, true
}
func getTorIsolationUsername(auth *protocol.AuthMethods) (string, bool) {
if auth == nil {
return "", false
}
method := auth.Get(protocol.PassAuthCode)
if method == nil {
return "", false
}
passMethod, ok := method.(*protocol.PassAuthMethod)
if !ok {
return "", false
}
return passMethod.User, true
}