Browse Source

refine obfs, support comment in config.json

dev
破娃酱 8 years ago
parent
commit
e0b4bdf551
  1. 8
      mujson_mgr.py
  2. 4
      shadowsocks/obfsplugin/http_simple.py
  3. 14
      shadowsocks/obfsplugin/obfs_tls.py
  4. 2
      shadowsocks/server.py
  5. 43
      shadowsocks/shell.py

8
mujson_mgr.py

@ -91,7 +91,7 @@ class MuMgr(object):
return ''.join([random.choice('''ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~-_=+(){}[]^&%$@''') for i in range(8)]) return ''.join([random.choice('''ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~-_=+(){}[]^&%$@''') for i in range(8)])
def add(self, user): def add(self, user):
up = {'enable': True, 'u': 0, 'd': 0, 'method': "aes-128-cfb", up = {'enable': 1, 'u': 0, 'd': 0, 'method': "aes-128-cfb",
'protocol': "auth_sha1_v4_compatible", 'protocol': "auth_sha1_v4_compatible",
'obfs': "tls1.2_ticket_auth_compatible", 'obfs': "tls1.2_ticket_auth_compatible",
'transfer_enable': 1125899906842624} 'transfer_enable': 1125899906842624}
@ -217,7 +217,11 @@ def main():
'3': 'auth_sha1_v2', '3': 'auth_sha1_v2',
'+4': 'auth_sha1_v4_compatible', '+4': 'auth_sha1_v4_compatible',
'4': 'auth_sha1_v4', '4': 'auth_sha1_v4',
'a1': 'auth_aes128'} 'am': 'auth_aes128_md5',
'as': 'auth_aes128_sha1',
'+am': 'auth_aes128_md5_compatible',
'+as': 'auth_aes128_sha1_compatible'
}
fast_set_method = {'a0': 'aes-128-cfb', fast_set_method = {'a0': 'aes-128-cfb',
'a1': 'aes-192-cfb', 'a1': 'aes-192-cfb',
'a2': 'aes-256-cfb', 'a2': 'aes-256-cfb',

4
shadowsocks/obfsplugin/http_simple.py

@ -138,7 +138,7 @@ class http_simple(plain.plain):
def get_data_from_http_header(self, buf): def get_data_from_http_header(self, buf):
ret_buf = b'' ret_buf = b''
lines = buf.split(b'\r\n') lines = buf.split(b'\r\n')
if lines and len(lines) > 4: if lines and len(lines) > 1:
hex_items = lines[0].split(b'%') hex_items = lines[0].split(b'%')
if hex_items and len(hex_items) > 1: if hex_items and len(hex_items) > 1:
for index in range(1, len(hex_items)): for index in range(1, len(hex_items)):
@ -156,7 +156,7 @@ class http_simple(plain.plain):
def get_host_from_http_header(self, buf): def get_host_from_http_header(self, buf):
ret_buf = b'' ret_buf = b''
lines = buf.split(b'\r\n') lines = buf.split(b'\r\n')
if lines and len(lines) > 4: if lines and len(lines) > 1:
for line in lines: for line in lines:
if match_begin(line, b"Host: "): if match_begin(line, b"Host: "):
return line[6:] return line[6:]

14
shadowsocks/obfsplugin/obfs_tls.py

@ -62,7 +62,7 @@ class tls_ticket_auth(plain.plain):
self.send_buffer = b'' self.send_buffer = b''
self.recv_buffer = b'' self.recv_buffer = b''
self.client_id = b'' self.client_id = b''
self.max_time_dif = 0 # time dif (second) setting self.max_time_dif = 60 * 60 * 24 # time dif (second) setting
self.tls_version = b'\x03\x03' self.tls_version = b'\x03\x03'
def init_data(self): def init_data(self):
@ -215,14 +215,18 @@ class tls_ticket_auth(plain.plain):
return self.server_decode(b'') return self.server_decode(b'')
#raise Exception("handshake data = %s" % (binascii.hexlify(buf))) #raise Exception("handshake data = %s" % (binascii.hexlify(buf)))
self.handshake_status = 2 self.recv_buffer += buf
buf = self.recv_buffer
ogn_buf = buf ogn_buf = buf
if len(buf) < 3:
return (b'', False, False)
if not match_begin(buf, b'\x16\x03\x01'): if not match_begin(buf, b'\x16\x03\x01'):
return self.decode_error_return(ogn_buf) return self.decode_error_return(ogn_buf)
buf = buf[3:] buf = buf[3:]
if struct.unpack('>H', buf[:2])[0] != len(buf) - 2: if struct.unpack('>H', buf[:2])[0] > len(buf) - 2:
logging.info("tls_auth wrong tls head size") return (b'', False, False)
return self.decode_error_return(ogn_buf)
self.handshake_status = 2
buf = buf[2:] buf = buf[2:]
if not match_begin(buf, b'\x01\x00'): #client hello if not match_begin(buf, b'\x01\x00'): #client hello
logging.info("tls_auth not client hello message") logging.info("tls_auth not client hello message")

2
shadowsocks/server.py

@ -103,7 +103,7 @@ def main():
a_config = config.copy() a_config = config.copy()
ipv6_ok = False ipv6_ok = False
logging.info("server start with protocol[%s] password [%s] method [%s] obfs [%s] obfs_param [%s]" % logging.info("server start with protocol[%s] password [%s] method [%s] obfs [%s] obfs_param [%s]" %
(protocol, password, a_config['method'], obfs, obfs_param)) (protocol, password, method, obfs, obfs_param))
if 'server_ipv6' in a_config: if 'server_ipv6' in a_config:
try: try:
if len(a_config['server_ipv6']) > 2 and a_config['server_ipv6'][0] == "[" and a_config['server_ipv6'][-1] == "]": if len(a_config['server_ipv6']) > 2 and a_config['server_ipv6'][0] == "[" and a_config['server_ipv6'][-1] == "]":

43
shadowsocks/shell.py

@ -171,7 +171,7 @@ def get_config(is_local):
logging.info('loading config from %s' % config_path) logging.info('loading config from %s' % config_path)
with open(config_path, 'rb') as f: with open(config_path, 'rb') as f:
try: try:
config = parse_json_in_str(f.read().decode('utf8')) config = parse_json_in_str(remove_comment(f.read().decode('utf8')))
except ValueError as e: except ValueError as e:
logging.error('found an error in config.json: %s', logging.error('found an error in config.json: %s',
e.message) e.message)
@ -410,6 +410,47 @@ def _decode_dict(data):
rv[key] = value rv[key] = value
return rv return rv
class JSFormat:
def __init__(self):
self.state = 0
def push(self, ch):
ch = ord(ch)
if self.state == 0:
if ch == ord('"'):
self.state = 1
return to_str(chr(ch))
elif ch == ord('/'):
self.state = 3
else:
return to_str(chr(ch))
elif self.state == 1:
if ch == ord('"'):
self.state = 0
return to_str(chr(ch))
elif ch == ord('\\'):
self.state = 2
return to_str(chr(ch))
elif self.state == 2:
self.state = 1
if ch == ord('"'):
return to_str(chr(ch))
return "\\" + to_str(chr(ch))
elif self.state == 3:
if ch == ord('/'):
self.state = 4
else:
return "/" + to_str(chr(ch))
elif self.state == 4:
if ch == ord('\n'):
self.state = 0
return "\n"
return ""
def remove_comment(json):
fmt = JSFormat()
return "".join([fmt.push(c) for c in json])
def parse_json_in_str(data): def parse_json_in_str(data):
# parse json and convert everything from unicode to str # parse json and convert everything from unicode to str

Loading…
Cancel
Save