Advertisement
johnmahugu

python proxy script

May 6th, 2016
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. #!/usr/bin/python
  2. # This is a simple port-forward / proxy, written using only the default python
  3. # library. If you want to make a suggestion or fix something you can contact-me
  4. # at hack.kesh_at_gmail.com
  5. # Distributed over IDC(I Don't Care) license
  6. import socket
  7. import select
  8. import time
  9. import sys
  10.  
  11. # Changing the buffer_size and delay, you can improve the speed and bandwidth.
  12. # But when buffer get to high or delay go too down, you can broke things
  13. buffer_size = 4096
  14. delay = 0.0001
  15. forward_to = ('smtp.zaz.ufsk.br', 25)
  16.  
  17. class Forward:
  18.     def __init__(self):
  19.         self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  20.  
  21.     def start(self, host, port):
  22.         try:
  23.             self.forward.connect((host, port))
  24.             return self.forward
  25.         except Exception, e:
  26.             print e
  27.             return False
  28.  
  29. class TheServer:
  30.     input_list = []
  31.     channel = {}
  32.  
  33.     def __init__(self, host, port):
  34.         self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  35.         self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  36.         self.server.bind((host, port))
  37.         self.server.listen(200)
  38.  
  39.     def main_loop(self):
  40.         self.input_list.append(self.server)
  41.         while 1:
  42.             time.sleep(delay)
  43.             ss = select.select
  44.             inputready, outputready, exceptready = ss(self.input_list, [], [])
  45.             for self.s in inputready:
  46.                 if self.s == self.server:
  47.                     self.on_accept()
  48.                     break
  49.  
  50.                 self.data = self.s.recv(buffer_size)
  51.                 if len(self.data) == 0:
  52.                     self.on_close()
  53.                     break
  54.                 else:
  55.                     self.on_recv()
  56.  
  57.     def on_accept(self):
  58.         forward = Forward().start(forward_to[0], forward_to[1])
  59.         clientsock, clientaddr = self.server.accept()
  60.         if forward:
  61.             print clientaddr, "has connected"
  62.             self.input_list.append(clientsock)
  63.             self.input_list.append(forward)
  64.             self.channel[clientsock] = forward
  65.             self.channel[forward] = clientsock
  66.         else:
  67.             print "Can't establish connection with remote server.",
  68.             print "Closing connection with client side", clientaddr
  69.             clientsock.close()
  70.  
  71.     def on_close(self):
  72.         print self.s.getpeername(), "has disconnected"
  73.         #remove objects from input_list
  74.         self.input_list.remove(self.s)
  75.         self.input_list.remove(self.channel[self.s])
  76.         out = self.channel[self.s]
  77.         # close the connection with client
  78.         self.channel[out].close()  # equivalent to do self.s.close()
  79.         # close the connection with remote server
  80.         self.channel[self.s].close()
  81.         # delete both objects from channel dict
  82.         del self.channel[out]
  83.         del self.channel[self.s]
  84.  
  85.     def on_recv(self):
  86.         data = self.data
  87.         # here we can parse and/or modify the data before send forward
  88.         print data
  89.         self.channel[self.s].send(data)
  90.  
  91. if __name__ == '__main__':
  92.         server = TheServer('', 9090)
  93.         try:
  94.             server.main_loop()
  95.         except KeyboardInterrupt:
  96.             print "Ctrl C - Stopping server"
  97.             sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement