Skip to content

Commit 62b4e1f

Browse files
committed
3.8.8
1 parent c1a25c5 commit 62b4e1f

26 files changed

+546
-853
lines changed

shadowsocks-csharp/Controller/HttpPortForwarder.cs

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Net;
44
using System.Net.Sockets;
55
using System.Text;
6+
using System.Threading;
67
using Shadowsocks.Model;
78

89
namespace Shadowsocks.Controller
@@ -32,8 +33,6 @@ class Handler
3233
private Socket _local;
3334
private Socket _remote;
3435
private bool _closed = false;
35-
private bool _localShutdown = false;
36-
private bool _remoteShutdown = false;
3736
private Configuration _config;
3837
HttpPraser httpProxyState;
3938
public const int RecvSize = 16384;
@@ -44,11 +43,11 @@ class Handler
4443

4544
public void Start(Configuration config, byte[] firstPacket, int length, Socket socket, int targetPort)
4645
{
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;
5251
if ((_config.authUser ?? "").Length == 0 || Util.Utils.isMatchSubNet(((IPEndPoint)this._local.RemoteEndPoint).Address, "127.0.0.0/8"))
5352
{
5453
Connect();
@@ -119,13 +118,13 @@ private void HttpHandshakeRecv(IAsyncResult ar)
119118
else
120119
{
121120
Console.WriteLine("failed to recv data in HttpHandshakeRecv");
122-
this.Close();
121+
Close();
123122
}
124123
}
125124
catch (Exception e)
126125
{
127126
Logging.LogUsefulException(e);
128-
this.Close();
127+
Close();
129128
}
130129
}
131130

@@ -144,7 +143,7 @@ private void HttpHandshakeAuthEndSend(IAsyncResult ar)
144143
catch (Exception e)
145144
{
146145
Logging.LogUsefulException(e);
147-
this.Close();
146+
Close();
148147
}
149148
}
150149

@@ -158,7 +157,7 @@ private void StartConnect(IAsyncResult ar)
158157
catch (Exception e)
159158
{
160159
Logging.LogUsefulException(e);
161-
this.Close();
160+
Close();
162161
}
163162

164163
}
@@ -172,7 +171,6 @@ private void Connect()
172171
bool parsed = IPAddress.TryParse("127.0.0.1", out ipAddress);
173172
IPEndPoint remoteEP = new IPEndPoint(ipAddress, _targetPort);
174173

175-
176174
_remote = new Socket(ipAddress.AddressFamily,
177175
SocketType.Stream, ProtocolType.Tcp);
178176
_remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
@@ -184,7 +182,7 @@ private void Connect()
184182
catch (Exception e)
185183
{
186184
Logging.LogUsefulException(e);
187-
this.Close();
185+
Close();
188186
}
189187
}
190188

@@ -202,7 +200,7 @@ private void ConnectCallback(IAsyncResult ar)
202200
catch (Exception e)
203201
{
204202
Logging.LogUsefulException(e);
205-
this.Close();
203+
Close();
206204
}
207205
}
208206

@@ -219,7 +217,7 @@ private void HandshakeReceive()
219217
catch (Exception e)
220218
{
221219
Logging.LogUsefulException(e);
222-
this.Close();
220+
Close();
223221
}
224222
}
225223

@@ -241,7 +239,7 @@ private void StartPipe(IAsyncResult ar)
241239
catch (Exception e)
242240
{
243241
Logging.LogUsefulException(e);
244-
this.Close();
242+
Close();
245243
}
246244
}
247245

@@ -261,15 +259,13 @@ private void PipeRemoteReceiveCallback(IAsyncResult ar)
261259
}
262260
else
263261
{
264-
_local.Shutdown(SocketShutdown.Send);
265-
_localShutdown = true;
266-
CheckClose();
262+
Close();
267263
}
268264
}
269265
catch (Exception e)
270266
{
271267
Logging.LogUsefulException(e);
272-
this.Close();
268+
Close();
273269
}
274270
}
275271

