#!/usr/bin/env python # Copyright (c) 2012 clowwindy # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import sys import socket import struct import string import hashlib import os import json import logging import getopt from tornado import ioloop from tornado import iostream from tornado import netutil class Crypto(object): def __init__(self, password): m = hashlib.md5() m.update(password) s = m.digest() a, b = struct.unpack('H', crypto.decrypt(port))[0] logging.debug("Connecting to %s:%d" % (self.remote_addr, self.remote_port)) remote_soc = socket.socket() remote_soc.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) self.remote = PairedStream(remote_soc) self.set_close_callback(self.remote.on_close) self.remote.set_close_callback(self.on_close) self.remote.connect((self.remote_addr, self.remote_port), self.on_remote_connected) def on_remote_connected(self): self.read_until_close(callback=self.on_client_read, streaming_callback=self.on_client_read) self.remote.read_until_close(callback=self.on_remote_read, streaming_callback=self.on_remote_read) def on_client_read(self, data): self.remote.write(crypto.encrypt(data)) def on_remote_read(self, data): self.write(crypto.decrypt(data)) if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') with open(os.path.join(os.path.dirname(__file__), "config.json"), "rb") as f: config = json.load(f) server_port = config['server_port'] server_password = config['password'] optlist, args = getopt.getopt(sys.argv[1:], 'p:k:') for key, value in optlist: if key == '-p': server_port = int(value) elif key == '-k': server_password = value if '-6' in sys.argv[1:]: address_family = socket.AF_INET6 else: address_family = socket.AF_INET crypto = Crypto(server_password) logging.info("starting server at port %d ..." % server_port) server = Socks5Server() server.bind(port=server_port, family=address_family) server.start() try: ioloop.IOLoop.instance().start() except KeyboardInterrupt: pass except Exception: logging.exception("Uncaught Exception")