diff --git a/FastGithub.DomainResolve/DnscryptProxy.cs b/FastGithub.DomainResolve/DnscryptProxy.cs index 7fc15da..7905e0d 100644 --- a/FastGithub.DomainResolve/DnscryptProxy.cs +++ b/FastGithub.DomainResolve/DnscryptProxy.cs @@ -40,7 +40,9 @@ namespace FastGithub.DomainResolve var tomlPath = Path.Combine(PATH, $"{NAME}.toml"); var port = GetAvailablePort(IPAddress.Loopback.AddressFamily); var localEndPoint = new IPEndPoint(IPAddress.Loopback, port); + await TomlUtil.SetListensAsync(tomlPath, localEndPoint, cancellationToken); + await TomlUtil.SetEdnsClientSubnetAsync(tomlPath, cancellationToken); foreach (var process in Process.GetProcessesByName(NAME)) { diff --git a/FastGithub.DomainResolve/TomlUtil.cs b/FastGithub.DomainResolve/TomlUtil.cs index 2b73381..148c4d1 100644 --- a/FastGithub.DomainResolve/TomlUtil.cs +++ b/FastGithub.DomainResolve/TomlUtil.cs @@ -1,6 +1,9 @@ -using System.IO; +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; @@ -27,6 +30,44 @@ namespace FastGithub.DomainResolve return SetAsync(tomlPath, "listen_addresses", value, cancellationToken); } + /// + /// 设置ecs + /// + /// + /// + /// + public static async Task SetEdnsClientSubnetAsync(string tomlPath, CancellationToken cancellationToken = default) + { + 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; + } + } + + /// + /// 获取公网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; + } + /// /// 设置指定键的值 ///