#!/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. from __future__ import with_statement import sys if sys.version_info < (2, 6): import simplejson as json else: import json import struct import string import hashlib import os import logging import getopt import socket def get_table(key): m = hashlib.md5() m.update(key) s = m.digest() (a, b) = struct.unpack('H', remote_port)[0] self.remote_addr_pair = (remote_addr, remote_port) logging.info('connecting %s:%d' % self.remote_addr_pair) remote_conn = ssloop.Socket() self.remote_handler = RemoteHandler(remote_conn, self) if len(data) > header_length: self.cached_pieces.append(data[header_length:]) # TODO save other bytes self.stage = 4 return except: import traceback traceback.print_exc() if self.stage == 4: self.cached_pieces.append(data) def on_end(self, s): if self.remote_handler: self.remote_handler.conn.end() def on_close(self, s): pass # self.remote_handler.conn.end() def __init__(self, conn): self.stage = 0 self.remote = None self.addr_len = 0 self.addr_to_send = '' self.conn = conn self.cached_pieces = [] conn.on('data', self.on_data) conn.on('end', self.on_end) conn.on('close', self.on_close) def on_connection(s, conn): LocalHandler(conn) if __name__ == '__main__': os.chdir(os.path.dirname(__file__) or '.') sys.path.append('./ssloop') import ssloop print 'shadowsocks v2.0' with open('config.json', 'rb') as f: config = json.load(f) PORT = config['server_port'] KEY = config['password'] argv = sys.argv[1:] if '-6' in sys.argv[1:]: argv.remove('-6') optlist, args = getopt.getopt(argv, 'p:k:') for key, value in optlist: if key == '-p': PORT = int(value) elif key == '-k': KEY = value logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') encrypt_table = ''.join(get_table(KEY)) decrypt_table = string.maketrans(encrypt_table, string.maketrans('', '')) try: logging.info("starting server at port %d ..." % PORT) loop = ssloop.instance() s = ssloop.Server(('0.0.0.0', PORT)) s.on('connection', on_connection) s.listen() loop.start() except KeyboardInterrupt: sys.exit(0)