Browse Source

lint code

1.4
clowwindy 11 years ago
parent
commit
89a7508f26
  1. 4
      CHANGES
  2. 6
      README.md
  3. 4
      config.json
  4. 134
      shadowsocks/local.py
  5. 6
      shadowsocks/utils.py

4
CHANGES

@ -1,3 +1,7 @@
1.4.0 2014-05-02
- Adds UDP relay
- TCP fast open supports on Linux 3.7+
1.3.7 2014-04-10 1.3.7 2014-04-10
- Fix a typo in help - Fix a typo in help

6
README.md

@ -40,6 +40,7 @@ Explanation of the fields:
password a password used to encrypt transfer password a password used to encrypt transfer
timeout in seconds timeout in seconds
method encryption method, "bf-cfb", "aes-256-cfb", "des-cfb", "rc4", etc. Default is table, which is not secure. "aes-256-cfb" is recommended method encryption method, "bf-cfb", "aes-256-cfb", "des-cfb", "rc4", etc. Default is table, which is not secure. "aes-256-cfb" is recommended
fast_open use TCP_FASTOPEN, true/false
`cd` into the directory of `config.json`. Run `ssserver` on your server. To run it in the background, run `cd` into the directory of `config.json`. Run `ssserver` on your server. To run it in the background, run
`nohup ssserver > log &`. `nohup ssserver > log &`.
@ -112,6 +113,11 @@ Or:
$ sudo apt-get install libevent-dev python-pip $ sudo apt-get install libevent-dev python-pip
$ sudo pip install gevent $ sudo pip install gevent
If both of your server and client are deployed on Linux 3.7+, you can turn on
fast_open for lower latency.
echo 3 > /proc/sys/net/ipv4/tcp_fastopen
License License
------- -------
MIT MIT

4
config.json

@ -4,5 +4,7 @@
"local_port":1080, "local_port":1080,
"password":"barfoo!", "password":"barfoo!",
"timeout":600, "timeout":600,
"method":"table" "method":"table",
"local_address":"127.0.0.1",
"fast_open":false
} }

134
shadowsocks/local.py

