-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-server.py
64 lines (64 loc) · 2.07 KB
/
proxy-server.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
import socket, sys
from threading import Thread
try:
listening_port=int(sys.argv[2])
except Exception,e:
print "Usage : celare --port 8000"
max_conn = 5
buffer_size = 8192
def start():
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print "Sockets Initialized ... [OK]"
s.bind(('',listening_port))
print "Sockets Binded ... [OK]"
s.listen(max_conn)
print "Celare Started on port {0} ... [OK]".format(listening_port)
while True:
try:
conn,addr=s.accept()
data=conn.recv(buffer_size)
t=Thread(target=conn_string,args=(conn,data,addr,))
t.start()
except Exception,e:
fp=open("/var/log/celare.log","wb")
fp.write(str(e))
fp.close()
print "Error Occurred ... [LOGGED]"
s.close()
except Exception, e:
print "Celare Started ... [FAILED]"
print "Error : {0}".format(str(e))
sys.exit(2)
def conn_string(conn,data,addr):
try:
request_server=data.split("Host")[1].split("\n")[0].split(" ")[1]
request_port=80
if request_server.find(":")!=-1:
request_port=int(request_server.split(":")[1])
request_server=request_server.split(":")[0]
proxy_server(request_server,request_port,conn,data,addr)
except Exception,e:
fp=open("/var/log/celare.log","wb")
fp.write(str(e))
fp.close()
print "Error Occurred ... [LOGGED]"
def proxy_server(server,port,conn,data,addr):
try:
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((server,port))
s.send(data)
while 1:
reply=s.recv(buffer_size)
if len(reply)>0:
conn.send(reply)
print "Processed {0} > {1} ... [DONE]".format(str(addr[0]),len(reply))
else:
print "Server Sent Nothing back"
break
s.close()
conn.close()
except Exception, e:
print e
start()
#Fri-26Jan