@@ -74,14 +74,22 @@ def server_activate(self):
74
74
logging .error ('warning: fast open is not available' )
75
75
self .socket .listen (self .request_queue_size )
76
76
77
+ def get_request (self ):
78
+ connection = self .socket .accept ()
79
+ connection [0 ].settimeout (config_timeout )
80
+ return connection
81
+
77
82
78
83
class Socks5Server (SocketServer .StreamRequestHandler ):
79
84
def handle_tcp (self , sock , remote ):
80
85
try :
81
86
fdset = [sock , remote ]
82
87
while True :
83
88
should_break = False
84
- r , w , e = select .select (fdset , [], [])
89
+ r , w , e = select .select (fdset , [], [], config_timeout )
90
+ if not r :
91
+ logging .warn ('read time out' )
92
+ break
85
93
if sock in r :
86
94
data = self .decrypt (sock .recv (4096 ))
87
95
if len (data ) <= 0 :
@@ -147,7 +155,9 @@ def handle(self):
147
155
port = struct .unpack ('>H' , self .decrypt (self .rfile .read (2 )))
148
156
try :
149
157
logging .info ('connecting %s:%d' % (addr , port [0 ]))
150
- remote = socket .create_connection ((addr , port [0 ]))
158
+ remote = socket .create_connection ((addr , port [0 ]),
159
+ timeout = config_timeout )
160
+ remote .settimeout (config_timeout )
151
161
remote .setsockopt (socket .IPPROTO_TCP , socket .TCP_NODELAY , 1 )
152
162
except socket .error , e :
153
163
# Connection refused
@@ -159,7 +169,8 @@ def handle(self):
159
169
160
170
161
171
def main ():
162
- global config_server , config_server_port , config_method , config_fast_open
172
+ global config_server , config_server_port , config_method , config_fast_open , \
173
+ config_timeout
163
174
164
175
logging .basicConfig (level = logging .DEBUG ,
165
176
format = '%(asctime)s %(levelname)-8s %(message)s' ,
@@ -176,7 +187,7 @@ def main():
176
187
177
188
config_path = utils .find_config ()
178
189
try :
179
- optlist , args = getopt .getopt (sys .argv [1 :], 's:p:k:m:c:' ,
190
+ optlist , args = getopt .getopt (sys .argv [1 :], 's:p:k:m:c:t: ' ,
180
191
['fast-open' , 'workers:' ])
181
192
for key , value in optlist :
182
193
if key == '-c' :
@@ -194,7 +205,7 @@ def main():
194
205
else :
195
206
config = {}
196
207
197
- optlist , args = getopt .getopt (sys .argv [1 :], 's:p:k:m:c:' ,
208
+ optlist , args = getopt .getopt (sys .argv [1 :], 's:p:k:m:c:t: ' ,
198
209
['fast-open' , 'workers=' ])
199
210
for key , value in optlist :
200
211
if key == '-p' :
@@ -205,6 +216,8 @@ def main():
205
216
config ['server' ] = value
206
217
elif key == '-m' :
207
218
config ['method' ] = value
219
+ elif key == '-t' :
220
+ config ['timeout' ] = value
208
221
elif key == '--fast-open' :
209
222
config ['fast_open' ] = True
210
223
elif key == '--workers' :
@@ -218,7 +231,7 @@ def main():
218
231
config_key = config ['password' ]
219
232
config_method = config .get ('method' , None )
220
233
config_port_password = config .get ('port_password' , None )
221
- config_timeout = config .get ('timeout' , 600 )
234
+ config_timeout = int ( config .get ('timeout' , 300 ) )
222
235
config_fast_open = config .get ('fast_open' , False )
223
236
config_workers = config .get ('workers' , 1 )
224
237
0 commit comments