|
@@ -1,9 +1,7 @@
|
|
using System;
|
|
using System;
|
|
using System.IO;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net;
|
|
-using System.Net.Http;
|
|
|
|
using System.Text;
|
|
using System.Text;
|
|
-using System.Text.RegularExpressions;
|
|
|
|
using System.Threading;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
using Tommy;
|
|
using Tommy;
|
|
@@ -20,8 +18,9 @@ namespace FastGithub.DomainResolve
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <param name="tomlPath"></param>
|
|
/// <param name="tomlPath"></param>
|
|
/// <param name="endpoint"></param>
|
|
/// <param name="endpoint"></param>
|
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
/// <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
|
|
var value = new TomlArray
|
|
{
|
|
{
|
|
@@ -39,54 +38,37 @@ namespace FastGithub.DomainResolve
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
public static Task SetlogLevelAsync(string tomlPath, int logLevel, CancellationToken cancellationToken)
|
|
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>
|
|
/// <summary>
|
|
- /// 设置ecs
|
|
|
|
|
|
+ /// 设置TTL
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <param name="tomlPath"></param>
|
|
|
|
|
|
+ /// <param name="tomlPath"></param>
|
|
|
|
+ /// <param name="minTTL"></param>
|
|
|
|
+ /// <param name="maxTTL"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
/// <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>
|
|
/// 设置指定键的值
|
|
/// 设置指定键的值
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <param name="tomlPath"></param>
|
|
/// <param name="tomlPath"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></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 toml = await File.ReadAllTextAsync(tomlPath, cancellationToken);
|
|
var reader = new StringReader(toml);
|
|
var reader = new StringReader(toml);
|