|
@ -30,6 +30,10 @@ import random |
|
|
from shadowsocks import encrypt, eventloop, shell, common |
|
|
from shadowsocks import encrypt, eventloop, shell, common |
|
|
from shadowsocks.common import pre_parse_header, parse_header |
|
|
from shadowsocks.common import pre_parse_header, parse_header |
|
|
|
|
|
|
|
|
|
|
|
# set it 'False' to use both new protocol and the original shadowsocks protocal |
|
|
|
|
|
# set it 'True' to use new protocol ONLY, to avoid GFW detecting |
|
|
|
|
|
FORCE_NEW_PROTOCOL = False |
|
|
|
|
|
|
|
|
# we clear at most TIMEOUTS_CLEAN_SIZE timeouts each time |
|
|
# we clear at most TIMEOUTS_CLEAN_SIZE timeouts each time |
|
|
TIMEOUTS_CLEAN_SIZE = 512 |
|
|
TIMEOUTS_CLEAN_SIZE = 512 |
|
|
|
|
|
|
|
@ -315,7 +319,7 @@ class TCPRelayHandler(object): |
|
|
traceback.print_exc() |
|
|
traceback.print_exc() |
|
|
self.destroy() |
|
|
self.destroy() |
|
|
|
|
|
|
|
|
def _handle_stage_addr(self, data): |
|
|
def _handle_stage_addr(self, ogn_data, data): |
|
|
try: |
|
|
try: |
|
|
if self._is_local: |
|
|
if self._is_local: |
|
|
cmd = common.ord(data[1]) |
|
|
cmd = common.ord(data[1]) |
|
@ -341,13 +345,18 @@ class TCPRelayHandler(object): |
|
|
logging.error('unknown command %d', cmd) |
|
|
logging.error('unknown command %d', cmd) |
|
|
self.destroy() |
|
|
self.destroy() |
|
|
return |
|
|
return |
|
|
if False and ord(data[0]) != 0x88: # force new header |
|
|
|
|
|
|
|
|
before_parse_data = data |
|
|
|
|
|
if FORCE_NEW_PROTOCOL and ord(data[0]) != 0x88: |
|
|
|
|
|
logging.warn("TCP data %s decrypt %s" % (binascii.hexlify(ogn_data), binascii.hexlify(before_parse_data))) |
|
|
raise Exception('can not parse header') |
|
|
raise Exception('can not parse header') |
|
|
data = pre_parse_header(data) |
|
|
data = pre_parse_header(data) |
|
|
if data is None: |
|
|
if data is None: |
|
|
|
|
|
logging.warn("TCP data %s decrypt %s" % (binascii.hexlify(ogn_data), binascii.hexlify(before_parse_data))) |
|
|
raise Exception('can not parse header') |
|
|
raise Exception('can not parse header') |
|
|
header_result = parse_header(data) |
|
|
header_result = parse_header(data) |
|
|
if header_result is None: |
|
|
if header_result is None: |
|
|
|
|
|
logging.warn("TCP data %s decrypt %s" % (binascii.hexlify(ogn_data), binascii.hexlify(before_parse_data))) |
|
|
raise Exception('can not parse header') |
|
|
raise Exception('can not parse header') |
|
|
connecttype, remote_addr, remote_port, header_length = header_result |
|
|
connecttype, remote_addr, remote_port, header_length = header_result |
|
|
logging.info('%s connecting %s:%d from %s:%d' % |
|
|
logging.info('%s connecting %s:%d from %s:%d' % |
|
@ -493,6 +502,7 @@ class TCPRelayHandler(object): |
|
|
if not data: |
|
|
if not data: |
|
|
self.destroy() |
|
|
self.destroy() |
|
|
return |
|
|
return |
|
|
|
|
|
ogn_data = data |
|
|
self._update_activity(len(data)) |
|
|
self._update_activity(len(data)) |
|
|
if not is_local: |
|
|
if not is_local: |
|
|
data = self._encryptor.decrypt(data) |
|
|
data = self._encryptor.decrypt(data) |
|
@ -513,7 +523,7 @@ class TCPRelayHandler(object): |
|
|
self._handle_stage_connecting(data) |
|
|
self._handle_stage_connecting(data) |
|
|
elif (is_local and self._stage == STAGE_ADDR) or \ |
|
|
elif (is_local and self._stage == STAGE_ADDR) or \ |
|
|
(not is_local and self._stage == STAGE_INIT): |
|
|
(not is_local and self._stage == STAGE_INIT): |
|
|
self._handle_stage_addr(data) |
|
|
self._handle_stage_addr(ogn_data, data) |
|
|
|
|
|
|
|
|
def _on_remote_read(self, is_remote_sock): |
|
|
def _on_remote_read(self, is_remote_sock): |
|
|
# handle all remote read events |
|
|
# handle all remote read events |
|
|