johnmahugu

python proxy

Jun 3rd, 2015
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #!/usr/bin/python2.7
  2.  
  3. import sys
  4. import socket
  5. import threading
  6.  
  7. def server_loop(local_host,local_port,remote_host,remote_port,receive_first):
  8.  
  9. # create the server object
  10. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11.  
  12. # lets see if we can stand up the server
  13. try:
  14. server.bind((local_host,local_port))
  15. except:
  16. print "[!!] Failed to listen on %s:%d" % (local_host, local_port)
  17. print "[!!] Check for other listening sockets or correct permissions"
  18. sys.exit(0)
  19.  
  20. # listen with 5 backlogged--queued--connections
  21. server.listen(5)
  22.  
  23. while True:
  24. client_socket, addr = server.accept()
  25.  
  26. # print out the local connection information
  27. print"[+] Received incomming connections from %s:%d" % (addr[0],addr[1])
  28.  
  29. # start a new thread to talk to the remote host
  30. proxy_thread = threading.Thread(target=proxy_handler,args=(client_socket,remote_host,remote_port,receive_first))
  31.  
  32. proxy_thread.start()
  33.  
  34. def main():
  35. # cursory check of command line args
  36. if len(sys.argv[1:]) != 5:
  37. print "Usage: ./proxy.py [localhost] [localport] [remotehost] [remoteport] [reveive_first]"
  38. print "Example: ./proxy.py 127.0.0.1 9000 10.11.132.1 9000 True"
  39. sys.exit(0)
  40.  
  41. # set up listening parameters
  42. local_host = sys.argv[1]
  43. local_port = int(sys.argv[2])
  44.  
  45. # set up remote targets
  46. remote_host = sys.argv[3]
  47. remote_port = int(sys.argv[4])
  48.  
  49. # this tells our proxy to connect and receive data before sending to the remote host
  50. receive_first = sys.argv[5]
  51.  
  52. if "True" in receive_first:
  53. receive_first = True
  54. else:
  55. receive_first = False
  56.  
  57. # now spin up our listening socket
  58. server_loop(local_host,local_port,remote_host,remote_port,receive_first)
  59.  
  60. if __name__ == "__main__":
  61. main()
Advertisement
Add Comment
Please, Sign In to add comment