diff --git a/shadowsocks-csharp/Controller/FileManager.cs b/shadowsocks-csharp/Controller/FileManager.cs index 60dc66e4..1c761c88 100755 --- a/shadowsocks-csharp/Controller/FileManager.cs +++ b/shadowsocks-csharp/Controller/FileManager.cs @@ -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 ); } } diff --git a/shadowsocks-csharp/Controller/Service/Listener.cs b/shadowsocks-csharp/Controller/Service/Listener.cs index e9744988..1d755eca 100644 --- a/shadowsocks-csharp/Controller/Service/Listener.cs +++ b/shadowsocks-csharp/Controller/Service/Listener.cs @@ -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); diff --git a/shadowsocks-csharp/Controller/Service/TCPRelay.cs b/shadowsocks-csharp/Controller/Service/TCPRelay.cs index 019c2067..4bce227d 100644 --- a/shadowsocks-csharp/Controller/Service/TCPRelay.cs +++ b/shadowsocks-csharp/Controller/Service/TCPRelay.cs @@ -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) { diff --git a/shadowsocks-csharp/Controller/Service/UDPRelay.cs b/shadowsocks-csharp/Controller/Service/UDPRelay.cs index e1325a3d..5f0d2363 100644 --- a/shadowsocks-csharp/Controller/Service/UDPRelay.cs +++ b/shadowsocks-csharp/Controller/Service/UDPRelay.cs @@ -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) { diff --git a/shadowsocks-csharp/Data/cn.txt b/shadowsocks-csharp/Data/cn.txt index 86ad1567..5d48ef7d 100644 --- a/shadowsocks-csharp/Data/cn.txt +++ b/shadowsocks-csharp/Data/cn.txt @@ -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将退出。请提交此错误到 \ No newline at end of file diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index f277de6f..be7e2afe 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -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); diff --git a/shadowsocks-csharp/Proxy/DirectConnect.cs b/shadowsocks-csharp/Proxy/DirectConnect.cs index af8d70ae..7de4e59f 100644 --- a/shadowsocks-csharp/Proxy/DirectConnect.cs +++ b/shadowsocks-csharp/Proxy/DirectConnect.cs @@ -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) diff --git a/shadowsocks-csharp/Proxy/Socks5Proxy.cs b/shadowsocks-csharp/Proxy/Socks5Proxy.cs index b66a18c4..6e6c04a9 100644 --- a/shadowsocks-csharp/Proxy/Socks5Proxy.cs +++ b/shadowsocks-csharp/Proxy/Socks5Proxy.cs @@ -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) diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index d0699908..61160033 100644 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -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); }