forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSOCKS.java
More file actions
259 lines (252 loc) · 7.75 KB
/
Copy pathSOCKS.java
File metadata and controls
259 lines (252 loc) · 7.75 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
package javaforce;
/** SOCKS client connect functions
*
* @author pquiring
*/
import java.io.*;
import java.net.*;
public class SOCKS {
/** Connects to host via a SOCKS4 server.
* Socket must already be connected to SOCKS4 server.
* host must be x.x.x.x
*
* @return boolean state if SOCKS4 connection to host:port was successful.
*/
public static boolean connect(Socket s, String host, int port) {
byte[] req = new byte[9];
byte[] reply = new byte[8];
req[0] = 0x04;
req[1] = 0x01;
BE.setuint16(req, 2, port);
String[] ips = host.split("[.]");
if (ips == null || ips.length != 4) {
JFLog.log("SOCKS.connect() : host is not an IP4 address");
return false;
}
for(int a=0;a<4;a++) {
req[4+a] = (byte)JF.atoi(ips[a]);
}
try {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
os.write(req);
int totalread = 0;
while (totalread != reply.length) {
int read = is.read(reply, totalread, reply.length - totalread);
if (read < 0) throw new Exception("bad read");
if (read > 0) totalread += read;
}
return reply[1] == 0x5a;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** Binds to a remote SOCKS4 server port.
* @param host = client allowed to connect (0.0.0.0 = any host)
* @param port = port to listen on socks server
*/
public static boolean bind(Socket s, String host, int port) {
byte[] req = new byte[9];
byte[] reply = new byte[8];
req[0] = 0x04;
req[1] = 0x02;
BE.setuint16(req, 2, port);
String[] ips = host.split("[.]");
if (ips == null || ips.length != 4) {
JFLog.log("SOCKS.bind() : host is not an IP4 address");
return false;
}
for(int a=0;a<4;a++) {
req[4+a] = (byte)JF.atoi(ips[a]);
}
//first reply
try {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
os.write(req);
int totalread = 0;
while (totalread != reply.length) {
int read = is.read(reply, totalread, reply.length - totalread);
if (read < 0) throw new Exception("bad read");
if (read > 0) totalread += read;
}
if (reply[1] != 0x5a) return false;
} catch (Exception e) {
JFLog.log(e);
return false;
}
//second reply after connection is made
try {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
os.write(req);
int totalread = 0;
while (totalread != reply.length) {
int read = is.read(reply, totalread, reply.length - totalread);
if (read < 0) throw new Exception("bad read");
if (read > 0) totalread += read;
}
return reply[1] == 0x5a;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** Connects to host via a SOCKS5 server.
* Socket must already be connected to SOCKS5 server.
* host may be x.x.x.x or domain name.
*
* @return boolean state if SOCKS5 connection to host:port was successful.
*/
public static boolean connect(Socket s, String host, int port, String user, String pass) {
if (!socks5_authenticate(s, user, pass)) return false;
byte[] req = new byte[10];
byte[] reply = new byte[10];
req[0] = 0x05; //version
req[1] = 0x01; //connect cmd
req[2] = 0x00; //reserved
String[] ips = host.split("[.]");
if (ips == null || ips.length != 4) {
JFLog.log("SOCKS.connect() : host is not an IP4 address");
return false;
}
//TODO : support type 3 (domain name)
req[3] = 0x01; //IP4 address
for(int a=0;a<4;a++) {
req[4+a] = (byte)JF.atoi(ips[a]);
}
BE.setuint16(req, 8, port);
try {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
os.write(req);
int totalread = 0;
while (totalread != reply.length) {
int read = is.read(reply, totalread, reply.length - totalread);
if (read < 0) throw new Exception("bad read");
if (read > 0) totalread += read;
}
boolean ok = reply[1] == 0x00;
if (!ok) {
JFLog.log("SOCKS:bind:denied.");
}
return ok;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
/** Binds to a remote SOCKS5 server port. */
public static boolean bind(Socket s, String host, int port, String user, String pass) {
if (!socks5_authenticate(s, user, pass)) return false;
byte[] req = new byte[10];
byte[] reply = new byte[10];
req[0] = 0x05; //version
req[1] = 0x02; //bind cmd
req[2] = 0x00; //reserved
String[] ips = host.split("[.]");
if (ips == null || ips.length != 4) {
JFLog.log("SOCKS.connect() : host is not an IP4 address");
return false;
}
//TODO : support type 3 (domain name)
req[3] = 0x01; //IP4 address
for(int a=0;a<4;a++) {
req[4+a] = (byte)JF.atoi(ips[a]);
}
BE.setuint16(req, 8, port);
try {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
os.write(req);
//first reply
int totalread = 0;
while (totalread != reply.length) {
int read = is.read(reply, totalread, reply.length - totalread);
if (read < 0) throw new Exception("bad read");
if (read > 0) totalread += read;
}
if (reply[1] != 0x00) {
JFLog.log("SOCKS:bind:1st reply denied.");
return false;
}
//second reply
totalread = 0;
while (totalread != reply.length) {
int read = is.read(reply, totalread, reply.length - totalread);
if (read < 0) throw new Exception("bad read");
if (read > 0) totalread += read;
}
boolean ok = reply[1] == 0x00;
if (!ok) {
JFLog.log("SOCKS:bind:2nd reply denied.");
}
return ok;
} catch (Exception e) {
JFLog.log(e);
return false;
}
}
private static boolean socks5_authenticate(Socket s, String user, String pass) {
try {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
//send auth request
{
byte[] req = new byte[3];
req[0] = 0x05; //version
req[1] = 0x01; //# of auth
req[2] = 0x02; //auth type : user/pass
os.write(req);
}
//read server reply
{
byte[] reply = new byte[2];
int totalread = 0;
while (totalread != reply.length) {
int read = is.read(reply, totalread, reply.length - totalread);
if (read < 0) throw new Exception("bad read");
if (read > 0) totalread += read;
}
if (reply[1] == 0xff) {
JFLog.log("SOCKS:No auth types available.");
return false;
}
}
//send user/pass
{
byte[] req = new byte[1 + 1 + user.length() + 1 + pass.length()];
req[0] = 0x01; //version
int offset = 1;
int userlen = user.length();
req[offset++] = (byte)userlen; //length of user
BE.setString(req, offset, userlen, user);
offset += userlen;
int passlen = pass.length();
req[offset++] = (byte)passlen; //length of pass
BE.setString(req, offset, passlen, pass);
offset += passlen;
os.write(req);
}
//read server reply
{
byte[] reply = new byte[2];
int totalread = 0;
while (totalread != reply.length) {
int read = is.read(reply, totalread, reply.length - totalread);
if (read < 0) throw new Exception("bad read");
if (read > 0) totalread += read;
}
if (reply[1] != 0x00) {
JFLog.log("SOCKS:Auth denied.");
return false;
}
}
return true;
} catch (Exception e) {
JFLog.log(e);
}
return false;
}
}