Compare commits

..

3 commits

Author SHA1 Message Date
YYHEggEgg
196c6e3507 Make Guardian in separate 'tool' folder to avoid skill issue 2024-06-10 09:24:35 +08:00
YYHEggEgg
79be93f425 Close Proxy and exit when Guardian die 2024-06-10 09:15:40 +08:00
YYHEggEgg
5a6ad386fc System Settings Guardian & Publish AOT support 2024-06-10 00:01:47 +08:00
9 changed files with 198 additions and 17 deletions

View file

@ -10,7 +10,10 @@ on:
branches: [ "master" ] branches: [ "master" ]
jobs: jobs:
build: publish-aot:
strategy:
matrix:
runtime-id: [win-x64]
runs-on: windows-latest runs-on: windows-latest
@ -22,13 +25,16 @@ jobs:
dotnet-version: 8.0.x dotnet-version: 8.0.x
- name: Restore dependencies - name: Restore dependencies
run: dotnet restore run: dotnet restore
- name: Build
run: dotnet build --no-restore -c Release
- name: Test - name: Test
run: dotnet test --no-build --verbosity normal run: dotnet test --verbosity normal
- name: Publish
run: dotnet publish FireflySR.Tool.Proxy.csproj -c Release -r ${{ matrix.runtime-id }}
- name: Remove .pdb debug symbols
run: |
rm bin/Release/net8.0/${{ matrix.runtime-id }}/publish/*.pdb
rm bin/Release/net8.0/${{ matrix.runtime-id }}/publish/**/*.pdb
- name: Upload - name: Upload
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
with: with:
name: FireflySR.Tool.Proxy name: FireflySR.Tool.Proxy_${{ matrix.runtime-id }}
path: bin/Release/net7.0 path: bin/Release/net8.0/${{ matrix.runtime-id }}/publish
retention-days: 7

View file

@ -1,16 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<RollForward>Major</RollForward> <RollForward>Major</RollForward>
<LangVersion>Latest</LangVersion> <LangVersion>Latest</LangVersion>
<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<Version>2.0.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Titanium.Web.Proxy" Version="3.2.0" /> <Compile Remove="Guardian\**" />
<EmbeddedResource Remove="Guardian\**" />
<None Remove="Guardian\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Unobtanium.Web.Proxy" Version="0.1.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -19,4 +29,9 @@
</None> </None>
</ItemGroup> </ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="$(Os) == 'Windows_NT'">
<Exec Condition="$(RuntimeIdentifier) == ''" Command="echo ProjectDir: $(ProjectDir)&#xD;&#xA;echo Configuration: $(Configuration)&#xD;&#xA;echo OutDir: $(OutDir)&#xD;&#xA;echo Os: $(Os)&#xD;&#xA;echo RuntimeIdentifier: (unused)&#xD;&#xA;dotnet build $(ProjectDir)\Guardian --configuration $(Configuration) --output $(OutDir)\tool" />
<Exec Condition="$(RuntimeIdentifier) != ''" Command="echo ProjectDir: $(ProjectDir)&#xD;&#xA;echo Configuration: $(Configuration)&#xD;&#xA;echo OutDir: $(OutDir)&#xD;&#xA;echo Os: $(Os)&#xD;&#xA;echo RuntimeIdentifier: $(RuntimeIdentifier)&#xD;&#xA;dotnet publish $(ProjectDir)\Guardian --configuration $(Configuration) --output $(OutDir)\publish\tool" />
</Target>
</Project> </Project>

View file

