diff --git a/README.md b/README.md index 57783720..f3accda4 100644 --- a/README.md +++ b/README.md @@ -1,48 +1,34 @@ Shadowsocks for Windows ======================= -[![Build Status]][Appveyor] +#### Features compare with official version -#### Features +1. Add a speed test form +2. Get server location +3. Download speed test -1. System proxy configuration -2. Fast profile switching -3. PAC mode and global mode -4. GFWList and user rules -5. Supports HTTP proxy +#### 和官方版本相比有什么不同 -#### Download - -Download a [latest release]. +1. 新增了一个测速的功能 +2. 可以获取获取服务器IP归属地,如果不想联网获取,请下载[纯真IP库]并把qqwry.dat文件放置在当前目录 +3. 下载速度测试(通过下载谷歌浏览器的安装器来测试,大概900多K) -For >= Windows 8 or with .Net 4.0, download Shadowsocks-win-dotnet4.0-x.x.x.zip. +#### Download -For <= Windows 7 or with .Net 2.0, download Shadowsocks-win-x.x.x.zip. +Download [latest release]. #### Usage -1. Find Shadowsocks icon in the notification tray -2. You can add multiple servers in servers menu -3. Select Enable System Proxy menu to enable system proxy. Please disable other -proxy addons in your browser, or set them to use system proxy -4. You can also configure your browser proxy manually if you don't want to enable -system proxy. Set Socks5 or HTTP proxy to 127.0.0.1:1080. You can change this -port in Server -> Edit Servers -5. You can change PAC rules by editing the PAC file. When you save the PAC file -with any editor, Shadowsocks will notify browsers about the change automatically -6. You can also update the PAC file from GFWList. Note your modifications to the PAC -file will be lost. However you can put your rules in the user rule file for GFWList. -Don't forget to update from GFWList again after you've edited the user rule +See [official] ### Develop -Visual Studio Express 2012 is recommended. +Visual Studio 2015 is recommended. #### License GPLv3 - -[Appveyor]: https://ci.appveyor.com/project/clowwindy/shadowsocks-csharp -[Build Status]: https://ci.appveyor.com/api/projects/status/gknc8l1lxy423ehv/branch/master -[latest release]: https://sourceforge.net/projects/shadowsocksgui/files/dist/ +[latest release]: https://github.com/TkYu/shadowsocks-csharp-withping/releases +[official]: https://github.com/shadowsocks/shadowsocks-csharp +[纯真IP库]: http://update.cz88.net/soft/setup.zip \ No newline at end of file diff --git a/shadowsocks-csharp.sln b/shadowsocks-csharp.sln index 83869ae2..ac402ae1 100755 --- a/shadowsocks-csharp.sln +++ b/shadowsocks-csharp.sln @@ -1,11 +1,13 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Express 2012 for Windows Desktop -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shadowsocks-csharp", "shadowsocks-csharp\shadowsocks-csharp.csproj", "{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}" +# Visual Studio 14 +VisualStudioVersion = 14.0.22823.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "shadowsocks-csharp", "shadowsocks-csharp\shadowsocks-csharp.csproj", "{F58374A5-9AFB-430A-AF20-C509D3DCED3F}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{45913187-0685-4903-B250-DCEF0479CD86}" ProjectSection(ProjectDependencies) = postProject - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062} = {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062} + {F58374A5-9AFB-430A-AF20-C509D3DCED3F} = {F58374A5-9AFB-430A-AF20-C509D3DCED3F} EndProjectSection EndProject Global @@ -14,11 +16,10 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.ActiveCfg = Debug|x86 - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.Build.0 = Debug|x86 - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.Deploy.0 = Debug|x86 - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|x86.ActiveCfg = Release|x86 - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|x86.Build.0 = Release|x86 + {F58374A5-9AFB-430A-AF20-C509D3DCED3F}.Debug|x86.ActiveCfg = Debug|Any CPU + {F58374A5-9AFB-430A-AF20-C509D3DCED3F}.Debug|x86.Build.0 = Debug|Any CPU + {F58374A5-9AFB-430A-AF20-C509D3DCED3F}.Release|x86.ActiveCfg = Release|Any CPU + {F58374A5-9AFB-430A-AF20-C509D3DCED3F}.Release|x86.Build.0 = Release|Any CPU {45913187-0685-4903-B250-DCEF0479CD86}.Debug|x86.ActiveCfg = Debug|x86 {45913187-0685-4903-B250-DCEF0479CD86}.Debug|x86.Build.0 = Debug|x86 {45913187-0685-4903-B250-DCEF0479CD86}.Release|x86.ActiveCfg = Release|x86 diff --git a/shadowsocks-csharp/3rd/ProxySocket/AuthMethod.cs b/shadowsocks-csharp/3rd/ProxySocket/AuthMethod.cs new file mode 100644 index 00000000..ade427c9 --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/AuthMethod.cs @@ -0,0 +1,113 @@ +/* + Copyright ?2002, The KPD-Team + All rights reserved. + http://www.mentalis.org/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Neither the name of the KPD-Team, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Net; +using System.Net.Sockets; + +namespace Shadowsocks._3rd.ProxySocket { + /// + /// Implements a SOCKS authentication scheme. + /// + /// This is an abstract class; it must be inherited. + internal abstract class AuthMethod { + /// + /// Initializes an AuthMethod instance. + /// + /// The socket connection with the proxy server. + public AuthMethod(Socket server) { + Server = server; + } + /// + /// Authenticates the user. + /// + /// Authentication with the proxy server failed. + /// The proxy server uses an invalid protocol. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + public abstract void Authenticate(); + /// + /// Authenticates the user asynchronously. + /// + /// The method to call when the authentication is complete. + /// Authentication with the proxy server failed. + /// The proxy server uses an invalid protocol. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + public abstract void BeginAuthenticate(HandShakeComplete callback); + /// + /// Gets or sets the socket connection with the proxy server. + /// + /// The socket connection with the proxy server. + protected Socket Server { + get { + return m_Server; + } + set { + if (value == null) + throw new ArgumentNullException(); + m_Server = value; + } + } + /// + /// Gets or sets a byt array that can be used to store data. + /// + /// A byte array to store data. + protected byte[] Buffer { + get { + return m_Buffer; + } + set { + m_Buffer = value; + } + } + /// + /// Gets or sets the number of bytes that have been received from the remote proxy server. + /// + /// An integer that holds the number of bytes that have been received from the remote proxy server. + protected int Received { + get { + return m_Received; + } + set { + m_Received = value; + } + } + // private variables + /// Holds the value of the Buffer property. + private byte[] m_Buffer; + /// Holds the value of the Server property. + private Socket m_Server; + /// Holds the address of the method to call when the proxy has authenticated the client. + protected HandShakeComplete CallBack; + /// Holds the value of the Received property. + private int m_Received; + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/ProxySocket/AuthNone.cs b/shadowsocks-csharp/3rd/ProxySocket/AuthNone.cs new file mode 100644 index 00000000..8c1b0314 --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/AuthNone.cs @@ -0,0 +1,58 @@ +/* + Copyright ?2002, The KPD-Team + All rights reserved. + http://www.mentalis.org/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Neither the name of the KPD-Team, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System.Net.Sockets; + +namespace Shadowsocks._3rd.ProxySocket { + /// + /// This class implements the 'No Authentication' scheme. + /// + internal sealed class AuthNone : AuthMethod { + /// + /// Initializes an AuthNone instance. + /// + /// The socket connection with the proxy server. + public AuthNone(Socket server) : base(server) {} + /// + /// Authenticates the user. + /// + public override void Authenticate() { + return; // Do Nothing + } + /// + /// Authenticates the user asynchronously. + /// + /// The method to call when the authentication is complete. + /// This method immediately calls the callback method. + public override void BeginAuthenticate(HandShakeComplete callback) { + callback(null); + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/ProxySocket/AuthUserPass.cs b/shadowsocks-csharp/3rd/ProxySocket/AuthUserPass.cs new file mode 100644 index 00000000..58fc14ae --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/AuthUserPass.cs @@ -0,0 +1,156 @@ +/* + Copyright ?2002, The KPD-Team + All rights reserved. + http://www.mentalis.org/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Neither the name of the KPD-Team, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Net.Sockets; +using System.Text; + +namespace Shadowsocks._3rd.ProxySocket { + /// + /// This class implements the 'username/password authentication' scheme. + /// + internal sealed class AuthUserPass : AuthMethod { + /// + /// Initializes a new AuthUserPass instance. + /// + /// The socket connection with the proxy server. + /// The username to use. + /// The password to use. + /// user -or- pass is null. + public AuthUserPass(Socket server, string user, string pass) : base(server) { + Username = user; + Password = pass; + } + /// + /// Creates an array of bytes that has to be sent if the user wants to authenticate with the username/password authentication scheme. + /// + /// An array of bytes that has to be sent if the user wants to authenticate with the username/password authentication scheme. + private byte[] GetAuthenticationBytes() { + byte[] buffer = new byte[3 + Username.Length + Password.Length]; + buffer[0] = 1; + buffer[1] = (byte)Username.Length; + Array.Copy(Encoding.ASCII.GetBytes(Username), 0, buffer, 2, Username.Length); + buffer[Username.Length + 2] = (byte)Password.Length; + Array.Copy(Encoding.ASCII.GetBytes(Password), 0, buffer, Username.Length + 3, Password.Length); + return buffer; + } + /// + /// Starts the authentication process. + /// + public override void Authenticate() { + Server.Send(GetAuthenticationBytes()); + byte[] buffer = new byte[2]; + int received = 0; + while (received != 2) { + received += Server.Receive(buffer, received, 2 - received, SocketFlags.None); + } + if (buffer[1] != 0) { + Server.Close(); + throw new ProxyException("Username/password combination rejected."); + } + return; + } + /// + /// Starts the asynchronous authentication process. + /// + /// The method to call when the authentication is complete. + public override void BeginAuthenticate(HandShakeComplete callback) { + CallBack = callback; + Server.BeginSend(GetAuthenticationBytes(), 0, 3 + Username.Length + Password.Length, SocketFlags.None, new AsyncCallback(this.OnSent), Server); + return; + } + /// + /// Called when the authentication bytes have been sent. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnSent(IAsyncResult ar) { + try { + Server.EndSend(ar); + Buffer = new byte[2]; + Server.BeginReceive(Buffer, 0, 2, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); + } catch (Exception e) { + CallBack(e); + } + } + /// + /// Called when the socket received an authentication reply. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnReceive(IAsyncResult ar) { + try { + Received += Server.EndReceive(ar); + if (Received == Buffer.Length) + if (Buffer[1] == 0) + CallBack(null); + else + throw new ProxyException("Username/password combination not accepted."); + else + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); + } catch (Exception e) { + CallBack(e); + } + } + /// + /// Gets or sets the username to use when authenticating with the proxy server. + /// + /// The username to use when authenticating with the proxy server. + /// The specified value is null. + private string Username { + get { + return m_Username; + } + set { + if (value == null) + throw new ArgumentNullException(); + m_Username = value; + } + } + /// + /// Gets or sets the password to use when authenticating with the proxy server. + /// + /// The password to use when authenticating with the proxy server. + /// The specified value is null. + private string Password { + get { + return m_Password; + } + set { + if (value == null) + throw new ArgumentNullException(); + m_Password = value; + } + } + // private variables + /// Holds the value of the Username property. + private string m_Username; + /// Holds the value of the Password property. + private string m_Password; + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/ProxySocket/IAsyncProxyResult.cs b/shadowsocks-csharp/3rd/ProxySocket/IAsyncProxyResult.cs new file mode 100644 index 00000000..786fbb8b --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/IAsyncProxyResult.cs @@ -0,0 +1,96 @@ +/* + Copyright ?2002, The KPD-Team + All rights reserved. + http://www.mentalis.org/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Neither the name of the KPD-Team, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Threading; + +namespace Shadowsocks._3rd.ProxySocket { + /// + /// A class that implements the IAsyncResult interface. Objects from this class are returned by the BeginConnect method of the ProxySocket class. + /// + internal class IAsyncProxyResult : IAsyncResult { + /// Initializes the internal variables of this object + /// An object that contains state information for this request. + internal void Init(object stateObject) { + m_StateObject = stateObject; + m_Completed = false; + if (m_WaitHandle != null) + m_WaitHandle.Reset(); + + } + /// Initializes the internal variables of this object + internal void Reset() { + m_StateObject = null; + m_Completed = true; + if (m_WaitHandle != null) + m_WaitHandle.Set(); + } + /// Gets a value that indicates whether the server has completed processing the call. It is illegal for the server to use any client supplied resources outside of the agreed upon sharing semantics after it sets the IsCompleted property to "true". Thus, it is safe for the client to destroy the resources after IsCompleted property returns "true". + /// A boolean that indicates whether the server has completed processing the call. + public bool IsCompleted { + get { + return m_Completed; + } + } + /// Gets a value that indicates whether the BeginXXXX call has been completed synchronously. If this is detected in the AsyncCallback delegate, it is probable that the thread that called BeginInvoke is the current thread. + /// Returns false. + public bool CompletedSynchronously { + get { + return false; + } + } + /// Gets an object that was passed as the state parameter of the BeginXXXX method call. + /// The object that was passed as the state parameter of the BeginXXXX method call. + public object AsyncState { + get { + return m_StateObject; + } + } + /// + /// The AsyncWaitHandle property returns the WaitHandle that can use to perform a WaitHandle.WaitOne or WaitAny or WaitAll. The object which implements IAsyncResult need not derive from the System.WaitHandle classes directly. The WaitHandle wraps its underlying synchronization primitive and should be signaled after the call is completed. This enables the client to wait for the call to complete instead polling. The Runtime supplies a number of waitable objects that mirror Win32 synchronization primitives e.g. ManualResetEvent, AutoResetEvent and Mutex. + /// WaitHandle supplies methods that support waiting for such synchronization objects to become signaled with "any" or "all" semantics i.e. WaitHandle.WaitOne, WaitAny and WaitAll. Such methods are context aware to avoid deadlocks. The AsyncWaitHandle can be allocated eagerly or on demand. It is the choice of the IAsyncResult implementer. + /// + /// The WaitHandle associated with this asynchronous result. + public WaitHandle AsyncWaitHandle { + get { + if (m_WaitHandle == null) + m_WaitHandle = new ManualResetEvent(false); + return m_WaitHandle; + } + } + // private variables + /// Used internally to represent the state of the asynchronous request + internal bool m_Completed = true; + /// Holds the value of the StateObject property. + private object m_StateObject; + /// Holds the value of the WaitHandle property. + private ManualResetEvent m_WaitHandle; + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/ProxySocket/ProxyException.cs b/shadowsocks-csharp/3rd/ProxySocket/ProxyException.cs new file mode 100644 index 00000000..de791c63 --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/ProxyException.cs @@ -0,0 +1,82 @@ +/* + Copyright ?2002, The KPD-Team + All rights reserved. + http://www.mentalis.org/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Neither the name of the KPD-Team, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; + +namespace Shadowsocks._3rd.ProxySocket { + /// + /// The exception that is thrown when a proxy error occurs. + /// + public class ProxyException : Exception { + /// + /// Initializes a new instance of the ProxyException class. + /// + public ProxyException() : this("An error occured while talking to the proxy server.") {} + /// + /// Initializes a new instance of the ProxyException class. + /// + /// The message that describes the error. + public ProxyException(string message) : base(message) {} + /// + /// Initializes a new instance of the ProxyException class. + /// + /// The error number returned by a SOCKS5 server. + public ProxyException(int socks5Error) : this(ProxyException.Socks5ToString(socks5Error)) {} + /// + /// Converts a SOCKS5 error number to a human readable string. + /// + /// The error number returned by a SOCKS5 server. + /// A string representation of the specified SOCKS5 error number. + public static string Socks5ToString(int socks5Error) { + switch(socks5Error) { + case 0: + return "Connection succeeded."; + case 1: + return "General SOCKS server failure."; + case 2: + return "Connection not allowed by ruleset."; + case 3: + return "Network unreachable."; + case 4: + return "Host unreachable."; + case 5: + return "Connection refused."; + case 6: + return "TTL expired."; + case 7: + return "Command not supported."; + case 8: + return "Address type not supported."; + default: + return "Unspecified SOCKS error."; + } + } + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/ProxySocket/ProxySocket.cs b/shadowsocks-csharp/3rd/ProxySocket/ProxySocket.cs new file mode 100644 index 00000000..aa74d8ae --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/ProxySocket.cs @@ -0,0 +1,385 @@ +/* + Copyright ?2002, The KPD-Team + All rights reserved. + http://www.mentalis.org/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Neither the name of the KPD-Team, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Net; +using System.Net.Sockets; + +// Implements a number of classes to allow Sockets to connect trough a firewall. +namespace Shadowsocks._3rd.ProxySocket { + /// + /// Specifies the type of proxy servers that an instance of the ProxySocket class can use. + /// + public enum ProxyTypes { + /// No proxy server; the ProxySocket object behaves exactly like an ordinary Socket object. + None, + /// A SOCKS4[A] proxy server. + Socks4, + /// A SOCKS5 proxy server. + Socks5 + } + /// + /// Implements a Socket class that can connect trough a SOCKS proxy server. + /// + /// This class implements SOCKS4[A] and SOCKS5.
It does not, however, implement the BIND commands, so you cannot .
+ public class ProxySocket : Socket { + /// + /// Initializes a new instance of the ProxySocket class. + /// + /// One of the AddressFamily values. + /// One of the SocketType values. + /// One of the ProtocolType values. + /// The combination of addressFamily, socketType, and protocolType results in an invalid socket. + public ProxySocket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) : this(addressFamily, socketType, protocolType, "") {} + /// + /// Initializes a new instance of the ProxySocket class. + /// + /// One of the AddressFamily values. + /// One of the SocketType values. + /// One of the ProtocolType values. + /// The username to use when authenticating with the proxy server. + /// The combination of addressFamily, socketType, and protocolType results in an invalid socket. + /// proxyUsername is null. + public ProxySocket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, string proxyUsername) : this(addressFamily, socketType, protocolType, proxyUsername, "") {} + /// + /// Initializes a new instance of the ProxySocket class. + /// + /// One of the AddressFamily values. + /// One of the SocketType values. + /// One of the ProtocolType values. + /// The username to use when authenticating with the proxy server. + /// The password to use when authenticating with the proxy server. + /// The combination of addressFamily, socketType, and protocolType results in an invalid socket. + /// proxyUsername -or- proxyPassword is null. + public ProxySocket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, string proxyUsername, string proxyPassword) : base(addressFamily, socketType, protocolType) { + ProxyUser = proxyUsername; + ProxyPass = proxyPassword; + ToThrow = new InvalidOperationException(); + } + /// + /// Establishes a connection to a remote device. + /// + /// An EndPoint that represents the remote device. + /// The remoteEP parameter is a null reference (Nothing in Visual Basic). + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + /// An error occured while talking to the proxy server. + public new void Connect(EndPoint remoteEP) { + if (remoteEP == null) + throw new ArgumentNullException(" cannot be null."); + if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) + base.Connect(remoteEP); + else { + base.Connect(ProxyEndPoint); + if (ProxyType == ProxyTypes.Socks4) + (new Socks4Handler(this, ProxyUser)).Negotiate((IPEndPoint)remoteEP); + else if (ProxyType == ProxyTypes.Socks5) + (new Socks5Handler(this, ProxyUser, ProxyPass)).Negotiate((IPEndPoint)remoteEP); + } + } + /// + /// Establishes a connection to a remote device. + /// + /// The remote host to connect to. + /// The remote port to connect to. + /// The host parameter is a null reference (Nothing in Visual Basic). + /// The port parameter is invalid. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + /// An error occured while talking to the proxy server. + /// If you use this method with a SOCKS4 server, it will let the server resolve the hostname. Not all SOCKS4 servers support this 'remote DNS' though. + public void Connect(string host, int port) { + if (host == null) + throw new ArgumentNullException(" cannot be null."); + if (port <= 0 || port > 65535) + throw new ArgumentException("Invalid port."); + if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) + base.Connect(new IPEndPoint(Dns.Resolve(host).AddressList[0], port)); + else { + base.Connect(ProxyEndPoint); + if (ProxyType == ProxyTypes.Socks4) + (new Socks4Handler(this, ProxyUser)).Negotiate(host, port); + else if (ProxyType == ProxyTypes.Socks5) + (new Socks5Handler(this, ProxyUser, ProxyPass)).Negotiate(host, port); + } + } + /// + /// Begins an asynchronous request for a connection to a network device. + /// + /// An EndPoint that represents the remote device. + /// The AsyncCallback delegate. + /// An object that contains state information for this request. + /// An IAsyncResult that references the asynchronous connection. + /// The remoteEP parameter is a null reference (Nothing in Visual Basic). + /// An operating system error occurs while creating the Socket. + /// The Socket has been closed. + public new IAsyncResult BeginConnect(EndPoint remoteEP, AsyncCallback callback, object state) { + if (remoteEP == null || callback == null) + throw new ArgumentNullException(); + if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) { + return base.BeginConnect(remoteEP, callback, state); + } else { + CallBack = callback; + if (ProxyType == ProxyTypes.Socks4) { + AsyncResult = (new Socks4Handler(this, ProxyUser)).BeginNegotiate((IPEndPoint)remoteEP, new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } else if(ProxyType == ProxyTypes.Socks5) { + AsyncResult = (new Socks5Handler(this, ProxyUser, ProxyPass)).BeginNegotiate((IPEndPoint)remoteEP, new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } + return null; + } + } + /// + /// Begins an asynchronous request for a connection to a network device. + /// + /// The host to connect to. + /// The port on the remote host to connect to. + /// The AsyncCallback delegate. + /// An object that contains state information for this request. + /// An IAsyncResult that references the asynchronous connection. + /// The host parameter is a null reference (Nothing in Visual Basic). + /// The port parameter is invalid. + /// An operating system error occurs while creating the Socket. + /// The Socket has been closed. + public IAsyncResult BeginConnect(string host, int port, AsyncCallback callback, object state) { + if (host == null || callback == null) + throw new ArgumentNullException(); + if (port <= 0 || port > 65535) + throw new ArgumentException(); + CallBack = callback; + if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) { + RemotePort = port; + AsyncResult = BeginDns(host, new HandShakeComplete(this.OnHandShakeComplete)); + return AsyncResult; + } else { + if (ProxyType == ProxyTypes.Socks4) { + AsyncResult = (new Socks4Handler(this, ProxyUser)).BeginNegotiate(host, port, new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } else if(ProxyType == ProxyTypes.Socks5) { + AsyncResult = (new Socks5Handler(this, ProxyUser, ProxyPass)).BeginNegotiate(host, port, new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } + return null; + } + } + /// + /// Ends a pending asynchronous connection request. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + /// The asyncResult parameter is a null reference (Nothing in Visual Basic). + /// The asyncResult parameter was not returned by a call to the BeginConnect method. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + /// EndConnect was previously called for the asynchronous connection. + /// The proxy server refused the connection. + public new void EndConnect(IAsyncResult asyncResult) { + if (asyncResult == null) + throw new ArgumentNullException(); + if (!asyncResult.IsCompleted) + throw new ArgumentException(); + if (ToThrow != null) + throw ToThrow; + return; + } + /// + /// Begins an asynchronous request to resolve a DNS host name or IP address in dotted-quad notation to an IPAddress instance. + /// + /// The host to resolve. + /// The method to call when the hostname has been resolved. + /// An IAsyncResult instance that references the asynchronous request. + /// There was an error while trying to resolve the host. + internal IAsyncProxyResult BeginDns(string host, HandShakeComplete callback) { + try { + Dns.BeginResolve(host, new AsyncCallback(this.OnResolved), this); + return new IAsyncProxyResult(); + } catch { + throw new SocketException(); + } + } + /// + /// Called when the specified hostname has been resolved. + /// + /// The result of the asynchronous operation. + private void OnResolved(IAsyncResult asyncResult) { + try { + IPHostEntry dns = Dns.EndResolve(asyncResult); + base.BeginConnect(new IPEndPoint(dns.AddressList[0], RemotePort), new AsyncCallback(this.OnConnect), State); + } catch (Exception e) { + OnHandShakeComplete(e); + } + } + /// + /// Called when the Socket is connected to the remote host. + /// + /// The result of the asynchronous operation. + private void OnConnect(IAsyncResult asyncResult) { + try { + base.EndConnect(asyncResult); + OnHandShakeComplete(null); + } catch (Exception e) { + OnHandShakeComplete(e); + } + } + /// + /// Called when the Socket has finished talking to the proxy server and is ready to relay data. + /// + /// The error to throw when the EndConnect method is called. + private void OnHandShakeComplete(Exception error) { + if (error != null) + this.Close(); + ToThrow = error; + AsyncResult.Reset(); + if (CallBack != null) + CallBack(AsyncResult); + } + /// + /// Gets or sets the EndPoint of the proxy server. + /// + /// An IPEndPoint object that holds the IP address and the port of the proxy server. + public IPEndPoint ProxyEndPoint { + get { + return m_ProxyEndPoint; + } + set { + m_ProxyEndPoint = value; + } + } + /// + /// Gets or sets the type of proxy server to use. + /// + /// One of the ProxyTypes values. + public ProxyTypes ProxyType { + get { + return m_ProxyType; + } + set { + m_ProxyType = value; + } + } + /// + /// Gets or sets a user-defined object. + /// + /// The user-defined object. + private object State { + get { + return m_State; + } + set { + m_State = value; + } + } + /// + /// Gets or sets the username to use when authenticating with the proxy. + /// + /// A string that holds the username that's used when authenticating with the proxy. + /// The specified value is null. + public string ProxyUser { + get { + return m_ProxyUser; + } + set { + if (value == null) + throw new ArgumentNullException(); + m_ProxyUser = value; + } + } + /// + /// Gets or sets the password to use when authenticating with the proxy. + /// + /// A string that holds the password that's used when authenticating with the proxy. + /// The specified value is null. + public string ProxyPass { + get { + return m_ProxyPass; + } + set { + if (value == null) + throw new ArgumentNullException(); + m_ProxyPass = value; + } + } + /// + /// Gets or sets the asynchronous result object. + /// + /// An instance of the IAsyncProxyResult class. + private IAsyncProxyResult AsyncResult { + get { + return m_AsyncResult; + } + set { + m_AsyncResult = value; + } + } + /// + /// Gets or sets the exception to throw when the EndConnect method is called. + /// + /// An instance of the Exception class (or subclasses of Exception). + private Exception ToThrow { + get { + return m_ToThrow; + } + set { + m_ToThrow = value; + } + } + /// + /// Gets or sets the remote port the user wants to connect to. + /// + /// An integer that specifies the port the user wants to connect to. + private int RemotePort { + get { + return m_RemotePort; + } + set { + m_RemotePort = value; + } + } + // private variables + /// Holds the value of the State property. + private object m_State; + /// Holds the value of the ProxyEndPoint property. + private IPEndPoint m_ProxyEndPoint = null; + /// Holds the value of the ProxyType property. + private ProxyTypes m_ProxyType = ProxyTypes.None; + /// Holds the value of the ProxyUser property. + private string m_ProxyUser = null; + /// Holds the value of the ProxyPass property. + private string m_ProxyPass = null; + /// Holds a pointer to the method that should be called when the Socket is connected to the remote device. + private AsyncCallback CallBack = null; + /// Holds the value of the AsyncResult property. + private IAsyncProxyResult m_AsyncResult; + /// Holds the value of the ToThrow property. + private Exception m_ToThrow = null; + /// Holds the value of the RemotePort property. + private int m_RemotePort; + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/ProxySocket/Socks4Handler.cs b/shadowsocks-csharp/3rd/ProxySocket/Socks4Handler.cs new file mode 100644 index 00000000..9deb279b --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/Socks4Handler.cs @@ -0,0 +1,234 @@ +/* + Copyright ?2002, The KPD-Team + All rights reserved. + http://www.mentalis.org/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Neither the name of the KPD-Team, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Shadowsocks._3rd.ProxySocket { + /// + /// Implements the SOCKS4[A] protocol. + /// + internal sealed class Socks4Handler : SocksHandler { + /// + /// Initilizes a new instance of the SocksHandler class. + /// + /// The socket connection with the proxy server. + /// The username to use when authenticating with the server. + /// server -or- user is null. + public Socks4Handler(Socket server, string user) : base(server, user) {} + /// + /// Creates an array of bytes that has to be sent when the user wants to connect to a specific host/port combination. + /// + /// The host to connect to. + /// The port to connect to. + /// An array of bytes that has to be sent when the user wants to connect to a specific host/port combination. + /// Resolving the host name will be done at server side. Do note that some SOCKS4 servers do not implement this functionality. + /// host is null. + /// port is invalid. + private byte[] GetHostPortBytes(string host, int port) { + if (host == null) + throw new ArgumentNullException(); + if (port <= 0 || port > 65535) + throw new ArgumentException(); + byte [] connect = new byte[10 + Username.Length + host.Length]; + connect[0] = 4; + connect[1] = 1; + Array.Copy(PortToBytes(port), 0, connect, 2, 2); + connect[4] = connect[5] = connect[6] = 0; + connect[7] = 1; + Array.Copy(Encoding.ASCII.GetBytes(Username), 0, connect, 8, Username.Length); + connect[8 + Username.Length] = 0; + Array.Copy(Encoding.ASCII.GetBytes(host), 0, connect, 9 + Username.Length, host.Length); + connect[9 + Username.Length + host.Length] = 0; + return connect; + } + /// + /// Creates an array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. + /// + /// The IPEndPoint to connect to. + /// An array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. + /// remoteEP is null. + private byte[] GetEndPointBytes(IPEndPoint remoteEP) { + if (remoteEP == null) + throw new ArgumentNullException(); + byte [] connect = new byte[9 + Username.Length]; + connect[0] = 4; + connect[1] = 1; + Array.Copy(PortToBytes(remoteEP.Port), 0, connect, 2, 2); + Array.Copy(AddressToBytes(remoteEP.Address.Address), 0, connect, 4, 4); + Array.Copy(Encoding.ASCII.GetBytes(Username), 0, connect, 8, Username.Length); + connect[8 + Username.Length] = 0; + return connect; + } + /// + /// Starts negotiating with the SOCKS server. + /// + /// The host to connect to. + /// The port to connect to. + /// host is null. + /// port is invalid. + /// The proxy rejected the request. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + public override void Negotiate(string host, int port) { + Negotiate(GetHostPortBytes(host, port)); + } + /// + /// Starts negotiating with the SOCKS server. + /// + /// The IPEndPoint to connect to. + /// remoteEP is null. + /// The proxy rejected the request. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + public override void Negotiate(IPEndPoint remoteEP) { + Negotiate(GetEndPointBytes(remoteEP)); + } + /// + /// Starts negotiating with the SOCKS server. + /// + /// The bytes to send when trying to authenticate. + /// connect is null. + /// connect is too small. + /// The proxy rejected the request. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + private void Negotiate(byte [] connect) { + if (connect == null) + throw new ArgumentNullException(); + if (connect.Length < 2) + throw new ArgumentException(); + Server.Send(connect); + byte [] buffer = ReadBytes(8); + if (buffer[1] != 90) { + Server.Close(); + throw new ProxyException("Negotiation failed."); + } + } + /// + /// Starts negotiating asynchronously with a SOCKS proxy server. + /// + /// The remote server to connect to. + /// The remote port to connect to. + /// The method to call when the connection has been established. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, IPEndPoint proxyEndPoint) { + ProtocolComplete = callback; + Buffer = GetHostPortBytes(host, port); + Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); + AsyncResult = new IAsyncProxyResult(); + return AsyncResult; + } + /// + /// Starts negotiating asynchronously with a SOCKS proxy server. + /// + /// An IPEndPoint that represents the remote device. + /// The method to call when the connection has been established. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, IPEndPoint proxyEndPoint) { + ProtocolComplete = callback; + Buffer = GetEndPointBytes(remoteEP); + Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); + AsyncResult = new IAsyncProxyResult(); + return AsyncResult; + } + /// + /// Called when the Socket is connected to the remote proxy server. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnConnect(IAsyncResult ar) { + try { + Server.EndConnect(ar); + } catch (Exception e) { + ProtocolComplete(e); + return; + } + try { + Server.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnSent), Server); + } catch (Exception e) { + ProtocolComplete(e); + } + } + /// + /// Called when the Socket has sent the handshake data. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnSent(IAsyncResult ar) { + try { + if (Server.EndSend(ar) < Buffer.Length) { + ProtocolComplete(new SocketException()); + return; + } + } catch (Exception e) { + ProtocolComplete(e); + return; + } + try { + Buffer = new byte[8]; + Received = 0; + Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); + } catch (Exception e) { + ProtocolComplete(e); + } + } + /// + /// Called when the Socket has received a reply from the remote proxy server. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnReceive(IAsyncResult ar) { + try { + int received = Server.EndReceive(ar); + if (received <= 0) { + ProtocolComplete(new SocketException()); + return; + } + Received += received; + if (Received == 8) { + if (Buffer[1] == 90) + ProtocolComplete(null); + else { + Server.Close(); + ProtocolComplete(new ProxyException("Negotiation failed.")); + } + } else { + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); + } + } catch (Exception e) { + ProtocolComplete(e); + } + } + } +} + + diff --git a/shadowsocks-csharp/3rd/ProxySocket/Socks5Handler.cs b/shadowsocks-csharp/3rd/ProxySocket/Socks5Handler.cs new file mode 100644 index 00000000..6b417248 --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/Socks5Handler.cs @@ -0,0 +1,418 @@ +/* + Copyright ?2002, The KPD-Team + All rights reserved. + http://www.mentalis.org/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Neither the name of the KPD-Team, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Shadowsocks._3rd.ProxySocket { + /// + /// Implements the SOCKS5 protocol. + /// + internal sealed class Socks5Handler : SocksHandler { + /// + /// Initiliazes a new Socks5Handler instance. + /// + /// The socket connection with the proxy server. + /// server is null. + public Socks5Handler(Socket server) : this(server, "") {} + /// + /// Initiliazes a new Socks5Handler instance. + /// + /// The socket connection with the proxy server. + /// The username to use. + /// server -or- user is null. + public Socks5Handler(Socket server, string user) : this(server, user, "") {} + /// + /// Initiliazes a new Socks5Handler instance. + /// + /// The socket connection with the proxy server. + /// The username to use. + /// The password to use. + /// server -or- user -or- pass is null. + public Socks5Handler(Socket server, string user, string pass) : base(server, user) { + Password = pass; + } + /// + /// Starts the synchronous authentication process. + /// + /// Authentication with the proxy server failed. + /// The proxy server uses an invalid protocol. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + private void Authenticate() { + Server.Send(new byte [] {5, 2, 0, 2}); + byte[] buffer = ReadBytes(2); + if (buffer[1] == 255) + throw new ProxyException("No authentication method accepted."); + AuthMethod authenticate; + switch (buffer[1]) { + case 0: + authenticate = new AuthNone(Server); + break; + case 2: + authenticate = new AuthUserPass(Server, Username, Password); + break; + default: + throw new ProtocolViolationException(); + } + authenticate.Authenticate(); + } + /// + /// Creates an array of bytes that has to be sent when the user wants to connect to a specific host/port combination. + /// + /// The host to connect to. + /// The port to connect to. + /// An array of bytes that has to be sent when the user wants to connect to a specific host/port combination. + /// host is null. + /// port or host is invalid. + private byte[] GetHostPortBytes(string host, int port) { + if (host == null) + throw new ArgumentNullException(); + if (port <= 0 || port > 65535 || host.Length > 255) + throw new ArgumentException(); + byte [] connect = new byte[7 + host.Length]; + connect[0] = 5; + connect[1] = 1; + connect[2] = 0; //reserved + connect[3] = 3; + connect[4] = (byte)host.Length; + Array.Copy(Encoding.ASCII.GetBytes(host), 0, connect, 5, host.Length); + Array.Copy(PortToBytes(port), 0, connect, host.Length + 5, 2); + return connect; + } + /// + /// Creates an array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. + /// + /// The IPEndPoint to connect to. + /// An array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. + /// remoteEP is null. + private byte[] GetEndPointBytes(IPEndPoint remoteEP) { + if (remoteEP == null) + throw new ArgumentNullException(); + byte [] connect = new byte[10]; + connect[0] = 5; + connect[1] = 1; + connect[2] = 0; //reserved + connect[3] = 1; + Array.Copy(AddressToBytes(remoteEP.Address.Address), 0, connect, 4, 4); + Array.Copy(PortToBytes(remoteEP.Port), 0, connect, 8, 2); + return connect; + } + /// + /// Starts negotiating with the SOCKS server. + /// + /// The host to connect to. + /// The port to connect to. + /// host is null. + /// port is invalid. + /// The proxy rejected the request. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + /// The proxy server uses an invalid protocol. + public override void Negotiate(string host, int port) { + Negotiate(GetHostPortBytes(host, port)); + } + /// + /// Starts negotiating with the SOCKS server. + /// + /// The IPEndPoint to connect to. + /// remoteEP is null. + /// The proxy rejected the request. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + /// The proxy server uses an invalid protocol. + public override void Negotiate(IPEndPoint remoteEP) { + Negotiate(GetEndPointBytes(remoteEP)); + } + /// + /// Starts negotiating with the SOCKS server. + /// + /// The bytes to send when trying to authenticate. + /// connect is null. + /// connect is too small. + /// The proxy rejected the request. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + /// The proxy server uses an invalid protocol. + private void Negotiate(byte[] connect) { + Authenticate(); + Server.Send(connect); + byte[] buffer = ReadBytes(4); + if (buffer[1] != 0) { + Server.Close(); + throw new ProxyException(buffer[1]); + } + switch(buffer[3]) { + case 1: + buffer = ReadBytes(6); //IPv4 address with port + break; + case 3: + buffer = ReadBytes(1); + buffer = ReadBytes(buffer[0] + 2); //domain name with port + break; + case 4: + buffer = ReadBytes(18); //IPv6 address with port + break; + default: + Server.Close(); + throw new ProtocolViolationException(); + } + } + /// + /// Starts negotiating asynchronously with the SOCKS server. + /// + /// The host to connect to. + /// The port to connect to. + /// The method to call when the negotiation is complete. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, IPEndPoint proxyEndPoint) { + ProtocolComplete = callback; + HandShake = GetHostPortBytes(host, port); + Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); + AsyncResult = new IAsyncProxyResult(); + return AsyncResult; + } + /// + /// Starts negotiating asynchronously with the SOCKS server. + /// + /// An IPEndPoint that represents the remote device. + /// The method to call when the negotiation is complete. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, IPEndPoint proxyEndPoint) { + ProtocolComplete = callback; + HandShake = GetEndPointBytes(remoteEP); + Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); + AsyncResult = new IAsyncProxyResult(); + return AsyncResult; + } + /// + /// Called when the socket is connected to the remote server. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnConnect(IAsyncResult ar) { + try { + Server.EndConnect(ar); + } catch (Exception e) { + ProtocolComplete(e); + return; + } + try { + Server.BeginSend(new byte [] {5, 2, 0, 2}, 0, 4, SocketFlags.None, new AsyncCallback(this.OnAuthSent), Server); + } catch (Exception e) { + ProtocolComplete(e); + } + } + /// + /// Called when the authentication bytes have been sent. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnAuthSent(IAsyncResult ar) { + try { + Server.EndSend(ar); + } catch (Exception e) { + ProtocolComplete(e); + return; + } + try { + Buffer = new byte[1024]; + Received = 0; + Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnAuthReceive), Server); + } catch (Exception e) { + ProtocolComplete(e); + } + } + /// + /// Called when an authentication reply has been received. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnAuthReceive(IAsyncResult ar) { + try { + Received += Server.EndReceive(ar); + if (Received <= 0) + throw new SocketException(); + } catch (Exception e) { + ProtocolComplete(e); + return; + } + try { + if (Received < 2) { + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnAuthReceive), Server); + } else { + AuthMethod authenticate; + switch(Buffer[1]) { + case 0: + authenticate = new AuthNone(Server); + break; + case 2: + authenticate = new AuthUserPass(Server, Username, Password); + break; + default: + ProtocolComplete(new SocketException()); + return; + } + authenticate.BeginAuthenticate(new HandShakeComplete(this.OnAuthenticated)); + } + } catch (Exception e) { + ProtocolComplete(e); + } + } + /// + /// Called when the socket has been successfully authenticated with the server. + /// + /// The exception that has occured while authenticating, or null if no error occured. + private void OnAuthenticated(Exception e) { + if (e != null) { + ProtocolComplete(e); + return; + } + try { + Server.BeginSend(HandShake, 0, HandShake.Length, SocketFlags.None, new AsyncCallback(this.OnSent), Server); + } catch (Exception ex) { + ProtocolComplete(ex); + } + } + /// + /// Called when the connection request has been sent. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnSent(IAsyncResult ar) { + try { + Server.EndSend(ar); + } catch (Exception e) { + ProtocolComplete(e); + return; + } + try { + Buffer = new byte[5]; + Received = 0; + Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); + } catch (Exception e) { + ProtocolComplete(e); + } + } + /// + /// Called when a connection reply has been received. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnReceive(IAsyncResult ar) { + try { + Received += Server.EndReceive(ar); + } catch (Exception e) { + ProtocolComplete(e); + return; + } + try { + if (Received == Buffer.Length) + ProcessReply(Buffer); + else + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); + } catch (Exception e) { + ProtocolComplete(e); + } + } + /// + /// Processes the received reply. + /// + /// The received reply + /// The received reply is invalid. + private void ProcessReply(byte[] buffer) { + switch(buffer[3]) { + case 1: + Buffer = new byte[5]; //IPv4 address with port - 1 byte + break; + case 3: + Buffer = new byte[buffer[4] + 2]; //domain name with port + break; + case 4: + buffer = new byte[17]; //IPv6 address with port - 1 byte + break; + default: + throw new ProtocolViolationException(); + } + Received = 0; + Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReadLast), Server); + } + /// + /// Called when the last bytes are read from the socket. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnReadLast(IAsyncResult ar) { + try { + Received += Server.EndReceive(ar); + } catch (Exception e) { + ProtocolComplete(e); + return; + } + try { + if (Received == Buffer.Length) + ProtocolComplete(null); + else + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, new AsyncCallback(this.OnReadLast), Server); + } catch (Exception e) { + ProtocolComplete(e); + } + } + /// + /// Gets or sets the password to use when authenticating with the SOCKS5 server. + /// + /// The password to use when authenticating with the SOCKS5 server. + private string Password { + get { + return m_Password; + } + set { + if (value == null) + throw new ArgumentNullException(); + m_Password = value; + } + } + /// + /// Gets or sets the bytes to use when sending a connect request to the proxy server. + /// + /// The array of bytes to use when sending a connect request to the proxy server. + private byte[] HandShake { + get { + return m_HandShake; + } + set { + m_HandShake = value; + } + } + // private variables + /// Holds the value of the Password property. + private string m_Password; + /// Holds the value of the HandShake property. + private byte[] m_HandShake; + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/ProxySocket/SocksHandler.cs b/shadowsocks-csharp/3rd/ProxySocket/SocksHandler.cs new file mode 100644 index 00000000..3eb211df --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/SocksHandler.cs @@ -0,0 +1,204 @@ +/* + Copyright ?2002, The KPD-Team + All rights reserved. + http://www.mentalis.org/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + - Neither the name of the KPD-Team, nor the names of its contributors + may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +using System; +using System.Net; +using System.Net.Sockets; + +namespace Shadowsocks._3rd.ProxySocket { + /// + /// References the callback method to be called when the protocol negotiation is completed. + /// + internal delegate void HandShakeComplete(Exception error); + /// + /// Implements a specific version of the SOCKS protocol. This is an abstract class; it must be inherited. + /// + internal abstract class SocksHandler { + /// + /// Initilizes a new instance of the SocksHandler class. + /// + /// The socket connection with the proxy server. + /// The username to use when authenticating with the server. + /// server -or- user is null. + public SocksHandler(Socket server, string user) { + Server = server; + Username = user; + } + /// + /// Converts a port number to an array of bytes. + /// + /// The port to convert. + /// An array of two bytes that represents the specified port. + protected byte[] PortToBytes(int port) { + byte [] ret = new byte[2]; + ret[0] = (byte)(port / 256); + ret[1] = (byte)(port % 256); + return ret; + } + /// + /// Converts an IP address to an array of bytes. + /// + /// The IP address to convert. + /// An array of four bytes that represents the specified IP address. + protected byte[] AddressToBytes(long address) { + byte [] ret = new byte[4]; + ret[0] = (byte)(address % 256); + ret[1] = (byte)((address / 256) % 256); + ret[2] = (byte)((address / 65536) % 256); + ret[3] = (byte)(address / 16777216); + return ret; + } + /// + /// Reads a specified number of bytes from the Server socket. + /// + /// The number of bytes to return. + /// An array of bytes. + /// The number of bytes to read is invalid. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + protected byte[] ReadBytes(int count) { + if (count <= 0) + throw new ArgumentException(); + byte[] buffer = new byte[count]; + int received = 0; + while(received != count) { + received += Server.Receive(buffer, received, count - received, SocketFlags.None); + } + return buffer; + } + /// + /// Gets or sets the socket connection with the proxy server. + /// + /// A Socket object that represents the connection with the proxy server. + /// The specified value is null. + protected Socket Server { + get { + return m_Server; + } + set { + if (value == null) + throw new ArgumentNullException(); + m_Server = value; + } + } + /// + /// Gets or sets the username to use when authenticating with the proxy server. + /// + /// A string that holds the username to use when authenticating with the proxy server. + /// The specified value is null. + protected string Username { + get { + return m_Username; + } + set { + if (value == null) + throw new ArgumentNullException(); + m_Username = value; + } + } + /// + /// Gets or sets the return value of the BeginConnect call. + /// + /// An IAsyncProxyResult object that is the return value of the BeginConnect call. + protected IAsyncProxyResult AsyncResult { + get { + return m_AsyncResult; + } + set { + m_AsyncResult = value; + } + } + /// + /// Gets or sets a byte buffer. + /// + /// An array of bytes. + protected byte[] Buffer { + get { + return m_Buffer; + } + set { + m_Buffer = value; + } + } + /// + /// Gets or sets the number of bytes that have been received from the remote proxy server. + /// + /// An integer that holds the number of bytes that have been received from the remote proxy server. + protected int Received { + get { + return m_Received; + } + set { + m_Received = value; + } + } + // private variables + /// Holds the value of the Server property. + private Socket m_Server; + /// Holds the value of the Username property. + private string m_Username; + /// Holds the value of the AsyncResult property. + private IAsyncProxyResult m_AsyncResult; + /// Holds the value of the Buffer property. + private byte[] m_Buffer; + /// Holds the value of the Received property. + private int m_Received; + /// Holds the address of the method to call when the SOCKS protocol has been completed. + protected HandShakeComplete ProtocolComplete; + /// + /// Starts negotiating with a SOCKS proxy server. + /// + /// The remote server to connect to. + /// The remote port to connect to. + public abstract void Negotiate(string host, int port); + /// + /// Starts negotiating with a SOCKS proxy server. + /// + /// The remote endpoint to connect to. + public abstract void Negotiate(IPEndPoint remoteEP); + /// + /// Starts negotiating asynchronously with a SOCKS proxy server. + /// + /// An IPEndPoint that represents the remote device. + /// The method to call when the connection has been established. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public abstract IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, IPEndPoint proxyEndPoint); + /// + /// Starts negotiating asynchronously with a SOCKS proxy server. + /// + /// The remote server to connect to. + /// The remote port to connect to. + /// The method to call when the connection has been established. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public abstract IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, IPEndPoint proxyEndPoint); + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/3rd/ProxySocket/SocksHttpWebRequest.cs b/shadowsocks-csharp/3rd/ProxySocket/SocksHttpWebRequest.cs new file mode 100644 index 00000000..2a75542a --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/SocksHttpWebRequest.cs @@ -0,0 +1,283 @@ +using System; +using System.Collections.Specialized; +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Shadowsocks._3rd.ProxySocket +{ + public class SocksHttpWebRequest : WebRequest + { + + #region Private + private Encoding _correctEncoding; + #endregion Private + + #region Member Variables + + private readonly Uri _requestUri; + private WebHeaderCollection _requestHeaders; + private string _method; + private SocksHttpWebResponse _response; + private string _requestMessage; + private byte[] _requestContentBuffer; + + // darn MS for making everything internal (yeah, I'm talking about you, System.net.KnownHttpVerb) + static readonly StringCollection validHttpVerbs = + new StringCollection { "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS" }; + + #endregion + + #region Constructor + + private SocksHttpWebRequest(Uri requestUri) + { + _requestUri = requestUri; + _correctEncoding = Encoding.Default; + } + + #endregion + + #region WebRequest Members + + public override WebResponse GetResponse() + { + if (Proxy == null) + { + throw new InvalidOperationException("Proxy property cannot be null."); + } + if (String.IsNullOrEmpty(Method)) + { + throw new InvalidOperationException("Method has not been set."); + } + + if (RequestSubmitted) + { + return _response; + } + _response = InternalGetResponse(); + RequestSubmitted = true; + return _response; + } + + public override Uri RequestUri + { + get { return _requestUri; } + } + + public override IWebProxy Proxy { get; set; } + + public override WebHeaderCollection Headers + { + get + { + if (_requestHeaders == null) + { + _requestHeaders = new WebHeaderCollection(); + } + return _requestHeaders; + } + set + { + if (RequestSubmitted) + { + throw new InvalidOperationException("This operation cannot be performed after the request has been submitted."); + } + _requestHeaders = value; + } + } + + public bool RequestSubmitted { get; private set; } + + public override string Method + { + get + { + return _method ?? "GET"; + } + set + { + if (validHttpVerbs.Contains(value)) + { + _method = value; + } + else + { + throw new ArgumentOutOfRangeException("value", string.Format("'{0}' is not a known HTTP verb.", value)); + } + } + } + + public override long ContentLength { get; set; } + + public override string ContentType { get; set; } + + public override Stream GetRequestStream() + { + if (RequestSubmitted) + { + throw new InvalidOperationException("This operation cannot be performed after the request has been submitted."); + } + + if (_requestContentBuffer == null) + { + _requestContentBuffer = new byte[ContentLength]; + } + else if (ContentLength == default(long)) + { + _requestContentBuffer = new byte[int.MaxValue]; + } + else if (_requestContentBuffer.Length != ContentLength) + { + Array.Resize(ref _requestContentBuffer, (int)ContentLength); + } + return new MemoryStream(_requestContentBuffer); + } + + #endregion + + #region Methods + + public static new WebRequest Create(string requestUri) + { + return new SocksHttpWebRequest(new Uri(requestUri)); + } + + public static new WebRequest Create(Uri requestUri) + { + return new SocksHttpWebRequest(requestUri); + } + + private string BuildHttpRequestMessage() + { + if (RequestSubmitted) + { + throw new InvalidOperationException("This operation cannot be performed after the request has been submitted."); + } + + var message = new StringBuilder(); + message.AppendFormat("{0} {1} HTTP/1.0\r\nHost: {2}\r\n", Method, RequestUri.PathAndQuery, RequestUri.Host); + + // add the headers + foreach (var key in Headers.Keys) + { + message.AppendFormat("{0}: {1}\r\n", key, Headers[key.ToString()]); + } + + if (!string.IsNullOrEmpty(ContentType)) + { + message.AppendFormat("Content-Type: {0}\r\n", ContentType); + } + if (ContentLength > 0) + { + message.AppendFormat("Content-Length: {0}\r\n", ContentLength); + } + + // add a blank line to indicate the end of the headers + message.Append("\r\n"); + + // add content + if (_requestContentBuffer != null && _requestContentBuffer.Length > 0) + { + using (var stream = new MemoryStream(_requestContentBuffer, false)) + { + using (var reader = new StreamReader(stream)) + { + message.Append(reader.ReadToEnd()); + } + } + } + + return message.ToString(); + } + + private SocksHttpWebResponse InternalGetResponse() + { + var response = new StringBuilder(); + using (var _socksConnection = + new ProxySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + { + var proxyUri = Proxy.GetProxy(RequestUri); + var ipAddress = GetProxyIpAddress(proxyUri); + _socksConnection.ProxyEndPoint = new IPEndPoint(ipAddress, proxyUri.Port); + _socksConnection.ProxyType = ProxyTypes.Socks5; + + + + // open connection + _socksConnection.Connect(RequestUri.Host, 80); + // send an HTTP request + _socksConnection.Send(_correctEncoding.GetBytes(RequestMessage)); + // read the HTTP reply + var buffer = new byte[1024]; + + var bytesReceived = _socksConnection.Receive(buffer); + while (bytesReceived > 0) + { + string chunk = _correctEncoding.GetString(buffer, 0, bytesReceived); + string encString = EncodingHelper.GetEncodingFromChunk(chunk); + if (!string.IsNullOrEmpty(encString)) + { + try + { + _correctEncoding = Encoding.GetEncoding(encString); + } + catch + { + //TODO: do something here + } + } + response.Append(chunk); + bytesReceived = _socksConnection.Receive(buffer); + } + } + return new SocksHttpWebResponse(response.ToString(),_correctEncoding); + } + + private static IPAddress GetProxyIpAddress(Uri proxyUri) + { + IPAddress ipAddress; + if (!IPAddress.TryParse(proxyUri.Host, out ipAddress)) + { + try + { + return Dns.GetHostEntry(proxyUri.Host).AddressList[0]; + } + catch (Exception e) + { + throw new InvalidOperationException( + string.Format("Unable to resolve proxy hostname '{0}' to a valid IP address.", proxyUri.Host), e); + } + } + return ipAddress; + } + + #endregion + + #region Properties + + public Encoding CorrectEncoding + { + get + { + return _correctEncoding; + } + } + + public string RequestMessage + { + get + { + if (string.IsNullOrEmpty(_requestMessage)) + { + _requestMessage = BuildHttpRequestMessage(); + } + return _requestMessage; + } + } + + #endregion + + } +} diff --git a/shadowsocks-csharp/3rd/ProxySocket/SocksHttpWebResponse.cs b/shadowsocks-csharp/3rd/ProxySocket/SocksHttpWebResponse.cs new file mode 100644 index 00000000..a2a711b3 --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/SocksHttpWebResponse.cs @@ -0,0 +1,109 @@ +using System; +using System.IO; +using System.Net; +using System.Text; + +namespace Shadowsocks._3rd.ProxySocket +{ + public class SocksHttpWebResponse : WebResponse + { + + #region Member Variables + + private WebHeaderCollection _httpResponseHeaders; + private string _responseContent; + + #endregion + + #region Constructors + + public SocksHttpWebResponse(string httpResponseMessage) + { + SetHeadersAndResponseContent(httpResponseMessage); + CorrectEncoding = Encoding.Default; + } + + public SocksHttpWebResponse(string httpResponseMessage, Encoding encoding) + { + SetHeadersAndResponseContent(httpResponseMessage); + CorrectEncoding = encoding; + } + + #endregion + + #region WebResponse Members + + public override Stream GetResponseStream() + { + return ResponseContent.Length == 0 ? Stream.Null : new MemoryStream(CorrectEncoding.GetBytes(ResponseContent)); + } + + public override void Close() { /* the base implementation throws an exception */ } + + public override WebHeaderCollection Headers + { + get + { + if (_httpResponseHeaders == null) + { + _httpResponseHeaders = new WebHeaderCollection(); + } + return _httpResponseHeaders; + } + } + + public override long ContentLength + { + get + { + return ResponseContent.Length; + } + set + { + throw new NotSupportedException(); + } + } + + #endregion + + #region Methods + + private void SetHeadersAndResponseContent(string responseMessage) + { + if (string.IsNullOrEmpty(responseMessage)) + return; + + // the HTTP headers can be found before the first blank line + var indexOfFirstBlankLine = responseMessage.IndexOf("\r\n\r\n"); + + var headers = responseMessage.Substring(0, indexOfFirstBlankLine); + var headerValues = headers.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + // ignore the first line in the header since it is the HTTP response code + for (int i = 1; i < headerValues.Length; i++) + { + var headerEntry = headerValues[i].Split(new[] { ':' }); + Headers.Add(headerEntry[0], headerEntry[1]); + } + + ResponseContent = responseMessage.Substring(indexOfFirstBlankLine + 4); + } + + #endregion + + #region Properties + + private string ResponseContent + { + get { return _responseContent ?? string.Empty; } + set { _responseContent = value; } + } + + public Encoding CorrectEncoding + { + get;set; + } + + #endregion + + } +} diff --git a/shadowsocks-csharp/3rd/ProxySocket/SocksWebClient.cs b/shadowsocks-csharp/3rd/ProxySocket/SocksWebClient.cs new file mode 100644 index 00000000..4f787755 --- /dev/null +++ b/shadowsocks-csharp/3rd/ProxySocket/SocksWebClient.cs @@ -0,0 +1,312 @@ +using System; +using System.Net; + +namespace Shadowsocks._3rd.ProxySocket +{ + #region old + /* + public class Calculagraph + { + /// + /// 时间到事件 + /// + public event TimeoutCaller TimeOver; + + /// + /// 开始时间 + /// + private DateTime _startTime; + private TimeSpan _timeout = new TimeSpan(0, 0, 10); + private bool _hasStarted = false; + object _userdata; + + /// + /// 计时器构造方法 + /// + /// 计时结束时回调的用户数据 + public Calculagraph(object userdata) + { + TimeOver += new TimeoutCaller(OnTimeOver); + _userdata = userdata; + } + + /// + /// 超时退出 + /// + /// + public virtual void OnTimeOver(object userdata) + { + Stop(); + } + + /// + /// 过期时间(秒) + /// + public int Timeout + { + get + { + return _timeout.Seconds; + } + set + { + if (value <= 0) + return; + _timeout = new TimeSpan(0, 0, value); + } + } + + /// + /// 是否已经开始计时 + /// + public bool HasStarted + { + get + { + return _hasStarted; + } + } + + /// + /// 开始计时 + /// + public void Start() + { + Reset(); + _hasStarted = true; + System.Threading.Thread th = new System.Threading.Thread(WaitCall); + th.IsBackground = true; + th.Start(); + } + + /// + /// 重置 + /// + public void Reset() + { + _startTime = DateTime.Now; + } + + /// + /// 停止计时 + /// + public void Stop() + { + _hasStarted = false; + } + + /// + /// 检查是否过期 + /// + /// + private bool checkTimeout() + { + return (DateTime.Now - _startTime).Seconds >= Timeout; + } + + private void WaitCall() + { + try + { + //循环检测是否过期 + while (_hasStarted && !checkTimeout()) + { + System.Threading.Thread.Sleep(1000); + } + if (TimeOver != null) + TimeOver(_userdata); + } + catch (Exception) + { + Stop(); + } + } + } + + /// + /// 过期时回调委托 + /// + /// + public delegate void TimeoutCaller(object userdata); + + public class CNNWebClient : WebClient + { + + private Calculagraph _timer; + private int _timeOut = 5; + + /// + /// 过期时间 + /// + public int Timeout + { + get + { + return _timeOut; + } + set + { + if (value <= 0) + _timeOut = 5; + _timeOut = value; + } + } + + /// + /// 重写GetWebRequest,添加WebRequest对象超时时间 + /// + /// + /// + protected override WebRequest GetWebRequest(Uri address) + { + HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); + request.Timeout = 1000 * Timeout; + request.ReadWriteTimeout = 1000 * Timeout; + return request; + } + + /// + /// 带过期计时的下载 + /// + public void DownloadFileAsyncWithTimeout(Uri address, string fileName, object userToken) + { + if (_timer == null) + { + _timer = new Calculagraph(this); + _timer.Timeout = Timeout; + _timer.TimeOver += new TimeoutCaller(_timer_TimeOver); + this.DownloadProgressChanged += new DownloadProgressChangedEventHandler(CNNWebClient_DownloadProgressChanged); + } + + DownloadFileAsync(address, fileName, userToken); + _timer.Start(); + } + + /// + /// WebClient下载过程事件,接收到数据时引发 + /// + /// + /// + void CNNWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) + { + _timer.Reset();//重置计时器 + } + + /// + /// 计时器过期 + /// + /// + void _timer_TimeOver(object userdata) + { + this.CancelAsync();//取消下载 + } + } + */ + #endregion + + public class SocksWebClient : WebClient + { + public IProxyDetails ProxyDetails { get; set; } + public string UserAgent { get; set; } + + protected override WebRequest GetWebRequest(Uri address) + { + WebRequest result = null; + + if (ProxyDetails != null) + { + if (ProxyDetails.ProxyType == ProxyType.Proxy) + { + result = (HttpWebRequest)WebRequest.Create(address); + result.Proxy = new WebProxy(ProxyDetails.FullProxyAddress); + if (!string.IsNullOrEmpty(UserAgent)) + ((HttpWebRequest)result).UserAgent = UserAgent; + } + else if (ProxyDetails.ProxyType == ProxyType.Socks) + { + result = SocksHttpWebRequest.Create(address); + result.Proxy = new WebProxy(ProxyDetails.FullProxyAddress); + //TODO: implement user and password + + } + else if (ProxyDetails.ProxyType == ProxyType.None) + { + result = (HttpWebRequest)WebRequest.Create(address); + if (!string.IsNullOrEmpty(UserAgent)) + ((HttpWebRequest)result).UserAgent = UserAgent; + } + } + else + { + result = (HttpWebRequest)WebRequest.Create(address); + if (!string.IsNullOrEmpty(UserAgent)) + ((HttpWebRequest)result).UserAgent = UserAgent; + } + return result; + } + } + + public interface IProxyDetails + { + ProxyType ProxyType { get; set; } + /// + /// adress and port + /// + string FullProxyAddress { get; set; } + string ProxyAddress { get; set; } + int ProxyPort { get; set; } + string ProxyUserName { get; set; } + string ProxyPassword { get; set; } + } + public class ProxyDetails : IProxyDetails + { + public ProxyType ProxyType { get; set; } + /// + /// adress and port + /// + public string FullProxyAddress { get; set; } + public string ProxyAddress { get; set; } + public int ProxyPort { get; set; } + public string ProxyUserName { get; set; } + public string ProxyPassword { get; set; } + + public ProxyDetails() + { + } + public ProxyDetails(int port) + { + FullProxyAddress = IPAddress.Loopback + ":" + port; + ProxyAddress = IPAddress.Loopback.ToString(); + ProxyPort = port; + ProxyType = ProxyType.Socks; + } + } + + public enum ProxyType + { + None = 0, + Proxy = 1, + Socks = 2 + } + + public static class EncodingHelper + { + public static string GetEncodingFromChunk(string chunk) + { + string charset = null; + int charsetStart = chunk.IndexOf("charset="); + int charsetEnd = -1; + if (charsetStart != -1) + { + charsetEnd = chunk.IndexOfAny(new[] { ' ', '\"', ';', '\r', '\n' }, charsetStart); + if (charsetEnd != -1) + { + int start = charsetStart + 8; + charset = chunk.Substring(start, charsetEnd - start + 1); + charset = charset.TrimEnd(new Char[] { '>', '"', '\r', '\n' }); + } + } + return charset; + } + } +} diff --git a/shadowsocks-csharp/3rd/QQWry.cs b/shadowsocks-csharp/3rd/QQWry.cs new file mode 100644 index 00000000..f64331d1 --- /dev/null +++ b/shadowsocks-csharp/3rd/QQWry.cs @@ -0,0 +1,754 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; + +namespace Shadowsocks._3rd +{ + /// + /// QQWry 的摘要说明。 + /// + public class QQWry + { + #region Properties + /// + ///第一种模式 + /// + private const byte REDIRECT_MODE_1 = 0x01; + + /// + ///第二种模式 + /// + private const byte REDIRECT_MODE_2 = 0x02; + + /// + ///每条记录长度 + /// + private const int IP_RECORD_LENGTH = 7; + + /// + ///文件对象 + /// + private FileStream ipFile; + + private const string unCountry = "未知国家"; + private const string unArea = "未知地区"; + + /// + ///索引开始位置 + /// + private long ipBegin; + + /// + ///索引结束位置 + /// + private long ipEnd; + + /// + /// IP对象 + /// + private IPLocation loc; + + /// + ///存储文本内容 + /// + private byte[] buf; + + /// + ///存储3字节 + /// + private byte[] b3; + + /// + ///存储4字节IP地址 + /// + private byte[] b4; + #endregion + + #region 构造函数 + + /// + ///构造函数 + /// + ///IP数据库文件绝对路径 + public QQWry(string ipfile) + { + + buf = new byte[100]; + b3 = new byte[3]; + b4 = new byte[4]; + try + { + ipFile = new FileStream(ipfile, FileMode.Open); + } + catch (Exception ex) + { + throw new Exception(ex.Message); + } + ipBegin = readLong4(0); + ipEnd = readLong4(4); + loc = new IPLocation(); + } + #endregion + + #region 根据IP地址搜索 + + /// + ///搜索IP地址搜索 + /// + /// + /// + public IPLocation SearchIPLocation(string ip) + { + //将字符IP转换为字节 + string[] ipSp = ip.Split('.'); + if (ipSp.Length != 4) + { + throw new ArgumentOutOfRangeException("不是合法的IP地址!"); + } + byte[] IP = new byte[4]; + for (int i = 0; i < IP.Length; i++) + { + IP[i] = (byte) (Int32.Parse(ipSp[i]) & 0xFF); + } + + IPLocation local = null; + long offset = locateIP(IP); + + if (offset != -1) + { + local = getIPLocation(offset); + } + + if (local == null) + { + local = new IPLocation(); + local.area = unArea; + local.country = unCountry; + } + return local; + } + #endregion + + #region 取得具体信息 + /// + ///取得具体信息 + /// + /// + /// + private IPLocation getIPLocation(long offset) + { + ipFile.Position = offset + 4; + //读取第一个字节判断是否是标志字节 + byte one = (byte) ipFile.ReadByte(); + if (one == REDIRECT_MODE_1) + { + //第一种模式 + //读取国家偏移 + long countryOffset = readLong3(); + //转至偏移处 + ipFile.Position = countryOffset; + //再次检查标志字节 + byte b = (byte) ipFile.ReadByte(); + if (b == REDIRECT_MODE_2) + { + loc.country = readString(readLong3()); + ipFile.Position = countryOffset + 4; + } + else + loc.country = readString(countryOffset); + + //读取地区标志 + loc.area = readArea(ipFile.Position); + + } + else if (one == REDIRECT_MODE_2) + { + //第二种模式 + loc.country = readString(readLong3()); + loc.area = readArea(offset + 8); + } + else + { + //普通模式 + loc.country = readString(--ipFile.Position); + loc.area = readString(ipFile.Position); + } + return loc; + } + #endregion + + #region 取得地区信息 + /// + ///读取地区名称 + /// + /// + /// + private string readArea(long offset) + { + ipFile.Position = offset; + byte one = (byte) ipFile.ReadByte(); + if (one == REDIRECT_MODE_1 || one == REDIRECT_MODE_2) + { + long areaOffset = readLong3(offset + 1); + if (areaOffset == 0) + return unArea; + else + { + return readString(areaOffset); + } + } + else + { + return readString(offset); + } + } + + #endregion + + #region 读取字符串 + + /// + ///读取字符串 + /// + /// + /// + + private string readString(long offset) + { + ipFile.Position = offset; + int i = 0; + for (i = 0, buf[i] = (byte) ipFile.ReadByte(); buf[i] != (byte) (0); buf[++i] = (byte) ipFile.ReadByte()) ; + + if (i > 0) + return Encoding.Default.GetString(buf, 0, i); + else + return ""; + } + #endregion + + #region 查找IP地址所在的绝对偏移量 + + /**/ + + /// + ///查找IP地址所在的绝对偏移量 + /// + /// + /// + + private long locateIP(byte[] ip) + { + long m = 0; + int r; + + //比较第一个IP项 + readIP(ipBegin, b4); + r = compareIP(ip, b4); + if (r == 0) + return ipBegin; + else if (r < 0) + return -1; + //开始二分搜索 + for (long i = ipBegin, j = ipEnd; i < j;) + { + m = this.getMiddleOffset(i, j); + readIP(m, b4); + r = compareIP(ip, b4); + if (r > 0) + i = m; + else if (r < 0) + { + if (m == j) + { + j -= IP_RECORD_LENGTH; + m = j; + } + else + { + j = m; + } + } + else + return readLong3(m + 4); + } + m = readLong3(m + 4); + readIP(m, b4); + r = compareIP(ip, b4); + if (r <= 0) + return m; + else + return -1; + } + + #endregion + + #region 读出4字节的IP地址 + + /**/ + + /// + ///从当前位置读取四字节,此四字节是IP地址 + /// + /// + /// + + private void readIP(long offset, byte[] ip) + { + ipFile.Position = offset; + ipFile.Read(ip, 0, ip.Length); + byte tmp = ip[0]; + ip[0] = ip[3]; + ip[3] = tmp; + tmp = ip[1]; + ip[1] = ip[2]; + ip[2] = tmp; + } + + #endregion + + #region 比较IP地址是否相同 + + /**/ + + /// + ///比较IP地址是否相同 + /// + /// + /// + ///0:相等,1:ip大于beginIP,-1:小于 + + private int compareIP(byte[] ip, byte[] beginIP) + { + for (int i = 0; i < 4; i++) + { + int r = compareByte(ip[i], beginIP[i]); + if (r != 0) + return r; + } + return 0; + } + + #endregion + + #region 比较两个字节是否相等 + + /**/ + + /// + ///比较两个字节是否相等 + /// + /// + /// + /// + + private int compareByte(byte bsrc, byte bdst) + { + if ((bsrc & 0xFF) > (bdst & 0xFF)) + return 1; + else if ((bsrc ^ bdst) == 0) + return 0; + else + return -1; + } + + #endregion + + #region 根据当前位置读取4字节 + + /**/ + + /// + ///从当前位置读取4字节,转换为长整型 + /// + /// + /// + + private long readLong4(long offset) + { + long ret = 0; + ipFile.Position = offset; + ret |= (ipFile.ReadByte() & 0xFF); + ret |= ((ipFile.ReadByte() << 8) & 0xFF00); + ret |= ((ipFile.ReadByte() << 16) & 0xFF0000); + ret |= ((ipFile.ReadByte() << 24) & 0xFF000000); + return ret; + } + + #endregion + + #region 根据当前位置,读取3字节 + + /**/ + + /// + ///根据当前位置,读取3字节 + /// + /// + /// + + private long readLong3(long offset) + { + long ret = 0; + ipFile.Position = offset; + ret |= (ipFile.ReadByte() & 0xFF); + ret |= ((ipFile.ReadByte() << 8) & 0xFF00); + ret |= ((ipFile.ReadByte() << 16) & 0xFF0000); + return ret; + } + + #endregion + + #region 从当前位置读取3字节 + + /**/ + + /// + ///从当前位置读取3字节 + /// + /// + + private long readLong3() + { + long ret = 0; + ret |= (ipFile.ReadByte() & 0xFF); + ret |= ((ipFile.ReadByte() << 8) & 0xFF00); + ret |= ((ipFile.ReadByte() << 16) & 0xFF0000); + return ret; + } + + #endregion + + #region 取得begin和end之间的偏移量 + + /**/ + + /// + ///取得begin和end中间的偏移 + /// + /// + /// + /// + private long getMiddleOffset(long begin, long end) + { + long records = (end - begin)/IP_RECORD_LENGTH; + records >>= 1; + if (records == 0) + records = 1; + return begin + records*IP_RECORD_LENGTH; + } + + #endregion + } + + public class IPLocation + { + public String country; + public String area; + + public IPLocation() + { + country = area = ""; + } + + public IPLocation getCopy() + { + IPLocation ret = new IPLocation(); + ret.country = country; + ret.area = area; + return ret; + } + } + +#region Old + /* + /// + /// 存储地区的结构 + /// + public struct stLocation + { + /// + /// 未使用 + /// + public string Ip; + + /// + /// 国家名 + /// + public string Contry; + + /// + /// 城市名 + /// + public string City; + } + + + /// + /// 纯真IP数据库查询辅助类 + /// + public static class QqwryHelper + { + #region 成员变量 + + private const byte REDIRECT_MODE_1 = 0x01;//名称存储模式一 + private const byte REDIRECT_MODE_2 = 0x02;//名称存储模式二 + private const int IP_RECORD_LENGTH = 7; //每条索引的长度 + + private static long beginIndex = 0;//索引开始 + private static long endIndex = 0;//索引结束 + + private static stLocation loc = new stLocation() { City = "未知城市", Contry = "未知国家" }; + + private static Stream fs; + + #endregion + + #region 私有成员函数 + + /// + /// 在索引区查找指定IP对应的记录区地址 + /// + /// 字节型IP + /// + private static long SearchIpIndex(byte[] _ip) + { + long index = 0; + + byte[] nextIp = new byte[4]; + + ReadIp(beginIndex, ref nextIp); + + int flag = CompareIp(_ip, nextIp); + if (flag == 0) return beginIndex; + else if (flag < 0) return -1; + + for (long i = beginIndex, j = endIndex; i < j; ) + { + index = GetMiddleOffset(i, j); + + ReadIp(index, ref nextIp); + flag = CompareIp(_ip, nextIp); + + if (flag == 0) return ReadLong(index + 4, 3); + else if (flag > 0) i = index; + else if (flag < 0) + { + if (index == j) + { + j -= IP_RECORD_LENGTH; + index = j; + } + else + { + j = index; + } + } + } + + index = ReadLong(index + 4, 3); + ReadIp(index, ref nextIp); + + flag = CompareIp(_ip, nextIp); + if (flag <= 0) return index; + else return -1; + } + + /// + /// 获取两个索引的中间位置 + /// + /// 索引1 + /// 索引2 + /// + private static long GetMiddleOffset(long begin, long end) + { + long records = (end - begin) / IP_RECORD_LENGTH; + records >>= 1; + if (records == 0) records = 1; + return begin + records * IP_RECORD_LENGTH; + } + + /// + /// 读取记录区的地区名称 + /// + /// 位置 + /// + private static string ReadString(long offset) + { + fs.Position = offset; + + byte b = (byte)fs.ReadByte(); + if (b == REDIRECT_MODE_1 || b == REDIRECT_MODE_2) + { + long areaOffset = ReadLong(offset + 1, 3); + if (areaOffset == 0) + return "未知地区"; + + else fs.Position = areaOffset; + } + else + { + fs.Position = offset; + } + + List buf = new List(); + + int i = 0; + for (i = 0, buf.Add((byte)fs.ReadByte()); buf[i] != (byte)(0); ++i, buf.Add((byte)fs.ReadByte())) ; + + if (i > 0) return Encoding.Default.GetString(buf.ToArray(), 0, i); + else return ""; + } + + /// + /// 从自定位置读取指定长度的字节,并转换为big-endian字节序(数据源文件为little-endian字节序) + /// + /// 开始读取位置 + /// 读取长度 + /// + private static long ReadLong(long offset, int length) + { + long ret = 0; + fs.Position = offset; + for (int i = 0; i < length; i++) + { + ret |= ((fs.ReadByte() << (i * 8)) & (0xFF * ((int)Math.Pow(16, i * 2)))); + } + + return ret; + } + + /// + /// 从指定位置处读取一个IP + /// + /// 指定的位置 + /// 保存IP的缓存区 + private static void ReadIp(long offset, ref byte[] _buffIp) + { + fs.Position = offset; + fs.Read(_buffIp, 0, _buffIp.Length); + + for (int i = 0; i < _buffIp.Length / 2; i++) + { + byte temp = _buffIp[i]; + _buffIp[i] = _buffIp[_buffIp.Length - i - 1]; + _buffIp[_buffIp.Length - i - 1] = temp; + } + } + + /// + /// 比较两个IP是否相等,1:IP1大于IP2,-1:IP1小于IP2,0:IP1=IP2 + /// + /// IP1 + /// IP2 + /// + private static int CompareIp(byte[] _buffIp1, byte[] _buffIp2) + { + if (_buffIp1.Length > 4 || _buffIp2.Length > 4) throw new Exception("指定的IP无效。"); + + for (int i = 0; i < 4; i++) + { + if ((_buffIp1[i] & 0xFF) > (_buffIp2[i] & 0xFF)) return 1; + else if ((_buffIp1[i] & 0xFF) < (_buffIp2[i] & 0xFF)) return -1; + } + + return 0; + } + + /// + /// 从指定的地址获取区域名称 + /// + /// + private static void GetAreaName(long offset) + { + fs.Position = offset + 4; + long flag = fs.ReadByte(); + long contryIndex = 0; + if (flag == REDIRECT_MODE_1) + { + contryIndex = ReadLong(fs.Position, 3); + fs.Position = contryIndex; + + flag = fs.ReadByte(); + + if (flag == REDIRECT_MODE_2) //是否仍然为重定向 + { + loc.Contry = ReadString(ReadLong(fs.Position, 3)); + fs.Position = contryIndex + 4; + } + else + { + loc.Contry = ReadString(contryIndex); + } + loc.City = ReadString(fs.Position); + } + else if (flag == REDIRECT_MODE_2) + { + contryIndex = ReadLong(fs.Position, 3); + loc.Contry = ReadString(contryIndex); + loc.City = ReadString(contryIndex + 3); + } + else + { + loc.Contry = ReadString(offset + 4); + loc.City = ReadString(fs.Position); + } + } + + #endregion + + #region 公有成员函数 + + /// + /// 加载数据库文件到缓存 + /// + /// 数据库文件地址 + /// + public static void Init(string path) + { + if (fs != null) return; + var bt = File.ReadAllBytes(path); + fs = new MemoryStream(bt); + } + + /// + /// 根据IP获取区域名 + /// + /// 指定的IP + /// + public static stLocation GetLocation(string ip) + { + IPAddress ipAddress = null; + if (!IPAddress.TryParse(ip, out ipAddress)) throw new Exception("无效的IP地址。"); + + byte[] buff_local_ip = ipAddress.GetAddressBytes(); + + beginIndex = ReadLong(0, 4); + endIndex = ReadLong(4, 4); + + long offset = SearchIpIndex(buff_local_ip); + if (offset != -1) + { + GetAreaName(offset); + } + + loc.Contry = loc.Contry.Trim(); + loc.City = loc.City.Trim().Replace("CZ88.NET", ""); + + return loc; + } + + /// + /// 释放资源 + /// + public static void Dispose() + { + fs.Dispose(); + } + + #endregion + } */ +#endregion +} diff --git a/shadowsocks-csharp/Controller/AutoStartup.cs b/shadowsocks-csharp/Controller/AutoStartup.cs index 36ad775f..f406e87c 100644 --- a/shadowsocks-csharp/Controller/AutoStartup.cs +++ b/shadowsocks-csharp/Controller/AutoStartup.cs @@ -1,5 +1,5 @@ -using System; -using System.Windows.Forms; +using System; +using System.Windows.Forms; using Microsoft.Win32; namespace Shadowsocks.Controller @@ -52,4 +52,4 @@ namespace Shadowsocks.Controller } } } -} +} diff --git a/shadowsocks-csharp/Controller/FileManager.cs b/shadowsocks-csharp/Controller/FileManager.cs index cc8f3bf9..fe11adfd 100755 --- a/shadowsocks-csharp/Controller/FileManager.cs +++ b/shadowsocks-csharp/Controller/FileManager.cs @@ -29,6 +29,9 @@ namespace Shadowsocks.Controller public static void UncompressFile(string fileName, byte[] content) { + //some people use RamDisk, their Temp dir maybe disappeared, so we need check it first + if (!Directory.Exists(Path.GetDirectoryName(fileName)))Directory.CreateDirectory(Path.GetDirectoryName(fileName)); + FileStream destinationFile = File.Create(fileName); // Because the uncompressed size of the file is unknown, diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 734f27d1..c9408462 100755 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -35,7 +35,7 @@ namespace Shadowsocks.Controller public event EventHandler EnableStatusChanged; public event EventHandler EnableGlobalChanged; public event EventHandler ShareOverLANStatusChanged; - + // when user clicked Edit PAC, and PAC file has already created public event EventHandler PACFileReadyToOpen; public event EventHandler UserRuleFileReadyToOpen; @@ -137,6 +137,35 @@ namespace Shadowsocks.Controller SaveConfig(_config); } + public void SelectServerIndexTemp(int index) + { + _config.index = index; + + if (polipoRunner == null) + polipoRunner = new PolipoRunner(); + + if (_listener != null) + _listener.Stop(); + + try + { + Local local = new Local(_config); + List services = new List(); + services.Add(local); + _listener = new Listener(services); + _listener.Start(_config); + } + catch (Exception e) + { + var se = e as SocketException; + if (se?.SocketErrorCode == SocketError.AccessDenied) + e = new Exception(I18N.GetString("Port already in use"), e); + Logging.LogUsefulException(e); + ReportError(e); + } + Util.Utils.ReleaseMemory(); + } + public void Stop() { if (stopped) diff --git a/shadowsocks-csharp/Data/cn.txt b/shadowsocks-csharp/Data/cn.txt index e092575c..49b73c6f 100644 --- a/shadowsocks-csharp/Data/cn.txt +++ b/shadowsocks-csharp/Data/cn.txt @@ -71,4 +71,26 @@ No QRCode found. Try to zoom in or move it to the center of the screen.=未发 Failed to decode QRCode=无法解析二维码 Failed to update registry=无法修改注册表 System Proxy On: =系统代理已启用: -Running: Port {0}=正在运行:端口 {0} \ No newline at end of file +Running: Port {0}=正在运行:端口 {0} + + +# Pingform +UsableTest...=可用性测试... +UsableTest=服务器可用性测试 +SvcAddr=地址 +Location=物理地址 +Max=最大 +Min=最小 +Average=平均 +IP=IP地址 +Speed=下载速度 +TestSpeed=测速 +Pinging=正在获取 +PingFail=获取失败 +FailTime=失败次数 +CurrentStatus:=当前状态: +Ready=准备就绪 +DoSomething=我们正在处理一些事情 +Wrong=遇到一些问题 +Unknow=未知 +Seems your server is down, r u sure apply this server?=看起来你的服务器不可用,确定要使用这个服务器吗? \ No newline at end of file diff --git a/shadowsocks-csharp/Properties/Resources.Designer.cs b/shadowsocks-csharp/Properties/Resources.Designer.cs index a5d9c107..9d7942c5 100755 --- a/shadowsocks-csharp/Properties/Resources.Designer.cs +++ b/shadowsocks-csharp/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34209 +// Runtime Version:4.0.30319.18408 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -71,7 +71,12 @@ namespace Shadowsocks.Properties { } /// - /// Looks up a localized string similar to Shadowsocks=Shadowsocks + /// Looks up a localized string similar to # translation for Simplified Chinese + /// + ///Shadowsocks=Shadowsocks + /// + ///# Menu items + /// ///Enable System Proxy=启用系统代理 ///Mode=系统代理模式 ///PAC=PAC 模式 @@ -80,24 +85,16 @@ namespace Shadowsocks.Properties { ///Edit Servers...=编辑服务器... ///Start on Boot=开机启动 ///Allow Clients from LAN=允许来自局域网的连接 - ///Edit PAC File...=编辑 PAC 文件... + ///Local PAC=使用本地 PAC + ///Online PAC=使用在线 PAC + ///Edit Local PAC File...=编辑本地 PAC 文件... + ///Update Local PAC from GFWList=从 GFWList 更新本地 PAC ///Edit User Rule for GFWList...=编辑 GFWList 的用户规则... ///Show QRCode...=显示二维码... ///Scan QRCode from Screen...=扫描屏幕上的二维码... ///Show Logs...=显示日志... ///About...=关于... - ///Quit=退出 - ///Edit Servers=编辑服务器 - ///&Add=添加(&A) - ///&Delete=删除(&D) - ///Server=服务器 - ///Server IP=服务器 IP - ///Server Port=服务器端口 - ///Password=密码 - ///Encryption=加密 - ///Proxy Port=代理端口 - ///Remarks=备注 - ///OK= [rest of string was truncated]";. + ///Quit=退出 [rest of string was truncated]";. /// internal static string cn { get { diff --git a/shadowsocks-csharp/Util/Util.cs b/shadowsocks-csharp/Util/Util.cs index 15463a3a..0f674687 100755 --- a/shadowsocks-csharp/Util/Util.cs +++ b/shadowsocks-csharp/Util/Util.cs @@ -1,15 +1,26 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Drawing; using System.IO; using System.IO.Compression; +using System.Linq; using System.Runtime.InteropServices; using System.Text; +using System.Windows.Forms; +using Shadowsocks._3rd.ProxySocket; namespace Shadowsocks.Util { public class Utils { + public static Shadowsocks._3rd.QQWry qqwry; + public static Font GetFont() + { + var fs = FontFamily.Families; + var ret = fs.FirstOrDefault(f => f.Name == "Microsoft YaHei UI Light") ?? fs.FirstOrDefault(f => f.Name == "Microsoft YaHei UI"); + return ret == null ? SystemFonts.MessageBoxFont : new Font(ret,9f,FontStyle.Regular); + } public static void ReleaseMemory() { // release any unused pages diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 574fafd5..62bfe145 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -22,11 +22,12 @@ namespace Shadowsocks.View public ConfigForm(ShadowsocksController controller) { - this.Font = System.Drawing.SystemFonts.MessageBoxFont; + //this.Font = System.Drawing.SystemFonts.MessageBoxFont; + Font = Util.Utils.GetFont(); InitializeComponent(); // a dirty hack - this.ServersListBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.ServersListBox.Dock = DockStyle.Fill; this.PerformLayout(); UpdateTexts(); diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index ff5a018c..4fcc6c6f 100755 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -42,6 +42,9 @@ namespace Shadowsocks.View private MenuItem editGFWUserRuleItem; private MenuItem editOnlinePACItem; private ConfigForm configForm; + + private PingForm pingForm; + private string _urlToOpen; public MenuViewController(ShadowsocksController controller) @@ -132,12 +135,12 @@ namespace Shadowsocks.View + "\n" + config.GetCurrentServer().FriendlyName(); _notifyIcon.Text = text.Substring(0, Math.Min(63, text.Length)); } - private MenuItem CreateMenuItem(string text, EventHandler click) { return new MenuItem(I18N.GetString(text), click); } + private MenuItem CreateMenuGroup(string text, MenuItem[] items) { return new MenuItem(I18N.GetString(text), items); @@ -170,6 +173,8 @@ namespace Shadowsocks.View this.AutoStartupItem = CreateMenuItem("Start on Boot", new EventHandler(this.AutoStartupItem_Click)), this.ShareOverLANItem = CreateMenuItem("Allow Clients from LAN", new EventHandler(this.ShareOverLANItem_Click)), new MenuItem("-"), + CreateMenuItem("UsableTest...", new EventHandler(this.PingForm_Click)), + new MenuItem("-"), CreateMenuItem("Show Logs...", new EventHandler(this.ShowLogItem_Click)), CreateMenuItem("About...", new EventHandler(this.AboutItem_Click)), new MenuItem("-"), @@ -280,6 +285,29 @@ namespace Shadowsocks.View } } + private void ShowPingForm() + { + if (pingForm != null) + { + pingForm.Activate(); + } + else + { + pingForm = new PingForm(controller); + pingForm.Show(); + pingForm.FormClosed += pingForm_FormClosed; + } + } + private void PingForm_Click(object sender, EventArgs e) + { + ShowPingForm(); + } + void pingForm_FormClosed(object sender, FormClosedEventArgs e) + { + pingForm = null; + Util.Utils.ReleaseMemory(); + } + private void ShowConfigForm() { if (configForm != null) diff --git a/shadowsocks-csharp/View/PingForm.Designer.cs b/shadowsocks-csharp/View/PingForm.Designer.cs new file mode 100644 index 00000000..6f5e2a4c --- /dev/null +++ b/shadowsocks-csharp/View/PingForm.Designer.cs @@ -0,0 +1,237 @@ +using Microsoft.VisualBasic; + +namespace Shadowsocks.View +{ + sealed partial class PingForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dgvMain = new System.Windows.Forms.DataGridView(); + this.SvcAddr = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.IP = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Remarks = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Location = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Max = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Min = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Average = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.FailTime = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Speed = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.TestSpeed = new System.Windows.Forms.DataGridViewLinkColumn(); + this.statusStrip1 = new System.Windows.Forms.StatusStrip(); + this.tssStatusLabel = new System.Windows.Forms.ToolStripStatusLabel(); + this.tssStatus = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); + this.pBar = new System.Windows.Forms.ToolStripProgressBar(); + ((System.ComponentModel.ISupportInitialize)(this.dgvMain)).BeginInit(); + this.statusStrip1.SuspendLayout(); + this.SuspendLayout(); + // + // dgvMain + // + this.dgvMain.AllowUserToAddRows = false; + this.dgvMain.AllowUserToDeleteRows = false; + this.dgvMain.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dgvMain.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.SvcAddr, + this.IP, + this.Remarks, + this.Location, + this.Max, + this.Min, + this.Average, + this.FailTime, + this.Speed, + this.TestSpeed}); + this.dgvMain.Dock = System.Windows.Forms.DockStyle.Fill; + this.dgvMain.Location = new System.Drawing.Point(0, 0); + this.dgvMain.Name = "dgvMain"; + this.dgvMain.ReadOnly = true; + this.dgvMain.RowTemplate.Height = 23; + this.dgvMain.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.dgvMain.Size = new System.Drawing.Size(878, 507); + this.dgvMain.TabIndex = 0; + this.dgvMain.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMain_CellClick); + this.dgvMain.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dgvMain_CellMouseDoubleClick); + // + // SvcAddr + // + this.SvcAddr.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; + this.SvcAddr.Frozen = true; + this.SvcAddr.HeaderText = "Address"; + this.SvcAddr.Name = "SvcAddr"; + this.SvcAddr.ReadOnly = true; + this.SvcAddr.Width = 72; + // + // IP + // + this.IP.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; + this.IP.HeaderText = "IP"; + this.IP.Name = "IP"; + this.IP.ReadOnly = true; + this.IP.Width = 42; + // + // Remarks + // + this.Remarks.HeaderText = "Remark"; + this.Remarks.Name = "Remarks"; + this.Remarks.ReadOnly = true; + this.Remarks.Width = 80; + // + // Location + // + this.Location.HeaderText = "Location"; + this.Location.MinimumWidth = 70; + this.Location.Name = "Location"; + this.Location.ReadOnly = true; + this.Location.Width = 150; + // + // Max + // + this.Max.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; + this.Max.HeaderText = "Max"; + this.Max.Name = "Max"; + this.Max.ReadOnly = true; + this.Max.Width = 48; + // + // Min + // + this.Min.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; + this.Min.HeaderText = "Min"; + this.Min.Name = "Min"; + this.Min.ReadOnly = true; + this.Min.Width = 48; + // + // Average + // + this.Average.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; + this.Average.HeaderText = "Average"; + this.Average.Name = "Average"; + this.Average.ReadOnly = true; + this.Average.Width = 72; + // + // FailTime + // + this.FailTime.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; + this.FailTime.HeaderText = "FailTime"; + this.FailTime.Name = "FailTime"; + this.FailTime.ReadOnly = true; + this.FailTime.Width = 78; + // + // Speed + // + this.Speed.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; + this.Speed.HeaderText = "Speed"; + this.Speed.Name = "Speed"; + this.Speed.ReadOnly = true; + this.Speed.Width = 60; + // + // TestSpeed + // + this.TestSpeed.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells; + this.TestSpeed.HeaderText = "TestSpeed"; + this.TestSpeed.Name = "TestSpeed"; + this.TestSpeed.ReadOnly = true; + this.TestSpeed.Width = 65; + // + // statusStrip1 + // + this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.tssStatusLabel, + this.tssStatus, + this.toolStripStatusLabel1, + this.pBar}); + this.statusStrip1.Location = new System.Drawing.Point(0, 485); + this.statusStrip1.Name = "statusStrip1"; + this.statusStrip1.Size = new System.Drawing.Size(878, 22); + this.statusStrip1.TabIndex = 1; + this.statusStrip1.Text = "statusStrip1"; + // + // tssStatusLabel + // + this.tssStatusLabel.Name = "tssStatusLabel"; + this.tssStatusLabel.Size = new System.Drawing.Size(89, 17); + this.tssStatusLabel.Text = "CurrentStatus:"; + // + // tssStatus + // + this.tssStatus.Name = "tssStatus"; + this.tssStatus.Size = new System.Drawing.Size(55, 17); + this.tssStatus.Text = "Unknow"; + // + // toolStripStatusLabel1 + // + this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; + this.toolStripStatusLabel1.Size = new System.Drawing.Size(719, 17); + this.toolStripStatusLabel1.Spring = true; + // + // pBar + // + this.pBar.Name = "pBar"; + this.pBar.Size = new System.Drawing.Size(100, 16); + this.pBar.Style = System.Windows.Forms.ProgressBarStyle.Marquee; + this.pBar.Visible = false; + // + // PingForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; + this.AutoSize = true; + this.ClientSize = new System.Drawing.Size(878, 507); + this.Controls.Add(this.statusStrip1); + this.Controls.Add(this.dgvMain); + this.Name = "PingForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "PingForm"; + ((System.ComponentModel.ISupportInitialize)(this.dgvMain)).EndInit(); + this.statusStrip1.ResumeLayout(false); + this.statusStrip1.PerformLayout(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.DataGridView dgvMain; + private System.Windows.Forms.DataGridViewTextBoxColumn Local; + private System.Windows.Forms.StatusStrip statusStrip1; + private System.Windows.Forms.ToolStripStatusLabel tssStatusLabel; + private System.Windows.Forms.ToolStripStatusLabel tssStatus; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; + private System.Windows.Forms.ToolStripProgressBar pBar; + private System.Windows.Forms.DataGridViewTextBoxColumn SvcAddr; + private System.Windows.Forms.DataGridViewTextBoxColumn IP; + private System.Windows.Forms.DataGridViewTextBoxColumn Remarks; + private System.Windows.Forms.DataGridViewTextBoxColumn Location; + private System.Windows.Forms.DataGridViewTextBoxColumn Max; + private System.Windows.Forms.DataGridViewTextBoxColumn Min; + private System.Windows.Forms.DataGridViewTextBoxColumn Average; + private System.Windows.Forms.DataGridViewTextBoxColumn FailTime; + private System.Windows.Forms.DataGridViewTextBoxColumn Speed; + private System.Windows.Forms.DataGridViewLinkColumn TestSpeed; + } +} \ No newline at end of file diff --git a/shadowsocks-csharp/View/PingForm.cs b/shadowsocks-csharp/View/PingForm.cs new file mode 100644 index 00000000..187b4030 --- /dev/null +++ b/shadowsocks-csharp/View/PingForm.cs @@ -0,0 +1,325 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Net.NetworkInformation; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using System.Windows.Forms; +using Shadowsocks.Controller; +using Shadowsocks.Model; +using Shadowsocks.Properties; +using Shadowsocks._3rd; +using Shadowsocks._3rd.ProxySocket; + +namespace Shadowsocks.View +{ + public sealed partial class PingForm : Form + { + private readonly ShadowsocksController controller; + + #region Delegate + private delegate void selectMinMax(); + + private void SelectMinMax() + { + if (dgvMain.InvokeRequired) + { + var invoke = new selectMinMax(SelectMinMax); + Invoke(invoke); + } + else + { + var q = dgvMain.Rows.Cast().Where(row => (int)row.Cells["Average"].Value != 9999).ToArray(); + int max = q.Max(x => Convert.ToInt32(x.Cells["Average"].Value)); + int min = q.Min(x => Convert.ToInt32(x.Cells["Average"].Value)); + foreach (DataGridViewRow row in dgvMain.Rows) + { + if ((int)row.Cells["Average"].Value == min) row.DefaultCellStyle.ForeColor = Color.Green; + if ((int)row.Cells["Average"].Value == max) row.DefaultCellStyle.ForeColor = Color.Red; + } + } + } + + private delegate void changeStatus(string val); + private void ChangeStatus(string val) + { + if (statusStrip1.InvokeRequired) + { + var invoke = new changeStatus(ChangeStatus); + Invoke(invoke, val); + } + else + { + if (val == I18N.GetString("Ready")) + { + pBar.Visible = false; + dgvMain.Enabled = true; + tssStatus.Text = I18N.GetString("Ready"); + tssStatus.ForeColor = Color.Green; + } + else if (val == I18N.GetString("Wrong")) + { + pBar.Visible = false; + dgvMain.Enabled = true; + tssStatus.Text = I18N.GetString("Wrong"); + tssStatus.ForeColor = Color.Red; + } + else + { + pBar.Visible = true; + dgvMain.Enabled = false; + tssStatus.Text = val; + tssStatus.ForeColor = Color.Blue; + } + } + } + private delegate void modifyRow(int rowID, string ip,string loc, int max, int min, int avg, int failtime); + private void ModifyRow(int rowID, string ip,string loc, int max, int min, int avg, int failtime) + { + if (dgvMain.InvokeRequired) + { + var del = new modifyRow(ModifyRow); + Invoke(del, new object[] { rowID, ip,loc, max, min, avg, failtime }); + } + else + { + dgvMain.Rows[rowID].Cells[1].Value = string.IsNullOrEmpty(ip) ? I18N.GetString("PingFail") : ip; + dgvMain.Rows[rowID].Cells[4].Value = max; + dgvMain.Rows[rowID].Cells[5].Value = min; + dgvMain.Rows[rowID].Cells[6].Value = avg; + dgvMain.Rows[rowID].Cells[7].Value = failtime; + if (!string.IsNullOrEmpty(loc)) dgvMain.Rows[rowID].Cells[3].Value = loc; + } + } + + private delegate void modifyRowSpeed(int rowID,string ip, string loc,string speed); + + private void ModifyRowSpeed(int rowID, string ip, string loc, string speed) + { + if (dgvMain.InvokeRequired) + { + var del = new modifyRowSpeed(ModifyRowSpeed); + Invoke(del, new object[] {rowID, ip, loc, speed}); + } + else + { + dgvMain.Rows[rowID].Cells[1].Value = string.IsNullOrEmpty(ip) ? I18N.GetString("PingFail") : ip; + if (!string.IsNullOrEmpty(loc)) dgvMain.Rows[rowID].Cells[3].Value = loc; + if (!string.IsNullOrEmpty(speed)) dgvMain.Rows[rowID].Cells[8].Value = speed; + } + } + + #endregion + + #region Method + private void Ping(object r) + { + var row = r as DataGridViewRow; + if (row == null) return; + var addr = ""; + var result = new List(); + var failTime = 0; + + using (var ping = new Ping()) + { + for (var times = 0; times < 4; times++) + { + try + { + var reply = ping.Send((string)row.Cells[0].Value, 2000); + if (reply == null) { failTime++; continue;} + addr = reply.Address.ToString(); + if (reply.Status == IPStatus.Success) + { + result.Add(reply.RoundtripTime); + } + else + { + failTime++; + } + } + catch + { + failTime++; + } + } + } + var location = ""; + if (Util.Utils.qqwry != null & addr != "") + { + var v = Util.Utils.qqwry.SearchIPLocation(addr); + location = (v.country + v.area.Replace("CZ88.NET", "")); + } + if(result.Count == 0) + ModifyRow(row.Index, addr,location, 9999, 9999, 9999, 10); + else + ModifyRow(row.Index, addr,location, (int)result.Max(), (int)result.Min(), (int)result.Average(), failTime); + } + + private void Go(object rc) + { + var rows = rc as DataGridViewRowCollection; + if (rows != null) + { + foreach (DataGridViewRow row in rows) + Ping(row); + SelectMinMax(); + ChangeStatus(I18N.GetString("Ready")); + } + else + { + ChangeStatus(I18N.GetString("Wrong")); + } + } + + private void Test(object index) + { + try + { + var rowIndex = (int)index; + string ip, location; + float speed; + GetIPAddress(rowIndex, out ip, out location, out speed); + ModifyRowSpeed(rowIndex, ip, location, speed.ToString("N0") + "KB/s"); + ChangeStatus(I18N.GetString("Ready")); + } + catch + { + ChangeStatus(I18N.GetString("Wrong")); + } + } + + private void GetIPAddress(int svc, out string addr, out string stat, out float speed) + { + var currentIndex = controller.GetConfiguration().index; + controller.SelectServerIndexTemp(svc); + var webClient = new SocksWebClient { ProxyDetails = new ProxyDetails(controller.GetConfiguration().localPort)}; + + try + { + //get location + var regx1 = new Regex(@"\d+\.\d+\.\d+\.\d+"); + var regx2 = new Regex(@"来自:(.*?)\<"); + var response = webClient.DownloadString(@"http://1111.ip138.com/ic.asp"); + + var mc1 = regx1.Match(response); + addr = mc1.Success ? mc1.Value : I18N.GetString("Unknow"); + + var mc2 = regx2.Match(response); + stat = mc2.Success ? mc2.Groups[1].Value : I18N.GetString("…(⊙_⊙;)…"); + } + catch + { + addr = I18N.GetString("Unknow"); + stat = I18N.GetString("…(⊙_⊙;)…"); + } + + try + { + //speed test + var sw = System.Diagnostics.Stopwatch.StartNew(); + var dl = webClient.DownloadData("https://dl.google.com/tag/s/appguid%3D%7B8A69D345-D564-463C-AFF1-A69D9E530F96%7D%26iid%3D%7B7B1E2CBF-95F1-5FDD-C836-E5930E3E51CD%7D%26lang%3Den%26browser%3D4%26usagestats%3D0%26appname%3DGoogle%2520Chrome%26needsadmin%3Dprefers%26installdataindex%3Ddefaultbrowser/update2/installers/ChromeSetup.exe");//http://dl.google.com/googletalk/googletalk-setup.exe + sw.Stop(); + var len = dl.Length / 1024f; + var sec = sw.Elapsed.Milliseconds / 1000f; + speed = len / sec; + } + catch + { + speed = 0; + } + webClient.Dispose(); + controller.SelectServerIndexTemp(currentIndex); + } + #endregion + public PingForm(ShadowsocksController sc) + { + InitializeComponent(); + + var qqwryPath = Environment.CurrentDirectory + "\\qqwry.dat"; + if (Util.Utils.qqwry == null && File.Exists(qqwryPath)) Util.Utils.qqwry = new QQWry(qqwryPath); + + controller = sc; + + Font = Util.Utils.GetFont(); + + PerformLayout(); + + UpdateTexts(); + + Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); + + LoadConfiguration(controller.GetConfiguration()); + + if (dgvMain.Rows.Count <= 5) + { + foreach (var row in dgvMain.Rows) + { + var t = new Thread(Ping); + t.Start(row); + } + } + else + { + ChangeStatus(I18N.GetString("DoSomething")); + var t = new Thread(Go); + t.Start(dgvMain.Rows); + } + + } + + private void UpdateTexts() + { + foreach (DataGridViewColumn col in dgvMain.Columns) + col.HeaderText = I18N.GetString(col.Name); + Text = I18N.GetString("UsableTest"); + tssStatusLabel.Text = I18N.GetString("CurrentStatus:"); + tssStatus.Text = I18N.GetString("Ready"); + } + + + private void LoadConfiguration(Configuration configuration) + { + dgvMain.Rows.Clear(); + foreach (Server server in configuration.configs) + { + int index = dgvMain.Rows.Add(); + dgvMain.Rows[index].Cells[0].Value = server.server; + dgvMain.Rows[index].Cells[1].Value = I18N.GetString("Pinging"); + dgvMain.Rows[index].Cells[2].Value = server.remarks; + dgvMain.Rows[index].Cells[9].Value = I18N.GetString("TestSpeed"); + } + var row = dgvMain.Rows[configuration.index]; + dgvMain.ClearSelection(); + dgvMain.CurrentCell = row.Cells[0]; + row.Selected = true; + } + + + private void dgvMain_CellClick(object sender, DataGridViewCellEventArgs e) + { + if (dgvMain.Columns[e.ColumnIndex].Name != "TestSpeed") return; + ChangeStatus(I18N.GetString("DoSomething")); + var t = new Thread(Test) {IsBackground = true}; + t.Start(e.RowIndex); + //10sec time out because WebClient dont have Timeout property + var tm = new System.Timers.Timer(10000); + tm.Elapsed += (ss, ee) =>{ tm.Stop(); t.Abort(); }; + tm.Start(); + } + + private void dgvMain_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) + { + if (e.RowIndex < 0) return; + if ((int) dgvMain.Rows[e.RowIndex].Cells[4].Value == 9999) + { if (MessageBox.Show(I18N.GetString("Seems your server is down, r u sure apply this server?"), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return; } + controller.SelectServerIndex(e.RowIndex); + Close(); + } + } +} diff --git a/shadowsocks-csharp/View/PingForm.resx b/shadowsocks-csharp/View/PingForm.resx new file mode 100644 index 00000000..c18db4ca --- /dev/null +++ b/shadowsocks-csharp/View/PingForm.resx @@ -0,0 +1,147 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + 17, 17 + + \ No newline at end of file diff --git a/shadowsocks-csharp/app.config b/shadowsocks-csharp/app.config index 867ff468..a7ba1069 100755 --- a/shadowsocks-csharp/app.config +++ b/shadowsocks-csharp/app.config @@ -1,6 +1,19 @@ - + - - - + + + + + + + + + + + + + + + + diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index aa2c9000..fc6239c2 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -1,76 +1,70 @@  - + + Debug AnyCPU - 9.0.21022 - 2.0 - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062} + {F58374A5-9AFB-430A-AF20-C509D3DCED3F} WinExe Properties Shadowsocks Shadowsocks - v2.0 + v4.0 512 - - - shadowsocks.ico - false - - - - - 3.5 - - - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 1 - 1.0.0.%2a - false - true - + + x86 true - bin\x86\Debug\ - TRACE;DEBUG full - x86 + false + bin\Debug\ + DEBUG;TRACE prompt - ManagedMinimumRules.ruleset - false + 4 - - bin\x86\Release\ - TRACE - true - pdbonly + x86 + pdbonly + true + bin\Release\ + TRACE prompt - ManagedMinimumRules.ruleset - false - true + 4 - app.manifest + shadowsocks.ico + + + + + + - + + + + + + + + + + + + + + Component + + + @@ -117,7 +111,6 @@ - @@ -126,29 +119,30 @@ - + + + + + + + - - - - - True - True - Resources.resx - + + + Form @@ -156,13 +150,13 @@ ConfigForm.cs - - - - - - + + Form + + + PingForm.cs + Form @@ -172,68 +166,41 @@ Form - - ConfigForm.cs - Designer - ResXFileCodeGenerator - Designer Resources.Designer.cs + Designer + + + True + Resources.resx + True + + + ConfigForm.cs + + + PingForm.cs QRCodeForm.cs - - Designer - + - - - - + + + + + - - - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 2.0 %28x86%29 - false - - - False - .NET Framework 3.0 %28x86%29 - false - - - False - .NET Framework 3.5 - true - - - False - .NET Framework 3.5 SP1 - false - - - False - Windows Installer 3.1 - true -