3030from shadowsocks import encrypt , eventloop , shell , common
3131from shadowsocks .common import pre_parse_header , parse_header
3232
33+ # set it 'False' to use both new protocol and the original shadowsocks protocal
34+ # set it 'True' to use new protocol ONLY, to avoid GFW detecting
35+ FORCE_NEW_PROTOCOL = False
36+
3337# we clear at most TIMEOUTS_CLEAN_SIZE timeouts each time
3438TIMEOUTS_CLEAN_SIZE = 512
3539
@@ -315,7 +319,7 @@ def _handle_stage_connecting(self, data):
315319 traceback .print_exc ()
316320 self .destroy ()
317321
318- def _handle_stage_addr (self , data ):
322+ def _handle_stage_addr (self , ogn_data , data ):
319323 try :
320324 if self ._is_local :
321325 cmd = common .ord (data [1 ])
@@ -341,13 +345,18 @@ def _handle_stage_addr(self, data):
341345 logging .error ('unknown command %d' , cmd )
342346 self .destroy ()
343347 return
344- if False and ord (data [0 ]) != 0x88 : # force new header
348+
349+ before_parse_data = data
350+ if FORCE_NEW_PROTOCOL and ord (data [0 ]) != 0x88 :
351+ logging .warn ("TCP data %s decrypt %s" % (binascii .hexlify (ogn_data ), binascii .hexlify (before_parse_data )))
345352 raise Exception ('can not parse header' )
346353 data = pre_parse_header (data )
347354 if data is None :
355+ logging .warn ("TCP data %s decrypt %s" % (binascii .hexlify (ogn_data ), binascii .hexlify (before_parse_data )))
348356 raise Exception ('can not parse header' )
349357 header_result = parse_header (data )
350358 if header_result is None :
359+ logging .warn ("TCP data %s decrypt %s" % (binascii .hexlify (ogn_data ), binascii .hexlify (before_parse_data )))
351360 raise Exception ('can not parse header' )
352361 connecttype , remote_addr , remote_port , header_length = header_result
353362 logging .info ('%s connecting %s:%d from %s:%d' %
@@ -493,6 +502,7 @@ def _on_local_read(self):
493502 if not data :
494503 self .destroy ()
495504 return
505+ ogn_data = data
496506 self ._update_activity (len (data ))
497507 if not is_local :
498508 data = self ._encryptor .decrypt (data )
@@ -513,7 +523,7 @@ def _on_local_read(self):
513523 self ._handle_stage_connecting (data )
514524 elif (is_local and self ._stage == STAGE_ADDR ) or \
515525 (not is_local and self ._stage == STAGE_INIT ):
516- self ._handle_stage_addr (data )
526+ self ._handle_stage_addr (ogn_data , data )
517527
518528 def _on_remote_read (self , is_remote_sock ):
519529 # handle all remote read events
0 commit comments