-
Notifications
You must be signed in to change notification settings - Fork 0
/
testsocks5.js
177 lines (172 loc) · 5.75 KB
/
testsocks5.js
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
import { BufReader } from "https://deno.land/[email protected]/io/bufio.ts";
import { decodeString as hexdecode } from "https://deno.land/[email protected]/encoding/hex.ts";
import { varnum } from "https://deno.land/[email protected]/encoding/binary.ts";
import { equals } from "https://deno.land/[email protected]/bytes/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { concat } from "https://deno.land/[email protected]/bytes/mod.ts";
var args = parse(Deno.args);
if(args.h || args.help || args.v || args.version || !args.s){
console.log("$ testsocks5 -s SOCKS5_HOST:SOCKS5_PORT");
console.log("$ testsocks5 -s SOCKS5_HOST:SOCKS5_PORT -u USERNAME -p PASSWORD");
Deno.exit(0);
}
var l = args.s.split(":");
if(l.length != 2 || isNaN(new Number(l[1]))){
console.log("Invalid socks5 server");
Deno.exit(0);
}
var host = l[0];
var port = new Number(l[1]);
try{
console.log("Info:\t", "Testing TCP")
var c = await Deno.connect({ hostname: host, port: port, transport: "tcp" });
var r = await new BufReader(c);
// For convenience, we create a new buffer every time
var b = new Uint8Array([0x05, 0x01, 0x00]);
if(args.u && args.p){
b[2] = 0x02;
}
await c.write(b);
var b1 = new Uint8Array(2);
await r.readFull(b1);
if (b1[0] != 0x05){
console.log("Error:\t", "server is not socks version 5");
Deno.exit(1);
}
if (b1[1] != b[2]){
console.log("Error:\t", "server does not support method", b[2]);
Deno.exit(1);
}
if(b[2] == 0x02){
b = new Uint8Array([0x01]);
var u = new TextEncoder().encode(args.u);
b = concat(b, new Uint8Array([u.length]));
b = concat(b, u);
var p = new TextEncoder().encode(args.p);
b = concat(b, new Uint8Array([p.length]));
b = concat(b, p);
await c.write(b);
b = new Uint8Array(2);
await r.readFull(b);
if (b[1] != 0x00){
console.log("Error:\t", "invalid username or password");
Deno.exit(1);
}
}
b = new Uint8Array([0x05, 0x01, 0x00, 0x01, 0x08, 0x08, 0x08, 0x08, 0x00, 0x35]);
await c.write(b);
b = new Uint8Array(4);
await r.readFull(b);
if(b[1] != 0x00){
console.log("Error:\t", "Rep is not success");
Deno.exit(1);
}
if(b[3] == 0x01){
var b1 = new Uint8Array(6);
await r.readFull(b1);
}
if(b[3] == 0x04){
console.log("Error:\t", "This script does not support IPv6");
Deno.exit(1);
}
if(b[3] == 0x03){
var b1 = new Uint8Array(1);
await r.readFull(b1);
b1 = new Uint8Array(b1[0]+2);
await r.readFull(b1);
}
b = hexdecode("00200001010000010000000000000a74787468696e6b696e6703636f6d0000010001");
await c.write(b);
b = new Uint8Array(65507);
var i = await r.read(b);
c.close();
if(equals(b.slice(i-4, i), new Uint8Array([0x68, 0xc7, 0x8b, 0x17]))){
console.log("OK:\t", "TCP response is OK");
}else{
console.log("Warning", "TCP response is not expected");
}
}catch(e){
console.log("Error:\t", e)
Deno.exit(1);
}
try{
console.log("Info:\t", "Testing UDP")
var c = await Deno.connect({ hostname: host, port: port, transport: "tcp" });
var r = await new BufReader(c);
var b = new Uint8Array([0x05, 0x01, 0x00]);
if(args.u && args.p){
b[2] = 0x02;
}
await c.write(b);
var b1 = new Uint8Array(2);
await r.readFull(b1);
if (b1[0] != 0x05){
console.log("Error:\t", "server is not socks version 5");
Deno.exit(1);
}
if (b1[1] != b[2]){
console.log("Error:\t", "server does not support method", b[2]);
Deno.exit(1);
}
if(b[2] == 0x02){
b = new Uint8Array([0x01]);
var u = new TextEncoder().encode(args.u);
b = concat(b, new Uint8Array([u.length]));
b = concat(b, u);
var p = new TextEncoder().encode(args.p);
b = concat(b, new Uint8Array([p.length]));
b = concat(b, p);
await c.write(b);
b = new Uint8Array(2);
await r.readFull(b);
if (b[1] != 0x00){
console.log("Error:\t", "invalid username or password");
Deno.exit(1);
}
}
b = new Uint8Array([0x05, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
await c.write(b);
b = new Uint8Array(4);
await r.readFull(b);
if(b[1] != 0x00){
console.log("Error:\t", "Rep is not success");
Deno.exit(1);
}
var h, p;
if(b[3] == 0x01){
var b1 = new Uint8Array(4);
await r.readFull(b1);
h = b1.join(".");
b1 = new Uint8Array(2);
await r.readFull(b1);
p = varnum(b1, {dataType: "uint16", endian: "big"});
}
if(b[3] == 0x04){
console.log("Error:\t", "This script does not support IPv6");
Deno.exit(1);
}
if(b[3] == 0x03){
var b1 = new Uint8Array(1);
await r.readFull(b1);
b1 = new Uint8Array(b1[0]);
await r.readFull(b1);
h = new TextDecoder("utf-8").decode(b1);
b1 = new Uint8Array(2);
await r.readFull(b1);
p = varnum(b1, {dataType: "uint16", endian: "big"});
}
var c1 = Deno.listenDatagram({hostname: c.localAddr.hostname, port: c.localAddr.port, transport: "udp"});
b = hexdecode("000000010808080800350001010000010000000000000a74787468696e6b696e6703636f6d0000010001");
await c1.send(b, {transport: "udp", hostname: h, port: p});
b = new Uint8Array(65507);
var l = await c1.receive(b);
c1.close();
c.close();
if(equals(l[0].slice(-4), new Uint8Array([0x68, 0xc7, 0x8b, 0x17]))){
console.log("OK:\t", "UDP response is OK");
}else{
console.log("Warning", "UDP response is not expected");
}
}catch(e){
console.log("Error:\t", e)
}