diff options
9 files changed, 73 insertions, 9 deletions
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<string>()) +    public ConfigGenerationWatcher() : this(Program.ExeDir, Program.ConfigTemplateFileName, Program.VmessConfigFileName, Program.ProxyConfigFileName, Program.HostsConfigFileName, Path.Combine(Program.ExeDir, Program.ConfigOutputFileName), new List<string>())      {      } -    public ConfigGenerationWatcher(string directory, string configTemplateFileName, string vmessConfigFileName, string proxyConfigFileName, string configOutputPath, List<string> otherWatchFiles) +    public ConfigGenerationWatcher(string directory, string configTemplateFileName, string vmessConfigFileName, string proxyConfigFileName, string hostsConfigFileName, string configOutputPath, List<string> 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<string> 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 @@      <None Update="vmess.txt">        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>      </None> +    <None Update="hosts.txt"> +      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> +    </None>    </ItemGroup>  </Project> 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<string, string>          {              [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<string> 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<string>(); +        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<V2rayHostRule> Rules) +{ +    public V2rayHosts() : this(new List<V2rayHostRule>()) { } + +    public Dictionary<string, List<string>> ToJsonObject() +    { +        var result = new Dictionary<string, List<string>>(); +        foreach (var rule in Rules) +        { +            result.Add(rule.Origin, rule.Resolved); +        } +        return result; +    } + +    public static V2rayHosts FromStringList(List<string> 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 +  | 
