Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

socks endpoint #713

Merged
merged 1 commit into from
Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ public void StartProxy()
//proxyServer.UpStreamHttpProxy = new ExternalProxy("46.63.0.17", 4145) { ProxyType = ExternalProxyType.Socks4 };
//proxyServer.UpStreamHttpsProxy = new ExternalProxy("46.63.0.17", 4145) { ProxyType = ExternalProxyType.Socks4 };

//var socksEndPoint = new SocksProxyEndPoint(IPAddress.Any, 1080, true)
//{
// // Generic Certificate hostname to use
// // When SNI is disabled by client
// GenericCertificateName = "google.com"
//};

//proxyServer.AddEndPoint(socksEndPoint);

foreach (var endPoint in proxyServer.ProxyEndPoints)
{
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ", endPoint.GetType().Name,
Expand Down
9 changes: 9 additions & 0 deletions examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public MainWindow()
// Password = "Titanium",
//};

//var socksEndPoint = new SocksProxyEndPoint(IPAddress.Any, 1080, true)
//{
// // Generic Certificate hostname to use
// // When SNI is disabled by client
// //GenericCertificateName = "google.com"
//};

//proxyServer.AddEndPoint(socksEndPoint);

proxyServer.BeforeRequest += ProxyServer_BeforeRequest;
proxyServer.BeforeResponse += ProxyServer_BeforeResponse;
proxyServer.AfterResponse += ProxyServer_AfterResponse;
Expand Down
5 changes: 2 additions & 3 deletions src/Titanium.Web.Proxy/Http2/Http2Helper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

using Titanium.Web.Proxy.Extensions;
#if NETSTANDARD2_1
#if NETSTANDARD2_1
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
Expand All @@ -12,6 +10,7 @@
using Titanium.Web.Proxy.Compression;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Http2.Hpack;
using Titanium.Web.Proxy.Models;
Expand Down
48 changes: 48 additions & 0 deletions src/Titanium.Web.Proxy/Models/SocksProxyEndPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Extensions;

namespace Titanium.Web.Proxy.Models
{
/// <summary>
/// A proxy end point client is not aware of.
/// Useful when requests are redirected to this proxy end point through port forwarding via router.
/// </summary>
[DebuggerDisplay("SOCKS: {IpAddress}:{Port}")]
public class SocksProxyEndPoint : TransparentBaseProxyEndPoint
{
/// <summary>
/// Initialize a new instance.
/// </summary>
/// <param name="ipAddress">Listening Ip address.</param>
/// <param name="port">Listening port.</param>
/// <param name="decryptSsl">Should we decrypt ssl?</param>
public SocksProxyEndPoint(IPAddress ipAddress, int port, bool decryptSsl = true) : base(ipAddress, port,
decryptSsl)
{
GenericCertificateName = "localhost";
}

/// <summary>
/// Name of the Certificate need to be sent (same as the hostname we want to proxy).
/// This is valid only when UseServerNameIndication is set to false.
/// </summary>
public override string GenericCertificateName { get; set; }

/// <summary>
/// Before Ssl authentication this event is fired.
/// </summary>
public event AsyncEventHandler<BeforeSslAuthenticateEventArgs>? BeforeSslAuthenticate;

internal override async Task InvokeBeforeSslAuthenticate(ProxyServer proxyServer,
BeforeSslAuthenticateEventArgs connectArgs, ExceptionHandler exceptionFunc)
{
if (BeforeSslAuthenticate != null)
{
await BeforeSslAuthenticate.InvokeAsync(proxyServer, connectArgs, exceptionFunc);
}
}
}
}
19 changes: 19 additions & 0 deletions src/Titanium.Web.Proxy/Models/TransparentBaseProxyEndPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments;

namespace Titanium.Web.Proxy.Models
{
public abstract class TransparentBaseProxyEndPoint : ProxyEndPoint
{
public abstract string GenericCertificateName { get; set; }

protected TransparentBaseProxyEndPoint(IPAddress ipAddress, int port, bool decryptSsl) : base(ipAddress, port, decryptSsl)
{
}

internal abstract Task InvokeBeforeSslAuthenticate(ProxyServer proxyServer,
BeforeSslAuthenticateEventArgs connectArgs, ExceptionHandler exceptionFunc);
}
}
6 changes: 3 additions & 3 deletions src/Titanium.Web.Proxy/Models/TransparentProxyEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Titanium.Web.Proxy.Models
/// Useful when requests are redirected to this proxy end point through port forwarding via router.
/// </summary>
[DebuggerDisplay("Transparent: {IpAddress}:{Port}")]
public class TransparentProxyEndPoint : ProxyEndPoint
public class TransparentProxyEndPoint : TransparentBaseProxyEndPoint
{
/// <summary>
/// Initialize a new instance.
Expand All @@ -29,14 +29,14 @@ public TransparentProxyEndPoint(IPAddress ipAddress, int port, bool decryptSsl =
/// Name of the Certificate need to be sent (same as the hostname we want to proxy).
/// This is valid only when UseServerNameIndication is set to false.
/// </summary>
public string GenericCertificateName { get; set; }
public override string GenericCertificateName { get; set; }

/// <summary>
/// Before Ssl authentication this event is fired.
/// </summary>
public event AsyncEventHandler<BeforeSslAuthenticateEventArgs>? BeforeSslAuthenticate;

internal async Task InvokeBeforeSslAuthenticate(ProxyServer proxyServer,
internal override async Task InvokeBeforeSslAuthenticate(ProxyServer proxyServer,
BeforeSslAuthenticateEventArgs connectArgs, ExceptionHandler exceptionFunc)
{
if (BeforeSslAuthenticate != null)
Expand Down
3 changes: 0 additions & 3 deletions src/Titanium.Web.Proxy/Net45Compatibility.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#if NET45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Titanium.Web.Proxy
Expand Down
1 change: 0 additions & 1 deletion src/Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Net.Sockets;
using System.Security.Authentication;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Models;

Expand Down
1 change: 0 additions & 1 deletion src/Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Net.Security;
using System.Net.Sockets;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Models;

Expand Down
10 changes: 7 additions & 3 deletions src/Titanium.Web.Proxy/ProxyServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -798,13 +798,17 @@ private async Task handleClient(Socket tcpClientSocket, ProxyEndPoint endPoint)

using (var clientConnection = new TcpClientConnection(this, tcpClientSocket))
{
if (endPoint is TransparentProxyEndPoint tep)
if (endPoint is ExplicitProxyEndPoint eep)
{
await handleClient(eep, clientConnection);
}
else if (endPoint is TransparentProxyEndPoint tep)
{
await handleClient(tep, clientConnection);
}
else
else if (endPoint is SocksProxyEndPoint sep)
{
await handleClient((ExplicitProxyEndPoint)endPoint, clientConnection);
await handleClient(sep, clientConnection);
}
}
}
Expand Down
64 changes: 8 additions & 56 deletions src/Titanium.Web.Proxy/ProxySocket/SocksHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,8 @@ protected void HandleEndSend(IAsyncResult ar, int expectedLength)
/// <exception cref="ArgumentNullException">The specified value is null.</exception>
protected Socket Server
{
get
{
return _server;
}
set
{
if (value == null)
throw new ArgumentNullException();
_server = value;
}
get => _server;
set => _server = value ?? throw new ArgumentNullException();
}

/// <summary>
Expand All @@ -163,16 +155,8 @@ protected Socket Server
/// <exception cref="ArgumentNullException">The specified value is null.</exception>
protected string Username
{
get
{
return _username;
}
set
{
if (value == null)
throw new ArgumentNullException();
_username = value;
}
get => _username;
set => _username = value ?? throw new ArgumentNullException();
}

/// <summary>
Expand All @@ -181,47 +165,21 @@ protected string Username
/// <value>An IAsyncProxyResult object that is the return value of the BeginConnect call.</value>
protected IAsyncProxyResult AsyncResult
{
get
{
return _asyncResult;
}
set
{
_asyncResult = value;
}
get => _asyncResult;
set => _asyncResult = value;
}

/// <summary>
/// Gets or sets a byte buffer.
/// </summary>
/// <value>An array of bytes.</value>
protected byte[] Buffer
{
get
{
return _buffer;
}
set
{
_buffer = value;
}
}
protected byte[] Buffer { get; set; }

/// <summary>
/// Gets or sets the number of bytes that have been received from the remote proxy server.
/// </summary>
/// <value>An integer that holds the number of bytes that have been received from the remote proxy server.</value>
protected int Received
{
get
{
return _received;
}
set
{
_received = value;
}
}
protected int Received { get; set; }

// private variables
/// <summary>Holds the value of the Server property.</summary>
Expand All @@ -233,12 +191,6 @@ protected int Received
/// <summary>Holds the value of the AsyncResult property.</summary>
private IAsyncProxyResult _asyncResult;

/// <summary>Holds the value of the Buffer property.</summary>
private byte[] _buffer;

/// <summary>Holds the value of the Received property.</summary>
private int _received;

/// <summary>Holds the address of the method to call when the SOCKS protocol has been completed.</summary>
protected HandShakeComplete ProtocolComplete;

Expand Down
6 changes: 4 additions & 2 deletions src/Titanium.Web.Proxy/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public partial class ProxyServer
/// <param name="cancellationTokenSource">The cancellation token source for this async task.</param>
/// <param name="connectArgs">The Connect request if this is a HTTPS request from explicit endpoint.</param>
/// <param name="prefetchConnectionTask">Prefetched server connection for current client using Connect/SNI headers.</param>
/// <param name="isHttps">Is HTTPS</param>
private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, HttpClientStream clientStream,
CancellationTokenSource cancellationTokenSource, TunnelConnectSessionEventArgs? connectArgs = null,
Task<TcpServerConnection>? prefetchConnectionTask = null)
Task<TcpServerConnection>? prefetchConnectionTask = null, bool isHttps = false)
{
var connectRequest = connectArgs?.HttpClient.ConnectRequest;

Expand Down Expand Up @@ -67,6 +68,8 @@ private async Task handleHttpSessionRequest(ProxyEndPoint endPoint, HttpClientSt
UserData = connectArgs?.UserData
};

args.HttpClient.Request.IsHttps = isHttps;

try
{
try
Expand Down Expand Up @@ -217,7 +220,6 @@ await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
await tcpConnectionFactory.Release(connection);
connection = null;
}

}
catch (Exception e) when (!(e is ProxyHttpException))
{
Expand Down
1 change: 0 additions & 1 deletion src/Titanium.Web.Proxy/ResponseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ await serverStream.CopyBodyAsync(response, false, clientStream, TransformationMo
}
}


args.TimeLine["Response Sent"] = DateTime.Now;
}

Expand Down
Loading