@ -69,43 +69,51 @@ class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
class Socks5Server(SocketServer.StreamRequestHandler): class Socks5Server(SocketServer.StreamRequestHandler):
def getServer(self): @staticmethod
aPort = REMOTE_PORT def get_server():
aServer = SERVER a_port = config_server_port
if isinstance(REMOTE_PORT, list): a_server = config_server
if isinstance(config_server_port, list):
# support config like "server_port": [8081, 8082] # support config like "server_port": [8081, 8082]
aPort = random.choice(REMOTE_PORT) a_port = random.choice(config_server_port)
if isinstance(SERVER, list): if isinstance(config_server, list):
# support config like "server": ["123.123.123.1", "123.123.123.2"] # support config like "server": ["123.123.123.1", "123.123.123.2"]
aServer = random.choice(SERVER) a_server = random.choice(config_server)
r = re.match(r'^(.*)\:(\d+)$', aServer) r = re.match(r'^(.*):(\d+)$', a_server)
if r: if r:
# support config like "server": "123.123.123.1:8381" # support config like "server": "123.123.123.1:8381"
# or "server": ["123.123.123.1:8381", "123.123.123.2:8381"] # or "server": ["123.123.123.1:8381", "123.123.123.2:8381"]
aServer = r.group(1) a_server = r.group(1)
aPort = int(r.group(2)) a_port = int(r.group(2))
return (aServer, aPort) return a_server, a_port
def handle_tcp(self, sock, remote, pending_data=None, server=None, port=None): @staticmethod
def handle_tcp(sock, remote, encryptor, pending_data=None,
server=None, port=None):
connected = False connected = False
try: try:
if FAST_OPEN: if config_fast_open:
fdset = [sock] fdset = [sock]
else: else:
fdset = [sock, remote] fdset = [sock, remote]
while True: while True:
r, w, e = select.select(fdset, [], []) r, w, e = select.select(fdset, [], [])
if sock in r: if sock in r:
if not connected and FAST_OPEN: if not connected and config_fast_open:
data = sock.recv(4096) data = sock.recv(4096)
data = self.encrypt(pending_data + data) data = encryptor.encrypt(pending_data + data)
pending_data = None
logging.info('fast open %s:%d' % (server, port))
remote.sendto(data, MSG_FASTOPEN, (server, port)) remote.sendto(data, MSG_FASTOPEN, (server, port))
connected = True connected = True
fdset = [sock, remote] fdset = [sock, remote]
logging.info('fast open %s:%d' % (server, port))
else: else:
data = self.encrypt(sock.recv(4096)) data = sock.recv(4096)
if pending_data:
data = pending_data + data
pending_data = None
data = encryptor.encrypt(data)
if len(data) <= 0: if len(data) <= 0:
break break
result = send_all(remote, data) result = send_all(remote, data)
@ -113,7 +121,7 @@ class Socks5Server(SocketServer.StreamRequestHandler):
raise Exception('failed to send all data') raise Exception('failed to send all data')
if remote in r: if remote in r:
data = self.decrypt(remote.recv(4096)) data = encryptor.decrypt(remote.recv(4096))
if len(data) <= 0: if len(data) <= 0:
break break
result = send_all(sock, data) result = send_all(sock, data)
@ -123,18 +131,9 @@ class Socks5Server(SocketServer.StreamRequestHandler):
sock.close() sock.close()
remote.close() remote.close()
def encrypt(self, data):
return self.encryptor.encrypt(data)
def decrypt(self, data):
return self.encryptor.decrypt(data)
def send_encrypt(self, sock, data):
sock.send(self.encrypt(data))
def handle(self): def handle(self):
try: try:
self.encryptor = encrypt.Encryptor(KEY, METHOD) encryptor = encrypt.Encryptor(config_password, config_method)
sock = self.connection sock = self.connection
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
data = sock.recv(262) data = sock.recv(262)
@ -216,23 +215,23 @@ class Socks5Server(SocketServer.StreamRequestHandler):
reply += socket.inet_aton('0.0.0.0') + struct.pack(">H", 2222) reply += socket.inet_aton('0.0.0.0') + struct.pack(">H", 2222)
self.wfile.write(reply) self.wfile.write(reply)
# reply immediately # reply immediately
aServer, aPort = self.getServer() a_server, a_port = Socks5Server.get_server()
addrs = socket.getaddrinfo(aServer, aPort) addrs = socket.getaddrinfo(a_server, a_port)
if addrs: if addrs:
af, socktype, proto, canonname, sa = addrs[0] af, socktype, proto, canonname, sa = addrs[0]
if FAST_OPEN: if config_fast_open:
remote = socket.socket(af, socktype, proto) remote = socket.socket(af, socktype, proto)
# remote.setsockopt(socket.IPPROTO_TCP,
# socket.TCP_NODELAY, 1)
self.handle_tcp(sock, remote, addr_to_send, aServer,
aPort)
else:
remote = socket.create_connection((aServer, aPort))
remote.setsockopt(socket.IPPROTO_TCP, remote.setsockopt(socket.IPPROTO_TCP,
socket.TCP_NODELAY, 1) socket.TCP_NODELAY, 1)
self.send_encrypt(remote, addr_to_send) Socks5Server.handle_tcp(sock, remote, encryptor,
addr_to_send, a_server, a_port)
else:
logging.info('connecting %s:%d' % (addr, port[0])) logging.info('connecting %s:%d' % (addr, port[0]))
self.handle_tcp(sock, remote) remote = socket.create_connection((a_server, a_port))
remote.setsockopt(socket.IPPROTO_TCP,
socket.TCP_NODELAY, 1)
Socks5Server.handle_tcp(sock, remote, encryptor,
addr_to_send)
finally: finally:
pass pass
# except socket.error, e: # except socket.error, e:
@ -247,7 +246,8 @@ class Socks5Server(SocketServer.StreamRequestHandler):
def main(): def main():
global SERVER, REMOTE_PORT, KEY, METHOD, FAST_OPEN global config_server, config_server_port, config_password, config_method,\
config_fast_open
logging.basicConfig(level=logging.DEBUG, logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s', format='%(asctime)s %(levelname)-8s %(message)s',
@ -266,14 +266,13 @@ def main():
pass pass
print 'shadowsocks %s' % version print 'shadowsocks %s' % version
KEY = None config_password = None
METHOD = None config_method = None
LOCAL = ''
IPv6 = False
config_path = utils.find_config() config_path = utils.find_config()
try: try:
optlist, args = getopt.getopt(sys.argv[1:], 's:b:p:k:l:m:c:6') optlist, args = getopt.getopt(sys.argv[1:], 's:b:p:k:l:m:c:',
['fast-open'])
for key, value in optlist: for key, value in optlist:
if key == '-c': if key == '-c':
config_path = value config_path = value
@ -290,7 +289,8 @@ def main():
else: else:
config = {} config = {}
optlist, args = getopt.getopt(sys.argv[1:], 's:b:p:k:l:m:c:6') optlist, args = getopt.getopt(sys.argv[1:], 's:b:p:k:l:m:c:',
['fast-open'])
for key, value in optlist: for key, value in optlist:
if key == '-p': if key == '-p':
config['server_port'] = int(value) config['server_port'] = int(value)
@ -304,35 +304,41 @@ def main():
config['method'] = value config['method'] = value
elif key == '-b': elif key == '-b':
config['local_address'] = value config['local_address'] = value
elif key == '-6': elif key == '--fast-open':
IPv6 = True config['fast_open'] = True
except getopt.GetoptError: except getopt.GetoptError as e:
logging.error(e)
utils.print_local_help() utils.print_local_help()
sys.exit(2) sys.exit(2)
SERVER = config['server'] config_server = config['server']
REMOTE_PORT = config['server_port'] config_server_port = config['server_port']
PORT = config['local_port'] config_local_port = config['local_port']
KEY = config['password'] config_password = config['password']
METHOD = config.get('method', None) config_method = config.get('method', None)
LOCAL = config.get('local_address', '127.0.0.1') config_local_address = config.get('local_address', '127.0.0.1')
TIMEOUT = config.get('timeout', 600) config_timeout = config.get('timeout', 600)
FAST_OPEN = config.get('fast_open', False) config_fast_open = config.get('fast_open', False)
if not KEY and not config_path: if not config_password and not config_path:
sys.exit('config not specified, please read ' sys.exit('config not specified, please read '
'https://github.com/clowwindy/shadowsocks') 'https://github.com/clowwindy/shadowsocks')
utils.check_config(config) utils.check_config(config)
encrypt.init_table(KEY, METHOD) encrypt.init_table(config_password, config_method)
if IPv6: addrs = socket.getaddrinfo(config_local_address, config_local_port)
ThreadingTCPServer.address_family = socket.AF_INET6 if not addrs:
logging.error('cant resolve local address')
sys.exit(1)
ThreadingTCPServer.address_family = addrs[0][0]
try: try:
udprelay.UDPRelay(LOCAL, int(PORT), SERVER, REMOTE_PORT, KEY, METHOD, udprelay.UDPRelay(config_local_address, int(config_local_port),
int(TIMEOUT), True).start() config_server, config_server_port, config_password,
server = ThreadingTCPServer((LOCAL, PORT), Socks5Server) config_method, int(config_timeout), True).start()
server = ThreadingTCPServer((config_local_address, config_local_port),
Socks5Server)
logging.info("starting local at %s:%d" % logging.info("starting local at %s:%d" %
tuple(server.server_address[:2])) tuple(server.server_address[:2]))
server.serve_forever() server.serve_forever()

6
shadowsocks/utils.py

@ -48,7 +48,7 @@ def check_config(config):
def print_local_help(): def print_local_help():
print '''usage: sslocal [-h] -s SERVER_ADDR -p SERVER_PORT [-b LOCAL_ADDR] print '''usage: sslocal [-h] -s SERVER_ADDR -p SERVER_PORT [-b LOCAL_ADDR]
-l LOCAL_PORT -k PASSWORD -m METHOD [-c config] -l LOCAL_PORT -k PASSWORD -m METHOD [-c config] [--fast-open]
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
@ -59,12 +59,13 @@ optional arguments:
-k PASSWORD password -k PASSWORD password
-m METHOD encryption method, for example, aes-256-cfb -m METHOD encryption method, for example, aes-256-cfb
-c CONFIG path to config file -c CONFIG path to config file
--fast-open use TCP_FASTOPEN, requires Linux 3.7+
''' '''
def print_server_help(): def print_server_help():
print '''usage: ssserver [-h] -s SERVER_ADDR -p SERVER_PORT -k PASSWORD print '''usage: ssserver [-h] -s SERVER_ADDR -p SERVER_PORT -k PASSWORD
-m METHOD [-c config] -m METHOD [-c config] [--fast-open]
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
@ -73,4 +74,5 @@ optional arguments:
-k PASSWORD password -k PASSWORD password
-m METHOD encryption method, for example, aes-256-cfb -m METHOD encryption method, for example, aes-256-cfb
-c CONFIG path to config file -c CONFIG path to config file
--fast-open use TCP_FASTOPEN, requires Linux 3.7+
''' '''
Loading…
Cancel
Save