@@ -289,15 +285,13 @@ private void PipeConnectionReceiveCallback(IAsyncResult ar)
289285
}
290286
else
291287
{
292-
_remote.Shutdown(SocketShutdown.Send);
293-
_remoteShutdown = true;
294-
CheckClose();
288+
Close();
295289
}
296290
}
297291
catch (Exception e)
298292
{
299293
Logging.LogUsefulException(e);
300-
this.Close();
294+
Close();
301295
}
302296
}
303297

@@ -316,7 +310,7 @@ private void PipeRemoteSendCallback(IAsyncResult ar)
316310
catch (Exception e)
317311
{
318312
Logging.LogUsefulException(e);
319-
this.Close();
313+
Close();
320314
}
321315
}
322316

@@ -335,15 +329,7 @@ private void PipeConnectionSendCallback(IAsyncResult ar)
335329
catch (Exception e)
336330
{
337331
Logging.LogUsefulException(e);
338-
this.Close();
339-
}
340-
}
341-
342-
private void CheckClose()
343-
{
344-
if (_localShutdown && _remoteShutdown)
345-
{
346-
this.Close();
332+
Close();
347333
}
348334
}
349335

@@ -357,6 +343,7 @@ public void Close()
357343
}
358344
_closed = true;
359345
}
346+
Thread.Sleep(100);
360347
if (_local != null)
361348
{
362349
try

shadowsocks-csharp/Controller/Local.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,8 @@ private void BeginConnect(IPAddress ipAddress, int serverPort)
533533
remoteUDP = new ProxySocket(ipAddress.AddressFamily,
534534
SocketType.Dgram, ProtocolType.Udp);
535535
remoteUDP.GetSocket().Bind(new IPEndPoint(ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0));
536+
537+
remoteUDP.SetEncryptor(EncryptorFactory.GetEncryptor(server.method, server.password));
536538
remoteUDP.SetProtocol(ObfsFactory.GetObfs(server.protocol));
537539
remoteUDP.SetObfs(ObfsFactory.GetObfs(server.obfs));
538540
}
@@ -699,6 +701,18 @@ public void Close()
699701
connection = null;
700702
connectionUDP = null;
701703
}
704+
705+
getCurrentServer = null;
706+
keepCurrentServer = null;
707+
708+
detector = null;
709+
speedTester = null;
710+
random = null;
711+
remoteUDPRecvBuffer = null;
712+
713+
server = null;
714+
select_server = null;
715+
cfg = null;
702716
}
703717
catch (Exception e)
704718
{
@@ -714,6 +728,11 @@ private bool ConnectProxyServer(string strRemoteHost, int iRemotePort)
714728
remoteUDPEndPoint = remote.GetProxyUdpEndPoint();
715729
remote.SetTcpServer(server.server, server.server_port);
716730
remote.SetUdpServer(server.server, server.server_udp_port == 0 ? server.server_port : server.server_udp_port);
731+
if (remoteUDP != null)
732+
{
733+
remoteUDP.GoS5Proxy = true;
734+
remoteUDP.SetUdpServer(server.server, server.server_udp_port == 0 ? server.server_port : server.server_udp_port);
735+
}
717736
return ret;
718737
}
719738
else if (cfg.proxyType == 1)
@@ -1336,7 +1355,7 @@ private void PipeRemoteUDPReceiveCallback(IAsyncResult ar)
13361355

13371356
int bytesRead = endRemoteUDPRecv(ar, ref tempEP);
13381357

1339-
if (remote.IsClose)
1358+
if (remoteUDP.IsClose)
13401359
{
13411360
final_close = true;
13421361
}
@@ -1361,7 +1380,7 @@ private void PipeRemoteUDPReceiveCallback(IAsyncResult ar)
13611380
}
13621381
else //if (bytesRead > 0)
13631382
{
1364-
ConnectionSend(remote.GetAsyncResultBuffer(ar), bytesRead);
1383+
ConnectionSend(remoteUDP.GetAsyncResultBuffer(ar), bytesRead);
13651384

13661385
speedTester.AddRecvSize(bytesRead);
13671386
server.ServerSpeedLog().AddDownloadRawBytes(bytesRead);

0 commit comments

Comments
 (0)