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