|
@ -25,16 +25,22 @@ import binascii |
|
|
import base64 |
|
|
import base64 |
|
|
import datetime |
|
|
import datetime |
|
|
import struct |
|
|
import struct |
|
|
|
|
|
import zlib |
|
|
|
|
|
|
|
|
from shadowsocks.obfsplugin import plain |
|
|
import shadowsocks |
|
|
from shadowsocks import common |
|
|
from shadowsocks import common |
|
|
|
|
|
from shadowsocks.obfsplugin import plain |
|
|
from shadowsocks.common import to_bytes, to_str, ord |
|
|
from shadowsocks.common import to_bytes, to_str, ord |
|
|
|
|
|
|
|
|
def create_verify_obfs(method): |
|
|
def create_verify_obfs(method): |
|
|
return verify_simple(method) |
|
|
return verify_simple(method) |
|
|
|
|
|
|
|
|
obfs = { |
|
|
def create_verify_deflate(method): |
|
|
|
|
|
return verify_deflate(method) |
|
|
|
|
|
|
|
|
|
|
|
obfs_map = { |
|
|
'verify_simple': (create_verify_obfs,), |
|
|
'verify_simple': (create_verify_obfs,), |
|
|
|
|
|
'verify_deflate': (create_verify_deflate,), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
def match_begin(str1, str2): |
|
|
def match_begin(str1, str2): |
|
@ -43,9 +49,64 @@ def match_begin(str1, str2): |
|
|
return True |
|
|
return True |
|
|
return False |
|
|
return False |
|
|
|
|
|
|
|
|
class verify_simple(plain.plain): |
|
|
class sub_encode_obfs(object): |
|
|
|
|
|
def __init__(self): |
|
|
|
|
|
self.sub_obfs = None |
|
|
|
|
|
|
|
|
|
|
|
class verify_base(plain.plain): |
|
|
def __init__(self, method): |
|
|
def __init__(self, method): |
|
|
|
|
|
super(verify_base, self).__init__(method) |
|
|
self.method = method |
|
|
self.method = method |
|
|
|
|
|
self.sub_obfs = None |
|
|
|
|
|
|
|
|
|
|
|
def init_data(self): |
|
|
|
|
|
return sub_encode_obfs() |
|
|
|
|
|
|
|
|
|
|
|
def set_server_info(self, server_info): |
|
|
|
|
|
try: |
|
|
|
|
|
if server_info.param: |
|
|
|
|
|
sub_param = '' |
|
|
|
|
|
param_list = server_info.param.split(',', 1) |
|
|
|
|
|
if len(param_list) > 1: |
|
|
|
|
|
self.sub_obfs = shadowsocks.obfs.obfs(param_list[0]) |
|
|
|
|
|
sub_param = param_list[1] |
|
|
|
|
|
else: |
|
|
|
|
|
self.sub_obfs = shadowsocks.obfs.obfs(server_info.param) |
|
|
|
|
|
if server_info.data.sub_obfs is None: |
|
|
|
|
|
server_info.data.sub_obfs = self.sub_obfs.init_data() |
|
|
|
|
|
_server_info = shadowsocks.obfs.server_info(server_info.data.sub_obfs) |
|
|
|
|
|
_server_info.host = server_info.host |
|
|
|
|
|
_server_info.port = server_info.port |
|
|
|
|
|
_server_info.tcp_mss = server_info.tcp_mss |
|
|
|
|
|
_server_info.param = sub_param |
|
|
|
|
|
self.sub_obfs.set_server_info(_server_info) |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
shadowsocks.shell.print_exception(e) |
|
|
|
|
|
self.server_info = server_info |
|
|
|
|
|
|
|
|
|
|
|
def client_encode(self, buf): |
|
|
|
|
|
if self.sub_obfs is not None: |
|
|
|
|
|
return self.sub_obfs.client_encode(buf) |
|
|
|
|
|
return buf |
|
|
|
|
|
|
|
|
|
|
|
def client_decode(self, buf): |
|
|
|
|
|
if self.sub_obfs is not None: |
|
|
|
|
|
return self.sub_obfs.client_decode(buf) |
|
|
|
|
|
return (buf, False) |
|
|
|
|
|
|
|
|
|
|
|
def server_encode(self, buf): |
|
|
|
|
|
if self.sub_obfs is not None: |
|
|
|
|
|
return self.sub_obfs.server_encode(buf) |
|
|
|
|
|
return buf |
|
|
|
|
|
|
|
|
|
|
|
def server_decode(self, buf): |
|
|
|
|
|
if self.sub_obfs is not None: |
|
|
|
|
|
return self.sub_obfs.server_decode(buf) |
|
|
|
|
|
return (buf, True, False) |
|
|
|
|
|
|
|
|
|
|
|
class verify_simple(verify_base): |
|
|
|
|
|
def __init__(self, method): |
|
|
|
|
|
super(verify_simple, self).__init__(method) |
|
|
self.recv_buf = b'' |
|
|
self.recv_buf = b'' |
|
|
self.unit_len = 8100 |
|
|
self.unit_len = 8100 |
|
|
self.decrypt_packet_num = 0 |
|
|
self.decrypt_packet_num = 0 |
|
@ -69,13 +130,6 @@ class verify_simple(plain.plain): |
|
|
ret += self.pack_data(buf) |
|
|
ret += self.pack_data(buf) |
|
|
return ret |
|
|
return ret |
|
|
|
|
|
|
|
|
def client_encode(self, buf): |
|
|
|
|
|
return buf |
|
|
|
|
|
|
|
|
|
|
|
def client_decode(self, buf): |
|
|
|
|
|
# (buffer_to_recv, is_need_to_encode_and_send_back) |
|
|
|
|
|
return (buf, False) |
|
|
|
|
|
|
|
|
|
|
|
def client_post_decrypt(self, buf): |
|
|
def client_post_decrypt(self, buf): |
|
|
if self.raw_trans: |
|
|
if self.raw_trans: |
|
|
return buf |
|
|
return buf |
|
@ -117,13 +171,6 @@ class verify_simple(plain.plain): |
|
|
ret += self.pack_data(buf) |
|
|
ret += self.pack_data(buf) |
|
|
return ret |
|
|
return ret |
|
|
|
|
|
|
|
|
def server_encode(self, buf): |
|
|
|
|
|
return buf |
|
|
|
|
|
|
|
|
|
|
|
def server_decode(self, buf): |
|
|
|
|
|
# (buffer_to_recv, is_need_decrypt, is_need_to_encode_and_send_back) |
|
|
|
|
|
return (buf, True, False) |
|
|
|
|
|
|
|
|
|
|
|
def server_post_decrypt(self, buf): |
|
|
def server_post_decrypt(self, buf): |
|
|
if self.raw_trans: |
|
|
if self.raw_trans: |
|
|
return buf |
|
|
return buf |
|
@ -157,3 +204,82 @@ class verify_simple(plain.plain): |
|
|
self.decrypt_packet_num += 1 |
|
|
self.decrypt_packet_num += 1 |
|
|
return out_buf |
|
|
return out_buf |
|
|
|
|
|
|
|
|
|
|
|
class verify_deflate(verify_base): |
|
|
|
|
|
def __init__(self, method): |
|
|
|
|
|
super(verify_deflate, self).__init__(method) |
|
|
|
|
|
self.recv_buf = b'' |
|
|
|
|
|
self.unit_len = 32700 |
|
|
|
|
|
self.decrypt_packet_num = 0 |
|
|
|
|
|
self.raw_trans = False |
|
|
|
|
|
|
|
|
|
|
|
def pack_data(self, buf): |
|
|
|
|
|
if len(buf) == 0: |
|
|
|
|
|
return b'' |
|
|
|
|
|
data = zlib.compress(buf) |
|
|
|
|
|
data = struct.pack('>H', len(data)) + data[2:] |
|
|
|
|
|
return data |
|
|
|
|
|
|
|
|
|
|
|
def client_pre_encrypt(self, buf): |
|
|
|
|
|
ret = b'' |
|
|
|
|
|
while len(buf) > self.unit_len: |
|
|
|
|
|
ret += self.pack_data(buf[:self.unit_len]) |
|
|
|
|
|
buf = buf[self.unit_len:] |
|
|
|
|
|
ret += self.pack_data(buf) |
|
|
|
|
|
return ret |
|
|
|
|
|
|
|
|
|
|
|
def client_post_decrypt(self, buf): |
|
|
|
|
|
if self.raw_trans: |
|
|
|
|
|
return buf |
|
|
|
|
|
self.recv_buf += buf |
|
|
|
|
|
out_buf = b'' |
|
|
|
|
|
while len(self.recv_buf) > 2: |
|
|
|
|
|
length = struct.unpack('>H', self.recv_buf[:2])[0] |
|
|
|
|
|
if length >= 32768: |
|
|
|
|
|
self.raw_trans = True |
|
|
|
|
|
self.recv_buf = b'' |
|
|
|
|
|
if self.decrypt_packet_num == 0: |
|
|
|
|
|
return None |
|
|
|
|
|
else: |
|
|
|
|
|
raise Exception('server_post_decrype data error') |
|
|
|
|
|
if length > len(self.recv_buf): |
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
out_buf += zlib.decompress(b'x\x9c' + self.recv_buf[2:length]) |
|
|
|
|
|
self.recv_buf = self.recv_buf[length:] |
|
|
|
|
|
|
|
|
|
|
|
if out_buf: |
|
|
|
|
|
self.decrypt_packet_num += 1 |
|
|
|
|
|
return out_buf |
|
|
|
|
|
|
|
|
|
|
|
def server_pre_encrypt(self, buf): |
|
|
|
|
|
ret = b'' |
|
|
|
|
|
while len(buf) > self.unit_len: |
|
|
|
|
|
ret += self.pack_data(buf[:self.unit_len]) |
|
|
|
|
|
buf = buf[self.unit_len:] |
|
|
|
|
|
ret += self.pack_data(buf) |
|
|
|
|
|
return ret |
|
|
|
|
|
|
|
|
|
|
|
def server_post_decrypt(self, buf): |
|
|
|
|
|
if self.raw_trans: |
|
|
|
|
|
return buf |
|
|
|
|
|
self.recv_buf += buf |
|
|
|
|
|
out_buf = b'' |
|
|
|
|
|
while len(self.recv_buf) > 2: |
|
|
|
|
|
length = struct.unpack('>H', self.recv_buf[:2])[0] |
|
|
|
|
|
if length >= 32768: |
|
|
|
|
|
self.raw_trans = True |
|
|
|
|
|
self.recv_buf = b'' |
|
|
|
|
|
if self.decrypt_packet_num == 0: |
|
|
|
|
|
return None |
|
|
|
|
|
else: |
|
|
|
|
|
raise Exception('server_post_decrype data error') |
|
|
|
|
|
if length > len(self.recv_buf): |
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
out_buf += zlib.decompress(b'\x78\x9c' + self.recv_buf[2:length]) |
|
|
|
|
|
self.recv_buf = self.recv_buf[length:] |
|
|
|
|
|
|
|
|
|
|
|
if out_buf: |
|
|
|
|
|
self.decrypt_packet_num += 1 |
|
|
|
|
|
return out_buf |
|
|
|
|
|
|
|
|