From f72f6dea7898c67f529fd7976e3313916f0a8ac2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Mon, 27 Sep 2021 17:46:06 +0800
Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8DEdns?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.DomainResolve/DnscryptProxy.cs | 2 ++
FastGithub.DomainResolve/TomlUtil.cs | 43 ++++++++++++++++++++++-
2 files changed, 44 insertions(+), 1 deletion(-)
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;
+ }
+
///
/// 设置指定键的值
///