@@ -57,59 +57,42 @@ def match_begin(str1, str2):
5757
5858class obfs_verify_data (object ):
5959 def __init__ (self ):
60- self . sub_obfs = None
60+ pass
6161
6262class verify_base (plain .plain ):
6363 def __init__ (self , method ):
6464 super (verify_base , self ).__init__ (method )
6565 self .method = method
66- self .sub_obfs = None
6766
6867 def init_data (self ):
6968 return obfs_verify_data ()
7069
7170 def set_server_info (self , server_info ):
72- try :
73- if server_info .param :
74- sub_param = ''
75- param_list = server_info .param .split (',' , 1 )
76- if len (param_list ) > 1 :
77- self .sub_obfs = shadowsocks .obfs .obfs (param_list [0 ])
78- sub_param = param_list [1 ]
79- else :
80- self .sub_obfs = shadowsocks .obfs .obfs (server_info .param )
81- if server_info .data .sub_obfs is None :
82- server_info .data .sub_obfs = self .sub_obfs .init_data ()
83- _server_info = shadowsocks .obfs .server_info (server_info .data .sub_obfs )
84- _server_info .host = server_info .host
85- _server_info .port = server_info .port
86- _server_info .tcp_mss = server_info .tcp_mss
87- _server_info .param = sub_param
88- self .sub_obfs .set_server_info (_server_info )
89- except Exception as e :
90- shadowsocks .shell .print_exception (e )
9171 self .server_info = server_info
9272
9373 def client_encode (self , buf ):
94- if self .sub_obfs is not None :
95- return self .sub_obfs .client_encode (buf )
9674 return buf
9775
9876 def client_decode (self , buf ):
99- if self .sub_obfs is not None :
100- return self .sub_obfs .client_decode (buf )
10177 return (buf , False )
10278
10379 def server_encode (self , buf ):
104- if self .sub_obfs is not None :
105- return self .sub_obfs .server_encode (buf )
10680 return buf
10781
10882 def server_decode (self , buf ):
109- if self .sub_obfs is not None :
110- return self .sub_obfs .server_decode (buf )
11183 return (buf , True , False )
11284
85+ def get_head_size (self , buf , def_value ):
86+ if len (buf ) < 2 :
87+ return def_value
88+ if ord (buf [0 ]) == 1 :
89+ return 7
90+ if ord (buf [0 ]) == 4 :
91+ return 19
92+ if ord (buf [0 ]) == 3 :
93+ return 4 + ord (buf [1 ])
94+ return def_value
95+
11396class verify_simple (verify_base ):
11497 def __init__ (self , method ):
11598 super (verify_simple , self ).__init__ (method )
@@ -336,28 +319,28 @@ def insert(self, connection_id):
336319
337320class obfs_auth_data (object ):
338321 def __init__ (self ):
339- self .sub_obfs = None
340322 self .client_id = {}
341323 self .startup_time = int (time .time () - 30 ) & 0xFFFFFFFF
342324 self .local_client_id = b''
343325 self .connection_id = 0
326+ self .max_client = 16 # max active client count
327+ self .max_buffer = max (self .max_client , 256 ) # max client id buffer size
344328
345329 def update (self , client_id , connection_id ):
346330 if client_id in self .client_id :
347331 self .client_id [client_id ].update ()
348332
349333 def insert (self , client_id , connection_id ):
350- max_client = 16
351334 if client_id not in self .client_id or not self .client_id [client_id ].enable :
352335 active = 0
353336 for c_id in self .client_id :
354337 if self .client_id [c_id ].is_active ():
355338 active += 1
356- if active >= max_client :
339+ if active >= self . max_client :
357340 logging .warn ('auth_simple: max active clients exceeded' )
358341 return False
359342
360- if len (self .client_id ) < max_client :
343+ if len (self .client_id ) < self . max_client :
361344 if client_id not in self .client_id :
362345 self .client_id [client_id ] = client_queue (connection_id )
363346 else :
@@ -367,7 +350,7 @@ def insert(self, client_id, connection_id):
367350 random .shuffle (keys )
368351 for c_id in keys :
369352 if not self .client_id [c_id ].is_active () and self .client_id [c_id ].enable :
370- if len (self .client_id ) >= 256 :
353+ if len (self .client_id ) >= self . max_buffer :
371354 del self .client_id [c_id ]
372355 else :
373356 self .client_id [c_id ].enable = False
@@ -392,6 +375,7 @@ def __init__(self, method):
392375 self .has_recv_header = False
393376 self .client_id = 0
394377 self .connection_id = 0
378+ self .max_time_dif = 60 * 5 # time dif (second) setting
395379
396380 def init_data (self ):
397381 return obfs_auth_data ()
@@ -422,7 +406,8 @@ def auth_data(self):
422406 def client_pre_encrypt (self , buf ):
423407 ret = b''
424408 if not self .has_sent_header :
425- datalen = min (len (buf ), common .ord (os .urandom (1 )[0 ]) % 32 + 4 )
409+ head_size = self .get_head_size (buf , 30 )
410+ datalen = min (len (buf ), random .randint (0 , 31 ) + head_size )
426411 ret += self .pack_data (self .auth_data () + buf [:datalen ])
427412 buf = buf [datalen :]
428413 self .has_sent_header = True
@@ -512,7 +497,8 @@ def server_post_decrypt(self, buf):
512497 client_id = struct .unpack ('<I' , out_buf [4 :8 ])[0 ]
513498 connection_id = struct .unpack ('<I' , out_buf [8 :12 ])[0 ]
514499 time_dif = common .int32 ((int (time .time ()) & 0xffffffff ) - utc_time )
515- if time_dif < 60 * - 3 or time_dif > 60 * 3 or common .int32 (utc_time - self .server_info .data .startup_time ) < 0 :
500+ if time_dif < - self .max_time_dif or time_dif > self .max_time_dif \
501+ or common .int32 (utc_time - self .server_info .data .startup_time ) < 0 :
516502 self .raw_trans = True
517503 self .recv_buf = b''
518504 logging .info ('auth_simple: wrong timestamp, time_dif %d, data %s' % (time_dif , binascii .hexlify (out_buf ),))
0 commit comments