diff options
Diffstat (limited to 'tools/Crupest.V2ray/Crupest.V2ray/Program.cs')
-rw-r--r-- | tools/Crupest.V2ray/Crupest.V2ray/Program.cs | 50 |
1 files changed, 32 insertions, 18 deletions
diff --git a/tools/Crupest.V2ray/Crupest.V2ray/Program.cs b/tools/Crupest.V2ray/Crupest.V2ray/Program.cs index e623a88..e06a92d 100644 --- a/tools/Crupest.V2ray/Crupest.V2ray/Program.cs +++ b/tools/Crupest.V2ray/Crupest.V2ray/Program.cs @@ -4,14 +4,32 @@ namespace Crupest.V2ray; public static class Program { - public const string ConfigTemplateFileName = "config.json.template"; - public const string VmessConfigFileName = "vmess.txt"; - public const string ProxyConfigFileName = "proxy.txt"; - public const string HostsConfigFileName = "hosts.txt"; - public const string ConfigOutputFileName = "config.json"; + public static string CrupestV2rayDirectory { get; } = + Environment.GetEnvironmentVariable("CRUPEST_V2RAY_DIR") ?? + Path.GetFullPath(Path.GetDirectoryName( + Assembly.GetExecutingAssembly().Location) ?? throw new Exception("Can't get the path of Crupest.V2ray.")); - public static string ExeDir { get; } = Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new Exception("Can't get the path of exe.")); + private const string ConfigOutputFileName = "config.json"; + public static void RunV2rayAndWatchConfigChange() + { + var v2rayPath = V2rayController.FindExecutable(CrupestV2rayDirectory) ?? + throw new Exception("Can't find v2ray executable either in Crupest.V2ray directory or in PATH."); + + var v2rayController = new V2rayController(v2rayPath, Path.Combine(CrupestV2rayDirectory, ConfigOutputFileName), CrupestV2rayDirectory); + var configFileWatcher = new FileWatcher(CrupestV2rayDirectory, V2rayConfig.ConfigFileNames); + + V2rayConfig.FromDirectoryAndWriteToFile(CrupestV2rayDirectory, Path.Join(CrupestV2rayDirectory, ConfigOutputFileName)); + v2rayController.Start(); + + configFileWatcher.OnChanged += () => + { + V2rayConfig.FromDirectoryAndWriteToFile(CrupestV2rayDirectory, Path.Join(CrupestV2rayDirectory, ConfigOutputFileName)); + v2rayController.Restart(); + }; + + configFileWatcher.Run(); + } public static void Main(string[] args) { @@ -25,22 +43,18 @@ public static class Program if (verb == "download-geodata" || verb == "dg") { var geoDataDownloader = new GeoDataDownloader(); - geoDataDownloader.Download(ExeDir); + geoDataDownloader.Download(CrupestV2rayDirectory); + return; + } + else if (verb == "generate" || verb == "g") + { + var config = V2rayConfig.FromDirectory(CrupestV2rayDirectory); + Console.Out.WriteLine(config.ToJsonStringV4()); return; } throw new Exception("Invalid command line arguments."); } - var v2rayController = new V2rayController(); - var configGenerationWatcher = new ConfigGenerationWatcher(); - - configGenerationWatcher.Generate(); - v2rayController.Start(); - - configGenerationWatcher.Run(() => - { - configGenerationWatcher.Generate(); - v2rayController.Restart(); - }); + RunV2rayAndWatchConfigChange(); } } |