Let proxy module handle name resolving itself.
Fix wrong output in proxyConnectTimer_Elapsed. Signed-off-by: noisyfox <timemanager.rick@gmail.com>
This commit is contained in:
@@ -302,7 +302,8 @@ namespace Shadowsocks.Controller
|
||||
if (ar.AsyncState != null)
|
||||
{
|
||||
connection.EndSend(ar);
|
||||
Logging.Debug(remote.LocalEndPoint, remote.DestEndPoint, RecvSize, "TCP Relay");
|
||||
// TODO: need fix
|
||||
//Logging.Debug(remote.LocalEndPoint, remote.DestEndPoint, RecvSize, "TCP Relay");
|
||||
connection.BeginReceive(_connetionRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(ReadAll), null);
|
||||
}
|
||||
else
|
||||
@@ -310,7 +311,8 @@ namespace Shadowsocks.Controller
|
||||
int bytesRead = connection.EndReceive(ar);
|
||||
if (bytesRead > 0)
|
||||
{
|
||||
Logging.Debug(remote.LocalEndPoint, remote.DestEndPoint, RecvSize, "TCP Relay");
|
||||
// TODO: need fix
|
||||
//Logging.Debug(remote.LocalEndPoint, remote.DestEndPoint, RecvSize, "TCP Relay");
|
||||
connection.BeginReceive(_connetionRecvBuffer, 0, RecvSize, SocketFlags.None, new AsyncCallback(ReadAll), null);
|
||||
}
|
||||
else
|
||||
@@ -341,7 +343,9 @@ namespace Shadowsocks.Controller
|
||||
// inner class
|
||||
private class ProxyTimer : Timer
|
||||
{
|
||||
public EndPoint DestEndPoint;
|
||||
public IProxy Proxy;
|
||||
public string DestHost;
|
||||
public int DestPort;
|
||||
public Server Server;
|
||||
|
||||
public ProxyTimer(int p) : base(p)
|
||||
@@ -361,23 +365,15 @@ namespace Shadowsocks.Controller
|
||||
{
|
||||
CreateRemote();
|
||||
|
||||
// TODO async resolving
|
||||
IPAddress ipAddress;
|
||||
bool parsed = IPAddress.TryParse(server.server, out ipAddress);
|
||||
if (!parsed)
|
||||
{
|
||||
IPHostEntry ipHostInfo = Dns.GetHostEntry(server.server);
|
||||
ipAddress = ipHostInfo.AddressList[0];
|
||||
}
|
||||
IPEndPoint destEP = new IPEndPoint(ipAddress, server.server_port);
|
||||
|
||||
// Setting up proxy
|
||||
IPEndPoint proxyEP;
|
||||
if (_config.useProxy)
|
||||
{
|
||||
parsed = IPAddress.TryParse(_config.proxyServer, out ipAddress);
|
||||
IPAddress ipAddress;
|
||||
bool parsed = IPAddress.TryParse(_config.proxyServer, out ipAddress);
|
||||
if (!parsed)
|
||||
{
|
||||
// TODO really necessary to resolve a proxy's address? Maybe from local hosts?
|
||||
IPHostEntry ipHostInfo = Dns.GetHostEntry(_config.proxyServer);
|
||||
ipAddress = ipHostInfo.AddressList[0];
|
||||
}
|
||||
@@ -388,7 +384,7 @@ namespace Shadowsocks.Controller
|
||||
else
|
||||
{
|
||||
remote = new DirectConnect();
|
||||
proxyEP = destEP;
|
||||
proxyEP = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -396,7 +392,10 @@ namespace Shadowsocks.Controller
|
||||
proxyTimer.AutoReset = false;
|
||||
proxyTimer.Elapsed += proxyConnectTimer_Elapsed;
|
||||
proxyTimer.Enabled = true;
|
||||
proxyTimer.DestEndPoint = destEP;
|
||||
|
||||
proxyTimer.Proxy = remote;
|
||||
proxyTimer.DestHost = server.server;
|
||||
proxyTimer.DestPort = server.server_port;
|
||||
proxyTimer.Server = server;
|
||||
|
||||
_proxyConnected = false;
|
||||
@@ -417,9 +416,9 @@ namespace Shadowsocks.Controller
|
||||
{
|
||||
return;
|
||||
}
|
||||
var ep = ((ProxyTimer)sender).DestEndPoint;
|
||||
var proxy = ((ProxyTimer)sender).Proxy;
|
||||
|
||||
Logging.Info($"Proxy {ep} timed out");
|
||||
Logging.Info($"Proxy {proxy.ProxyEndPoint} timed out");
|
||||
remote?.Close();
|
||||
RetryConnect();
|
||||
}
|
||||
@@ -434,7 +433,8 @@ namespace Shadowsocks.Controller
|
||||
try
|
||||
{
|
||||
ProxyTimer timer = (ProxyTimer)ar.AsyncState;
|
||||
var destEP = timer.DestEndPoint;
|
||||
var destHost = timer.DestHost;
|
||||
var destPort = timer.DestPort;
|
||||
server = timer.Server;
|
||||
timer.Elapsed -= proxyConnectTimer_Elapsed;
|
||||
timer.Enabled = false;
|
||||
@@ -462,7 +462,7 @@ namespace Shadowsocks.Controller
|
||||
|
||||
_destConnected = false;
|
||||
// Connect to the remote endpoint.
|
||||
remote.BeginConnectDest(destEP, new AsyncCallback(ConnectCallback), connectTimer);
|
||||
remote.BeginConnectDest(destHost, destPort, new AsyncCallback(ConnectCallback), connectTimer);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
|
||||
@@ -20,19 +20,28 @@ namespace Shadowsocks.Proxy
|
||||
public bool CompletedSynchronously { get; } = true;
|
||||
}
|
||||
|
||||
private class FakeEndPoint : EndPoint
|
||||
{
|
||||
public override AddressFamily AddressFamily { get; } = AddressFamily.Unspecified;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "null proxy";
|
||||
}
|
||||
}
|
||||
|
||||
private Socket _remote;
|
||||
|
||||
public EndPoint LocalEndPoint => _remote.LocalEndPoint;
|
||||
|
||||
public EndPoint ProxyEndPoint { get; private set; }
|
||||
|
||||
public EndPoint DestEndPoint { get; private set; }
|
||||
public EndPoint ProxyEndPoint { get; } = new FakeEndPoint();
|
||||
public string DestHost { get; private set; }
|
||||
public int DestPort { get; private set; }
|
||||
|
||||
|
||||
public void BeginConnectProxy(EndPoint remoteEP, AsyncCallback callback, object state)
|
||||
{
|
||||
// do nothing
|
||||
ProxyEndPoint = remoteEP;
|
||||
|
||||
var r = new FakeAsyncResult(state);
|
||||
callback?.Invoke(r);
|
||||
@@ -43,16 +52,26 @@ namespace Shadowsocks.Proxy
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public void BeginConnectDest(EndPoint remoteEP, AsyncCallback callback, object state)
|
||||
public void BeginConnectDest(string host, int port, AsyncCallback callback, object state)
|
||||
{
|
||||
// TODO async resolving
|
||||
IPAddress ipAddress;
|
||||
bool parsed = IPAddress.TryParse(host, out ipAddress);
|
||||
if (!parsed)
|
||||
{
|
||||
IPHostEntry ipHostInfo = Dns.GetHostEntry(host);
|
||||
ipAddress = ipHostInfo.AddressList[0];
|
||||
}
|
||||
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
|
||||
|
||||
DestHost = host;
|
||||
DestPort = port;
|
||||
|
||||
if (_remote == null)
|
||||
{
|
||||
_remote = new Socket(remoteEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||
_remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
|
||||
}
|
||||
|
||||
DestEndPoint = remoteEP;
|
||||
|
||||
_remote.BeginConnect(remoteEP, callback, state);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,13 +11,15 @@ namespace Shadowsocks.Proxy
|
||||
|
||||
EndPoint ProxyEndPoint { get; }
|
||||
|
||||
EndPoint DestEndPoint { get; }
|
||||
string DestHost { get; }
|
||||
|
||||
int DestPort { get; }
|
||||
|
||||
void BeginConnectProxy(EndPoint remoteEP, AsyncCallback callback, object state);
|
||||
|
||||
void EndConnectProxy(IAsyncResult asyncResult);
|
||||
|
||||
void BeginConnectDest(EndPoint remoteEP, AsyncCallback callback, object state);
|
||||
void BeginConnectDest(string host, int port, AsyncCallback callback, object state);
|
||||
|
||||
void EndConnectDest(IAsyncResult asyncResult);
|
||||
|
||||
|
||||
@@ -47,7 +47,8 @@ namespace Shadowsocks.Proxy
|
||||
|
||||
public EndPoint LocalEndPoint => _remote.LocalEndPoint;
|
||||
public EndPoint ProxyEndPoint { get; private set; }
|
||||
public EndPoint DestEndPoint { get; private set; }
|
||||
public string DestHost { get; private set; }
|
||||
public int DestPort { get; private set; }
|
||||
|
||||
public void BeginConnectProxy(EndPoint remoteEP, AsyncCallback callback, object state)
|
||||
{
|
||||
@@ -73,13 +74,20 @@ namespace Shadowsocks.Proxy
|
||||
}
|
||||
}
|
||||
|
||||
public void BeginConnectDest(EndPoint remoteEP, AsyncCallback callback, object state)
|
||||
public void BeginConnectDest(string host, int port, AsyncCallback callback, object state)
|
||||
{
|
||||
var ep = remoteEP as IPEndPoint;
|
||||
if (ep == null)
|
||||
// TODO resolving by proxy
|
||||
IPAddress ipAddress;
|
||||
bool parsed = IPAddress.TryParse(host, out ipAddress);
|
||||
if (!parsed)
|
||||
{
|
||||
throw new Exception(I18N.GetString("Proxy request faild"));
|
||||
IPHostEntry ipHostInfo = Dns.GetHostEntry(host);
|
||||
ipAddress = ipHostInfo.AddressList[0];
|
||||
}
|
||||
IPEndPoint ep = new IPEndPoint(ipAddress, port);
|
||||
|
||||
DestHost = host;
|
||||
DestPort = port;
|
||||
|
||||
byte[] request = null;
|
||||
byte atyp = 0;
|
||||
@@ -113,8 +121,6 @@ namespace Shadowsocks.Proxy
|
||||
st.Callback = callback;
|
||||
st.AsyncState = state;
|
||||
|
||||
DestEndPoint = remoteEP;
|
||||
|
||||
_remote?.BeginSend(request, 0, request.Length, 0, Socks5RequestSendCallback, st);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user