FireflySR.Tool.Proxy/Program.cs

130 lines
4.3 KiB
C#
Raw Normal View History

using System.Diagnostics;
using System.Net;
2024-05-22 14:45:22 +00:00
using System.Text.Json;
namespace FireflySR.Tool.Proxy
{
internal static class Program
{
private const string Title = "FreeSR Proxy (Alter)";
private const string ConfigPath = "config.json";
2024-05-22 15:49:10 +00:00
private const string ConfigTemplatePath = "config.tmpl.json";
private const string GuardianPath = "tool/FireflySR.Tool.Proxy.Guardian.exe";
2024-05-22 14:45:22 +00:00
private static ProxyService s_proxyService = null!;
2024-06-10 01:15:40 +00:00
private static bool s_clearupd = false;
2024-05-22 14:45:22 +00:00
private static void Main(string[] args)
{
Console.Title = Title;
Console.WriteLine($"Firefly.Tool.Proxy - Credits for original FreeSR Proxy");
2024-06-10 01:15:40 +00:00
_ = Task.Run(WatchGuardianAsync);
2024-05-22 14:45:22 +00:00
CheckProxy();
2024-05-22 15:49:10 +00:00
InitConfig();
2024-05-22 14:45:22 +00:00
var conf = JsonSerializer.Deserialize(File.ReadAllText(ConfigPath), ProxyConfigContext.Default.ProxyConfig) ?? throw new FileLoadException("Please correctly configure config.json.");
2024-05-22 14:45:22 +00:00
s_proxyService = new ProxyService(conf.DestinationHost, conf.DestinationPort, conf);
2024-06-10 01:15:40 +00:00
Console.WriteLine($"Proxy now running");
2024-05-22 14:45:22 +00:00
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
Console.CancelKeyPress += OnProcessExit;
Thread.Sleep(-1);
}
2024-06-10 01:15:40 +00:00
private static async Task WatchGuardianAsync()
{
2024-06-10 01:15:40 +00:00
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
{
2024-06-10 01:15:40 +00:00
return Process.Start(new ProcessStartInfo(GuardianPath, $"{Environment.ProcessId}")
{
UseShellExecute = false,
});
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine();
2024-06-10 01:15:40 +00:00
return null;
}
}
2024-05-22 15:49:10 +00:00
private static void InitConfig()
{
if (!File.Exists(ConfigPath))
{
File.Copy(ConfigTemplatePath, ConfigPath);
}
}
2024-06-10 01:15:40 +00:00
private static void OnProcessExit(object? sender, EventArgs? args)
2024-05-22 14:45:22 +00:00
{
2024-06-10 01:15:40 +00:00
if (s_clearupd) return;
s_proxyService?.Shutdown();
s_clearupd = true;
2024-05-22 14:45:22 +00:00
}
public static void CheckProxy()
{
try
{
string? ProxyInfo = GetProxyInfo();
if (ProxyInfo != null)
{
Console.WriteLine("well... It seems you are using other proxy software(such as Clash,V2RayN,Fiddler,etc)");
Console.WriteLine($"You system proxy: {ProxyInfo}");
Console.WriteLine("You have to close all other proxy software to make sure FireflySR.Tool.Proxy can work well.");
Console.WriteLine("Press any key to continue if you closed other proxy software, or you think you are not using other proxy.");
Console.ReadKey();
}
}
catch (NullReferenceException)
{
}
}
public static string? GetProxyInfo()
{
try
{
IWebProxy proxy = WebRequest.GetSystemWebProxy();
Uri? proxyUri = proxy.GetProxy(new Uri("https://www.example.com"));
if (proxyUri == null) return null;
string proxyIP = proxyUri.Host;
int proxyPort = proxyUri.Port;
string info = proxyIP + ":" + proxyPort;
return info;
}
catch
{
return null;
}
}
}
2024-05-22 15:49:10 +00:00
}