3
3
using System . Net ;
4
4
using System . Net . Sockets ;
5
5
using System . Text ;
6
+ using System . Threading ;
6
7
using Shadowsocks . Model ;
7
8
8
9
namespace Shadowsocks . Controller
@@ -32,8 +33,6 @@ class Handler
32
33
private Socket _local ;
33
34
private Socket _remote ;
34
35
private bool _closed = false ;
35
- private bool _localShutdown = false ;
36
- private bool _remoteShutdown = false ;
37
36
private Configuration _config ;
38
37
HttpPraser httpProxyState ;
39
38
public const int RecvSize = 16384 ;
@@ -44,11 +43,11 @@ class Handler
44
43
45
44
public void Start ( Configuration config , byte [ ] firstPacket , int length , Socket socket , int targetPort )
46
45
{
47
- this . _firstPacket = firstPacket ;
48
- this . _firstPacketLength = length ;
49
- this . _local = socket ;
50
- this . _targetPort = targetPort ;
51
- this . _config = config ;
46
+ _firstPacket = firstPacket ;
47
+ _firstPacketLength = length ;
48
+ _local = socket ;
49
+ _targetPort = targetPort ;
50
+ _config = config ;
52
51
if ( ( _config . authUser ?? "" ) . Length == 0 || Util . Utils . isMatchSubNet ( ( ( IPEndPoint ) this . _local . RemoteEndPoint ) . Address , "127.0.0.0/8" ) )
53
52
{
54
53
Connect ( ) ;
@@ -119,13 +118,13 @@ private void HttpHandshakeRecv(IAsyncResult ar)
119
118
else
120
119
{
121
120
Console . WriteLine ( "failed to recv data in HttpHandshakeRecv" ) ;
122
- this . Close ( ) ;
121
+ Close ( ) ;
123
122
}
124
123
}
125
124
catch ( Exception e )
126
125
{
127
126
Logging . LogUsefulException ( e ) ;
128
- this . Close ( ) ;
127
+ Close ( ) ;
129
128
}
130
129
}
131
130
@@ -144,7 +143,7 @@ private void HttpHandshakeAuthEndSend(IAsyncResult ar)
144
143
catch ( Exception e )
145
144
{
146
145
Logging . LogUsefulException ( e ) ;
147
- this . Close ( ) ;
146
+ Close ( ) ;
148
147
}
149
148
}
150
149
@@ -158,7 +157,7 @@ private void StartConnect(IAsyncResult ar)
158
157
catch ( Exception e )
159
158
{
160
159
Logging . LogUsefulException ( e ) ;
161
- this . Close ( ) ;
160
+ Close ( ) ;
162
161
}
163
162
164
163
}
@@ -172,7 +171,6 @@ private void Connect()
172
171
bool parsed = IPAddress . TryParse ( "127.0.0.1" , out ipAddress ) ;
173
172
IPEndPoint remoteEP = new IPEndPoint ( ipAddress , _targetPort ) ;
174
173
175
-
176
174
_remote = new Socket ( ipAddress . AddressFamily ,
177
175
SocketType . Stream , ProtocolType . Tcp ) ;
178
176
_remote . SetSocketOption ( SocketOptionLevel . Tcp , SocketOptionName . NoDelay , true ) ;
@@ -184,7 +182,7 @@ private void Connect()
184
182
catch ( Exception e )
185
183
{
186
184
Logging . LogUsefulException ( e ) ;
187
- this . Close ( ) ;
185
+ Close ( ) ;
188
186
}
189
187
}
190
188
@@ -202,7 +200,7 @@ private void ConnectCallback(IAsyncResult ar)
202
200
catch ( Exception e )
203
201
{
204
202
Logging . LogUsefulException ( e ) ;
205
- this . Close ( ) ;
203
+ Close ( ) ;
206
204
}
207
205
}
208
206
@@ -219,7 +217,7 @@ private void HandshakeReceive()
219
217
catch ( Exception e )
220
218
{
221
219
Logging . LogUsefulException ( e ) ;
222
- this . Close ( ) ;
220
+ Close ( ) ;
223
221
}
224
222
}
225
223
@@ -241,7 +239,7 @@ private void StartPipe(IAsyncResult ar)
241
239
catch ( Exception e )
242
240
{
243
241
Logging . LogUsefulException ( e ) ;
244
- this . Close ( ) ;
242
+ Close ( ) ;
245
243
}
246
244
}
247
245
@@ -261,15 +259,13 @@ private void PipeRemoteReceiveCallback(IAsyncResult ar)
261
259
}
262
260
else
263
261
{
264
- _local . Shutdown ( SocketShutdown . Send ) ;
265
- _localShutdown = true ;
266
- CheckClose ( ) ;
262
+ Close ( ) ;
267
263
}
268
264
}
269
265
catch ( Exception e )
270
266
{
271
267
Logging . LogUsefulException ( e ) ;
272
- this . Close ( ) ;
268
+ Close ( ) ;
273
269
}
274
270
}
275
271
@@ -289,15 +285,13 @@ private void PipeConnectionReceiveCallback(IAsyncResult ar)
289
285
}
290
286
else
291
287
{
292
- _remote . Shutdown ( SocketShutdown . Send ) ;
293
- _remoteShutdown = true ;
294
- CheckClose ( ) ;
288
+ Close ( ) ;
295
289
}
296
290
}
297
291
catch ( Exception e )
298
292
{
299
293
Logging . LogUsefulException ( e ) ;
300
- this . Close ( ) ;
294
+ Close ( ) ;
301
295
}
302
296
}
303
297
@@ -316,7 +310,7 @@ private void PipeRemoteSendCallback(IAsyncResult ar)
316
310
catch ( Exception e )
317
311
{
318
312
Logging . LogUsefulException ( e ) ;
319
- this . Close ( ) ;
313
+ Close ( ) ;
320
314
}
321
315
}
322
316
@@ -335,15 +329,7 @@ private void PipeConnectionSendCallback(IAsyncResult ar)
335
329
catch ( Exception e )
336
330
{
337
331
Logging . LogUsefulException ( e ) ;
338
- this . Close ( ) ;
339
- }
340
- }
341
-
342
- private void CheckClose ( )
343
- {
344
- if ( _localShutdown && _remoteShutdown )
345
- {
346
- this . Close ( ) ;
332
+ Close ( ) ;
347
333
}
348
334
}
349
335
@@ -357,6 +343,7 @@ public void Close()
357
343
}
358
344
_closed = true ;
359
345
}
346
+ Thread . Sleep ( 100 ) ;
360
347
if ( _local != null )
361
348
{
362
349
try
0 commit comments