Przeglądaj źródła

自动设置dnscrypt的缓存时间

老九 3 lat temu
rodzic
commit
646e483c57

+ 1 - 1
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)
             {

+ 18 - 36
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
         /// </summary>
         /// <param name="tomlPath"></param>
         /// <param name="endpoint"></param>
+        /// <param name="cancellationToken"></param>
         /// <returns></returns>
-        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
         /// <returns></returns>
         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);
         }
 
         /// <summary>
-        /// 设置ecs
+        /// 设置TTL
         /// </summary>
-        /// <param name="tomlPath"></param> 
+        /// <param name="tomlPath"></param>
+        /// <param name="minTTL"></param>
+        /// <param name="maxTTL"></param>
         /// <param name="cancellationToken"></param>
         /// <returns></returns>
-        public static async Task<bool> 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 };
 
-        /// <summary>
-        /// 获取公网ip
-        /// </summary>
-        /// <param name="cancellationToken"></param>
-        /// <returns></returns>
-        private static async Task<IPAddress?> 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);
         }
-
+         
         /// <summary>
         /// 设置指定键的值
         /// </summary>
         /// <param name="tomlPath"></param>
         /// <param name="key"></param>
         /// <param name="value"></param>
-        public static async Task SetAsync(string tomlPath, string key, TomlNode value, CancellationToken cancellationToken = default)
+        /// <param name="cancellationToken"></param>
+        /// <returns></returns>
+        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);