-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyDaemon.java
247 lines (177 loc) · 6.41 KB
/
ProxyDaemon.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
package networkProject;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class ProxyDaemon {
final static ArrayList<String> forbiddenAddresses = new ArrayList<>();
public static void main(String args[]) throws Exception {
ServerSocket welcomeSocket = new ServerSocket(8080);
while (true) {
Socket connectionSocket = welcomeSocket.accept();
new ServerHandler(connectionSocket,null);
}
}
}
class ServerHandler implements Runnable {
Socket clientSocket;
DataInputStream inFromClient;
DataOutputStream outToClient;
String host;
String path;
PrinterClass pC;
public ServerHandler(Socket s, String request) {
try {
clientSocket = s;
pC = new PrinterClass();
pC.add("A connection from a client is initiated...");
inFromClient = new DataInputStream(s.getInputStream());
outToClient = new DataOutputStream(s.getOutputStream());
String hd = request;
int sp1 = hd.indexOf(' ');
int sp2 = hd.indexOf(' ', sp1 + 1);
int eol = hd.indexOf('\r');
String reqHeaderRemainingLines = hd.substring(eol + 2);
MimeHeader reqMH = new MimeHeader(hd);
String url = hd.substring(sp1 + 1, sp2);
System.out.println(url);
String method = hd.substring(0, sp1);
host = reqMH.get("Host");
reqMH.put("Connection", "close");
URL u = new URL(url);
String tmpPath = u.getPath();
String tmpHost = u.getHost();
path = ((tmpPath == "") ? "/" : tmpPath);
host = tmpHost;
if (host.equals(tmpHost))
{
if (method.equalsIgnoreCase("get")) {
pC.add("Client requests...\r\nHost: " + host + "\r\nPath: " + path);
handleProxy(url, reqMH,"GET");
}
else if(method.equalsIgnoreCase("post"))
{
pC.add("Client requests...\r\nHost: " + host + "\r\nPath: " + path);
handleProxy(url, reqMH,"POST");
}
else if(method.equalsIgnoreCase("head"))
{
pC.add("Client requests...\r\nHost: " + host + "\r\nPath: " + path);
handleProxy(url, reqMH,"HEAD");
}
else
{
pC.add("Requested method " + method + " is not allowed on proxy server");
outToClient.writeBytes(createErrorPage(405, "Method Not Allowed", method));
}
}
else
{
pC.add("Error for request: " + url);
}
pC.removeThread();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
String hd = getHeader(inFromClient);
int sp1 = hd.indexOf(' ');
int sp2 = hd.indexOf(' ', sp1 + 1);
int eol = hd.indexOf('\r');
String reqHeaderRemainingLines = hd.substring(eol + 2);
MimeHeader reqMH = new MimeHeader(reqHeaderRemainingLines);
String url = hd.substring(sp1 + 1, sp2);
System.out.println(url);
String method = hd.substring(0, sp1);
host = reqMH.get("Host");
reqMH.put("Connection", "close");
URL u = new URL(url);
String tmpPath = u.getPath();
String tmpHost = u.getHost();
path = ((tmpPath == "") ? "/" : tmpPath);
if (ProxyDaemon.forbiddenAddresses.contains(tmpHost) == true) {
pC.add("Connection blocked to the host due to the proxy policy");
outToClient.writeBytes(createErrorPage(401, "Not Allowed", method));
} else if (host.equals(tmpHost)) {
if (method.equalsIgnoreCase("get")) {
pC.add("Client requests...\r\nHost: " + host + "\r\nPath: " + path);
handleProxy(url, reqMH,"GET");
} else {
pC.add("Requested method " + method + " is not allowed on proxy server");
outToClient.writeBytes(createErrorPage(405, "Method Not Allowed", method));
}
} else {
pC.add("Error for request: " + url);
}
pC.removeThread();
} catch (Exception e) {
e.printStackTrace();
}
}
private void handleProxy(String url, MimeHeader reqMH,String method) {
try {
pC.add("\r\nInitiating the server connection");
Socket sSocket = new Socket(host, 80);
DataInputStream inFromServer = new DataInputStream(sSocket.getInputStream());
DataOutputStream outToServer = new DataOutputStream(sSocket.getOutputStream());
reqMH.put("User-Agent", reqMH.get("User-Agent") + " via CSE471 Proxy");
pC.add("\r\nSending to server...\r\n" + method+ " " + path + " HTTP/1.1\r\n" + reqMH + "\r\n");
outToServer.writeBytes(method+" " + path + " HTTP/1.1\r\n" + reqMH + "\r\n");
pC.add("HTTP request sent to: " + host);
ByteArrayOutputStream bAOS = new ByteArrayOutputStream(10000);
int a;
byte[] buffer = new byte[1024];
while ((a = inFromServer.read(buffer)) != -1) {
bAOS.write(buffer, 0, a);
}
byte[] response = bAOS.toByteArray();
String rawResponse = new String(response);
String responseHeader = rawResponse.substring(0, rawResponse.indexOf("\r\n\r\n"));
pC.add("\r\nResponse Header\r\n" + responseHeader);
pC.add("\r\n\r\nGot " + response.length + " bytes of response data...\r\n"
+ "Sending it back to the client...\r\n");
outToClient.write(response);
outToClient.close();
sSocket.close();
pC.add("Served http://" + host + path + "\r\nExiting ServerHelper thread...\r\n"
+ "\r\n----------------------------------------------------" + "\r\n");
} catch (Exception e) {
e.printStackTrace();
}
}
private String createErrorPage(int code, String msg, String address) {
String html_page = "";
html_page = code + " " + msg + " " + address + "\n";
MimeHeader mh = makeMimeHeader("text/html", html_page.length());
HttpResponse hr = new HttpResponse(code, msg, mh);
return hr + html_page;
}
private MimeHeader makeMimeHeader(String type, int length) {
MimeHeader mh = new MimeHeader();
Date d = new Date();
TimeZone gmt = TimeZone.getTimeZone("GMT");
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
sdf.setTimeZone(gmt);
String sdf_date = sdf.format(d);
mh.put("Date:", sdf_date);
mh.put("Type:", type);
if (length >= 0)
mh.put("Content-Length", String.valueOf(length));
return mh;
}
public String getHeader(DataInputStream in) throws Exception {
byte[] header = new byte[1024];
int data;
int h = 0;
while ((data = in.read()) != -1) {
header[h++] = (byte) data;
if (header[h - 1] == '\n' && header[h - 2] == '\r' && header[h - 3] == '\n' && header[h - 4] == '\r') {
break;
}
}
return new String(header, 0, h);
}
}