-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestProxyServer.java
317 lines (296 loc) · 12.9 KB
/
TestProxyServer.java
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package networkProject;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestProxyServer extends Thread {
static ArrayList<String> avoidedList = new ArrayList<String>();
static HashMap<String, String> report = new HashMap<String, String>();
public static void main(String[] args) {
(new TestProxyServer()).run();
}
public TestProxyServer() {
super("Server Thread");
}
@Override
public void run() {
try (ServerSocket serverSocket = new ServerSocket(8080)) {
Socket socket;
try {
while ((socket = serverSocket.accept()) != null) {
(new Handler(socket)).start();
}
} catch (IOException e) {
e.printStackTrace(); // TODO: implement catch
}
} catch (IOException e) {
e.printStackTrace(); // TODO: implement catch
return;
}
}
public static class Handler extends Thread {
boolean readFlag = false;
public static final Pattern CONNECT_PATTERN = Pattern.compile("CONNECT (.+):(.+) HTTP/(1\\.[01])",
Pattern.CASE_INSENSITIVE);
private final Socket clientSocket;
private boolean previousWasR = false;
public Handler(Socket clientSocket) {
this.clientSocket = clientSocket;
}
@Override
public void run() {
try {
String request = readLine(clientSocket);
Matcher matcher = CONNECT_PATTERN.matcher(request);
if (matcher.matches()) {
System.out.println(request);
boolean avoidFlag = false;
for (var str : avoidedList) {
if(matcher.group(1).contains(str))
{
avoidFlag = true;
//System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
break;
}
// fruit is an element of the `fruits` array.
}
if(avoidFlag)
{
System.out.println("401 Unauthorized");
}
else
{
//System.out.println("Tried to connect: "+ matcher.group(1));
String header;
do {
if(!report.containsKey(clientSocket.getLocalAddress().toString()))
{
//System.out.println(clientSocket.getLocalAddress().toString());
report.put(clientSocket.getLocalAddress().toString(), "Start of report for" + clientSocket.getLocalAddress().toString()+"\n");
}
LocalTime myObj = LocalTime.now();
String tmpStr = report.get(clientSocket.getLocalAddress().toString());
report.put(clientSocket.getLocalAddress().toString(),tmpStr +"CONNECT "+ matcher.group(1) + " " + myObj.getHour()+":"+myObj.getMinute()+":"+myObj.getSecond()+"\n");
//String tmpReport = report.get(clientSocket.getLocalAddress().toString());
//tmpReport = tmpReport + "\n";
//report.replace(clientSocket.getLocalAddress().toString(), tmpReport);
header = readLine(clientSocket);
//System.out.println(clientSocket);
} while (!"".equals(header));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(clientSocket.getOutputStream(),
"ISO-8859-1");
final Socket forwardSocket;
try {
forwardSocket = new Socket(matcher.group(1), Integer.parseInt(matcher.group(2)));
System.out.println(forwardSocket);
} catch (IOException | NumberFormatException e) {
e.printStackTrace(); // TODO: implement catch
outputStreamWriter.write("HTTP/" + matcher.group(3) + " 502 Bad Gateway\r\n");
outputStreamWriter.write("Proxy-agent: Simple/0.1\r\n");
outputStreamWriter.write("\r\n");
outputStreamWriter.flush();
return;
}
try {
outputStreamWriter.write("HTTP/" + matcher.group(3) + " 200 Connection established\r\n");
outputStreamWriter.write("Proxy-agent: Simple/0.1\r\n");
outputStreamWriter.write("\r\n");
outputStreamWriter.flush();
Thread remoteToClient = new Thread() {
@Override
public void run() {
forwardData(forwardSocket, clientSocket);
}
};
remoteToClient.start();
try {
if (previousWasR)
{
int read = clientSocket.getInputStream().read();
if (read != -1)
{
if (read != '\n')
{
forwardSocket.getOutputStream().write(read);
}
forwardData(clientSocket, forwardSocket);
}
else
{
if (!forwardSocket.isOutputShutdown())
{
forwardSocket.shutdownOutput();
}
if (!clientSocket.isInputShutdown())
{
clientSocket.shutdownInput();
}
}
}
else
{
forwardData(clientSocket, forwardSocket);
}
}
finally
{
try
{
remoteToClient.join();
} catch (InterruptedException e)
{
e.printStackTrace(); // TODO: implement catch
}
}
} finally
{
forwardSocket.close();
}
}
}
else
{
//request = request +"\n" +readLine(clientSocket) +"\n" +readLine(clientSocket)+"\n" + readLine(clientSocket)+"\n" + readLine(clientSocket)+"\n" +readLine(clientSocket)+"\n" +readLine(clientSocket)
//+"\n"+readLine(clientSocket)+"\n"+readLine(clientSocket)+"\n"+readLine(clientSocket)+"\r\n"+"\r\n";
boolean avoidFlag = false;
byte[] header = new byte[1024];
char data;
int h = 0;
String line ="";
while ((data = (char)clientSocket.getInputStream().read()) != -1) {
line = line + data;
h++;
if(line.charAt(line.length()-1) == '\n' && h>4)
{
if(line.charAt(line.length()-2) == '\r')
{
if(line.charAt(line.length()-3) == '\n')
{
if(line.charAt(line.length()-4) == '\r')
{
break;
}
}
}
}
}
request = request+line;
String lines[] = request.split("\n");
for (var str : avoidedList) {
if(lines[0].contains(str))
{
avoidFlag = true;
break;
}
// fruit is an element of the `fruits` array.
}
if(avoidFlag)
{
System.out.println("401 Unauthorized");
}
else
{
if(!report.containsKey(clientSocket.getLocalAddress().toString()))
{
//System.out.println(clientSocket.getLocalAddress().toString());
report.put(clientSocket.getLocalAddress().toString(), "Start of report for" + clientSocket.getLocalAddress().toString()+"\n");
}
LocalTime myObj = LocalTime.now();
String tmpStr = report.get(clientSocket.getLocalAddress().toString());
report.put(clientSocket.getLocalAddress().toString(),tmpStr + lines[0] + " " + myObj.getHour()+":"+myObj.getMinute()+":"+myObj.getSecond()+"\n");
new ServerHandler(clientSocket,request);
}
}
} catch (IOException e)
{
e.printStackTrace(); // TODO: implement catch
}
finally
{
try
{
clientSocket.close();
} catch (IOException e)
{
e.printStackTrace(); // TODO: implement catch
}
}
}
private static String forwardData(Socket inputSocket, Socket outputSocket) {
try
{
InputStream inputStream = inputSocket.getInputStream();
try
{
OutputStream outputStream = outputSocket.getOutputStream();
try
{
byte[] buffer = new byte[4096];
int read;
do
{
read = inputStream.read(buffer);
if (read > 0)
{
outputStream.write(buffer, 0, read);
if (inputStream.available() < 1)
{
outputStream.flush();
}
}
} while (read >= 0);
}
finally
{
if (!outputSocket.isOutputShutdown())
{
outputSocket.shutdownOutput();
}
}
}
finally
{
if (!inputSocket.isInputShutdown())
{
inputSocket.shutdownInput();
}
}
}
catch (IOException e)
{
e.printStackTrace(); // TODO: implement catch
}
String tmpReport = null;
return tmpReport;
}
private String readLine(Socket socket) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int next;
readerLoop:
while ((next = socket.getInputStream().read()) != -1)
{
if (previousWasR && next == '\n')
{
previousWasR = false;
continue;
}
previousWasR = false;
switch (next) {
case '\r':
previousWasR = true;
break readerLoop;
case '\n':
break readerLoop;
default:
byteArrayOutputStream.write(next);
break;
}
}
return byteArrayOutputStream.toString("ISO-8859-1");
}
}
}