Elisa/Elisa.GameServer/ProxyControl.cs

56 lines
1.7 KiB
C#
Raw Normal View History

using Serilog;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models;
namespace Elisa.GameServer;
public class ProxyControl
{
static readonly Regex HostFilter = new Regex("(.*).(girlfrontline\\.co\\.kr|ppgame\\.com|txwy\\.tw|sunborngame\\.com|aihelp.net.cn).*");
readonly string forwardIpAddress;
readonly ProxyServer proxyServer;
readonly ExplicitProxyEndPoint explicitEndPoint;
public ProxyControl(string forwardIpAddress, int port = 9000)
{
this.forwardIpAddress = forwardIpAddress;
proxyServer = new ProxyServer();
explicitEndPoint = new ExplicitProxyEndPoint(System.Net.IPAddress.Any, port, true);
proxyServer.BeforeRequest += OnRequest;
}
public void Start()
{
proxyServer.AddEndPoint(explicitEndPoint);
proxyServer.Start();
// proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
// proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
}
public void Stop()
{
proxyServer.BeforeRequest -= OnRequest;
proxyServer.Stop();
}
private async Task OnRequest(object sender, SessionEventArgs e)
{
Uri requestUri = e.HttpClient.Request.RequestUri;
if (HostFilter.IsMatch(requestUri.Host))
{
Log.Information($"Forwarding request for {requestUri} to {forwardIpAddress}");
Uri newUri = new Uri("http://" + forwardIpAddress + requestUri.PathAndQuery, UriKind.Absolute);
Log.Information($"Forwarded request to {newUri}");
e.HttpClient.Request.RequestUri = newUri;
// Bubble it out to user
}
}
}