Browse Source

add obfs: 'tls_simple'

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

49
shadowsocks/obfsplugin/http_simple.py

@ -32,9 +32,13 @@ def create_http_obfs(method):
def create_http2_obfs(method):
return http2_simple(method)
def create_tls_obfs(method):
return tls_simple(method)
obfs = {
'http_simple': (create_http_obfs,),
'http2_simple': (create_http2_obfs,),
'tls_simple': (create_tls_obfs,),
}
def match_begin(str1, str2):
@ -63,7 +67,7 @@ class http_simple(object):
def server_encode(self, buf):
if self.has_sent_header:
return buf
else:
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 += 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'
@ -87,11 +91,15 @@ class http_simple(object):
def server_decode(self, buf):
if self.has_recv_header:
return (buf, True, False)
else:
buf = self.recv_buffer + buf
if len(buf) > 10:
if match_begin(buf, b'GET /') or match_begin(buf, b'POST /'):
pass
if len(buf) > 65536:
self.has_sent_header = True
self.has_recv_header = True
self.recv_buffer = None
return (buf, True, False)
else: #not http header, run on original protocol
self.has_sent_header = True
self.has_recv_header = True
@ -102,7 +110,6 @@ class http_simple(object):
return (b'', True, False)
datas = buf.split(b'\r\n\r\n', 1)
ret_buf = b''
if datas and len(datas) > 1:
ret_buf = self.get_data_from_http_header(buf)
ret_buf += datas[1]
@ -138,7 +145,7 @@ class http2_simple(object):
def server_encode(self, buf):
if self.has_sent_header:
return buf
else:
header = b'HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: h2c\r\n\r\n'
self.has_sent_header = True
return header + buf
@ -146,7 +153,7 @@ class http2_simple(object):
def server_decode(self, buf):
if self.has_recv_header:
return (buf, True, False)
else:
buf = self.recv_buffer + buf
if len(buf) > 10:
if match_begin(buf, b'GET /'):
@ -178,3 +185,33 @@ class http2_simple(object):
self.has_recv_header = True
return (buf, True, False)
class tls_simple(object):
def __init__(self, method):
self.method = method
self.has_sent_header = False
self.has_recv_header = False
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 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)
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)

3
shadowsocks/tcprelay.py

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

Loading…
Cancel
Save