Skip to content

Commit 8233cf1

Browse files
committed
Merge pull request #1 from dfmogk/master
Interface Improvement, Blocking Feature
2 parents 21d302b + 84ec6a9 commit 8233cf1

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

proxy.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
#********* CONSTANT VARIABLES *********
1212
BACKLOG = 50 # how many pending connections queue will hold
13-
MAX_DATA_RECV = 4096 # max number of bytes we receive at once
14-
DEBUG = False # set to True to see the debug msgs
13+
MAX_DATA_RECV = 999999 # max number of bytes we receive at once
14+
DEBUG = True # set to True to see the debug msgs
15+
BLOCKED = [] # just an example. Remove with [""] for no blocking at all.
1516

1617
#**************************************
1718
#********* MAIN PROGRAM ***************
@@ -27,6 +28,8 @@ def main():
2728
host = '' # blank for localhost
2829
port = int(sys.argv[1]) # port from argument
2930

31+
print "Proxy Server Running on ",host,":",sys.argv[1]
32+
3033
try:
3134
# create a socket
3235
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -69,11 +72,14 @@ def proxy_thread(conn, client_addr):
6972
# get url
7073
url = first_line.split(' ')[1]
7174

72-
if (DEBUG):
73-
print first_line
74-
print
75-
print "URL:",url
76-
print
75+
for i in range(0,len(BLOCKED)):
76+
if BLOCKED[i] in url:
77+
print "\033[91mBlocked: ",first_line,"\033[0m"
78+
sys.exit(1)
79+
80+
print "\033[92mRequest: ",first_line,"\033[0m"
81+
# print "URL:",url
82+
# print
7783

7884
# find the webserver and port
7985
http_pos = url.find("://") # find pos of ://
@@ -98,14 +104,12 @@ def proxy_thread(conn, client_addr):
98104
port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
99105
webserver = temp[:port_pos]
100106

101-
print "Connect to:", webserver, port
102-
103107
try:
104108
# create a socket to connect to the web server
105109
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
106110
s.connect((webserver, port))
107111
s.send(request) # send request to webserver
108-
112+
109113
while 1:
110114
# receive data from web server
111115
data = s.recv(MAX_DATA_RECV)
@@ -122,7 +126,7 @@ def proxy_thread(conn, client_addr):
122126
s.close()
123127
if conn:
124128
conn.close()
125-
print "Runtime Error:", message
129+
print "\033[93mPeer Reset:",first_line,"\033[0m"
126130
sys.exit(1)
127131
#********** END PROXY_THREAD ***********
128132

0 commit comments

Comments
 (0)