Commit 270c57c9 authored by Honfika's avatar Honfika

soks4 cleanup

parent 5bd2c5ce
......@@ -110,7 +110,7 @@ namespace Titanium.Web.Proxy.ProxySocket.Authentication
{
CallBack = callback;
Server.BeginSend(GetAuthenticationBytes(), 0, GetAuthenticationLength(), SocketFlags.None,
new AsyncCallback(this.OnSent), Server);
this.OnSent, Server);
return;
}
......@@ -125,7 +125,7 @@ namespace Titanium.Web.Proxy.ProxySocket.Authentication
if (Server.EndSend(ar) < GetAuthenticationLength())
throw new SocketException(10054);
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)
{
......@@ -152,7 +152,7 @@ namespace Titanium.Web.Proxy.ProxySocket.Authentication
throw new ProxyException("Username/password combination not accepted.");
else
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None,
new AsyncCallback(this.OnReceive), Server);
this.OnReceive, Server);
}
catch (Exception e)
{
......
......@@ -170,11 +170,12 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <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="proxyEndPoint">The IPEndPoint of the HTTPS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
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>
......@@ -184,14 +185,15 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <param name="port">The port to connect to.</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="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback,
IPEndPoint proxyEndPoint)
IPEndPoint proxyEndPoint, object state)
{
ProtocolComplete = callback;
Buffer = GetConnectBytes(host, port);
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server);
AsyncResult = new IAsyncProxyResult();
Server.BeginConnect(proxyEndPoint, this.OnConnect, Server);
AsyncResult = new IAsyncProxyResult(state);
return AsyncResult;
}
......@@ -213,7 +215,7 @@ namespace Titanium.Web.Proxy.ProxySocket
try
{
Server.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnConnectSent),
Server.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, this.OnConnectSent,
null);
}
catch (Exception e)
......@@ -233,7 +235,7 @@ namespace Titanium.Web.Proxy.ProxySocket
HandleEndSend(ar, Buffer.Length);
Buffer = new byte[13];
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)
{
......@@ -262,7 +264,7 @@ namespace Titanium.Web.Proxy.ProxySocket
if (Received < 13)
{
Server.BeginReceive(Buffer, Received, 13 - Received, SocketFlags.None,
new AsyncCallback(this.OnConnectReceive), Server);
this.OnConnectReceive, Server);
}
else
{
......@@ -305,7 +307,7 @@ namespace Titanium.Web.Proxy.ProxySocket
}
else
{
Server.BeginReceive(Buffer, 0, 1, SocketFlags.None, new AsyncCallback(this.OnEndHeadersReceive),
Server.BeginReceive(Buffer, 0, 1, SocketFlags.None, this.OnEndHeadersReceive,
Server);
}
}
......
......@@ -220,14 +220,15 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <param name="port">The port to connect to.</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="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback,
IPEndPoint proxyEndPoint)
IPEndPoint proxyEndPoint, object state)
{
ProtocolComplete = callback;
HandShake = GetHostPortBytes(host, port);
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server);
AsyncResult = new IAsyncProxyResult();
Server.BeginConnect(proxyEndPoint, this.OnConnect, Server);
AsyncResult = new IAsyncProxyResult(state);
return AsyncResult;
}
......@@ -237,14 +238,15 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <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="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback,
IPEndPoint proxyEndPoint)
IPEndPoint proxyEndPoint, object state)
{
ProtocolComplete = callback;
HandShake = GetEndPointBytes(remoteEP);
Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server);
AsyncResult = new IAsyncProxyResult();
Server.BeginConnect(proxyEndPoint, this.OnConnect, Server);
AsyncResult = new IAsyncProxyResult(state);
return AsyncResult;
}
......@@ -266,7 +268,7 @@ namespace Titanium.Web.Proxy.ProxySocket
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);
}
catch (Exception e)
......@@ -295,7 +297,7 @@ namespace Titanium.Web.Proxy.ProxySocket
{
Buffer = new byte[1024];
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);
}
catch (Exception e)
......@@ -325,7 +327,7 @@ namespace Titanium.Web.Proxy.ProxySocket
if (Received < 2)
{
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None,
new AsyncCallback(this.OnAuthReceive), Server);
this.OnAuthReceive, Server);
}
else
{
......@@ -343,7 +345,7 @@ namespace Titanium.Web.Proxy.ProxySocket
return;
}
authenticate.BeginAuthenticate(new HandShakeComplete(this.OnAuthenticated));
authenticate.BeginAuthenticate(this.OnAuthenticated);
}
}
catch (Exception e)
......@@ -366,7 +368,7 @@ namespace Titanium.Web.Proxy.ProxySocket
try
{
Server.BeginSend(HandShake, 0, HandShake.Length, SocketFlags.None, new AsyncCallback(this.OnSent),
Server.BeginSend(HandShake, 0, HandShake.Length, SocketFlags.None, this.OnSent,
Server);
}
catch (Exception ex)
......@@ -395,7 +397,7 @@ namespace Titanium.Web.Proxy.ProxySocket
{
Buffer = new byte[5];
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);
}
catch (Exception e)
......@@ -426,7 +428,7 @@ namespace Titanium.Web.Proxy.ProxySocket
ProcessReply(Buffer);
else
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None,
new AsyncCallback(this.OnReceive), Server);
this.OnReceive, Server);
}
catch (Exception e)
{
......@@ -457,7 +459,7 @@ namespace Titanium.Web.Proxy.ProxySocket
}
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>
......@@ -482,7 +484,7 @@ namespace Titanium.Web.Proxy.ProxySocket
ProtocolComplete(null);
else
Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None,
new AsyncCallback(this.OnReadLast), Server);
this.OnReadLast, Server);
}
catch (Exception e)
{
......
......@@ -37,7 +37,7 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <summary>
/// References the callback method to be called when the protocol negotiation is completed.
/// </summary>
internal delegate void HandShakeComplete(Exception error);
internal delegate void HandShakeComplete(Exception? error);
/// <summary>
/// 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
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>
/// Converts a port number to an array of bytes.
/// </summary>
......@@ -163,11 +175,7 @@ namespace Titanium.Web.Proxy.ProxySocket
/// Gets or sets the return value of the BeginConnect call.
/// </summary>
/// <value>An IAsyncProxyResult object that is the return value of the BeginConnect call.</value>
protected IAsyncProxyResult AsyncResult
{
get => _asyncResult;
set => _asyncResult = value;
}
protected IAsyncProxyResult AsyncResult { get; set; }
/// <summary>
/// Gets or sets a byte buffer.
......@@ -175,6 +183,11 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <value>An array of bytes.</value>
protected byte[] Buffer { get; set; }
/// <summary>
/// Gets or sets actual data count in the buffer.
/// </summary>
protected int BufferCount { get; set; }
/// <summary>
/// Gets or sets the number of bytes that have been received from the remote proxy server.
/// </summary>
......@@ -188,9 +201,6 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <summary>Holds the value of the Username property.</summary>
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>
protected HandShakeComplete ProtocolComplete;
......@@ -213,9 +223,10 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <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="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
public abstract IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback,
IPEndPoint proxyEndPoint);
IPEndPoint proxyEndPoint, object state);
/// <summary>
/// Starts negotiating asynchronously with a SOCKS proxy server.
......@@ -224,8 +235,9 @@ namespace Titanium.Web.Proxy.ProxySocket
/// <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="proxyEndPoint">The IPEndPoint of the SOCKS proxy server.</param>
/// <param name="state">The state.</param>
/// <returns>An IAsyncProxyResult that references the asynchronous connection.</returns>
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