littleshop/TeleBot/TeleBot/Http/Socks5HttpHandler.cs
SysAdmin d31c0b4aeb CI/CD: Add GitLab CI/CD pipeline for Hostinger deployment
- Updated .gitlab-ci.yml with complete build, test, and deploy stages
- Added authentication redirect fix in Program.cs (302 redirect for admin routes)
- Fixed Cookie vs Bearer authentication conflict for admin panel
- Configure pipeline to build from .NET 9.0 source
- Deploy to Hostinger VPS with proper environment variables
- Include rollback capability for production deployments

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-01 13:10:48 +01:00

84 lines
3.2 KiB
C#

using System;
using System.Net;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace TeleBot.Http
{
/// <summary>
/// Factory for creating HTTP handlers that route traffic through a SOCKS5 proxy (e.g., TOR).
/// Uses native .NET 9.0 SOCKS5 support for maximum security and reliability.
/// </summary>
public class Socks5HttpHandler
{
/// <summary>
/// Creates an HttpMessageHandler configured with TOR proxy if enabled in configuration
/// </summary>
public static SocketsHttpHandler Create(IConfiguration configuration, ILogger? logger = null)
{
var torEnabled = configuration.GetValue<bool>("Privacy:EnableTor");
if (torEnabled)
{
var torSocksPort = configuration.GetValue<int>("Privacy:TorSocksPort", 9050);
var proxyUri = $"socks5://127.0.0.1:{torSocksPort}";
logger?.LogInformation("SOCKS5 proxy configured: {ProxyUri} (TOR enabled)", proxyUri);
// Configure SOCKS5 proxy using native .NET support
return new SocketsHttpHandler
{
Proxy = new WebProxy(proxyUri)
{
BypassProxyOnLocal = false, // Force all traffic through TOR
UseDefaultCredentials = false
},
UseProxy = true,
AllowAutoRedirect = false, // Prevent redirect-based deanonymization
MaxAutomaticRedirections = 0,
PooledConnectionLifetime = TimeSpan.FromMinutes(5), // Rotate circuits
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(2)
};
}
else
{
// TOR disabled - use direct connection
logger?.LogWarning("TOR is DISABLED - all traffic will expose real IP address");
return new SocketsHttpHandler();
}
}
/// <summary>
/// Factory method to create handler with TOR enabled
/// </summary>
public static SocketsHttpHandler CreateWithTor(int torSocksPort = 9050, ILogger? logger = null)
{
var proxyUri = $"socks5://127.0.0.1:{torSocksPort}";
logger?.LogInformation("SOCKS5 proxy configured: {ProxyUri}", proxyUri);
return new SocketsHttpHandler
{
Proxy = new WebProxy(proxyUri)
{
BypassProxyOnLocal = false,
UseDefaultCredentials = false
},
UseProxy = true,
AllowAutoRedirect = false,
PooledConnectionLifetime = TimeSpan.FromMinutes(5),
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(2)
};
}
/// <summary>
/// Factory method to create handler without TOR (direct connection)
/// </summary>
public static SocketsHttpHandler CreateDirect(ILogger? logger = null)
{
logger?.LogWarning("Creating direct HTTP handler - no proxy");
return new SocketsHttpHandler();
}
}
}