This commit is contained in:
2024-02-20 17:15:27 +08:00
committed by huty
parent 6706e1a633
commit 34158042ad
1529 changed files with 177765 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS builder
WORKDIR /src
COPY src/SimpleProxy.csproj .
RUN dotnet restore
COPY src /src
RUN dotnet publish -c Release -o /out SimpleProxy.csproj
# app image
FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine
EXPOSE 1080
WORKDIR /app
ENTRYPOINT ["dotnet", "SimpleProxy.dll"]
COPY --from=builder /out/ .

View File

@@ -0,0 +1,65 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models;
namespace SimpleProxy
{
class Program
{
private static ManualResetEvent _ResetEvent = new ManualResetEvent(false);
private static IConfiguration _Config;
static void Main(string[] args)
{
_Config = GetConfig();
var port = _Config.GetValue<int>("Proxy:Port");
var proxyServer = new ProxyServer();
proxyServer.BeforeRequest += OnRequest;
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, port, false);
proxyServer.AddEndPoint(explicitEndPoint);
proxyServer.Start();
Console.WriteLine($"** Logging proxy listening on port: {port} **");
_ResetEvent.WaitOne();
proxyServer.BeforeRequest -= OnRequest;
proxyServer.Stop();
}
private static IConfiguration GetConfig()
{
return new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
}
public static Task OnRequest(object sender, SessionEventArgs e)
{
var sourceUri = e.HttpClient.Request.RequestUri.AbsoluteUri;
if (sourceUri == _Config.GetValue<string>("Proxy:Request:UriMap:Source"))
{
var targetUri = _Config.GetValue<string>("Proxy:Request:UriMap:Target");
Console.WriteLine($"{e.HttpClient.Request.Method} {sourceUri} -> {targetUri}");
e.HttpClient.Request.RequestUri = new Uri(targetUri);
}
else if (_Config.GetValue<bool>("Proxy:Request:RejectUnknown"))
{
Console.WriteLine($"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url} [BLOCKED]");
e.Ok("");
}
else
{
Console.WriteLine($"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url}");
}
return Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Titanium.Web.Proxy" Version="3.1.1301" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,12 @@
{
"Proxy": {
"Port": 1080,
"Request": {
"RejectUnknown": true,
"UriMap": {
"Source": "http://localhost/api",
"Target": "http://blog.sixeyed.com"
}
}
}
}