I am using the Titanium.Web.Proxy library to set up a proxy connection in the format host:port:username:password
and convert it to IPlocal:port
. The HTTPS proxy works correctly, but the Socks5 proxy does not.
When I test the Socks5 proxy, it returns the local IP address instead of routing through the proxy. What am I missing or doing wrong in my implementation? Any help or suggestions would be greatly appreciated.
Here is the relevant code for starting the Socks5 proxy:
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Titanium.Web.Proxy;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models;
using System.Net;
using System;
namespace XXXXX
{
internal class Titanium
{
private ProxyServer proxyServer;
private string remoteHost;
private int remotePort;
private string remoteUser;
private string remotePass;
public Titanium(string remoteHost, int remotePort, string remoteUser, string remotePass)
{
this.remoteHost = remoteHost;
this.remotePort = remotePort;
this.remoteUser = remoteUser;
this.remotePass = remotePass;
proxyServer = new ProxyServer();
proxyServer.BeforeRequest -= OnBeforeRequest;
proxyServer.BeforeRequest += OnBeforeRequest;
proxyServer.BeforeResponse += OnBeforeResponse;
}
public bool IsRunning => proxyServer?.ProxyRunning ?? false;
private Task OnBeforeRequest(object sender, SessionEventArgs e)
{
try
{
if (!IsBypassed(e.HttpClient.Request.RequestUri.Host))
{
e.CustomUpStreamProxy = new ExternalProxy
{
HostName = remoteHost,
Port = remotePort,
UserName = remoteUser,
Password = remotePass,
ProxyType = ExternalProxyType.Socks5
};
}
else
{
e.CustomUpStreamProxy = null;
}
Log($"BeforeRequest: {e.HttpClient.Request.Url}");
}
catch (Exception ex)
{
Log($"OnBeforeRequest Error: {ex.Message}n{ex.StackTrace}");
}
return Task.CompletedTask;
}
private Task OnBeforeResponse(object sender, SessionEventArgs e)
{
try
{
Log($"BeforeResponse: {e.HttpClient.Request.Url}");
}
catch (Exception ex)
{
Log($"OnBeforeResponse Error: {ex.Message}n{ex.StackTrace}");
}
return Task.CompletedTask;
}
public bool StopProxy()
{
if (this.proxyServer != null)
{
var endPointsToRemove = new List<ProxyEndPoint> (this.proxyServer.ProxyEndPoints);
foreach (var endPoint in endPointsToRemove)
{
this.proxyServer.RemoveEndPoint(endPoint);
}
this.proxyServer.Stop();
this.proxyServer = null;
}
return true;
}
public bool StartHttpProxy(int localPort)
{
try
{
var endPoint = new ExplicitProxyEndPoint(IPAddress.Any, localPort, false);
proxyServer.AddEndPoint(endPoint);
proxyServer.BeforeRequest -= OnBeforeRequest;
proxyServer.BeforeRequest += OnBeforeRequest;
proxyServer.Start();
Log("HTTP Proxy started");
return true;
}
catch (Exception ex)
{
Log($"HTTP Proxy Error: {ex.Message}n{ex.StackTrace}");
return false;
}
}
public bool StartSocks5Proxy(int localPort)
{
try
{
var endPoint = new SocksProxyEndPoint(IPAddress.Any, localPort, false);
proxyServer.AddEndPoint(endPoint);
proxyServer.BeforeRequest -= OnBeforeRequest;
proxyServer.BeforeRequest += OnBeforeRequest;
proxyServer.Start();
Log("Socks5 Proxy started");
return true;
}
catch (Exception ex)
{
Log($"Socks5 Proxy Error: {ex.Message}n{ex.StackTrace}");
return false;
}
}
}
}
braim US is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.