Skip to content

Commit 90e324d

Browse files
committed
review all code and optimized some code.
1 parent 26846b6 commit 90e324d

File tree

13 files changed

+223
-240
lines changed

13 files changed

+223
-240
lines changed

CHANGE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
+ Reduce RAM usage.
44
+ Improve crypt operating speed.
55
+ Update tun2socks from shadowsocks/tun2socks repo.
6+
+ Add backupagent, this only worked on android6+
7+
+ Clean code.
68
+ ```May be contains many bug, even not work properly```.
79

810
##1.1.0 Test

app/src/main/java/com/proxy/shadowsocksr/impl/AddressUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static int ipv4BytesToInt(byte[] bytes)
5151
return addr;
5252
}
5353

54+
//TODO How to optimized cidr range check
5455
public static boolean checkInCIDRRange(int ip, List<String> cidrs)
5556
{
5657
Matcher matcher;
@@ -77,8 +78,8 @@ public static boolean checkInCIDRRange(int ip, List<String> cidrs)
7778
netmask |= (1 << 31 - j);
7879
}
7980

80-
network = (address & netmask);
81-
broadcast = network | ~(netmask);
81+
network = address & netmask;
82+
broadcast = network | ~netmask;
8283
//
8384
long addLong = ip & UNSIGNED_INT_MASK;
8485
long lowLong =

app/src/main/java/com/proxy/shadowsocksr/impl/SSRLocal.java

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,26 @@
2020
public final class SSRLocal extends Thread
2121
{
2222
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;
3232

3333
private volatile boolean isRunning = true;
3434

35-
private ExecutorService exec;
35+
private ExecutorService localThreadPool;
36+
private ExecutorService remoteThreadPool;
3637

3738
private OnNeedProtectTCPListener onNeedProtectTCPListener;
3839

3940
private List<String> aclList;
4041

41-
private HashMap<String, Object> shareParam;
42+
private final HashMap<String, Object> shareParam;
4243

4344
public SSRLocal(String locIP, String rmtIP, int rmtPort, int locPort, String pwd,
4445
String cryptMethod, String tcpProtocol, String obfsMethod, String obfsParam,
@@ -54,6 +55,7 @@ public SSRLocal(String locIP, String rmtIP, int rmtPort, int locPort, String pwd
5455
this.obfsMethod = obfsMethod;
5556
this.obfsParam = obfsParam;
5657
this.aclList = aclList;
58+
shareParam = new HashMap<>();
5759
}
5860

5961
public void setOnNeedProtectTCPListener(
@@ -77,22 +79,22 @@ class ChannelAttach
7779

7880
@Override public void run()
7981
{
80-
shareParam = new HashMap<>();
81-
exec = Executors.newCachedThreadPool();
82+
localThreadPool = Executors.newCachedThreadPool();
83+
remoteThreadPool = Executors.newCachedThreadPool();
8284
//new ThreadPoolExecutor(1, Integer.MAX_VALUE, 300L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
8385

8486
while (isRunning)//When tcp server crashed, restart it.
8587
{
8688
try
8789
{
8890
ssc = ServerSocketChannel.open();
89-
ssc.configureBlocking(true);
91+
//default is block
9092
ssc.socket().bind(new InetSocketAddress(locIP, locPort));
9193
while (isRunning)
9294
{
9395
ChannelAttach attach = new ChannelAttach();
9496
attach.localSkt = ssc.accept();
95-
exec.submit(new LocalSocketHandler(attach));
97+
localThreadPool.execute(new LocalSocketHandler(attach));
9698
}
9799
}
98100
catch (Exception ignored)
@@ -108,7 +110,6 @@ class ChannelAttach
108110
}
109111
catch (Exception ignored)
110112
{
111-
ssc = null;
112113
}
113114
}
114115
}
@@ -173,8 +174,8 @@ else if (atype == (byte) 0x04)
173174
}
174175
attach.localReadBuf.position(cnt);
175176
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.
178179
}
179180
else
180181
{
@@ -196,7 +197,7 @@ else if (atype == (byte) 0x04)
196197
}
197198
}
198199
//
199-
new Thread(new RemoteSocketHandler(attach)).start();
200+
remoteThreadPool.execute(new RemoteSocketHandler(attach));
200201
//
201202
while (isRunning)
202203
{
@@ -206,12 +207,12 @@ else if (atype == (byte) 0x04)
206207
break;
207208
}
208209
int rcnt = attach.localSkt.read(attach.localReadBuf);
209-
if (rcnt < 1)
210+
if (rcnt < 0)
210211
{
211212
break;
212213
}
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.
215216
attach.localReadBuf.get(recv);
216217

217218
if (!attach.isDirect)
@@ -251,9 +252,8 @@ else if (atype == (byte) 0x04)
251252
Log.e("EXC", "CMD OK");
252253
handleData();
253254
}
254-
catch (Exception e)
255+
catch (Exception ignored)
255256
{
256-
Log.e("EXC", "LOCAL EXEC: " + e.getMessage());
257257
}
258258
cleanSession(attach);
259259
}
@@ -268,12 +268,12 @@ private boolean doAuth(final ChannelAttach attach) throws Exception
268268
return false;
269269
}
270270
attach.localReadBuf.flip();
271-
if (attach.localReadBuf.get() != 0x05)//Socks Version
271+
if (attach.localReadBuf.get() != (byte) 0x05)//Socks Version
272272
{
273273
return false;
274274
}
275275

276-
int methodCnt = attach.localReadBuf.get();
276+
int methodCnt = attach.localReadBuf.get() & 0xFF;
277277
int mCnt = attach.localReadBuf.limit() - attach.localReadBuf.position();
278278
if (mCnt < methodCnt || mCnt > methodCnt)
279279
{
@@ -305,7 +305,6 @@ private boolean processCMD(final ChannelAttach attach) throws Exception
305305
}
306306

307307
attach.localReadBuf.flip();
308-
//Utils.bufHexDmp("CMD", attach.localReadBuf.duplicate());
309308
if (attach.localReadBuf.get() != 0x05)//Socks Version
310309
{
311310
return false;
@@ -321,8 +320,7 @@ private boolean processCMD(final ChannelAttach attach) throws Exception
321320
{
322321
case 0x01:
323322
//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}));
326324
attach.localReadBuf.clear();
327325
return true;
328326
case 0x03:
@@ -345,8 +343,8 @@ private boolean processCMD(final ChannelAttach attach) throws Exception
345343
respb[respb.length - 2] = (byte) ((locPort >> 8) & 0xFF);
346344
attach.localSkt.write(ByteBuffer.wrap(respb));
347345
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}));
350348
default:
351349
return false;
352350
}
@@ -371,9 +369,7 @@ private boolean prepareRemote(ChannelAttach attach, String remoteIP, int remoteP
371369
private boolean checkSessionAlive(ChannelAttach attach)
372370
{
373371
return attach.localSkt != null &&
374-
attach.remoteSkt != null &&
375-
attach.localSkt.socket().isConnected() &&
376-
attach.remoteSkt.socket().isConnected();
372+
attach.remoteSkt != null;
377373
}
378374

379375
private void cleanSession(ChannelAttach attach)
@@ -388,6 +384,9 @@ private void cleanSession(ChannelAttach attach)
388384
}
389385
attach.remoteSkt = null;
390386
attach.localSkt = null;
387+
attach.obfs = null;
388+
attach.proto = null;
389+
attach.crypto = null;
391390
attach.localReadBuf = null;
392391
attach.remoteReadBuf = null;
393392
}
@@ -402,13 +401,13 @@ public void stopSSRLocal()
402401
catch (Exception ignored)
403402
{
404403
}
405-
exec.shutdown();
404+
localThreadPool.shutdown();
406405
ssc = null;
407406
}
408407

409408
class RemoteSocketHandler implements Runnable
410409
{
411-
private ChannelAttach attach;
410+
private final ChannelAttach attach;
412411

413412
public RemoteSocketHandler(ChannelAttach attach)
414413
{
@@ -427,11 +426,11 @@ public RemoteSocketHandler(ChannelAttach attach)
427426
break;
428427
}
429428
int rcnt = attach.remoteSkt.read(attach.remoteReadBuf);
430-
if (rcnt < 1)
429+
if (rcnt < 0)
431430
{
432431
break;
433432
}
434-
Log.e("EXC", "READ RMT CNT: " + rcnt);
433+
435434
attach.remoteReadBuf.flip();
436435
byte[] recv = new byte[rcnt];
437436
attach.remoteReadBuf.get(recv);
@@ -446,9 +445,8 @@ public RemoteSocketHandler(ChannelAttach attach)
446445
attach.remoteReadBuf.clear();
447446
}
448447
}
449-
catch (Exception e)
448+
catch (Exception ignored)
450449
{
451-
Log.e("EXC", "REMOTE EXEC L: " + e.getMessage());
452450
}
453451
cleanSession(attach);
454452
}

0 commit comments

Comments
 (0)