From 8f6ebee7c0a9190ea89761bfbca6834b704be553 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 24 Sep 2023 17:22:18 +0800 Subject: Upgrade v2ray. --- .../Crupest.V2ray/ConfigGenerationWatcher.cs | 9 +++++--- .../Crupest.V2ray/Crupest.V2ray.csproj | 3 +++ tools/Crupest.V2ray/Crupest.V2ray/Program.cs | 3 ++- tools/Crupest.V2ray/Crupest.V2ray/V2rayConfig.cs | 15 +++++++++---- tools/Crupest.V2ray/Crupest.V2ray/V2rayHostRule.cs | 19 ++++++++++++++++ tools/Crupest.V2ray/Crupest.V2ray/V2rayHosts.cs | 26 ++++++++++++++++++++++ .../Crupest.V2ray/config.json.template | 2 +- tools/Crupest.V2ray/Crupest.V2ray/hosts.txt | 2 ++ tools/Crupest.V2ray/Crupest.V2ray/proxy.txt | 3 +++ 9 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 tools/Crupest.V2ray/Crupest.V2ray/V2rayHostRule.cs create mode 100644 tools/Crupest.V2ray/Crupest.V2ray/V2rayHosts.cs create mode 100644 tools/Crupest.V2ray/Crupest.V2ray/hosts.txt (limited to 'tools') diff --git a/tools/Crupest.V2ray/Crupest.V2ray/ConfigGenerationWatcher.cs b/tools/Crupest.V2ray/Crupest.V2ray/ConfigGenerationWatcher.cs index 9ca5741..32be5b0 100644 --- a/tools/Crupest.V2ray/Crupest.V2ray/ConfigGenerationWatcher.cs +++ b/tools/Crupest.V2ray/Crupest.V2ray/ConfigGenerationWatcher.cs @@ -2,17 +2,18 @@ namespace Crupest.V2ray; public class ConfigGenerationWatcher { - public ConfigGenerationWatcher() : this(Program.ExeDir, Program.ConfigTemplateFileName, Program.VmessConfigFileName, Program.ProxyConfigFileName, Path.Combine(Program.ExeDir, Program.ConfigOutputFileName), new List()) + public ConfigGenerationWatcher() : this(Program.ExeDir, Program.ConfigTemplateFileName, Program.VmessConfigFileName, Program.ProxyConfigFileName, Program.HostsConfigFileName, Path.Combine(Program.ExeDir, Program.ConfigOutputFileName), new List()) { } - public ConfigGenerationWatcher(string directory, string configTemplateFileName, string vmessConfigFileName, string proxyConfigFileName, string configOutputPath, List otherWatchFiles) + public ConfigGenerationWatcher(string directory, string configTemplateFileName, string vmessConfigFileName, string proxyConfigFileName, string hostsConfigFileName, string configOutputPath, List otherWatchFiles) { Directory = directory; ConfigTemplateFileName = configTemplateFileName; VmessConfigFileName = vmessConfigFileName; ProxyConfigFileName = proxyConfigFileName; + HostsConfigFileName = hostsConfigFileName; ConfigOutputPath = configOutputPath; OtherWatchFiles = otherWatchFiles; } @@ -21,18 +22,20 @@ public class ConfigGenerationWatcher public string ConfigTemplateFileName { get; set; } public string VmessConfigFileName { get; set; } public string ProxyConfigFileName { get; set; } + public string HostsConfigFileName { get; set; } public List OtherWatchFiles { get; set; } public string ConfigOutputPath { get; set; } public string ConfigTemplateFilePath => Path.Combine(Directory, ConfigTemplateFileName); public string VmessConfigFilePath => Path.Combine(Directory, VmessConfigFileName); public string ProxyConfigFilePath => Path.Combine(Directory, ProxyConfigFileName); + public string HostsConfigFilePath => Path.Combine(Directory, HostsConfigFileName); public delegate void OnConfigChangedHandler(); public void Generate() { - var config = V2rayConfig.FromFiles(ConfigTemplateFilePath, VmessConfigFilePath, ProxyConfigFilePath); + var config = V2rayConfig.FromFiles(ConfigTemplateFilePath, VmessConfigFilePath, ProxyConfigFilePath, HostsConfigFilePath); File.WriteAllText(ConfigOutputPath, config.ToJson()); } diff --git a/tools/Crupest.V2ray/Crupest.V2ray/Crupest.V2ray.csproj b/tools/Crupest.V2ray/Crupest.V2ray/Crupest.V2ray.csproj index 38f0937..3962fe4 100644 --- a/tools/Crupest.V2ray/Crupest.V2ray/Crupest.V2ray.csproj +++ b/tools/Crupest.V2ray/Crupest.V2ray/Crupest.V2ray.csproj @@ -17,6 +17,9 @@ PreserveNewest + + PreserveNewest + diff --git a/tools/Crupest.V2ray/Crupest.V2ray/Program.cs b/tools/Crupest.V2ray/Crupest.V2ray/Program.cs index 793f6e7..e0c0d6d 100644 --- a/tools/Crupest.V2ray/Crupest.V2ray/Program.cs +++ b/tools/Crupest.V2ray/Crupest.V2ray/Program.cs @@ -1,4 +1,3 @@ -using System.Diagnostics; using System.Reflection; namespace Crupest.V2ray; @@ -8,6 +7,7 @@ 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 ExeDir { get; } = Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new Exception("Can't get the path of exe.")); @@ -28,6 +28,7 @@ public static class Program geoDataDonwloader.Download(ExeDir); return; } + throw new Exception("Invalid command line arguments."); } var v2rayController = new V2rayController(); diff --git a/tools/Crupest.V2ray/Crupest.V2ray/V2rayConfig.cs b/tools/Crupest.V2ray/Crupest.V2ray/V2rayConfig.cs index 82dc11c..1ae9683 100644 --- a/tools/Crupest.V2ray/Crupest.V2ray/V2rayConfig.cs +++ b/tools/Crupest.V2ray/Crupest.V2ray/V2rayConfig.cs @@ -6,17 +6,20 @@ public class V2rayConfig { private const string VmessAnchor = "VMESS_PROXY_ANCHOR"; private const string RoutingAnchor = "ROUTING_ANCHOR"; + private const string HostsAnchor = "HOSTS_ANCHOR"; - public V2rayConfig(string template, V2rayVmessProxy vmess, V2rayRouting router) + public V2rayConfig(string template, V2rayVmessProxy vmess, V2rayRouting router, V2rayHosts hosts) { Template = template; Vmess = vmess; Routing = router; + Hosts = hosts; } public string Template { get; set; } public V2rayVmessProxy Vmess { get; set; } public V2rayRouting Routing { get; set; } + public V2rayHosts Hosts { get; set; } public string ToJson(bool pretty = true) { @@ -29,21 +32,25 @@ public class V2rayConfig var templateValues = new Dictionary { [VmessAnchor] = JsonSerializer.Serialize(Vmess.ToOutboundJsonObject(), jsonOptions), - [RoutingAnchor] = JsonSerializer.Serialize(Routing.ToJsonObject(), jsonOptions) + [RoutingAnchor] = JsonSerializer.Serialize(Routing.ToJsonObject(), jsonOptions), + [HostsAnchor] = JsonSerializer.Serialize(Hosts.ToJsonObject(), jsonOptions), }; return FileUtility.JsonFormat(FileUtility.TextFromTemplate(Template, templateValues)); } - public static V2rayConfig FromFiles(string templatePath, string vmessPath, string routingPath) + public static V2rayConfig FromFiles(string templatePath, string vmessPath, string routingPath, string hostsPath) { var template = File.ReadAllText(templatePath); + var vmessDict = FileUtility.ReadDictionaryFile(vmessPath); var proxyRoutingList = FileUtility.ReadListFile(routingPath); + var hostsList = FileUtility.ReadListFile(hostsPath); var vmess = V2rayVmessProxy.FromDictionary(vmessDict); var routing = V2rayRouting.FromStringList(proxyRoutingList); + var hosts = V2rayHosts.FromStringList(hostsList); - return new V2rayConfig(template, vmess, routing); + return new V2rayConfig(template, vmess, routing, hosts); } } diff --git a/tools/Crupest.V2ray/Crupest.V2ray/V2rayHostRule.cs b/tools/Crupest.V2ray/Crupest.V2ray/V2rayHostRule.cs new file mode 100644 index 0000000..31feaaf --- /dev/null +++ b/tools/Crupest.V2ray/Crupest.V2ray/V2rayHostRule.cs @@ -0,0 +1,19 @@ +namespace Crupest.V2ray; + +public record V2rayHostRule(string Origin, List Resolved) +{ + public static V2rayHostRule Parse(string str) + { + var segments = str.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); + if (segments.Length == 1) + { + throw new Exception("Host rule only contains 1 segment."); + } + + var resolved = new List(); + resolved.AddRange(segments[1..]); + + return new V2rayHostRule(segments[0], resolved); + } +} + diff --git a/tools/Crupest.V2ray/Crupest.V2ray/V2rayHosts.cs b/tools/Crupest.V2ray/Crupest.V2ray/V2rayHosts.cs new file mode 100644 index 0000000..a1c8d61 --- /dev/null +++ b/tools/Crupest.V2ray/Crupest.V2ray/V2rayHosts.cs @@ -0,0 +1,26 @@ +namespace Crupest.V2ray; + +public record V2rayHosts(List Rules) +{ + public V2rayHosts() : this(new List()) { } + + public Dictionary> ToJsonObject() + { + var result = new Dictionary>(); + foreach (var rule in Rules) + { + result.Add(rule.Origin, rule.Resolved); + } + return result; + } + + public static V2rayHosts FromStringList(List list) + { + var hosts = new V2rayHosts(); + foreach (var str in list) + { + hosts.Rules.Add(V2rayHostRule.Parse(str)); + } + return hosts; + } +} diff --git a/tools/Crupest.V2ray/Crupest.V2ray/config.json.template b/tools/Crupest.V2ray/Crupest.V2ray/config.json.template index 9ba8af6..53aee76 100644 --- a/tools/Crupest.V2ray/Crupest.V2ray/config.json.template +++ b/tools/Crupest.V2ray/Crupest.V2ray/config.json.template @@ -37,7 +37,7 @@ ], "routing": ${ROUTING_ANCHOR}, "dns": { - "hosts": {}, + "hosts": ${HOSTS_ANCHOR}, "servers": [ "https://doh.pub/dns-query", "1.1.1.1", diff --git a/tools/Crupest.V2ray/Crupest.V2ray/hosts.txt b/tools/Crupest.V2ray/Crupest.V2ray/hosts.txt new file mode 100644 index 0000000..88d5015 --- /dev/null +++ b/tools/Crupest.V2ray/Crupest.V2ray/hosts.txt @@ -0,0 +1,2 @@ +cdn.jsdelivr.net cdn.jsdelivr.net.cdn.cloudflare.net + diff --git a/tools/Crupest.V2ray/Crupest.V2ray/proxy.txt b/tools/Crupest.V2ray/Crupest.V2ray/proxy.txt index 4e9d3e8..beeab15 100644 --- a/tools/Crupest.V2ray/Crupest.V2ray/proxy.txt +++ b/tools/Crupest.V2ray/Crupest.V2ray/proxy.txt @@ -14,3 +14,6 @@ GeoSite creativecommons GeoSite sci-hub GeoSite v2ray GeoSite imgur +GeoSite npmjs +GeoSite onedrive + -- cgit v1.2.3