stoker

Python Proxy Script

Jan 12th, 2014
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Author: Muhammed Adeel aka Innoxent Stoker
  3. # usage: python script.py 1234 (1234 is port number.)
  4. # fb: http://facebook.com/xtoker
  5.  
  6. print "+----------------+"
  7. print "| Muhammad Adeel |"
  8. print "+      aka       +"
  9. print "|Innoxent Stoker |"
  10. print "+----------------+"
  11.  
  12.  
  13.  
  14. import os,sys,thread,socket
  15.  
  16. # Variables..
  17.  
  18. BACKLOG = 50 # how many pending connections queue will hold
  19. MAX_DATA_RECV = 999999 # max number of bytes we receive at once
  20. DEBUG = True # set to True to see the debug msgs
  21. BLOCKED = [] # just an example. Remove with [""] for no blocking at all.
  22.  
  23. # Main Script ...
  24.  
  25.  
  26. def main():
  27.  
  28.     # check the length of command running
  29.     if (len(sys.argv)<2):
  30.         print "No port given, using :8080 (http-alt)"
  31.         port = 8080
  32.     else:
  33.         port = int(sys.argv[1]) # port from argument
  34.  
  35.     # host and port info.
  36.     host = '' # blank for localhost
  37.    
  38.     print "Proxy Server Running on ",host,":",port
  39.  
  40.     try:
  41.         # create a socket
  42.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  43.  
  44.         # associate the socket to host and port
  45.         s.bind((host, port))
  46.  
  47.         # listenning
  48.         s.listen(BACKLOG)
  49.    
  50.     except socket.error, (value, message):
  51.         if s:
  52.             s.close()
  53.         print "Could not open socket:", message
  54.         sys.exit(1)
  55.  
  56.     # get the connection from client
  57.     while 1:
  58.         conn, client_addr = s.accept()
  59.  
  60.         # create a thread to handle request
  61.         thread.start_new_thread(proxy_thread, (conn, client_addr))
  62.        
  63.     s.close()
  64.  
  65. # End of Programme...
  66.  
  67. def printout(type,request,address):
  68.     if "Block" in type or "Blacklist" in type:
  69.         colornum = 91
  70.     elif "Request" in type:
  71.         colornum = 92
  72.     elif "Reset" in type:
  73.         colornum = 93
  74.  
  75.     print "\033[",colornum,"m",address[0],"\t",type,"\t",request,"\033[0m"
  76.  
  77. # This is Actual Proxy Script..
  78.  
  79. def proxy_thread(conn, client_addr):
  80.  
  81.     # get the request from browser
  82.     request = conn.recv(MAX_DATA_RECV)
  83.  
  84.     # parse the first line
  85.     first_line = request.split('\n')[0]
  86.  
  87.     # get url
  88.     url = first_line.split(' ')[1]
  89.  
  90.     for i in range(0,len(BLOCKED)):
  91.         if BLOCKED[i] in url:
  92.             printout("Blacklisted",first_line,client_addr)
  93.             conn.close()
  94.             sys.exit(1)
  95.  
  96.  
  97.     printout("Request",first_line,client_addr)
  98.     # print "URL:",url
  99.     # print
  100.    
  101.     # find the webserver and port
  102.     http_pos = url.find("://") # find pos of ://
  103.     if (http_pos==-1):
  104.         temp = url
  105.     else:
  106.         temp = url[(http_pos+3):] # get the rest of url
  107.    
  108.     port_pos = temp.find(":") # find the port pos (if any)
  109.  
  110.     # find end of web server
  111.     webserver_pos = temp.find("/")
  112.     if webserver_pos == -1:
  113.         webserver_pos = len(temp)
  114.  
  115.     webserver = ""
  116.     port = -1
  117.     if (port_pos==-1 or webserver_pos < port_pos): # default port
  118.         port = 80
  119.         webserver = temp[:webserver_pos]
  120.     else: # specific port
  121.         port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
  122.         webserver = temp[:port_pos]
  123.  
  124.     try:
  125.         # create a socket to connect to the web server
  126.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  127.         s.connect((webserver, port))
  128.         s.send(request) # send request to webserver
  129.        
  130.         while 1:
  131.             # receive data from web server
  132.             data = s.recv(MAX_DATA_RECV)
  133.            
  134.             if (len(data) > 0):
  135.                 # send to browser
  136.                 conn.send(data)
  137.             else:
  138.                 break
  139.         s.close()
  140.         conn.close()
  141.     except socket.error, (value, message):
  142.         if s:
  143.             s.close()
  144.         if conn:
  145.             conn.close()
  146.         printout("Peer Reset",first_line,client_addr)
  147.         sys.exit(1)
  148. # End of Proxy  ...
  149.    
  150. if __name__ == '__main__':
  151.     main()
Add Comment
Please, Sign In to add comment