Commit 270c57c9 authored by Honfika's avatar Honfika

soks4 cleanup

parent 5bd2c5ce
...@@ -110,7 +110,7 @@ namespace Titanium.Web.Proxy.ProxySocket.Authentication ...@@ -110,7 +110,7 @@ namespace Titanium.Web.Proxy.ProxySocket.Authentication
{ {
CallBack = callback; CallBack = callback;
Server.BeginSend(GetAuthenticationBytes(), 0, GetAuthenticationLength(), SocketFlags.None, Server.BeginSend(GetAuthenticationBytes(), 0, GetAuthenticationLength(), SocketFlags.None,
new AsyncCallback(this.OnSent), Server); this.OnSent, Server);
return; return;
} }
...@@ -125,7 +125,7 @@ namespace Titanium.Web.Proxy.ProxySocket.Authentication ...@@ -125,7 +125,7 @@ namespace Titanium.Web.Proxy.ProxySocket.Authentication
if (Server.EndSend(ar) < GetAuthenticationLength()) if (Server.EndSend(ar) < GetAuthenticationLength())
throw new SocketException(10054); throw new SocketException(10054);
Buffer = new byte[2]; Buffer = new byte[2];
Server.BeginReceive(Buffer, 0, 2, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); Server.BeginReceive(Buffer, 0, 2, SocketFlags.None, this.OnReceive, Server);
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -152,7 +152,7 @@ namespace Titanium.Web.Proxy.ProxySocket.Authentication ...@@ -152,7 +152,7 @@ namespace Titanium.Web.Proxy.ProxySocket.Authentication
throw new ProxyException("Username/password combination not accepted."); throw new ProxyException("Username/password combination not accepted.");
else else
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None,
new AsyncCallback(this.OnReceive), Server); this.OnReceive, Server);
} }
catch (Exception e) catch (Exception e)
{ {
......
...@@ -170,11 +170,12 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -170,11 +170,12 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <param name="remoteEP">An IPEndPoint that represents the remote device.</param> /// <param name="remoteEP">An IPEndPoint that represents the remote device.</param>
/// <param name="callback">The method to call when the negotiation is complete.</param> /// <param name="callback">The method to call when the negotiation is complete.</param>
/// <param name="proxyEndPoint">The IPEndPoint of the HTTPS proxy server.</param> /// <param name="proxyEndPoint">The IPEndPoint of the HTTPS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> /// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback,
IPEndPoint proxyEndPoint) IPEndPoint proxyEndPoint, object state)
{ {
return BeginNegotiate(remoteEP.Address.ToString(), remoteEP.Port, callback, proxyEndPoint); return BeginNegotiate(remoteEP.Address.ToString(), remoteEP.Port, callback, proxyEndPoint, state);
} }
/// <summary> /// <summary>
...@@ -184,14 +185,15 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -184,14 +185,15 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <param name="port">The port to connect to.</param> /// <param name="port">The port to connect to.</param>
/// <param name="callback">The method to call when the negotiation is complete.</param> /// <param name="callback">The method to call when the negotiation is complete.</param>
/// <param name="proxyEndPoint">The IPEndPoint of the HTTPS proxy server.</param> /// <param name="proxyEndPoint">The IPEndPoint of the HTTPS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> /// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback,
IPEndPoint proxyEndPoint) IPEndPoint proxyEndPoint, object state)
{ {
ProtocolComplete = callback; ProtocolComplete = callback;
Buffer = GetConnectBytes(host, port); Buffer = GetConnectBytes(host, port);
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); Server.BeginConnect(proxyEndPoint, this.OnConnect, Server);
AsyncResult = new IAsyncProxyResult(); AsyncResult = new IAsyncProxyResult(state);
return AsyncResult; return AsyncResult;
} }
...@@ -213,7 +215,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -213,7 +215,7 @@ namespace Titanium.Web.Proxy.ProxySocket
try try
{ {
Server.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnConnectSent), Server.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, this.OnConnectSent,
null); null);
} }
catch (Exception e) catch (Exception e)
...@@ -233,7 +235,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -233,7 +235,7 @@ namespace Titanium.Web.Proxy.ProxySocket
HandleEndSend(ar, Buffer.Length); HandleEndSend(ar, Buffer.Length);
Buffer = new byte[13]; Buffer = new byte[13];
Received = 0; Received = 0;
Server.BeginReceive(Buffer, 0, 13, SocketFlags.None, new AsyncCallback(this.OnConnectReceive), Server); Server.BeginReceive(Buffer, 0, 13, SocketFlags.None, this.OnConnectReceive, Server);
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -262,7 +264,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -262,7 +264,7 @@ namespace Titanium.Web.Proxy.ProxySocket
if (Received < 13) if (Received < 13)
{ {
Server.BeginReceive(Buffer, Received, 13 - Received, SocketFlags.None, Server.BeginReceive(Buffer, Received, 13 - Received, SocketFlags.None,
new AsyncCallback(this.OnConnectReceive), Server); this.OnConnectReceive, Server);
} }
else else
{ {
...@@ -305,7 +307,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -305,7 +307,7 @@ namespace Titanium.Web.Proxy.ProxySocket
} }
else else
{ {
Server.BeginReceive(Buffer, 0, 1, SocketFlags.None, new AsyncCallback(this.OnEndHeadersReceive), Server.BeginReceive(Buffer, 0, 1, SocketFlags.None, this.OnEndHeadersReceive,
Server); Server);
} }
} }
......
...@@ -220,14 +220,15 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -220,14 +220,15 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <param name="port">The port to connect to.</param> /// <param name="port">The port to connect to.</param>
/// <param name="callback">The method to call when the negotiation is complete.</param> /// <param name="callback">The method to call when the negotiation is complete.</param>
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> /// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> /// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback,
IPEndPoint proxyEndPoint) IPEndPoint proxyEndPoint, object state)
{ {
ProtocolComplete = callback; ProtocolComplete = callback;
HandShake = GetHostPortBytes(host, port); HandShake = GetHostPortBytes(host, port);
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); Server.BeginConnect(proxyEndPoint, this.OnConnect, Server);
AsyncResult = new IAsyncProxyResult(); AsyncResult = new IAsyncProxyResult(state);
return AsyncResult; return AsyncResult;
} }
...@@ -237,14 +238,15 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -237,14 +238,15 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <param name="remoteEP">An IPEndPoint that represents the remote device.</param> /// <param name="remoteEP">An IPEndPoint that represents the remote device.</param>
/// <param name="callback">The method to call when the negotiation is complete.</param> /// <param name="callback">The method to call when the negotiation is complete.</param>
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> /// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> /// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback,
IPEndPoint proxyEndPoint) IPEndPoint proxyEndPoint, object state)
{ {
ProtocolComplete = callback; ProtocolComplete = callback;
HandShake = GetEndPointBytes(remoteEP); HandShake = GetEndPointBytes(remoteEP);
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); Server.BeginConnect(proxyEndPoint, this.OnConnect, Server);
AsyncResult = new IAsyncProxyResult(); AsyncResult = new IAsyncProxyResult(state);
return AsyncResult; return AsyncResult;
} }
...@@ -266,7 +268,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -266,7 +268,7 @@ namespace Titanium.Web.Proxy.ProxySocket
try try
{ {
Server.BeginSend(new byte[] { 5, 2, 0, 2 }, 0, 4, SocketFlags.None, new AsyncCallback(this.OnAuthSent), Server.BeginSend(new byte[] { 5, 2, 0, 2 }, 0, 4, SocketFlags.None, this.OnAuthSent,
Server); Server);
} }
catch (Exception e) catch (Exception e)
...@@ -295,7 +297,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -295,7 +297,7 @@ namespace Titanium.Web.Proxy.ProxySocket
{ {
Buffer = new byte[1024]; Buffer = new byte[1024];
Received = 0; Received = 0;
Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnAuthReceive), Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, this.OnAuthReceive,
Server); Server);
} }
catch (Exception e) catch (Exception e)
...@@ -325,7 +327,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -325,7 +327,7 @@ namespace Titanium.Web.Proxy.ProxySocket
if (Received < 2) if (Received < 2)
{ {
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None,
new AsyncCallback(this.OnAuthReceive), Server); this.OnAuthReceive, Server);
} }
else else
{ {
...@@ -343,7 +345,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -343,7 +345,7 @@ namespace Titanium.Web.Proxy.ProxySocket
return; return;
} }
authenticate.BeginAuthenticate(new HandShakeComplete(this.OnAuthenticated)); authenticate.BeginAuthenticate(this.OnAuthenticated);
} }
} }
catch (Exception e) catch (Exception e)
...@@ -366,7 +368,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -366,7 +368,7 @@ namespace Titanium.Web.Proxy.ProxySocket
try try
{ {
Server.BeginSend(HandShake, 0, HandShake.Length, SocketFlags.None, new AsyncCallback(this.OnSent), Server.BeginSend(HandShake, 0, HandShake.Length, SocketFlags.None, this.OnSent,
Server); Server);
} }
catch (Exception ex) catch (Exception ex)
...@@ -395,7 +397,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -395,7 +397,7 @@ namespace Titanium.Web.Proxy.ProxySocket
{ {
Buffer = new byte[5]; Buffer = new byte[5];
Received = 0; Received = 0;
Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceive), Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, this.OnReceive,
Server); Server);
} }
catch (Exception e) catch (Exception e)
...@@ -426,7 +428,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -426,7 +428,7 @@ namespace Titanium.Web.Proxy.ProxySocket
ProcessReply(Buffer); ProcessReply(Buffer);
else else
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None,
new AsyncCallback(this.OnReceive), Server); this.OnReceive, Server);
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -457,7 +459,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -457,7 +459,7 @@ namespace Titanium.Web.Proxy.ProxySocket
} }
Received = 0; Received = 0;
Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReadLast), Server); Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, this.OnReadLast, Server);
} }
/// <summary> /// <summary>
...@@ -482,7 +484,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -482,7 +484,7 @@ namespace Titanium.Web.Proxy.ProxySocket
ProtocolComplete(null); ProtocolComplete(null);
else else
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None,
new AsyncCallback(this.OnReadLast), Server); this.OnReadLast, Server);
} }
catch (Exception e) catch (Exception e)
{ {
......
...@@ -37,7 +37,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -37,7 +37,7 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <summary> /// <summary>
/// References the callback method to be called when the protocol negotiation is completed. /// References the callback method to be called when the protocol negotiation is completed.
/// </summary> /// </summary>
internal delegate void HandShakeComplete(Exception error); internal delegate void HandShakeComplete(Exception? error);
/// <summary> /// <summary>
/// Implements a specific version of the SOCKS protocol. This is an abstract class; it must be inherited. /// Implements a specific version of the SOCKS protocol. This is an abstract class; it must be inherited.
...@@ -56,6 +56,18 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -56,6 +56,18 @@ namespace Titanium.Web.Proxy.ProxySocket
Username = user; Username = user;
} }
/// <summary>
/// Converts a port number to an array of bytes.
/// </summary>
/// <param name="port">The port to convert.</param>
/// <param name="buffer">The buffer which contains the result data.</param>
/// <returns>An array of two bytes that represents the specified port.</returns>
protected void PortToBytes(int port, Span<byte> buffer)
{
buffer[0] = (byte)(port / 256);
buffer[1] = (byte)(port % 256);
}
/// <summary> /// <summary>
/// Converts a port number to an array of bytes. /// Converts a port number to an array of bytes.
/// </summary> /// </summary>
...@@ -163,11 +175,7 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -163,11 +175,7 @@ namespace Titanium.Web.Proxy.ProxySocket
/// Gets or sets the return value of the BeginConnect call. /// Gets or sets the return value of the BeginConnect call.
/// </summary> /// </summary>
/// <value>An IAsyncProxyResult object that is the return value of the BeginConnect call.</value> /// <value>An IAsyncProxyResult object that is the return value of the BeginConnect call.</value>
protected IAsyncProxyResult AsyncResult protected IAsyncProxyResult AsyncResult { get; set; }
{
get => _asyncResult;
set => _asyncResult = value;
}
/// <summary> /// <summary>
/// Gets or sets a byte buffer. /// Gets or sets a byte buffer.
...@@ -175,6 +183,11 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -175,6 +183,11 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <value>An array of bytes.</value> /// <value>An array of bytes.</value>
protected byte[] Buffer { get; set; } protected byte[] Buffer { get; set; }
/// <summary>
/// Gets or sets actual data count in the buffer.
/// </summary>
protected int BufferCount { get; set; }
/// <summary> /// <summary>
/// Gets or sets the number of bytes that have been received from the remote proxy server. /// Gets or sets the number of bytes that have been received from the remote proxy server.
/// </summary> /// </summary>
...@@ -188,9 +201,6 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -188,9 +201,6 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <summary>Holds the value of the Username property.</summary> /// <summary>Holds the value of the Username property.</summary>
private string _username; private string _username;
/// <summary>Holds the value of the AsyncResult property.</summary>
private IAsyncProxyResult _asyncResult;
/// <summary>Holds the address of the method to call when the SOCKS protocol has been completed.</summary> /// <summary>Holds the address of the method to call when the SOCKS protocol has been completed.</summary>
protected HandShakeComplete ProtocolComplete; protected HandShakeComplete ProtocolComplete;
...@@ -213,9 +223,10 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -213,9 +223,10 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <param name="remoteEP">An IPEndPoint that represents the remote device. </param> /// <param name="remoteEP">An IPEndPoint that represents the remote device. </param>
/// <param name="callback">The method to call when the connection has been established.</param> /// <param name="callback">The method to call when the connection has been established.</param>
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> /// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> /// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public abstract IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, public abstract IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback,
IPEndPoint proxyEndPoint); IPEndPoint proxyEndPoint, object state);
/// <summary> /// <summary>
/// Starts negotiating asynchronously with a SOCKS proxy server. /// Starts negotiating asynchronously with a SOCKS proxy server.
...@@ -224,8 +235,9 @@ namespace Titanium.Web.Proxy.ProxySocket ...@@ -224,8 +235,9 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <param name="port">The remote port to connect to.</param> /// <param name="port">The remote port to connect to.</param>
/// <param name="callback">The method to call when the connection has been established.</param> /// <param name="callback">The method to call when the connection has been established.</param>
/// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param> /// <param name="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns> /// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public abstract IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, public abstract IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback,
IPEndPoint proxyEndPoint); IPEndPoint proxyEndPoint, object state);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment