From 646e483c575c5ed1eaf6370ed83b87a445738c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Thu, 18 Nov 2021 21:58:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=AE=BE=E7=BD=AEdnscrypt?= =?UTF-8?q?=E7=9A=84=E7=BC=93=E5=AD=98=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.DomainResolve/DnscryptProxy.cs | 2 +- FastGithub.DomainResolve/TomlUtil.cs | 54 ++++++++--------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/FastGithub.DomainResolve/DnscryptProxy.cs b/FastGithub.DomainResolve/DnscryptProxy.cs index ee2d0c7..6cf2cd3 100644 --- a/FastGithub.DomainResolve/DnscryptProxy.cs +++ b/FastGithub.DomainResolve/DnscryptProxy.cs @@ -80,7 +80,7 @@ namespace FastGithub.DomainResolve await TomlUtil.SetListensAsync(this.tomlFilePath, localEndPoint, cancellationToken); await TomlUtil.SetlogLevelAsync(this.tomlFilePath, 6, cancellationToken); - await TomlUtil.SetEdnsClientSubnetAsync(this.tomlFilePath, cancellationToken); + await TomlUtil.SetMinMaxTTLAsync(this.tomlFilePath, TimeSpan.FromMinutes(1d), TimeSpan.FromMinutes(2d), cancellationToken); if (OperatingSystem.IsWindows() && Environment.UserInteractive == false) { diff --git a/FastGithub.DomainResolve/TomlUtil.cs b/FastGithub.DomainResolve/TomlUtil.cs index 29845ef..4ec35f6 100644 --- a/FastGithub.DomainResolve/TomlUtil.cs +++ b/FastGithub.DomainResolve/TomlUtil.cs @@ -1,9 +1,7 @@ using System; using System.IO; using System.Net; -using System.Net.Http; using System.Text; -using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Tommy; @@ -20,8 +18,9 @@ namespace FastGithub.DomainResolve /// /// /// + /// /// - public static Task SetListensAsync(string tomlPath, IPEndPoint endpoint, CancellationToken cancellationToken = default) + public static Task SetListensAsync(string tomlPath, IPEndPoint endpoint, CancellationToken cancellationToken) { var value = new TomlArray { @@ -39,54 +38,37 @@ namespace FastGithub.DomainResolve /// public static Task SetlogLevelAsync(string tomlPath, int logLevel, CancellationToken cancellationToken) { - return SetAsync(tomlPath, "log_level", new TomlInteger { Value = logLevel }); + return SetAsync(tomlPath, "log_level", new TomlInteger { Value = logLevel }, cancellationToken); } /// - /// 设置ecs + /// 设置TTL /// - /// + /// + /// + /// /// /// - public static async Task SetEdnsClientSubnetAsync(string tomlPath, CancellationToken cancellationToken = default) + public static async Task SetMinMaxTTLAsync(string tomlPath, TimeSpan minTTL, TimeSpan maxTTL, CancellationToken cancellationToken) { - try - { - var address = await GetPublicIPAddressAsync(cancellationToken); - if (address != null) - { - var value = new TomlArray { $"{address}/32" }; - await SetAsync(tomlPath, "edns_client_subnet", value, cancellationToken); - } - return true; - } - catch (Exception) - { - return false; - } - } + var minValue = new TomlInteger { Value = (int)minTTL.TotalSeconds }; + var maxValue = new TomlInteger { Value = (int)maxTTL.TotalSeconds }; - /// - /// 获取公网ip - /// - /// - /// - private static async Task GetPublicIPAddressAsync(CancellationToken cancellationToken) - { - using var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(3d) }; - var response = await httpClient.GetStringAsync("https://pv.sohu.com/cityjson?ie=utf-8", cancellationToken); - var match = Regex.Match(response, @"\d+\.\d+\.\d+\.\d+"); - IPAddress.TryParse(match.Value, out var address); - return address; + await SetAsync(tomlPath, "cache_min_ttl", minValue, cancellationToken); + await SetAsync(tomlPath, "cache_neg_min_ttl", minValue, cancellationToken); + await SetAsync(tomlPath, "cache_max_ttl", maxValue, cancellationToken); + await SetAsync(tomlPath, "cache_neg_max_ttl", maxValue, cancellationToken); } - + /// /// 设置指定键的值 /// /// /// /// - public static async Task SetAsync(string tomlPath, string key, TomlNode value, CancellationToken cancellationToken = default) + /// + /// + public static async Task SetAsync(string tomlPath, string key, TomlNode value, CancellationToken cancellationToken) { var toml = await File.ReadAllTextAsync(tomlPath, cancellationToken); var reader = new StringReader(toml);