@ -5,6 +5,8 @@ VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FireflySR.Tool.Proxy", "FireflySR.Tool.Proxy.csproj", "{C9867CEA-1659-4AC3-8590-7FD72A8867D6}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FireflySR.Tool.Proxy", "FireflySR.Tool.Proxy.csproj", "{C9867CEA-1659-4AC3-8590-7FD72A8867D6}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Guardian", "Guardian\Guardian.csproj", "{E5E60B4C-AC20-40B9-8C9C-34FE4E1141C2}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{C9867CEA-1659-4AC3-8590-7FD72A8867D6}.Debug|Any CPU.Build.0 = Debug|Any CPU {C9867CEA-1659-4AC3-8590-7FD72A8867D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9867CEA-1659-4AC3-8590-7FD72A8867D6}.Release|Any CPU.ActiveCfg = Release|Any CPU {C9867CEA-1659-4AC3-8590-7FD72A8867D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9867CEA-1659-4AC3-8590-7FD72A8867D6}.Release|Any CPU.Build.0 = Release|Any CPU {C9867CEA-1659-4AC3-8590-7FD72A8867D6}.Release|Any CPU.Build.0 = Release|Any CPU
{E5E60B4C-AC20-40B9-8C9C-34FE4E1141C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5E60B4C-AC20-40B9-8C9C-34FE4E1141C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5E60B4C-AC20-40B9-8C9C-34FE4E1141C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5E60B4C-AC20-40B9-8C9C-34FE4E1141C2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

14
Guardian/Guardian.csproj Normal file
View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<RootNamespace>FireflySR.Tool.Proxy.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
<AssemblyName>FireflySR.Tool.Proxy.$(MSBuildProjectName)</AssemblyName>
</PropertyGroup>
</Project>

54
Guardian/Program.cs Normal file
View file

@ -0,0 +1,54 @@
using Microsoft.Win32;
using System.Diagnostics;
namespace FireflySR.Tool.Proxy.Guardian;
internal class Program
{
static async Task Main(string[] args)
{
if (args.Length != 1 || !int.TryParse(args[0], out var watchPid))
{
Console.WriteLine("Usage: FireflySR.Tool.Proxy.Guardian [watch-pid]");
Environment.Exit(1);
return;
}
Console.WriteLine("FireflySR.Tool.Proxy.Guardian start.");
Process proc;
try
{
proc = Process.GetProcessById(watchPid);
Console.WriteLine($"Guardian find process {proc.ProcessName}:{watchPid}.");
}
catch
{
DisableSystemProxy();
Environment.Exit(2);
return;
}
while (!proc.HasExited)
{
await Task.Delay(1000);
}
DisableSystemProxy();
}
private static void DisableSystemProxy()
{
try
{
using RegistryKey? key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
key?.SetValue("ProxyEnable", 0);
Console.WriteLine($"Guardian successfully disabled System Proxy.");
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine();
Console.WriteLine($"Guardian failed to disable System Proxy.");
}
}
}

View file

@ -1,4 +1,5 @@
using System.Net; using System.Diagnostics;
using System.Net;
using System.Text.Json; using System.Text.Json;
namespace FireflySR.Tool.Proxy namespace FireflySR.Tool.Proxy
@ -8,24 +9,69 @@ namespace FireflySR.Tool.Proxy
private const string Title = "FreeSR Proxy (Alter)"; private const string Title = "FreeSR Proxy (Alter)";
private const string ConfigPath = "config.json"; private const string ConfigPath = "config.json";
private const string ConfigTemplatePath = "config.tmpl.json"; private const string ConfigTemplatePath = "config.tmpl.json";
private const string GuardianPath = "tool/FireflySR.Tool.Proxy.Guardian.exe";
private static ProxyService s_proxyService = null!; private static ProxyService s_proxyService = null!;
private static bool s_clearupd = false;
private static void Main(string[] args) private static void Main(string[] args)
{ {
Console.Title = Title; Console.Title = Title;
Console.WriteLine($"Firefly.Tool.Proxy - Credits for original FreeSR Proxy"); Console.WriteLine($"Firefly.Tool.Proxy - Credits for original FreeSR Proxy");
_ = Task.Run(WatchGuardianAsync);
CheckProxy(); CheckProxy();
InitConfig(); InitConfig();
var conf = JsonSerializer.Deserialize<ProxyConfig>(File.ReadAllText(ConfigPath)) ?? throw new FileLoadException("Please correctly configure config.json."); var conf = JsonSerializer.Deserialize(File.ReadAllText(ConfigPath), ProxyConfigContext.Default.ProxyConfig) ?? throw new FileLoadException("Please correctly configure config.json.");
s_proxyService = new ProxyService(conf.DestinationHost, conf.DestinationPort, conf); s_proxyService = new ProxyService(conf.DestinationHost, conf.DestinationPort, conf);
Console.WriteLine($"Proxy now running");
AppDomain.CurrentDomain.ProcessExit += OnProcessExit; AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
Console.CancelKeyPress += OnProcessExit; Console.CancelKeyPress += OnProcessExit;
Thread.Sleep(-1); Thread.Sleep(-1);
} }
private static async Task WatchGuardianAsync()
{
var proc = StartGuardian();
if (proc == null)
{
Console.WriteLine("Guardian start failed. Your proxy settings may not be able to recover after closing.");
return;
}
// Notice that on some PTY, closing it may lead
// to Guardian be killed, not the Proxy itself.
// Therefore, Proxy should also watch Guardian
// and exit when Guardian dies.
while (!proc.HasExited)
{
await Task.Delay(1000);
}
Console.WriteLine("! Guardian exit");
OnProcessExit(null, null);
Environment.Exit(0);
}
private static Process? StartGuardian()
{
if (!OperatingSystem.IsWindows()) return null;
try
{
return Process.Start(new ProcessStartInfo(GuardianPath, $"{Environment.ProcessId}")
{
UseShellExecute = false,
});
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine();
return null;
}
}
private static void InitConfig() private static void InitConfig()
{ {
if (!File.Exists(ConfigPath)) if (!File.Exists(ConfigPath))
@ -34,9 +80,11 @@ namespace FireflySR.Tool.Proxy
} }
} }
private static void OnProcessExit(object? sender, EventArgs args) private static void OnProcessExit(object? sender, EventArgs? args)
{ {
s_proxyService.Shutdown(); if (s_clearupd) return;
s_proxyService?.Shutdown();
s_clearupd = true;
} }
public static void CheckProxy() public static void CheckProxy()

