Browse Source

add obfs: 'tls_simple'

dev
breakwa11 9 years ago
parent
commit
3e5cbb955e
  1. 165
      shadowsocks/obfsplugin/http_simple.py
  2. 11
      shadowsocks/tcprelay.py

165
shadowsocks/obfsplugin/http_simple.py

@ -32,9 +32,13 @@ def create_http_obfs(method):
def create_http2_obfs(method): def create_http2_obfs(method):
return http2_simple(method) return http2_simple(method)
def create_tls_obfs(method):
return tls_simple(method)
obfs = { obfs = {
'http_simple': (create_http_obfs,), 'http_simple': (create_http_obfs,),
'http2_simple': (create_http2_obfs,), 'http2_simple': (create_http2_obfs,),
'tls_simple': (create_tls_obfs,),
} }
def match_begin(str1, str2): def match_begin(str1, str2):
@ -63,12 +67,12 @@ class http_simple(object):
def server_encode(self, buf): def server_encode(self, buf):
if self.has_sent_header: if self.has_sent_header:
return buf return buf
else:
header = b'HTTP/1.1 200 OK\r\nServer: openresty\r\nDate: ' header = b'HTTP/1.1 200 OK\r\nServer: openresty\r\nDate: '
header += to_bytes(datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S GMT')) header += to_bytes(datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S GMT'))
header += b'\r\nContent-Type: text/plain; charset=utf-8\r\nTransfer-Encoding: chunked\r\nConnection: keep-alive\r\nKeep-Alive: timeout=20\r\nVary: Accept-Encoding\r\nContent-Encoding: gzip\r\n\r\n' header += b'\r\nContent-Type: text/plain; charset=utf-8\r\nTransfer-Encoding: chunked\r\nConnection: keep-alive\r\nKeep-Alive: timeout=20\r\nVary: Accept-Encoding\r\nContent-Encoding: gzip\r\n\r\n'
self.has_sent_header = True self.has_sent_header = True
return header + buf return header + buf
def get_data_from_http_header(self, buf): def get_data_from_http_header(self, buf):
ret_buf = b'' ret_buf = b''
@ -87,36 +91,39 @@ class http_simple(object):
def server_decode(self, buf): def server_decode(self, buf):
if self.has_recv_header: if self.has_recv_header:
return (buf, True, False) return (buf, True, False)
else:
buf = self.recv_buffer + buf buf = self.recv_buffer + buf
if len(buf) > 10: if len(buf) > 10:
if match_begin(buf, b'GET /') or match_begin(buf, b'POST /'): if match_begin(buf, b'GET /') or match_begin(buf, b'POST /'):
pass if len(buf) > 65536:
else: #not http header, run on original protocol
self.has_sent_header = True self.has_sent_header = True
self.has_recv_header = True self.has_recv_header = True
self.recv_buffer = None self.recv_buffer = None
return (buf, True, False) return (buf, True, False)
else: else: #not http header, run on original protocol
self.recv_buffer = buf self.has_sent_header = True
return (b'', True, False) self.has_recv_header = True
self.recv_buffer = None
datas = buf.split(b'\r\n\r\n', 1) return (buf, True, False)
ret_buf = b'' else:
if datas and len(datas) > 1: self.recv_buffer = buf
ret_buf = self.get_data_from_http_header(buf) return (b'', True, False)
ret_buf += datas[1]
if len(ret_buf) >= 15: datas = buf.split(b'\r\n\r\n', 1)
self.has_recv_header = True if datas and len(datas) > 1:
return (ret_buf, True, False) ret_buf = self.get_data_from_http_header(buf)
self.recv_buffer = buf ret_buf += datas[1]
return (b'', True, False) if len(ret_buf) >= 15:
else: self.has_recv_header = True
self.recv_buffer = buf return (ret_buf, True, False)
return (b'', True, False) self.recv_buffer = buf
self.has_sent_header = True return (b'', True, False)
self.has_recv_header = True else:
return (buf, True, False) self.recv_buffer = buf
return (b'', True, False)
self.has_sent_header = True
self.has_recv_header = True
return (buf, True, False)
class http2_simple(object): class http2_simple(object):
def __init__(self, method): def __init__(self, method):
@ -138,43 +145,73 @@ class http2_simple(object):
def server_encode(self, buf): def server_encode(self, buf):
if self.has_sent_header: if self.has_sent_header:
return buf return buf
else:
header = b'HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: h2c\r\n\r\n' header = b'HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: h2c\r\n\r\n'
self.has_sent_header = True self.has_sent_header = True
return header + buf return header + buf
def server_decode(self, buf): def server_decode(self, buf):
if self.has_recv_header: if self.has_recv_header:
return (buf, True, False) return (buf, True, False)
buf = self.recv_buffer + buf
if len(buf) > 10:
if match_begin(buf, b'GET /'):
pass
else: #not http header, run on original protocol
self.has_sent_header = True
self.has_recv_header = True
self.recv_buffer = None
return (buf, True, False)
else: else:
buf = self.recv_buffer + buf self.recv_buffer = buf
if len(buf) > 10: return (b'', True, False)
if match_begin(buf, b'GET /'):
pass datas = buf.split(b'\r\n\r\n', 1)
else: #not http header, run on original protocol if datas and len(datas) > 1 and len(datas[0]) >= 4:
self.has_sent_header = True lines = buf.split(b'\r\n')
if lines and len(lines) >= 4:
if match_begin(lines[4], b'HTTP2-Settings: '):
ret_buf = base64.urlsafe_b64decode(lines[4][16:])
ret_buf += datas[1]
self.has_recv_header = True self.has_recv_header = True
self.recv_buffer = None return (ret_buf, True, False)
return (buf, True, False) self.recv_buffer = buf
else: return (b'', True, False)
self.recv_buffer = buf else:
return (b'', True, False) self.recv_buffer = buf
return (b'', True, False)
datas = buf.split(b'\r\n\r\n', 1) self.has_sent_header = True
if datas and len(datas) > 1 and len(datas[0]) >= 4: self.has_recv_header = True
lines = buf.split(b'\r\n') return (buf, True, False)
if lines and len(lines) >= 4:
if match_begin(lines[4], b'HTTP2-Settings: '): class tls_simple(object):
ret_buf = base64.urlsafe_b64decode(lines[4][16:]) def __init__(self, method):
ret_buf += datas[1] self.method = method
self.has_recv_header = True self.has_sent_header = False
return (ret_buf, True, False) self.has_recv_header = False
self.recv_buffer = buf
return (b'', True, False) def client_encode(self, buf):
else: return buf
self.recv_buffer = buf
return (b'', True, False) def client_decode(self, buf):
self.has_sent_header = True # (buffer_to_recv, is_need_to_encode_and_send_back)
self.has_recv_header = True return (buf, False)
def server_encode(self, buf):
if self.has_sent_header:
return buf
self.has_sent_header = True
# TODO
#server_hello = b''
return b'\x16\x03\x01'
def server_decode(self, buf):
if self.has_recv_header:
return (buf, True, False) return (buf, True, False)
self.has_recv_header = True
if not match_begin(buf, b'\x16\x03\x01'):
return (buf, True, False)
# (buffer_to_recv, is_need_decrypt, is_need_to_encode_and_send_back)
return (b'', False, True)

11
shadowsocks/tcprelay.py

@ -259,10 +259,13 @@ class TCPRelayHandler(object):
obfs_encode = self._obfs.server_encode(data) obfs_encode = self._obfs.server_encode(data)
data = obfs_encode data = obfs_encode
l = len(data) l = len(data)
s = sock.send(data) if l > 0:
if s < l: s = sock.send(data)
data = data[s:] if s < l:
uncomplete = True data = data[s:]
uncomplete = True
else:
return
except (OSError, IOError) as e: except (OSError, IOError) as e:
error_no = eventloop.errno_from_exception(e) error_no = eventloop.errno_from_exception(e)
if error_no in (errno.EAGAIN, errno.EINPROGRESS, if error_no in (errno.EAGAIN, errno.EINPROGRESS,

Loading…
Cancel
Save