Skip to content

Commit

Permalink
3.9.6
Browse files Browse the repository at this point in the history
  • Loading branch information
breakwa11 committed Oct 17, 2016
1 parent 4a070ab commit f8d8f27
Show file tree
Hide file tree
Showing 29 changed files with 1,086 additions and 595 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ TestResults
*.user
*.exe

/templates
13 changes: 8 additions & 5 deletions shadowsocks-csharp/Controller/APIServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ public bool Handle(byte[] firstPacket, int length, Socket socket)
}
else if (kv.Length == 1)
{
if (line.IndexOf("api?") > 0)
if (line.IndexOf("auth=" + _config.localAuthPassword) > 0)
{
req = line.Substring(line.IndexOf("api?") + 4);
if (line.IndexOf("GET ") == 0 || line.IndexOf("POST ") == 0)
if (line.IndexOf(" /api?") > 0)
{
pathMatch = true;
req = req.Substring(0, req.IndexOf(" "));
req = line.Substring(line.IndexOf("api?") + 4);
if (line.IndexOf("GET ") == 0 || line.IndexOf("POST ") == 0)
{
pathMatch = true;
req = req.Substring(0, req.IndexOf(" "));
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion shadowsocks-csharp/Controller/HttpPortForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ private void Connect()
{
try
{
// TODO async resolving
IPAddress ipAddress;
bool parsed = IPAddress.TryParse("127.0.0.1", out ipAddress);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, _targetPort);
Expand Down
137 changes: 56 additions & 81 deletions shadowsocks-csharp/Controller/Local.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Timers;
using System.Threading;
using OpenDNS;
using Shadowsocks.Util;

namespace Shadowsocks.Controller
{
Expand Down Expand Up @@ -55,10 +56,13 @@ class Local : Listener.Service
{
private Configuration _config;
private ServerTransferTotal _transfer;
public Local(Configuration config, ServerTransferTotal transfer)
private IPRangeSet _IPRange;

public Local(Configuration config, ServerTransferTotal transfer, IPRangeSet IPRange)
{
this._config = config;
this._transfer = transfer;
_config = config;
_transfer = transfer;
_IPRange = IPRange;
}

protected bool Accept(byte[] firstPacket, int length)
Expand Down Expand Up @@ -93,7 +97,7 @@ public bool Handle(byte[] firstPacket, int length, Socket socket)
{
return false;
}
new ProxyAuthHandler(_config, _transfer, firstPacket, length, socket);
new ProxyAuthHandler(_config, _transfer, _IPRange, firstPacket, length, socket);
return true;
}
}
Expand Down Expand Up @@ -142,9 +146,8 @@ class Handler
protected ProtocolResponseDetector detector = new ProtocolResponseDetector();
// remote socket.
//protected Socket remote;
protected ProxySocket remote;
protected ProxySocket remoteUDP;
protected DnsQuery dns;
protected ProxyEncryptSocket remote;
protected ProxyEncryptSocket remoteUDP;
// Size of receive buffer.
protected const int RecvSize = 4096;
protected const int BufferSize = 16384;
Expand Down Expand Up @@ -257,7 +260,7 @@ private void timer_Elapsed(object sender, ElapsedEventArgs e)

try
{
if (cfg.try_keep_alive <= 0 && State == ConnectState.CONNECTED && remote != null && remote.CanSendKeepAlive)
if (cfg.try_keep_alive <= 0 && State == ConnectState.CONNECTED && remote != null && remoteUDP == null && remote.CanSendKeepAlive)
{
cfg.try_keep_alive++;
RemoteSendWithoutCallback(null, -1);
Expand Down Expand Up @@ -528,7 +531,7 @@ private void BeginConnect(IPAddress ipAddress, int serverPort)
|| connectionUDP == null
|| connectionUDP != null && server.udp_over_tcp)
{
remote = new ProxySocket(ipAddress.AddressFamily,
remote = new ProxyEncryptSocket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
remote.GetSocket().SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
try
Expand All @@ -547,7 +550,7 @@ private void BeginConnect(IPAddress ipAddress, int serverPort)
{
try
{
remoteUDP = new ProxySocket(ipAddress.AddressFamily,
remoteUDP = new ProxyEncryptSocket(ipAddress.AddressFamily,
SocketType.Dgram, ProtocolType.Udp);
remoteUDP.GetSocket().Bind(new IPEndPoint(ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0));

Expand Down Expand Up @@ -648,13 +651,13 @@ private void CloseSocket(ref Socket sock)
}
}

private void CloseSocket(ref ProxySocket sock)
private void CloseSocket(ref ProxyEncryptSocket sock)
{
lock (this)
{
if (sock != null)
{
ProxySocket s = sock;
ProxyEncryptSocket s = sock;
sock = null;
try
{
Expand Down Expand Up @@ -783,72 +786,6 @@ private bool ConnectProxyServer(string strRemoteHost, int iRemotePort)
}
}

private IPAddress QueryDns(string host, string dns_servers)
{
IPAddress ipAddress;
bool parsed = IPAddress.TryParse(host, out ipAddress);
if (!parsed)
{
if (server.DnsBuffer().isExpired(host))
{
if (dns_servers != null && dns_servers.Length > 0)
{
OpenDNS.Types[] types;
//if (false)
// types = new Types[] { Types.AAAA, Types.A };
//else
types = new Types[] { Types.A, Types.AAAA };
string[] dns_server = dns_servers.Split(',');
for (int query_i = 0; query_i < types.Length; ++query_i)
{
dns = new DnsQuery(host, types[query_i]);
dns.RecursionDesired = true;
foreach (string server in dns_server)
{
dns.Servers.Add(server);
}
if (dns.Send())
{
int count = dns.Response.Answers.Count;
if (count > 0)
{
for (int i = 0; i < count; ++i)
{
if (((ResourceRecord)dns.Response.Answers[i]).Type != types[query_i])
continue;
return ((OpenDNS.Address)dns.Response.Answers[i]).IP;
}
}
}
}
}
{
try
{
GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
IAsyncResult result = callback.BeginInvoke(host, null, null);
if (result.AsyncWaitHandle.WaitOne(5, false))
{
foreach(IPAddress ad in callback.EndInvoke(result).AddressList)
{
return ad;
//if (ad.AddressFamily == AddressFamily.InterNetwork)
//{
// return ad;
//}
}
}
}
catch
{

}
}
}
}
return ipAddress;
}

private void Connect()
{
remote = null;
Expand Down Expand Up @@ -876,13 +813,44 @@ private void Connect()
ResetTimeout(cfg.TTL);
if (cfg.fouce_local_dns_query && cfg.targetHost != null)
{
IPAddress ipAddress = QueryDns(cfg.targetHost, cfg.dns_servers);
IPAddress ipAddress;
//if (server.DnsBuffer().isExpired(cfg.targetHost))
//{
// ipAddress = Util.Utils.QueryDns(cfg.targetHost, cfg.dns_servers);
//}
//else
//{
// ipAddress = server.DnsBuffer().ip;
//}
//if (ipAddress != null)
//{
// server.DnsBuffer().UpdateDns(cfg.targetHost, ipAddress);
// cfg.targetHost = ipAddress.ToString();
// ResetTimeout(cfg.TTL);
//}

string host = cfg.targetHost;

if (!IPAddress.TryParse(host, out ipAddress))
{
ipAddress = Utils.DnsBuffer.Get(host);
}
if (ipAddress == null)
{
ipAddress = Utils.QueryDns(host, cfg.dns_servers);
}
if (ipAddress != null)
{
server.DnsBuffer().UpdateDns(cfg.targetHost, ipAddress);
Utils.DnsBuffer.Set(host, ipAddress);
Utils.DnsBuffer.Sweep();

cfg.targetHost = ipAddress.ToString();
ResetTimeout(cfg.TTL);
}
else
{
//throw new SocketException((int)SocketError.HostNotFound);
}
}

lock (this)
Expand Down Expand Up @@ -911,7 +879,14 @@ private void Connect()
bool dns_ok = false;
if (!dns_ok)
{
ipAddress = QueryDns(serverURI, cfg.dns_servers);
if (server.DnsBuffer().isExpired(serverURI))
{
ipAddress = Util.Utils.QueryDns(serverURI, cfg.dns_servers);
}
else
{
ipAddress = server.DnsBuffer().ip;
}
if (ipAddress != null)
{
server.DnsBuffer().UpdateDns(serverURI, ipAddress);
Expand Down
59 changes: 31 additions & 28 deletions shadowsocks-csharp/Controller/PACServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,44 +72,47 @@ public bool Handle(byte[] firstPacket, int length, Socket socket)
}
else if (kv.Length == 1)
{
if (line.IndexOf("pac") > 0 && line.IndexOf("GET") == 0)
if (line.IndexOf("auth=" + _config.localAuthPassword) > 0)
{
string url = line.Substring(line.IndexOf(" ") + 1);
url = url.Substring(0, url.IndexOf(" "));
pathMatch = true;
int port_pos = url.IndexOf("port=");
if (port_pos > 0)
if (line.IndexOf(" /pac?") > 0 && line.IndexOf("GET") == 0)
{
string port = url.Substring(port_pos + 5);
if (port.IndexOf("&") >= 0)
string url = line.Substring(line.IndexOf(" ") + 1);
url = url.Substring(0, url.IndexOf(" "));
pathMatch = true;
int port_pos = url.IndexOf("port=");
if (port_pos > 0)
{
port = port.Substring(0, port.IndexOf("&"));
}
string port = url.Substring(port_pos + 5);
if (port.IndexOf("&") >= 0)
{
port = port.Substring(0, port.IndexOf("&"));
}

int ip_pos = url.IndexOf("ip=");
if (ip_pos > 0)
{
proxy = url.Substring(ip_pos + 3);
if (proxy.IndexOf("&") >= 0)
int ip_pos = url.IndexOf("ip=");
if (ip_pos > 0)
{
proxy = proxy.Substring(0, proxy.IndexOf("&"));
proxy = url.Substring(ip_pos + 3);
if (proxy.IndexOf("&") >= 0)
{
proxy = proxy.Substring(0, proxy.IndexOf("&"));
}
proxy += ":" + port + ";";
}
proxy += ":" + port + ";";
else
{
proxy = "127.0.0.1:" + port + ";";
}
}

if (url.IndexOf("type=socks4") > 0 || url.IndexOf("type=s4") > 0)
{
socksType = 4;
}
else
if (url.IndexOf("type=socks5") > 0 || url.IndexOf("type=s5") > 0)
{
proxy = "127.0.0.1:" + port + ";";
socksType = 5;
}
}

if (url.IndexOf("type=socks4") > 0 || url.IndexOf("type=s4") > 0)
{
socksType = 4;
}
if (url.IndexOf("type=socks5") > 0 || url.IndexOf("type=s5") > 0)
{
socksType = 5;
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions shadowsocks-csharp/Controller/ProxyAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ProxyAuthHandler
{
private Configuration _config;
private ServerTransferTotal _transfer;

private IPRangeSet _IPRange;

private byte[] _firstPacket;
private int _firstPacketLength;
Expand All @@ -40,12 +40,13 @@ class ProxyAuthHandler

protected HttpPraser httpProxyState;

public ProxyAuthHandler(Configuration config, ServerTransferTotal transfer, byte[] firstPacket, int length, Socket socket)
public ProxyAuthHandler(Configuration config, ServerTransferTotal transfer, IPRangeSet IPRange, byte[] firstPacket, int length, Socket socket)
{
int local_port = ((IPEndPoint)socket.LocalEndPoint).Port;

_config = config;
_transfer = transfer;
_IPRange = IPRange;
_firstPacket = firstPacket;
_firstPacketLength = length;
_connection = socket;
Expand Down Expand Up @@ -527,7 +528,7 @@ private void Connect()
}
else
{
if (_connectionUDP == null && new Socks5Forwarder(_config).Handle(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, _connection))
if (_connectionUDP == null && new Socks5Forwarder(_config, _IPRange).Handle(_remoteHeaderSendBuffer, _remoteHeaderSendBuffer.Length, _connection))
{
return;
}
Expand Down
Loading

0 comments on commit f8d8f27

Please sign in to comment.