15
ProxyConfigContext.cs Normal file
View file

@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
using System.Text.Json;
namespace FireflySR.Tool.Proxy;
#if NET8_0_OR_GREATER
[JsonSourceGenerationOptions(
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip
)]
[JsonSerializable(typeof(ProxyConfig))]
internal partial class ProxyConfigContext : JsonSerializerContext
{
}
#endif

View file

@ -4,7 +4,22 @@ A simple Proxy for playing Private Servers.
## Usage ## Usage
`dotnet run` to build and run this program. Pre-built binaries may be uploaded later. Go to [releases page](https://git.xeondev.com/YYHEggEgg/FireflySR.Tool.Proxy/releases) for the latest pre-built binary. It don't require any .NET versions.
## Update
### v2.0.0
- Fixed the issue whereby the proxy has been closed by the user while the System Proxy Settings is still enabled, which may cause the user "can't access the network".
Notice you may still need to change the setting if you meet a PC crash (or just start the Proxy software again and close it).
- Updated the building .NET requirement to `8.0`; however, `PublishAot` is enabled, so the generated binaries won't require any .NET versions. You can generate AOT build by the following command:
```sh
dotnet publish FireflySR.Tool.Proxy.csproj -r win-x64
```
Notice a [Guardian](Guardian/Guardian.csproj) is introduced **only on Windows** and will be copied to the output directory only when the build is triggered on Windows.
- Updated the `BlockUrls` list in `config.tmpl.json`.
## Configuration ## Configuration
@ -12,4 +27,5 @@ Simple: see [config.json](config.json). You can also see full options in [ProxyC
## Credits ## Credits
[FreeSR](https://git.xeondev.com/Moux23333/FreeSR) & `FreeSR.Tool.Proxy` - [FreeSR](https://git.xeondev.com/Moux23333/FreeSR) & `FreeSR.Tool.Proxy`
- Rebooted `Titanium.Web.Proxy` [Unobtanium.Web.Proxy](https://github.com/svrooij/titanium-web-proxy.git)

View file

@ -31,7 +31,14 @@
"/client/event/dataUpload", "/client/event/dataUpload",
"/log", "/log",
"/asm/dataUpload", "/asm/dataUpload",
"/sophon/dataUpload" "/sophon/dataUpload",
"/apm/dataUpload",
"/2g/dataUpload",
"/v1/firelog/legacy/log",
"/h5/upload",
"/_ts",
"/perf/config/verify",
"/ptolemaios_api/api/reportStrategyData"
], ],
"ForceRedirectOnUrlContains": [ "ForceRedirectOnUrlContains": [
"query_dispatch", "query_dispatch",