Skip to content

Instantly share code, notes, and snippets.

@koehlma
Created September 30, 2012 15:06
Show Gist options
  • Save koehlma/3807061 to your computer and use it in GitHub Desktop.
Save koehlma/3807061 to your computer and use it in GitHub Desktop.
MTS - Forward Proxy
# -*- coding:utf-8 -*-
#
# Copyright (C) 2012, Maximilian Köhl <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncore
import socket
class Endpoint(asyncore.dispatcher_with_send):
def __init__(self, connection, target):
super().__init__()
self.connection = connection
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect(target)
def handle_read(self):
self.connection.send(self.recv(8192))
def handle_close(self):
self.connection.close()
self.close()
class Connection(asyncore.dispatcher_with_send):
def __init__(self, socket, target):
self.endpoint = None
super().__init__(socket)
self.endpoint = Endpoint(self, target)
def handle_read(self):
if self.endpoint is not None: self.endpoint.send(self.recv(8192))
def handle_close(self):
if self.endpoint is not None: self.endpoint.close()
self.close()
class Forward(asyncore.dispatcher):
def __init__(self, port, target):
super().__init__()
self.target = target
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(('', port))
self.listen(5)
def handle_accepted(self, socket, address):
Connection(socket, self.target)
if __name__ == '__main__':
forward = Forward(44444, ('mts-s-server', 800))
asyncore.loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment