@@ -149,13 +149,13 @@ namespace shadowsocks_csharp.Encrypt | |||||
} | } | ||||
if (_encryptCtx != IntPtr.Zero) | |||||
if (_encryptCtx.ToInt64() != 0) | |||||
{ | { | ||||
OpenSSL.EVP_CIPHER_CTX_cleanup(_encryptCtx); | OpenSSL.EVP_CIPHER_CTX_cleanup(_encryptCtx); | ||||
OpenSSL.EVP_CIPHER_CTX_free(_encryptCtx); | OpenSSL.EVP_CIPHER_CTX_free(_encryptCtx); | ||||
_encryptCtx = IntPtr.Zero; | _encryptCtx = IntPtr.Zero; | ||||
} | |||||
if (_decryptCtx != IntPtr.Zero) | |||||
} | |||||
if (_decryptCtx.ToInt64() != 0) | |||||
{ | { | ||||
OpenSSL.EVP_CIPHER_CTX_cleanup(_decryptCtx); | OpenSSL.EVP_CIPHER_CTX_cleanup(_decryptCtx); | ||||
OpenSSL.EVP_CIPHER_CTX_free(_decryptCtx); | OpenSSL.EVP_CIPHER_CTX_free(_decryptCtx); | ||||
@@ -11,6 +11,7 @@ namespace shadowsocks_csharp | |||||
public partial class Form1 : Form | public partial class Form1 : Form | ||||
{ | { | ||||
Local local; | Local local; | ||||
PACServer pacServer; | |||||
Config config; | Config config; | ||||
public Form1() | public Form1() | ||||
@@ -45,6 +46,8 @@ namespace shadowsocks_csharp | |||||
this.Hide(); | this.Hide(); | ||||
})); | })); | ||||
} | } | ||||
pacServer = new PACServer(); | |||||
pacServer.Start(); | |||||
} | } | ||||
private void reload(Config config) | private void reload(Config config) | ||||
@@ -144,7 +144,17 @@ namespace shadowsocks_csharp | |||||
public void Close() | public void Close() | ||||
{ | { | ||||
connection.Shutdown(SocketShutdown.Send); | |||||
if (connection != null) | |||||
{ | |||||
try | |||||
{ | |||||
connection.Shutdown(SocketShutdown.Send); | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
Console.WriteLine(e.ToString()); | |||||
} | |||||
} | |||||
if (remote != null) | if (remote != null) | ||||
{ | { | ||||
try | try | ||||
@@ -198,9 +208,14 @@ namespace shadowsocks_csharp | |||||
{ | { | ||||
int bytesRead = connection.EndReceive(ar); | int bytesRead = connection.EndReceive(ar); | ||||
if (bytesRead > 0) | |||||
if (bytesRead > 1) | |||||
{ | { | ||||
byte[] response = { 5, 0 }; | byte[] response = { 5, 0 }; | ||||
if (connetionRecvBuffer[0] != 5) | |||||
{ | |||||
// reject socks 4 | |||||
response = new byte[]{ 0, 91 }; | |||||
} | |||||
connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(handshakeSendCallback), null); | connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(handshakeSendCallback), null); | ||||
} | } | ||||
else | else | ||||
@@ -0,0 +1,89 @@ | |||||
using shadowsocks_csharp.Properties; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Net; | |||||
using System.Net.Sockets; | |||||
using System.Text; | |||||
namespace shadowsocks_csharp | |||||
{ | |||||
class PACServer | |||||
{ | |||||
Socket listener; | |||||
public void Start() | |||||
{ | |||||
// Create a TCP/IP socket. | |||||
listener = new Socket(AddressFamily.InterNetwork, | |||||
SocketType.Stream, ProtocolType.Tcp); | |||||
IPEndPoint localEndPoint = new IPEndPoint(0, 8090); | |||||
// Bind the socket to the local endpoint and listen for incoming connections. | |||||
listener.Bind(localEndPoint); | |||||
listener.Listen(100); | |||||
listener.BeginAccept( | |||||
new AsyncCallback(AcceptCallback), | |||||
listener); | |||||
} | |||||
public void AcceptCallback(IAsyncResult ar) | |||||
{ | |||||
try | |||||
{ | |||||
Socket listener = (Socket)ar.AsyncState; | |||||
listener.BeginAccept( | |||||
new AsyncCallback(AcceptCallback), | |||||
listener); | |||||
Socket conn = listener.EndAccept(ar); | |||||
conn.BeginReceive(new byte[1024], 0, 256, 0, | |||||
new AsyncCallback(receiveCallback), conn); | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
Console.WriteLine(e.ToString()); | |||||
} | |||||
} | |||||
private void receiveCallback(IAsyncResult ar) | |||||
{ | |||||
Socket conn = (Socket)ar.AsyncState; | |||||
try | |||||
{ | |||||
int bytesRead = conn.EndReceive(ar); | |||||
string pac = Resources.proxy_pac; | |||||
if (bytesRead > 0) | |||||
{ | |||||
string text = String.Format(@"HTTP/1.1 200 OK | |||||
Server: Shadowsocks | |||||
Content-Type: application/x-ns-proxy-autoconfig | |||||
Content-Length: {0} | |||||
Connection: Close | |||||
", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac; | |||||
byte[] response = System.Text.Encoding.UTF8.GetBytes(text); | |||||
conn.BeginSend(response, 0, response.Length, 0, new AsyncCallback(sendCallback), conn); | |||||
} | |||||
else | |||||
{ | |||||
conn.Close(); | |||||
} | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
Console.WriteLine(e.ToString()); | |||||
conn.Close(); | |||||
} | |||||
} | |||||
private void sendCallback(IAsyncResult ar) | |||||
{ | |||||
Socket conn = (Socket)ar.AsyncState; | |||||
conn.Shutdown(SocketShutdown.Send); | |||||
} | |||||
} | |||||
} |
@@ -1,7 +1,7 @@ | |||||
//------------------------------------------------------------------------------ | //------------------------------------------------------------------------------ | ||||
// <auto-generated> | // <auto-generated> | ||||
// This code was generated by a tool. | // This code was generated by a tool. | ||||
// Runtime Version:4.0.30319.17929 | |||||
// Runtime Version:4.0.30319.18444 | |||||
// | // | ||||
// Changes to this file may cause incorrect behavior and will be lost if | // Changes to this file may cause incorrect behavior and will be lost if | ||||
// the code is regenerated. | // the code is regenerated. | ||||
@@ -59,5 +59,37 @@ namespace shadowsocks_csharp.Properties { | |||||
resourceCulture = value; | resourceCulture = value; | ||||
} | } | ||||
} | } | ||||
/// <summary> | |||||
/// Looks up a localized string similar to // Generated by gfwlist2pac | |||||
///// https://github.com/clowwindy/gfwlist2pac | |||||
/// | |||||
///var domains = { | |||||
/// "gimpshop.com": 1, | |||||
/// "directcreative.com": 1, | |||||
/// "speedpluss.org": 1, | |||||
/// "mingpaovan.com": 1, | |||||
/// "wikinews.org": 1, | |||||
/// "joachims.org": 1, | |||||
/// "maiio.net": 1, | |||||
/// "idv.tw": 1, | |||||
/// "mail-archive.com": 1, | |||||
/// "surfeasy.com.au": 1, | |||||
/// "hihistory.net": 1, | |||||
/// "alexlur.org": 1, | |||||
/// "finalion.jp": 1, | |||||
/// "nrk.no": 1, | |||||
/// "nyt.com": 1, | |||||
/// "cmule.com": 1, | |||||
/// "gappp.org": 1, | |||||
/// "givemesomethingtoread.com": 1, | |||||
/// "yahoo.com.tw": 1, | |||||
/// [rest of string was truncated]";. | |||||
/// </summary> | |||||
internal static string proxy_pac { | |||||
get { | |||||
return ResourceManager.GetString("proxy_pac", resourceCulture); | |||||
} | |||||
} | |||||
} | } | ||||
} | } |
@@ -46,7 +46,7 @@ | |||||
mimetype: application/x-microsoft.net.object.binary.base64 | mimetype: application/x-microsoft.net.object.binary.base64 | ||||
value : The object must be serialized with | value : The object must be serialized with | ||||
: System.Serialization.Formatters.Binary.BinaryFormatter | |||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter | |||||
: and then encoded with base64 encoding. | : and then encoded with base64 encoding. | ||||
mimetype: application/x-microsoft.net.object.soap.base64 | mimetype: application/x-microsoft.net.object.soap.base64 | ||||
@@ -60,6 +60,7 @@ | |||||
: and then encoded with base64 encoding. | : and then encoded with base64 encoding. | ||||
--> | --> | ||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> | <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> | ||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> | |||||
<xsd:element name="root" msdata:IsDataSet="true"> | <xsd:element name="root" msdata:IsDataSet="true"> | ||||
<xsd:complexType> | <xsd:complexType> | ||||
<xsd:choice maxOccurs="unbounded"> | <xsd:choice maxOccurs="unbounded"> | ||||
@@ -68,9 +69,10 @@ | |||||
<xsd:sequence> | <xsd:sequence> | ||||
<xsd:element name="value" type="xsd:string" minOccurs="0" /> | <xsd:element name="value" type="xsd:string" minOccurs="0" /> | ||||
</xsd:sequence> | </xsd:sequence> | ||||
<xsd:attribute name="name" type="xsd:string" /> | |||||
<xsd:attribute name="name" use="required" type="xsd:string" /> | |||||
<xsd:attribute name="type" type="xsd:string" /> | <xsd:attribute name="type" type="xsd:string" /> | ||||
<xsd:attribute name="mimetype" type="xsd:string" /> | <xsd:attribute name="mimetype" type="xsd:string" /> | ||||
<xsd:attribute ref="xml:space" /> | |||||
</xsd:complexType> | </xsd:complexType> | ||||
</xsd:element> | </xsd:element> | ||||
<xsd:element name="assembly"> | <xsd:element name="assembly"> | ||||
@@ -85,9 +87,10 @@ | |||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | ||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> | <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> | ||||
</xsd:sequence> | </xsd:sequence> | ||||
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" /> | |||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> | |||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> | <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> | ||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> | <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> | ||||
<xsd:attribute ref="xml:space" /> | |||||
</xsd:complexType> | </xsd:complexType> | ||||
</xsd:element> | </xsd:element> | ||||
<xsd:element name="resheader"> | <xsd:element name="resheader"> | ||||
@@ -114,4 +117,8 @@ | |||||
<resheader name="writer"> | <resheader name="writer"> | ||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||||
</resheader> | </resheader> | ||||
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> | |||||
<data name="proxy_pac" type="System.Resources.ResXFileRef, System.Windows.Forms"> | |||||
<value>..\proxy.pac.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;gb2312</value> | |||||
</data> | |||||
</root> | </root> |
@@ -87,6 +87,7 @@ | |||||
<Compile Include="Encrypt\OpensslEncryptor.cs" /> | <Compile Include="Encrypt\OpensslEncryptor.cs" /> | ||||
<Compile Include="Encrypt\TableEncryptor.cs" /> | <Compile Include="Encrypt\TableEncryptor.cs" /> | ||||
<Compile Include="Encrypt\IEncryptor.cs" /> | <Compile Include="Encrypt\IEncryptor.cs" /> | ||||
<Compile Include="PACServer.cs" /> | |||||
<Compile Include="RC4.cs" /> | <Compile Include="RC4.cs" /> | ||||
<Compile Include="Config.cs" /> | <Compile Include="Config.cs" /> | ||||
<Compile Include="Form1.cs"> | <Compile Include="Form1.cs"> | ||||
@@ -126,6 +127,7 @@ | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<Content Include="icon144.ico" /> | <Content Include="icon144.ico" /> | ||||
<None Include="proxy.pac.txt" /> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<BootstrapperPackage Include="Microsoft.Net.Client.3.5"> | <BootstrapperPackage Include="Microsoft.Net.Client.3.5"> | ||||