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
|
package com.android.proxyhandler;
import android.net.ProxyProperties;
import android.util.Log;
import com.google.android.collect.Lists;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @hide
*/
public class ProxyServer extends Thread {
private static final String CONNECT = "CONNECT";
private static final String HTTP_OK = "HTTP/1.1 200 OK\n";
private static final String TAG = "ProxyServer";
private ExecutorService threadExecutor;
public boolean mIsRunning = false;
private ServerSocket serverSocket;
private ProxyProperties mProxy;
private class ProxyConnection implements Runnable {
private Socket connection;
private ProxyConnection(Socket connection) {
this.connection = connection;
}
@Override
public void run() {
try {
android.net.Proxy.setHttpProxySystemProperty(mProxy);
String requestLine = getLine(connection.getInputStream());
if (requestLine == null) {
connection.close();
return;
}
String[] splitLine = requestLine.split(" ");
if (splitLine.length < 3) {
connection.close();
return;
}
String requestType = splitLine[0];
String urlString = splitLine[1];
String host = "";
int port = 80;
if (requestType.equals(CONNECT)) {
String[] hostPortSplit = urlString.split(":");
host = hostPortSplit[0];
try {
port = Integer.parseInt(hostPortSplit[1]);
} catch (NumberFormatException nfe) {
port = 443;
}
urlString = "Https://" + host + ":" + port;
} else {
try {
URI url = new URI(urlString);
host = url.getHost();
port = url.getPort();
if (port < 0) {
port = 80;
}
} catch (URISyntaxException e) {
connection.close();
return;
}
}
List<Proxy> list = Lists.newArrayList();
try {
list = ProxySelector.getDefault().select(new URI(urlString));
} catch (URISyntaxException e) {
e.printStackTrace();
}
Socket server = null;
for (Proxy proxy : list) {
try {
if (!proxy.equals(Proxy.NO_PROXY)) {
// Only Inets created by PacProxySelector.
InetSocketAddress inetSocketAddress =
(InetSocketAddress)list.get(0).address();
server = new Socket(inetSocketAddress.getAddress(),
inetSocketAddress.getPort());
sendLine(server, requestLine);
} else {
server = new Socket(host, port);
if (requestType.equals(CONNECT)) {
while (getLine(connection.getInputStream()).length() != 0);
// No proxy to respond so we must.
sendLine(connection, HTTP_OK);
} else {
sendLine(server, requestLine);
}
}
} catch (IOException ioe) {
}
if (server != null) {
break;
}
}
if (server == null) {
server = new Socket(host, port);
if (requestType.equals(CONNECT)) {
while (getLine(connection.getInputStream()).length() != 0);
// No proxy to respond so we must.
sendLine(connection, HTTP_OK);
} else {
sendLine(server, requestLine);
}
}
// Pass data back and forth until complete.
SocketConnect.connect(connection, server);
} catch (IOException e) {
Log.d(TAG, "Problem Proxying", e);
}
try {
connection.close();
} catch (IOException ioe) {
}
}
private String getLine(InputStream inputStream) throws IOException {
StringBuffer buffer = new StringBuffer();
int byteBuffer = inputStream.read();
if (byteBuffer < 0) return "";
do {
if (byteBuffer != '\r') {
buffer.append((char)byteBuffer);
}
byteBuffer = inputStream.read();
} while ((byteBuffer != '\n') && (byteBuffer >= 0));
return buffer.toString();
}
private void sendLine(Socket socket, String line) throws IOException {
OutputStream os = socket.getOutputStream();
os.write(line.getBytes());
os.write('\r');
os.write('\n');
os.flush();
}
}
public ProxyServer() {
threadExecutor = Executors.newCachedThreadPool();
}
@Override
public void run() {
try {
serverSocket = new ServerSocket(ProxyService.PORT);
serverSocket.setReuseAddress(true);
while (mIsRunning) {
try {
ProxyConnection parser = new ProxyConnection(serverSocket.accept());
threadExecutor.execute(parser);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mIsRunning = false;
}
public synchronized void startServer() {
mIsRunning = true;
start();
}
public synchronized void stopServer() {
mIsRunning = false;
if (serverSocket != null) {
try {
serverSocket.close();
serverSocket = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void setProxy(ProxyProperties proxy) {
mProxy = proxy;
}
}
|