20
20
public final class SSRLocal extends Thread
21
21
{
22
22
private ServerSocketChannel ssc ;
23
- private String locIP ;
24
- private String rmtIP ;
25
- private String pwd ;
26
- private String cryptMethod ;
27
- private String tcpProtocol ;
28
- private String obfsMethod ;
29
- private String obfsParam ;
30
- private int rmtPort ;
31
- private int locPort ;
23
+ private final String locIP ;
24
+ private final String rmtIP ;
25
+ private final String pwd ;
26
+ private final String cryptMethod ;
27
+ private final String tcpProtocol ;
28
+ private final String obfsMethod ;
29
+ private final String obfsParam ;
30
+ private final int rmtPort ;
31
+ private final int locPort ;
32
32
33
33
private volatile boolean isRunning = true ;
34
34
35
- private ExecutorService exec ;
35
+ private ExecutorService localThreadPool ;
36
+ private ExecutorService remoteThreadPool ;
36
37
37
38
private OnNeedProtectTCPListener onNeedProtectTCPListener ;
38
39
39
40
private List <String > aclList ;
40
41
41
- private HashMap <String , Object > shareParam ;
42
+ private final HashMap <String , Object > shareParam ;
42
43
43
44
public SSRLocal (String locIP , String rmtIP , int rmtPort , int locPort , String pwd ,
44
45
String cryptMethod , String tcpProtocol , String obfsMethod , String obfsParam ,
@@ -54,6 +55,7 @@ public SSRLocal(String locIP, String rmtIP, int rmtPort, int locPort, String pwd
54
55
this .obfsMethod = obfsMethod ;
55
56
this .obfsParam = obfsParam ;
56
57
this .aclList = aclList ;
58
+ shareParam = new HashMap <>();
57
59
}
58
60
59
61
public void setOnNeedProtectTCPListener (
@@ -77,22 +79,22 @@ class ChannelAttach
77
79
78
80
@ Override public void run ()
79
81
{
80
- shareParam = new HashMap <> ();
81
- exec = Executors .newCachedThreadPool ();
82
+ localThreadPool = Executors . newCachedThreadPool ();
83
+ remoteThreadPool = Executors .newCachedThreadPool ();
82
84
//new ThreadPoolExecutor(1, Integer.MAX_VALUE, 300L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
83
85
84
86
while (isRunning )//When tcp server crashed, restart it.
85
87
{
86
88
try
87
89
{
88
90
ssc = ServerSocketChannel .open ();
89
- ssc . configureBlocking ( true );
91
+ //default is block
90
92
ssc .socket ().bind (new InetSocketAddress (locIP , locPort ));
91
93
while (isRunning )
92
94
{
93
95
ChannelAttach attach = new ChannelAttach ();
94
96
attach .localSkt = ssc .accept ();
95
- exec . submit (new LocalSocketHandler (attach ));
97
+ localThreadPool . execute (new LocalSocketHandler (attach ));
96
98
}
97
99
}
98
100
catch (Exception ignored )
@@ -108,7 +110,6 @@ class ChannelAttach
108
110
}
109
111
catch (Exception ignored )
110
112
{
111
- ssc = null ;
112
113
}
113
114
}
114
115
}
@@ -173,8 +174,8 @@ else if (atype == (byte) 0x04)
173
174
}
174
175
attach .localReadBuf .position (cnt );
175
176
attach .localReadBuf .limit (attach .localReadBuf .capacity ());
176
- //TODO: not ipv6 list yet, but may be bypass loopback ::1, cidr fc00::/7,
177
- //TODO and... how to process ipv6 cidr.
177
+ //not ipv6 list yet, but may be bypass loopback ::1, cidr fc00::/7,
178
+ //and... how to process ipv6 cidr.
178
179
}
179
180
else
180
181
{
@@ -196,7 +197,7 @@ else if (atype == (byte) 0x04)
196
197
}
197
198
}
198
199
//
199
- new Thread (new RemoteSocketHandler (attach )). start ( );
200
+ remoteThreadPool . execute (new RemoteSocketHandler (attach ));
200
201
//
201
202
while (isRunning )
202
203
{
@@ -206,12 +207,12 @@ else if (atype == (byte) 0x04)
206
207
break ;
207
208
}
208
209
int rcnt = attach .localSkt .read (attach .localReadBuf );
209
- if (rcnt < 1 )
210
+ if (rcnt < 0 )
210
211
{
211
212
break ;
212
213
}
213
- attach .localReadBuf .flip ();
214
- byte [] recv = new byte [ attach . localReadBuf .limit ()];//size must be limit, not rcnt.
214
+ byte [] recv = new byte [ attach .localReadBuf .flip ()
215
+ .limit ()];//size must be limit, not rcnt.
215
216
attach .localReadBuf .get (recv );
216
217
217
218
if (!attach .isDirect )
@@ -251,9 +252,8 @@ else if (atype == (byte) 0x04)
251
252
Log .e ("EXC" , "CMD OK" );
252
253
handleData ();
253
254
}
254
- catch (Exception e )
255
+ catch (Exception ignored )
255
256
{
256
- Log .e ("EXC" , "LOCAL EXEC: " + e .getMessage ());
257
257
}
258
258
cleanSession (attach );
259
259
}
@@ -268,12 +268,12 @@ private boolean doAuth(final ChannelAttach attach) throws Exception
268
268
return false ;
269
269
}
270
270
attach .localReadBuf .flip ();
271
- if (attach .localReadBuf .get () != 0x05 )//Socks Version
271
+ if (attach .localReadBuf .get () != ( byte ) 0x05 )//Socks Version
272
272
{
273
273
return false ;
274
274
}
275
275
276
- int methodCnt = attach .localReadBuf .get ();
276
+ int methodCnt = attach .localReadBuf .get () & 0xFF ;
277
277
int mCnt = attach .localReadBuf .limit () - attach .localReadBuf .position ();
278
278
if (mCnt < methodCnt || mCnt > methodCnt )
279
279
{
@@ -305,7 +305,6 @@ private boolean processCMD(final ChannelAttach attach) throws Exception
305
305
}
306
306
307
307
attach .localReadBuf .flip ();
308
- //Utils.bufHexDmp("CMD", attach.localReadBuf.duplicate());
309
308
if (attach .localReadBuf .get () != 0x05 )//Socks Version
310
309
{
311
310
return false ;
@@ -321,8 +320,7 @@ private boolean processCMD(final ChannelAttach attach) throws Exception
321
320
{
322
321
case 0x01 :
323
322
//Response CMD
324
- attach .localSkt .write (ByteBuffer .wrap (new byte []{0x5 , 0x0 , 0x0 , 0x1 , 0x0 , 0x0 , 0x0 , 0x0 ,
325
- 0x0 , 0x0 }));
323
+ attach .localSkt .write (ByteBuffer .wrap (new byte []{5 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 }));
326
324
attach .localReadBuf .clear ();
327
325
return true ;
328
326
case 0x03 :
@@ -345,8 +343,8 @@ private boolean processCMD(final ChannelAttach attach) throws Exception
345
343
respb [respb .length - 2 ] = (byte ) ((locPort >> 8 ) & 0xFF );
346
344
attach .localSkt .write (ByteBuffer .wrap (respb ));
347
345
return true ;
348
- case 0x02 :
349
- //May be need reply 0x07(Cmd Not Support)
346
+ case 0x02 ://not support BIND
347
+ attach . localSkt . write ( ByteBuffer . wrap ( new byte []{ 5 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }));
350
348
default :
351
349
return false ;
352
350
}
@@ -371,9 +369,7 @@ private boolean prepareRemote(ChannelAttach attach, String remoteIP, int remoteP
371
369
private boolean checkSessionAlive (ChannelAttach attach )
372
370
{
373
371
return attach .localSkt != null &&
374
- attach .remoteSkt != null &&
375
- attach .localSkt .socket ().isConnected () &&
376
- attach .remoteSkt .socket ().isConnected ();
372
+ attach .remoteSkt != null ;
377
373
}
378
374
379
375
private void cleanSession (ChannelAttach attach )
@@ -388,6 +384,9 @@ private void cleanSession(ChannelAttach attach)
388
384
}
389
385
attach .remoteSkt = null ;
390
386
attach .localSkt = null ;
387
+ attach .obfs = null ;
388
+ attach .proto = null ;
389
+ attach .crypto = null ;
391
390
attach .localReadBuf = null ;
392
391
attach .remoteReadBuf = null ;
393
392
}
@@ -402,13 +401,13 @@ public void stopSSRLocal()
402
401
catch (Exception ignored )
403
402
{
404
403
}
405
- exec .shutdown ();
404
+ localThreadPool .shutdown ();
406
405
ssc = null ;
407
406
}
408
407
409
408
class RemoteSocketHandler implements Runnable
410
409
{
411
- private ChannelAttach attach ;
410
+ private final ChannelAttach attach ;
412
411
413
412
public RemoteSocketHandler (ChannelAttach attach )
414
413
{
@@ -427,11 +426,11 @@ public RemoteSocketHandler(ChannelAttach attach)
427
426
break ;
428
427
}
429
428
int rcnt = attach .remoteSkt .read (attach .remoteReadBuf );
430
- if (rcnt < 1 )
429
+ if (rcnt < 0 )
431
430
{
432
431
break ;
433
432
}
434
- Log . e ( "EXC" , "READ RMT CNT: " + rcnt );
433
+
435
434
attach .remoteReadBuf .flip ();
436
435
byte [] recv = new byte [rcnt ];
437
436
attach .remoteReadBuf .get (recv );
@@ -446,9 +445,8 @@ public RemoteSocketHandler(ChannelAttach attach)
446
445
attach .remoteReadBuf .clear ();
447
446
}
448
447
}
449
- catch (Exception e )
448
+ catch (Exception ignored )
450
449
{
451
- Log .e ("EXC" , "REMOTE EXEC L: " + e .getMessage ());
452
450
}
453
451
cleanSession (attach );
454
452
}
0 commit comments