- use null conditional access in case some connections been cancelled unexpectedly and may cause null reference exception - Refine error message - use conditional operator to simplify some code Signed-off-by: Syrone Wong <wong.syrone@gmail.com>tags/3.2
@@ -24,20 +24,23 @@ namespace Shadowsocks.Controller | |||
public static void UncompressFile(string fileName, byte[] content) | |||
{ | |||
// Because the uncompressed size of the file is unknown, | |||
// we are using an arbitrary buffer size. | |||
byte[] buffer = new byte[4096]; | |||
int n; | |||
try { | |||
// Because the uncompressed size of the file is unknown, | |||
// we are using an arbitrary buffer size. | |||
byte[] buffer = new byte[4096]; | |||
int n; | |||
using(var fs = File.Create(fileName)) | |||
using (var input = new GZipStream( | |||
new MemoryStream(content), | |||
CompressionMode.Decompress, false)) | |||
{ | |||
while ((n = input.Read(buffer, 0, buffer.Length)) > 0) | |||
{ | |||
fs.Write(buffer, 0, n); | |||
} | |||
using(var fs = File.Create(fileName)) | |||
using (var input = new GZipStream(new MemoryStream(content), | |||
CompressionMode.Decompress, false)) | |||
{ | |||
while ((n = input.Read(buffer, 0, buffer.Length)) > 0) | |||
{ | |||
fs.Write(buffer, 0, n); | |||
} | |||
} | |||
} catch ( Exception e ) { | |||
Logging.LogUsefulException( e ); | |||
} | |||
} | |||
@@ -63,14 +63,9 @@ namespace Shadowsocks.Controller | |||
_tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); | |||
_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); | |||
IPEndPoint localEndPoint = null; | |||
if (_shareOverLAN) | |||
{ | |||
localEndPoint = new IPEndPoint(IPAddress.Any, _config.localPort); | |||
} | |||
else | |||
{ | |||
localEndPoint = new IPEndPoint(IPAddress.Loopback, _config.localPort); | |||
} | |||
localEndPoint = _shareOverLAN | |||
? new IPEndPoint(IPAddress.Any, _config.localPort) | |||
: new IPEndPoint(IPAddress.Loopback, _config.localPort); | |||
// Bind the socket to the local endpoint and listen for incoming connections. | |||
_tcpSocket.Bind(localEndPoint); | |||
@@ -207,7 +207,7 @@ namespace Shadowsocks.Controller | |||
response = new byte[] { 0, 91 }; | |||
Logging.Error("socks 5 protocol error"); | |||
} | |||
connection.BeginSend(response, 0, response.Length, SocketFlags.None, new AsyncCallback(HandshakeSendCallback), null); | |||
connection?.BeginSend(response, 0, response.Length, SocketFlags.None, new AsyncCallback(HandshakeSendCallback), null); | |||
} | |||
else | |||
Close(); | |||
@@ -328,7 +328,7 @@ namespace Shadowsocks.Controller | |||
{ | |||
try | |||
{ | |||
connection.EndSend(ar); | |||
connection?.EndSend(ar); | |||
StartConnect(); | |||
} | |||
catch (Exception e) | |||
@@ -420,7 +420,7 @@ namespace Shadowsocks.Controller | |||
var ep = ((ProxyTimer)sender).DestEndPoint; | |||
Logging.Info($"Proxy {ep} timed out"); | |||
remote.Close(); | |||
remote?.Close(); | |||
RetryConnect(); | |||
} | |||
@@ -485,7 +485,7 @@ namespace Shadowsocks.Controller | |||
IStrategy strategy = controller.GetCurrentStrategy(); | |||
strategy?.SetFailure(server); | |||
Logging.Info($"{server.FriendlyName()} timed out"); | |||
remote.Close(); | |||
remote?.Close(); | |||
RetryConnect(); | |||
} | |||
@@ -513,7 +513,7 @@ namespace Shadowsocks.Controller | |||
timer.Dispose(); | |||
// Complete the connection. | |||
remote.EndConnectDest(ar); | |||
remote?.EndConnectDest(ar); | |||
_destConnected = true; | |||
@@ -550,8 +550,8 @@ namespace Shadowsocks.Controller | |||
try | |||
{ | |||
_startReceivingTime = DateTime.Now; | |||
remote.BeginReceive(_remoteRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(PipeRemoteReceiveCallback), null); | |||
connection.BeginReceive(_connetionRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(PipeConnectionReceiveCallback), null); | |||
remote?.BeginReceive(_remoteRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(PipeRemoteReceiveCallback), null); | |||
connection?.BeginReceive(_connetionRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(PipeConnectionReceiveCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
@@ -565,6 +565,7 @@ namespace Shadowsocks.Controller | |||
if (_closed) return; | |||
try | |||
{ | |||
if ( remote == null ) return; | |||
int bytesRead = remote.EndReceive(ar); | |||
_totalRead += bytesRead; | |||
_tcprelay.UpdateInboundCounter(server, bytesRead); | |||
@@ -600,6 +601,7 @@ namespace Shadowsocks.Controller | |||
if (_closed) return; | |||
try | |||
{ | |||
if(connection == null) return; | |||
int bytesRead = connection.EndReceive(ar); | |||
_totalWrite += bytesRead; | |||
if (bytesRead > 0) | |||
@@ -664,8 +666,8 @@ namespace Shadowsocks.Controller | |||
if (_closed) return; | |||
try | |||
{ | |||
remote.EndSend(ar); | |||
connection.BeginReceive(_connetionRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(PipeConnectionReceiveCallback), null); | |||
remote?.EndSend(ar); | |||
connection?.BeginReceive(_connetionRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(PipeConnectionReceiveCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
@@ -679,8 +681,8 @@ namespace Shadowsocks.Controller | |||
if (_closed) return; | |||
try | |||
{ | |||
connection.EndSend(ar); | |||
remote.BeginReceive(_remoteRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(PipeRemoteReceiveCallback), null); | |||
connection?.EndSend(ar); | |||
remote?.BeginReceive(_remoteRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(PipeRemoteReceiveCallback), null); | |||
} | |||
catch (Exception e) | |||
{ | |||
@@ -85,20 +85,21 @@ namespace Shadowsocks.Controller | |||
int outlen; | |||
encryptor.Encrypt(dataIn, length - 3, dataOut, out outlen); | |||
Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay"); | |||
_remote.SendTo(dataOut, outlen, SocketFlags.None, _remoteEndPoint); | |||
_remote?.SendTo(dataOut, outlen, SocketFlags.None, _remoteEndPoint); | |||
} | |||
public void Receive() | |||
{ | |||
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); | |||
Logging.Debug($"++++++Receive Server Port, size:" + _buffer.Length); | |||
_remote.BeginReceiveFrom(_buffer, 0, _buffer.Length, 0, ref remoteEndPoint, new AsyncCallback(RecvFromCallback), null); | |||
_remote?.BeginReceiveFrom(_buffer, 0, _buffer.Length, 0, ref remoteEndPoint, new AsyncCallback(RecvFromCallback), null); | |||
} | |||
public void RecvFromCallback(IAsyncResult ar) | |||
{ | |||
try | |||
{ | |||
if (_remote == null) return; | |||
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); | |||
int bytesRead = _remote.EndReceiveFrom(ar, ref remoteEndPoint); | |||
@@ -112,7 +113,7 @@ namespace Shadowsocks.Controller | |||
Array.Copy(dataOut, 0, sendBuf, 3, outlen); | |||
Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay"); | |||
_local.SendTo(sendBuf, outlen + 3, 0, _localEndPoint); | |||
_local?.SendTo(sendBuf, outlen + 3, 0, _localEndPoint); | |||
Receive(); | |||
} | |||
catch (ObjectDisposedException) | |||
@@ -132,7 +133,7 @@ namespace Shadowsocks.Controller | |||
{ | |||
try | |||
{ | |||
_remote.Close(); | |||
_remote?.Close(); | |||
} | |||
catch (ObjectDisposedException) | |||
{ | |||
@@ -112,5 +112,5 @@ Failed to decode QRCode=无法解析二维码 | |||
Failed to update registry=无法修改注册表 | |||
System Proxy On: =系统代理已启用: | |||
Running: Port {0}=正在运行:端口 {0} | |||
Unexpect error, shadowsocks will be exit. Please report to=非预期错误,Shadowsocks将退出。请提交此错误到 | |||
Unexpected error, shadowsocks will exit. Please report to=非预期错误,Shadowsocks将退出。请提交此错误到 | |||
@@ -61,7 +61,7 @@ namespace Shadowsocks | |||
if (Interlocked.Increment(ref exited) == 1) | |||
{ | |||
Logging.Error(e.ExceptionObject?.ToString()); | |||
MessageBox.Show(I18N.GetString("Unexpect error, shadowsocks will be exit. Please report to") + | |||
MessageBox.Show(I18N.GetString("Unexpected error, shadowsocks will exit. Please report to") + | |||
" https://github.com/shadowsocks/shadowsocks-windows/issues " + | |||
Environment.NewLine + (e.ExceptionObject?.ToString()), | |||
"Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | |||
@@ -58,13 +58,13 @@ namespace Shadowsocks.Proxy | |||
public void EndConnectDest(IAsyncResult asyncResult) | |||
{ | |||
_remote.EndConnect(asyncResult); | |||
_remote?.EndConnect(asyncResult); | |||
} | |||
public void BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, | |||
object state) | |||
{ | |||
_remote.BeginSend(buffer, offset, size, socketFlags, callback, state); | |||
_remote?.BeginSend(buffer, offset, size, socketFlags, callback, state); | |||
} | |||
public int EndSend(IAsyncResult asyncResult) | |||
@@ -75,7 +75,7 @@ namespace Shadowsocks.Proxy | |||
public void BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, | |||
object state) | |||
{ | |||
_remote.BeginReceive(buffer, offset, size, socketFlags, callback, state); | |||
_remote?.BeginReceive(buffer, offset, size, socketFlags, callback, state); | |||
} | |||
public int EndReceive(IAsyncResult asyncResult) | |||
@@ -115,7 +115,7 @@ namespace Shadowsocks.Proxy | |||
DestEndPoint = remoteEP; | |||
_remote.BeginSend(request, 0, request.Length, 0, Socks5RequestSendCallback, st); | |||
_remote?.BeginSend(request, 0, request.Length, 0, Socks5RequestSendCallback, st); | |||
} | |||
@@ -132,7 +132,7 @@ namespace Shadowsocks.Proxy | |||
public void BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, | |||
object state) | |||
{ | |||
_remote.BeginSend(buffer, offset, size, socketFlags, callback, state); | |||
_remote?.BeginSend(buffer, offset, size, socketFlags, callback, state); | |||
} | |||
public int EndSend(IAsyncResult asyncResult) | |||
@@ -143,7 +143,7 @@ namespace Shadowsocks.Proxy | |||
public void BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, | |||
object state) | |||
{ | |||
_remote.BeginReceive(buffer, offset, size, socketFlags, callback, state); | |||
_remote?.BeginReceive(buffer, offset, size, socketFlags, callback, state); | |||
} | |||
public int EndReceive(IAsyncResult asyncResult) | |||
@@ -332,7 +332,9 @@ namespace Shadowsocks.View | |||
void controller_UpdatePACFromGFWListCompleted(object sender, GFWListUpdater.ResultEventArgs e) | |||
{ | |||
string result = e.Success ? I18N.GetString("PAC updated") : I18N.GetString("No updates found. Please report to GFWList if you have problems with it."); | |||
string result = e.Success | |||
? I18N.GetString("PAC updated") | |||
: I18N.GetString("No updates found. Please report to GFWList if you have problems with it."); | |||
ShowBalloonTip(I18N.GetString("Shadowsocks"), result, ToolTipIcon.Info, 1000); | |||
} | |||