System Settings Guardian & Publish AOT support

This commit is contained in:
YYHEggEgg 2024-06-10 00:01:47 +08:00
parent 558b10e9dd
commit 5a6ad386fc
9 changed files with 168 additions and 15 deletions

View file

@ -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

View file

@ -1,16 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<RollForward>Major</RollForward>
<LangVersion>Latest</LangVersion>
<AssemblyVersion>2.0.0</AssemblyVersion>
<FileVersion>2.0.0</FileVersion>
<Version>2.0.0</Version>
</PropertyGroup>
<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>
@ -19,4 +29,9 @@
</None>
</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 publish $(ProjectDir)\Guardian --configuration $(Configuration) --output $(OutDir)" />
<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" />
</Target>
</Project>

View file

@ -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

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;
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<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);
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))

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
`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)

View file

@ -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",