├── .gitignore ├── LICENSE ├── README.md └── proxy.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ricardo Pascal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # proxy 2 | From post: "A python proxy in less than 100 lines of code" 3 | -------------------------------------------------------------------------------- /proxy.py: -------------------------------------------------------------------------------- 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 voorloop_at_gmail.com 5 | # Distributed over MIT license 6 | import socket 7 | import select 8 | import time 9 | import sys 10 | 11 | buffer_size = 4096 12 | forward_to = ('www.voorloopnul.com', 80) 13 | 14 | class Forward: 15 | def __init__(self): 16 | self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 17 | 18 | def start(self, host, port): 19 | try: 20 | self.forward.connect((host, port)) 21 | return self.forward 22 | except Exception as inst: 23 | print("[exception] - {0}".format(inst.strerror)) 24 | return False 25 | 26 | class TheServer: 27 | input_list = [] 28 | channel = {} 29 | 30 | def __init__(self, host, port): 31 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 32 | self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 33 | self.server.bind((host, port)) 34 | self.server.listen(200) 35 | 36 | def main_loop(self): 37 | self.input_list.append(self.server) 38 | while 1: 39 | ss = select.select 40 | inputready, outputready, exceptready = ss(self.input_list, [], []) 41 | for s in inputready: 42 | if s == self.server: 43 | self.on_accept(s) 44 | break 45 | 46 | self.data = s.recv(buffer_size) 47 | if len(self.data) == 0: 48 | self.on_close(s) 49 | break 50 | else: 51 | self.on_recv(s) 52 | 53 | def on_accept(self, s): 54 | forward = Forward().start(forward_to[0], forward_to[1]) 55 | clientsock, clientaddr = self.server.accept() 56 | if forward: 57 | print("{0} has connected".format(clientaddr)) 58 | self.input_list.append(clientsock) 59 | self.input_list.append(forward) 60 | self.channel[clientsock] = forward 61 | self.channel[forward] = clientsock 62 | else: 63 | print("Can't establish a connection with remote server. Closing connection with client side {0}".format(clientaddr)) 64 | clientsock.close() 65 | 66 | def on_close(self, s): 67 | print("{0} has disconnected".format(s.getpeername())) 68 | #remove objects from input_list 69 | self.input_list.remove(s) 70 | self.input_list.remove(self.channel[s]) 71 | out = self.channel[s] 72 | # close the connection with client 73 | self.channel[out].close() 74 | # close the connection with remote server 75 | self.channel[s].close() 76 | # delete both objects from channel dict 77 | del self.channel[out] 78 | del self.channel[s] 79 | 80 | def on_recv(self, s): 81 | data = self.data 82 | # here we can parse and/or modify the data before send forward 83 | print(data) 84 | self.channel[s].send(data) 85 | 86 | if __name__ == '__main__': 87 | server = TheServer('', 9090) 88 | try: 89 | server.main_loop() 90 | except KeyboardInterrupt: 91 | print("Ctrl C - Stopping server") 92 | sys.exit(1) --------------------------------------------------------------------------------