Skip to content

Commit e67e23c

Browse files
committed
performance improvement:
pure byte array operation without extra String. rebalance job assignments between the inner and outer .only request outer when necessary make up the files that forget to add to git in the last commit(config relative)
1 parent c3f9c4f commit e67e23c

8 files changed

Lines changed: 304 additions & 114 deletions

File tree

src/META-INF/MANIFEST.MF

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: com.fkgfw.proxy.Main
3+

src/com/fkgfw/proxy/BaseServerTask.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,28 @@
11
package com.fkgfw.proxy;
22

3-
import com.fkgfw.proxy.Config.ConfigPojo;
4-
53
import java.io.IOException;
64
import java.io.InputStream;
75
import java.io.OutputStream;
8-
import java.net.Socket;
9-
10-
import static com.fkgfw.proxy.Utils.byteArray2HexString;
116

127

138

149
public abstract class BaseServerTask implements Runnable {
15-
public static String SHAKE_RECEIVE = "050100";
16-
public static String SHAKE_SEND = "0500";
17-
18-
public static String REQUEST_HEADER = "050100";
10+
public static byte[] SHAKE_RECEIVE = {0x05, 0x01, 0x00};
11+
public static byte[] SHAKE_SEND = {0x05, 0x00};
12+
public static byte[] REQUEST_HEADER = {0x05, 0x01, 0x00};
1913
public static int REQUEST_HEADER_BYTE_OFFSET = 5;
20-
public static int REQUEST_HEADER_HEX_STR_OFFSET = REQUEST_HEADER_BYTE_OFFSET * 2;
21-
22-
public static String RESPONSE_SUCCESS_HEADER = "050000";
23-
public static String RESPONSE_FAILED_HEADER = "0501";
24-
25-
public static String ADDR_TYPE_IPV4 = "01";
26-
public static String ADDR_TYPE_HOSTNAME = "03";
27-
public static String ADDR_TYPE_IPV6 = "05";
14+
public static byte[] RESPONSE_SUCCESS_HEADER = {0x05, 0x00, 0x00};
15+
public static byte[] RESPONSE_FAILED_HEADER = {0x05, 0x01};
16+
public static byte[] ADDR_TYPE_IPV4 = {0x01};
17+
public static byte[] ADDR_TYPE_HOSTNAME = {0x03};
18+
public static byte[] ADDR_TYPE_IPV6 = {0x05};
2819

2920

3021
public enum ADDR_TYPE {
3122
IPV4, HOSTNAME, IPV6
3223
}
3324

34-
public void forwarding(InputStream in, OutputStream out,byte[] buffer) {
25+
public void forwarding(InputStream in, OutputStream out, byte[] buffer) {
3526
int receiveCount;
3627
try {
3728
while ((receiveCount = in.read(buffer)) != -1) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fkgfw.proxy.Config;
2+
3+
4+
import com.google.gson.Gson;
5+
import com.google.gson.GsonBuilder;
6+
7+
import java.io.*;
8+
9+
public class ConfigManager {
10+
11+
public static String CONFIG_NAME = "config.json";
12+
13+
14+
public ConfigManager() {
15+
}
16+
17+
public ConfigPojo getConfig() {
18+
19+
ConfigPojo config = null;
20+
File file = new File(CONFIG_NAME);
21+
Gson gson=new GsonBuilder().setPrettyPrinting().create();
22+
if (file.exists()) {
23+
24+
try {
25+
BufferedReader br = new BufferedReader(new FileReader(CONFIG_NAME));
26+
config = gson.fromJson(br, ConfigPojo.class);
27+
br.close();
28+
} catch (FileNotFoundException e) {
29+
e.printStackTrace();
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
}
33+
34+
35+
} else {
36+
//CREATE default config file
37+
config = new ConfigPojo();
38+
try {
39+
BufferedWriter bw = new BufferedWriter(new FileWriter(CONFIG_NAME));
40+
bw.write(gson.toJson(config));
41+
bw.close();
42+
43+
} catch (FileNotFoundException e) {
44+
e.printStackTrace();
45+
} catch (UnsupportedEncodingException e) {
46+
e.printStackTrace();
47+
} catch (IOException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
return config;
52+
}
53+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.fkgfw.proxy.Config;
2+
3+
/**
4+
* Created by Salmon on 2015/8/22.
5+
*/
6+
public class ConfigPojo {
7+
8+
private boolean DEBUG = true;
9+
private RUNModeEnum RUN_MODE = RUNModeEnum.inner;
10+
private String OuterServerIP = "127.0.0.1";
11+
private int OuterServerPort = 2333;
12+
private int InnerServerPort = 1080;
13+
14+
private String ENCRYPT_SEED = "admin";
15+
16+
private int BufferSize = 11024;
17+
18+
public ThreadPoolModeEnum ThreadPoolMode = ThreadPoolModeEnum.Cached;
19+
20+
private int MaxFixedThreadPoolSize = 1024;
21+
22+
23+
24+
public RUNModeEnum getRUN_MODE() {
25+
return RUN_MODE;
26+
}
27+
28+
public void setRUN_MODE(RUNModeEnum RUN_MODE) {
29+
this.RUN_MODE = RUN_MODE;
30+
}
31+
32+
public String getOuterServerIP() {
33+
return OuterServerIP;
34+
}
35+
36+
public void setOuterServerIP(String outerServerIP) {
37+
OuterServerIP = outerServerIP;
38+
}
39+
40+
public int getOuterServerPort() {
41+
return OuterServerPort;
42+
}
43+
44+
public void setOuterServerPort(int outerServerPort) {
45+
OuterServerPort = outerServerPort;
46+
}
47+
48+
public int getInnerServerPort() {
49+
return InnerServerPort;
50+
}
51+
52+
public void setInnerServerPort(int innerServerPort) {
53+
InnerServerPort = innerServerPort;
54+
}
55+
56+
public String getENCRYPT_SEED() {
57+
return ENCRYPT_SEED;
58+
}
59+
60+
public void setENCRYPT_SEED(String ENCRYPT_SEED) {
61+
this.ENCRYPT_SEED = ENCRYPT_SEED;
62+
}
63+
64+
public int getBufferSize() {
65+
return BufferSize;
66+
}
67+
68+
public void setBufferSize(int bufferSize) {
69+
BufferSize = bufferSize;
70+
}
71+
72+
public ThreadPoolModeEnum getThreadPoolMode() {
73+
return ThreadPoolMode;
74+
}
75+
76+
public void setThreadPoolMode(ThreadPoolModeEnum threadPoolMode) {
77+
this.ThreadPoolMode = threadPoolMode;
78+
}
79+
80+
public int getMaxFixedThreadPoolSize() {
81+
return MaxFixedThreadPoolSize;
82+
}
83+
84+
public void setMaxFixedThreadPoolSize(int maxFixedThreadPoolSize) {
85+
MaxFixedThreadPoolSize = maxFixedThreadPoolSize;
86+
}
87+
88+
public boolean isDEBUG() {
89+
return DEBUG;
90+
}
91+
92+
public void setDEBUG(boolean DEBUG) {
93+
this.DEBUG = DEBUG;
94+
}
95+
96+
97+
public enum RUNModeEnum {
98+
inner, outer
99+
}
100+
101+
102+
public enum ThreadPoolModeEnum {
103+
Cached, Fixed
104+
}
105+
106+
107+
//getter and setter methods
108+
109+
@Override
110+
public String toString() {
111+
return "ConfigPojo{" +
112+
"RUN_MODE=" + RUN_MODE +
113+
", OuterServerIP='" + OuterServerIP + '\'' +
114+
", OuterServerPort=" + OuterServerPort +
115+
", InnerServerPort=" + InnerServerPort +
116+
", ENCRYPT_SEED='" + ENCRYPT_SEED + '\'' +
117+
", BufferSize=" + BufferSize +
118+
", ThreadPoolMode=" + ThreadPoolMode +
119+
", MaxFixedThreadPoolSize=" + MaxFixedThreadPoolSize +
120+
", DEBUG=" + DEBUG +
121+
'}';
122+
}
123+
}

src/com/fkgfw/proxy/InnerServer/InnerServerMain.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fkgfw.proxy.InnerServer;
22

3-
import com.fkgfw.proxy.Config.ConfigManager;
43
import com.fkgfw.proxy.Config.ConfigPojo;
54

65
import java.io.IOException;

src/com/fkgfw/proxy/InnerServer/InnerWorkingTask.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.io.OutputStream;
1212
import java.net.Socket;
1313

14+
import static com.fkgfw.proxy.Utils.isByteArrayEqual;
15+
1416

1517
public class InnerWorkingTask extends BaseServerTask {
1618

@@ -36,17 +38,31 @@ public void run() {
3638
e.printStackTrace();
3739
}
3840
finally {
39-
System.out.println("close connection");
41+
System.out.println("inner close connection");
4042
}
4143

4244
}
4345

4446
private void startImpl(Socket socket) throws IOException, InterruptedException {
4547
final byte[] buffer = new byte[configObj.getBufferSize()];
48+
final InputStream broswerIn = socket.getInputStream();
49+
final OutputStream broswerOut = socket.getOutputStream();
50+
51+
int bufferReadCount;
52+
53+
bufferReadCount = broswerIn.read(buffer);
54+
if (bufferReadCount <= 0) {
55+
return;
56+
}
57+
58+
if (!isByteArrayEqual(SHAKE_RECEIVE, buffer, 0)) {
59+
return;
60+
}
4661

62+
broswerOut.write(SHAKE_SEND);
63+
broswerOut.flush();
64+
// System.out.println("sending shake£º" + SHAKE_SEND);
4765

48-
final InputStream localin = socket.getInputStream();
49-
final OutputStream localOut = socket.getOutputStream();
5066

5167
Socket OuterSocket = new Socket(configObj.getOuterServerIP(), configObj.getOuterServerPort());
5268
System.out.println("connected to Outer server");
@@ -60,7 +76,7 @@ private void startImpl(Socket socket) throws IOException, InterruptedException {
6076
Thread tSendOuterServer = new Thread(new Runnable() {
6177
@Override
6278
public void run() {
63-
forwarding(localin, OuterOUT, buffer);
79+
forwarding(broswerIn, OuterOUT, buffer);
6480
}
6581
});
6682

@@ -69,14 +85,14 @@ public void run() {
6985
@Override
7086
public void run() {
7187
final byte[] bufferAnother = new byte[configObj.getBufferSize()];
72-
forwarding(OuterIN, localOut, bufferAnother);
88+
forwarding(OuterIN, broswerOut, bufferAnother);
7389

7490
}
7591
});
7692

7793
tSendOuterServer.start();
7894
tGetFromOuterServer.start();
79-
System.out.println("transmission started");
95+
// System.out.println("inner transmission started");
8096

8197
tSendOuterServer.join();
8298
tGetFromOuterServer.join();

0 commit comments

Comments
 (0)