Skip to content

Commit 0150fae

Browse files
authored
Add files via upload
1 parent cbaae17 commit 0150fae

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

proxy3.py

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#****************************************************
2+
# *
3+
# HTTP PROXY *
4+
# Version: 1.0 *
5+
# Author: Luu Gia Thuy *
6+
# *
7+
#****************************************************
8+
9+
import os,sys,socket,time
10+
import _thread as thread
11+
12+
#********* CONSTANT VARIABLES *********
13+
BACKLOG = 50 # how many pending connections queue will hold
14+
MAX_DATA_RECV = 999999 # max number of bytes we receive at once
15+
DEBUG = True # set to True to see the debug msgs
16+
BLOCKED = [] # just an example. Remove with [""] for no blocking at all.
17+
18+
#**************************************
19+
#********* MAIN PROGRAM ***************
20+
#**************************************
21+
def main():
22+
23+
# check the length of command running
24+
if (len(sys.argv)<2):
25+
print ("No port given, using :8080 (http-alt)")
26+
port = 8080
27+
else:
28+
port = int(sys.argv[1]) # port from argument
29+
30+
# host and port info.
31+
host = '' # blank for localhost
32+
33+
print ("Proxy Server Running on ",host,":",port)
34+
35+
try:
36+
# create a socket
37+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
38+
39+
# associate the socket to host and port
40+
s.bind((host, port))
41+
42+
# listenning
43+
s.listen(BACKLOG)
44+
45+
except (socket.error, (value, message)):
46+
if s:
47+
s.close()
48+
print ("Could not open socket:", message)
49+
sys.exit(1)
50+
51+
# get the connection from client
52+
while 1:
53+
conn, client_addr = s.accept()
54+
55+
# create a thread to handle request
56+
thread.start_new_thread(proxy_thread, (conn, client_addr))
57+
58+
s.close()
59+
#************** END MAIN PROGRAM ***************
60+
61+
def printout(type,request,address):
62+
if "Block" in type or "Blacklist" in type:
63+
colornum = 91
64+
elif "Request" in type:
65+
colornum = 92
66+
elif "Reset" in type:
67+
colornum = 93
68+
69+
print ("\033[",colornum,"m",address[0],"\t",type,"\t",request,"\033[0m")
70+
71+
#*******************************************
72+
#********* PROXY_THREAD FUNC ***************
73+
# A thread to handle request from browser
74+
#*******************************************
75+
def proxy_thread(conn, client_addr):
76+
77+
# get the request from browser
78+
request = conn.recv(MAX_DATA_RECV)
79+
80+
# parse the first line
81+
first_line = request.split(b'\n')[0]
82+
83+
# get url
84+
url = first_line.split(b' ')[1]
85+
86+
for i in range(0,len(BLOCKED)):
87+
if BLOCKED[i] in url:
88+
printout("Blacklisted",first_line,client_addr)
89+
conn.close()
90+
sys.exit(1)
91+
92+
93+
printout("Request",first_line,client_addr)
94+
# print "URL:",url
95+
# print
96+
97+
# find the webserver and port
98+
http_pos = url.find(b'://') # find pos of ://
99+
if (http_pos==-1):
100+
temp = url
101+
else:
102+
temp = url[(http_pos+3):] # get the rest of url
103+
104+
port_pos = temp.find(b':') # find the port pos (if any)
105+
106+
# find end of web server
107+
webserver_pos = temp.find(b'/')
108+
if webserver_pos == -1:
109+
webserver_pos = len(temp)
110+
111+
webserver = ""
112+
port = -1
113+
if (port_pos==-1 or webserver_pos < port_pos): # default port
114+
port = 80
115+
webserver = temp[:webserver_pos]
116+
else: # specific port
117+
port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
118+
webserver = temp[:port_pos]
119+
120+
try:
121+
# create a socket to connect to the web server
122+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
123+
s.connect((webserver, port))
124+
s.send(request) # send request to webserver
125+
126+
while 1:
127+
# receive data from web server
128+
data = s.recv(MAX_DATA_RECV)
129+
130+
if (len(data) > 0):
131+
# send to browser
132+
conn.send(data)
133+
else:
134+
break
135+
s.close()
136+
conn.close()
137+
except (socket.error):
138+
if s:
139+
s.close()
140+
if conn:
141+
conn.close()
142+
printout("Peer Reset",first_line,client_addr)
143+
sys.exit(1)
144+
#********** END PROXY_THREAD ***********
145+
146+
if __name__ == '__main__':
147+
main()
148+
149+

0 commit comments

Comments
 (0)