- 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) | 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); | _tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); | ||||
_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); | _udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); | ||||
IPEndPoint localEndPoint = null; | 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. | // Bind the socket to the local endpoint and listen for incoming connections. | ||||
_tcpSocket.Bind(localEndPoint); | _tcpSocket.Bind(localEndPoint); | ||||
@@ -207,7 +207,7 @@ namespace Shadowsocks.Controller | |||||
response = new byte[] { 0, 91 }; | response = new byte[] { 0, 91 }; | ||||
Logging.Error("socks 5 protocol error"); | 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 | else | ||||
Close(); | Close(); | ||||
@@ -328,7 +328,7 @@ namespace Shadowsocks.Controller | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
connection.EndSend(ar); | |||||
connection?.EndSend(ar); | |||||
StartConnect(); | StartConnect(); | ||||
} | } | ||||
catch (Exception e) | catch (Exception e) | ||||
@@ -420,7 +420,7 @@ namespace Shadowsocks.Controller | |||||
var ep = ((ProxyTimer)sender).DestEndPoint; | var ep = ((ProxyTimer)sender).DestEndPoint; | ||||
Logging.Info($"Proxy {ep} timed out"); | Logging.Info($"Proxy {ep} timed out"); | ||||
remote.Close(); | |||||
remote?.Close(); | |||||
RetryConnect(); | RetryConnect(); | ||||
} | } | ||||
@@ -485,7 +485,7 @@ namespace Shadowsocks.Controller | |||||
IStrategy strategy = controller.GetCurrentStrategy(); | IStrategy strategy = controller.GetCurrentStrategy(); | ||||
strategy?.SetFailure(server); | strategy?.SetFailure(server); | ||||
Logging.Info($"{server.FriendlyName()} timed out"); | Logging.Info($"{server.FriendlyName()} timed out"); | ||||
remote.Close(); | |||||
remote?.Close(); | |||||
RetryConnect(); | RetryConnect(); | ||||
} | } | ||||
@@ -513,7 +513,7 @@ namespace Shadowsocks.Controller | |||||
timer.Dispose(); | timer.Dispose(); | ||||
// Complete the connection. | // Complete the connection. | ||||
remote.EndConnectDest(ar); | |||||
remote?.EndConnectDest(ar); | |||||
_destConnected = true; | _destConnected = true; | ||||
@@ -550,8 +550,8 @@ namespace Shadowsocks.Controller | |||||
try | try | ||||
{ | { | ||||
_startReceivingTime = DateTime.Now; | _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) | catch (Exception e) | ||||
{ | { | ||||
@@ -565,6 +565,7 @@ namespace Shadowsocks.Controller | |||||
if (_closed) return; | if (_closed) return; | ||||
try | try | ||||
{ | { | ||||
if ( remote == null ) return; | |||||
int bytesRead = remote.EndReceive(ar); | int bytesRead = remote.EndReceive(ar); | ||||
_totalRead += bytesRead; | _totalRead += bytesRead; | ||||
_tcprelay.UpdateInboundCounter(server, bytesRead); | _tcprelay.UpdateInboundCounter(server, bytesRead); | ||||
@@ -600,6 +601,7 @@ namespace Shadowsocks.Controller | |||||
if (_closed) return; | if (_closed) return; | ||||
try | try | ||||
{ | { | ||||
if(connection == null) return; | |||||
int bytesRead = connection.EndReceive(ar); | int bytesRead = connection.EndReceive(ar); | ||||
_totalWrite += bytesRead; | _totalWrite += bytesRead; | ||||
if (bytesRead > 0) | if (bytesRead > 0) | ||||
@@ -664,8 +666,8 @@ namespace Shadowsocks.Controller | |||||
if (_closed) return; | if (_closed) return; | ||||
try | 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) | catch (Exception e) | ||||
{ | { | ||||
@@ -679,8 +681,8 @@ namespace Shadowsocks.Controller | |||||
if (_closed) return; | if (_closed) return; | ||||
try | 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) | catch (Exception e) | ||||
{ | { | ||||
@@ -85,20 +85,21 @@ namespace Shadowsocks.Controller | |||||
int outlen; | int outlen; | ||||
encryptor.Encrypt(dataIn, length - 3, dataOut, out outlen); | encryptor.Encrypt(dataIn, length - 3, dataOut, out outlen); | ||||
Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay"); | Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay"); | ||||
_remote.SendTo(dataOut, outlen, SocketFlags.None, _remoteEndPoint); | |||||
_remote?.SendTo(dataOut, outlen, SocketFlags.None, _remoteEndPoint); | |||||
} | } | ||||
public void Receive() | public void Receive() | ||||
{ | { | ||||
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); | EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); | ||||
Logging.Debug($"++++++Receive Server Port, size:" + _buffer.Length); | 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) | public void RecvFromCallback(IAsyncResult ar) | ||||
{ | { | ||||
try | try | ||||
{ | { | ||||
if (_remote == null) return; | |||||
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); | EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); | ||||
int bytesRead = _remote.EndReceiveFrom(ar, ref remoteEndPoint); | int bytesRead = _remote.EndReceiveFrom(ar, ref remoteEndPoint); | ||||
@@ -112,7 +113,7 @@ namespace Shadowsocks.Controller | |||||
Array.Copy(dataOut, 0, sendBuf, 3, outlen); | Array.Copy(dataOut, 0, sendBuf, 3, outlen); | ||||
Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay"); | Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay"); | ||||
_local.SendTo(sendBuf, outlen + 3, 0, _localEndPoint); | |||||
_local?.SendTo(sendBuf, outlen + 3, 0, _localEndPoint); | |||||
Receive(); | Receive(); | ||||
} | } | ||||
catch (ObjectDisposedException) | catch (ObjectDisposedException) | ||||
@@ -132,7 +133,7 @@ namespace Shadowsocks.Controller | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
_remote.Close(); | |||||
_remote?.Close(); | |||||
} | } | ||||
catch (ObjectDisposedException) | catch (ObjectDisposedException) | ||||
{ | { | ||||
@@ -112,5 +112,5 @@ Failed to decode QRCode=无法解析二维码 | |||||
Failed to update registry=无法修改注册表 | Failed to update registry=无法修改注册表 | ||||
System Proxy On: =系统代理已启用: | System Proxy On: =系统代理已启用: | ||||
Running: Port {0}=正在运行:端口 {0} | 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) | if (Interlocked.Increment(ref exited) == 1) | ||||
{ | { | ||||
Logging.Error(e.ExceptionObject?.ToString()); | 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 " + | " https://github.com/shadowsocks/shadowsocks-windows/issues " + | ||||
Environment.NewLine + (e.ExceptionObject?.ToString()), | Environment.NewLine + (e.ExceptionObject?.ToString()), | ||||
"Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | "Shadowsocks Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||||
@@ -58,13 +58,13 @@ namespace Shadowsocks.Proxy | |||||
public void EndConnectDest(IAsyncResult asyncResult) | public void EndConnectDest(IAsyncResult asyncResult) | ||||
{ | { | ||||
_remote.EndConnect(asyncResult); | |||||
_remote?.EndConnect(asyncResult); | |||||
} | } | ||||
public void BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, | public void BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, | ||||
object state) | object state) | ||||
{ | { | ||||
_remote.BeginSend(buffer, offset, size, socketFlags, callback, state); | |||||
_remote?.BeginSend(buffer, offset, size, socketFlags, callback, state); | |||||
} | } | ||||
public int EndSend(IAsyncResult asyncResult) | 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, | public void BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, | ||||
object state) | object state) | ||||
{ | { | ||||
_remote.BeginReceive(buffer, offset, size, socketFlags, callback, state); | |||||
_remote?.BeginReceive(buffer, offset, size, socketFlags, callback, state); | |||||
} | } | ||||
public int EndReceive(IAsyncResult asyncResult) | public int EndReceive(IAsyncResult asyncResult) | ||||
@@ -115,7 +115,7 @@ namespace Shadowsocks.Proxy | |||||
DestEndPoint = remoteEP; | 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, | public void BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, | ||||
object state) | object state) | ||||
{ | { | ||||
_remote.BeginSend(buffer, offset, size, socketFlags, callback, state); | |||||
_remote?.BeginSend(buffer, offset, size, socketFlags, callback, state); | |||||
} | } | ||||
public int EndSend(IAsyncResult asyncResult) | 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, | public void BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, | ||||
object state) | object state) | ||||
{ | { | ||||
_remote.BeginReceive(buffer, offset, size, socketFlags, callback, state); | |||||
_remote?.BeginReceive(buffer, offset, size, socketFlags, callback, state); | |||||
} | } | ||||
public int EndReceive(IAsyncResult asyncResult) | public int EndReceive(IAsyncResult asyncResult) | ||||
@@ -332,7 +332,9 @@ namespace Shadowsocks.View | |||||
void controller_UpdatePACFromGFWListCompleted(object sender, GFWListUpdater.ResultEventArgs e) | 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); | ShowBalloonTip(I18N.GetString("Shadowsocks"), result, ToolTipIcon.Info, 1000); | ||||
} | } | ||||