-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproxyserver.py
315 lines (273 loc) · 11.7 KB
/
proxyserver.py
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
from socket import *
import sys
import re
import os
def createRequest(host, url, originalRequest):
try:
hostInd = url.index(host)
if len(url[hostInd + len(host):]) < 3:
url = 'http://www.' + url
pass
else:
url = url[hostInd + len(host):]
except ValueError:
pass
httpHeader = "GET " + url + " HTTP/1.1\r\n"
httpHeader += ("Host: " + host + '\r\n')
httpHeader += "Connection: close\r\n"
for line in originalRequest.split('\r\n'):
if 'User-Agent' in line:
httpHeader += (line + '\r\n')
if 'Accept:' in line:
httpHeader += (line + '\r\n')
if 'Referer:' in line:
httpHeader += (line + '\r\n')
if 'Accept-Encoding:' in line:
httpHeader += (line + '\r\n')
if 'Accept-Language:' in line:
httpHeader += (line + '\r\n')
if 'Cookie:' in line:
httpHeader += (line + '\r\n')
httpHeader += '\r\n\r\n'
return httpHeader
def createResponse(filename, data):
httpHeader = 'HTTP/1.1 200 OK\r\n'
if '.jpg' in filename:
httpHeader += 'Content-Type: image/jpeg\r\n'
elif 'html' or 'htm' in filename:
httpHeader += 'Content-Type: text/html\r\n'
elif '.ico' in filename:
httpHeader += 'Content-Type: image/x-icon\r\n'
elif '.css' in filename:
httpHeader += 'Content-Type: text/css\r\n'
elif '.js' in filename:
httpHeader += 'Content-Type: application/javascript\r\n'
elif '.txt' in filename:
httpHeader += 'Content-Type: text/plain\r\n'
else:
httpHeader += 'Content-Type: application/octet-stream\r\n'
httpHeader += ('Content-Length: ' + str(len(data)))
# Double space between header and data
httpHeader += '\r\n\r\n'
print 'RESPONSE HEADER FROM PROXY TO CLIENT'
print httpHeader
print 'END OF HEADER\n'
httpHeader += data
return httpHeader
def create404(data):
httpHeader = 'HTTP/1.1 404 Not Found\r\n'
httpHeader += 'Content-Type: text/html; charset=UTF-8\r\n'
print 'RESPONSE HEADER FROM PROXY TO CLIENT'
print httpHeader
print 'END OF HEADER\n'
httpHeader += '\r\n\r\n'
httpHeader += data
return httpHeader
if len(sys.argv) <= 1:
print 'Usage : "python proxyserver.py server_ip"\n[server_ip : Address of the proxy server'
sys.exit(2)
serverIP = sys.argv[1]
# buffer size
BUFFER_SIZE = 1048576
#port number is arbituary
welcomePort = 5005
# holds names of files sent with special encoding i.e gzip
encodeFlag = []
encodeDict = {}
try:
#create welcoming socket
welcomeSocket = socket(AF_INET, SOCK_STREAM)
#bind to the server address
welcomeSocket.bind((serverIP, welcomePort))
#begin listening (argument is number of allowed queued connections)
welcomeSocket.listen(5)
#listening loop
while True:
# Start receiving data from client
print('WEB PROXY SERVER IS LISTENING')
# Accept() returns a new connection socket along with the address
clientSocket, clientAddress = welcomeSocket.accept()
print('WEB PROXY SERVER CONNECTED WITH ' + str(clientAddress))
# wait 100ms second to hear from client
try:
clientSocket.settimeout(0.1)
request = clientSocket.recv(BUFFER_SIZE)
except timeout:
clientSocket.shutdown(SHUT_RDWR)
clientSocket.close()
continue
# do not parse empty message
if(len(request) > 0):
print('MESSAGE RECEIVED FROM CLIENT:')
print(request)
print 'END OF MESSAGE RECEIVED FROM CLIENT'
else:
clientSocket.shutdown(SHUT_RDWR)
clientSocket.close()
continue
# Parse Request Header
method = request.split(" ")[0]
url = request.split(" ")[1]
# url = url[1:]
try:
domainIndex = url.index('.')
host = url[:domainIndex+4]
except ValueError:
pass
for line in request.split('\r\n'):
if "Referer:" in line:
temp = line.split(" ")[1]
temp = temp.split(':'.join([str(serverIP), str(welcomePort)]))[1]
domIndex = temp.find(".")
host = temp[1:domIndex+4]
print '[PARSE MESSAGE HEADER]:'
print 'METHOD = ' + method + ', DESTADDRESS = ' + url + ', HTTPVersion = ' + str(request.split()[2])
writefile = True
# See if the URL contains a filename
filematcher = re.compile("((.?)*\.(jpg|htm|html|png|ico|js|css|gif)$)")
fmatch = filematcher.match(url)
if (fmatch):
filename = url.split('/')[-1]
if filename in os.listdir("."):
# Cache Hit
print 'Cache Hit'
with open(filename, 'r') as file:
# Assemble HTTP response
httpHeader = 'HTTP/1.1 200 OK\r\n'
if '.jpg' in filename:
httpHeader += 'Content-Type: image/jpeg\r\n'
elif 'html' or 'htm' in filename:
httpHeader += 'Content-Type: text/html\r\n'
elif '.ico' in filename:
httpHeader += 'Content-Type: image/x-icon\r\n'
elif '.css' in filename:
httpHeader += 'Content-Type: text/css\r\n'
elif '.js' in filename:
httpHeader += 'Content-Type: application/javascript\r\n'
elif '.txt' in filename:
httpHeader += 'Content-Type: text/plain\r\n'
else:
httpHeader += 'Content-Type: application/octet-stream\r\n'
# Reading the whole file until it doesn't work
data = file.read()
httpHeader += ('Content-Length: ' + str(len(data)))
# See if it was sent using special encoding
if filename in encodeFlag:
httpHeader += (encodeDict[filename] + '\r\n')
# Double space between header and data
httpHeader += '\r\n\r\n'
print 'HTTP Header sent to Client:'
print httpHeader
# Add the file data
httpHeader += data
# Send
clientSocket.send(httpHeader)
clientSocket.close()
continue
else:
writefile = False
# if url[0] == '/':
# url = url[1:]
#url = "/"
# if here then the request was not for a specific file (metadata?)
# Cache miss must send request to original destination
try:
print '[LOOK UP IN CACHE]: NOT FOUND, BUILD REQUEST TO SEND TO ORIGINAL SERVER'
forwardSocket = socket(AF_INET, SOCK_STREAM)
# Connect on port 80
if host[0] == '/':
host = host[1:]
host = host
print '[PARSE REQUEST HEADER] HOSTNAME IS ' + host
hostIP = gethostbyname(host)
address = (hostIP, 80)
forwardSocket.connect(address)
newRequest = createRequest(host, url, request)
print 'REQUEST MESSAGE SENT TO ORIGINAL SERVER:'
print newRequest
print 'END OF MESSAGE SENT TO ORIGINAL SERVER.\n'
forwardSocket.send(newRequest)
# keep reading and forwarding until nothing server stops
# count = 0
error404 = False
if(writefile):
with open(filename, 'w') as file:
while True:
forwardSocket.settimeout(0.5) # 500ms
response = forwardSocket.recv(BUFFER_SIZE)
if(len(response) > 0):
try:
if '404' in response.split('\r\n\r\n')[0]:
temp = response.split('\r\n\r\n')
header = temp[0]
print 'RESPONSE HEADER FROM ORIGINAL SERVER'
print header
print 'END OF HEADER'
if len(temp) < 3:
data = temp[1]
else:
data = '\r\n\r\n'.join(temp[1:])
proxy_response = create404(data)
clientSocket.send(response)
error404 = True
break
if 'HTTP' in response.split('\r\n\r\n')[0]:
temp = response.split('\r\n\r\n')
header = temp[0]
print 'RESPONSE HEADER FROM ORIGINAL SERVER'
print header
print 'END OF HEADER'
print '[WRITE FILE INTO CACHE]: ' + filename +'\n'
if len(temp) < 3:
data = temp[1]
else:
data = '\r\n\r\n'.join(temp[1:])
file.write(data)
else:
file.write(response)
# See if there is special encoding to note
for line in response.split('\r\n'):
if 'Content-Encoding:' in line:
encodeFlag.append(filename)
encodeDict[filename] = line
except IndexError:
file.write(response)
clientSocket.send(response)
else:
break
else:
while True:
forwardSocket.settimeout(1.0)
response = forwardSocket.recv(BUFFER_SIZE)
if(len(response) > 0):
clientSocket.send(response)
else:
break
if error404:
with open(filename, 'r') as file:
data = file.read()
proxy_response = create404(data)
clientSocket.send(proxy_response)
else:
with open(filename, 'r') as file:
data = file.read()
proxy_response = createResponse(filename, data)
clientSocket.send(proxy_response)
forwardSocket.close()
clientSocket.close()
except timeout:
print 'timeout'
forwardSocket.close()
clientSocket.close()
except Exception as e:
print(e)
print("Illegal Request")
# else:
# print('404 Error - File note found')
except KeyboardInterrupt:
print 'Exiting Gracefully'
welcomeSocket.shutdown(SHUT_RDWR)
welcomeSocket.close()
sys.exit()
if __name__ == '__main__':
main()