From 5a6ad386fc754d528071a34a9a1fe51d3457a3c2 Mon Sep 17 00:00:00 2001 From: YYHEggEgg <53960525+YYHEggEgg@users.noreply.github.com> Date: Mon, 10 Jun 2024 00:01:47 +0800 Subject: [PATCH] System Settings Guardian & Publish AOT support --- .github/workflows/dotnet.yml | 18 +++++++----- FireflySR.Tool.Proxy.csproj | 21 ++++++++++++-- FireflySR.Tool.Proxy.sln | 6 ++++ Guardian/Guardian.csproj | 14 ++++++++++ Guardian/Program.cs | 54 ++++++++++++++++++++++++++++++++++++ Program.cs | 26 +++++++++++++++-- ProxyConfigContext.cs | 15 ++++++++++ README.md | 20 +++++++++++-- config.tmpl.json | 9 +++++- 9 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 Guardian/Guardian.csproj create mode 100644 Guardian/Program.cs create mode 100644 ProxyConfigContext.cs diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index fba600c..ae94ae7 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -10,7 +10,10 @@ on: branches: [ "master" ] jobs: - build: + publish-aot: + strategy: + matrix: + runtime-id: [win-x64] runs-on: windows-latest @@ -22,13 +25,14 @@ jobs: dotnet-version: 8.0.x - name: Restore dependencies run: dotnet restore - - name: Build - run: dotnet build --no-restore -c Release - 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 - name: Upload uses: actions/upload-artifact@v4 with: - name: FireflySR.Tool.Proxy - path: bin/Release/net7.0 - retention-days: 7 + name: FireflySR.Tool.Proxy_${{ matrix.runtime-id }} + path: bin/Release/net8.0/${{ matrix.runtime-id }}/publish diff --git a/FireflySR.Tool.Proxy.csproj b/FireflySR.Tool.Proxy.csproj index 68b229d..5d9ba71 100644 --- a/FireflySR.Tool.Proxy.csproj +++ b/FireflySR.Tool.Proxy.csproj @@ -1,16 +1,26 @@ - + Exe - net7.0 + net8.0 enable enable + true Major Latest + 2.0.0 + 2.0.0 + 2.0.0 - + + + + + + + @@ -19,4 +29,9 @@ + + + + + diff --git a/FireflySR.Tool.Proxy.sln b/FireflySR.Tool.Proxy.sln index bb492f2..7c5587b 100644 --- a/FireflySR.Tool.Proxy.sln +++ b/FireflySR.Tool.Proxy.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.5.002.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FireflySR.Tool.Proxy", "FireflySR.Tool.Proxy.csproj", "{C9867CEA-1659-4AC3-8590-7FD72A8867D6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Guardian", "Guardian\Guardian.csproj", "{E5E60B4C-AC20-40B9-8C9C-34FE4E1141C2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Guardian/Guardian.csproj b/Guardian/Guardian.csproj new file mode 100644 index 0000000..030687b --- /dev/null +++ b/Guardian/Guardian.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0-windows + enable + enable + true + true + FireflySR.Tool.Proxy.$(MSBuildProjectName.Replace(" ", "_")) + FireflySR.Tool.Proxy.$(MSBuildProjectName) + + + diff --git a/Guardian/Program.cs b/Guardian/Program.cs new file mode 100644 index 0000000..747de5d --- /dev/null +++ b/Guardian/Program.cs @@ -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."); + } + } +} diff --git a/Program.cs b/Program.cs index bb21891..4757419 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ -using System.Net; +using System.Diagnostics; +using System.Net; using System.Text.Json; namespace FireflySR.Tool.Proxy @@ -8,6 +9,7 @@ namespace FireflySR.Tool.Proxy private const string Title = "FreeSR Proxy (Alter)"; private const string ConfigPath = "config.json"; private const string ConfigTemplatePath = "config.tmpl.json"; + private const string GuardianPath = "FireflySR.Tool.Proxy.Guardian.exe"; private static ProxyService s_proxyService = null!; @@ -15,10 +17,11 @@ namespace FireflySR.Tool.Proxy { Console.Title = Title; Console.WriteLine($"Firefly.Tool.Proxy - Credits for original FreeSR Proxy"); + StartGuardian(); CheckProxy(); InitConfig(); - var conf = JsonSerializer.Deserialize(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); AppDomain.CurrentDomain.ProcessExit += OnProcessExit; Console.CancelKeyPress += OnProcessExit; @@ -26,6 +29,25 @@ namespace FireflySR.Tool.Proxy Thread.Sleep(-1); } + private static void StartGuardian() + { + if (!OperatingSystem.IsWindows()) return; + + try + { + var proc = Process.Start(new ProcessStartInfo(GuardianPath, $"{Environment.ProcessId}") + { + UseShellExecute = false, + }); + } + catch (Exception ex) + { + Console.WriteLine(ex); + Console.WriteLine(); + Console.WriteLine("Guardian start failed. Your proxy settings may not be able to recover after closing."); + } + } + private static void InitConfig() { if (!File.Exists(ConfigPath)) diff --git a/ProxyConfigContext.cs b/ProxyConfigContext.cs new file mode 100644 index 0000000..c3b1775 --- /dev/null +++ b/ProxyConfigContext.cs @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index b9e97ab..8ef3d11 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,22 @@ A simple Proxy for playing Private Servers. ## 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 @@ -12,4 +27,5 @@ Simple: see [config.json](config.json). You can also see full options in [ProxyC ## 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) diff --git a/config.tmpl.json b/config.tmpl.json index c14dfbd..43dc79c 100644 --- a/config.tmpl.json +++ b/config.tmpl.json @@ -31,7 +31,14 @@ "/client/event/dataUpload", "/log", "/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": [ "query_dispatch",