From bae636808757686494fb9172f069ea1a6a36f1b7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com>
Date: Wed, 25 Aug 2021 22:53:00 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8toml=E5=BA=93?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
@libs/dnscrypt-proxy.toml | 2 -
.../FastGithub.DomainResolve.csproj | 7 +--
FastGithub.DomainResolve/TomlUtil.cs | 43 ++++++++++---------
3 files changed, 26 insertions(+), 26 deletions(-)
diff --git a/@libs/dnscrypt-proxy.toml b/@libs/dnscrypt-proxy.toml
index 762d53a..2a24387 100644
--- a/@libs/dnscrypt-proxy.toml
+++ b/@libs/dnscrypt-proxy.toml
@@ -36,8 +36,6 @@
## Example with both IPv4 and IPv6:
## listen_addresses = ['127.0.0.1:53', '[::1]:53']
-listen_addresses = ['127.0.0.1:5533']
-
## Maximum number of simultaneous client connections to accept
diff --git a/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj b/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj
index 1e02d91..3ee4dba 100644
--- a/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj
+++ b/FastGithub.DomainResolve/FastGithub.DomainResolve.csproj
@@ -4,6 +4,7 @@
+
@@ -11,9 +12,9 @@
PreserveNewest
-
- PreserveNewest
-
+
+ PreserveNewest
+
diff --git a/FastGithub.DomainResolve/TomlUtil.cs b/FastGithub.DomainResolve/TomlUtil.cs
index 7a4efbf..dfd8bc4 100644
--- a/FastGithub.DomainResolve/TomlUtil.cs
+++ b/FastGithub.DomainResolve/TomlUtil.cs
@@ -7,6 +7,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
+using Tommy;
namespace FastGithub.DomainResolve
{
@@ -21,9 +22,13 @@ namespace FastGithub.DomainResolve
///
///
///
- public static Task SetListensAsync(string tomlPath, IPEndPoint endpoint, CancellationToken cancellationToken = default)
+ public static Task SetListensAsync(string tomlPath, IPEndPoint endpoint, CancellationToken cancellationToken = default)
{
- return SetAsync(tomlPath, "listen_addresses", $"['{endpoint}']", cancellationToken);
+ var value = new TomlArray
+ {
+ endpoint.ToString()
+ };
+ return SetAsync(tomlPath, "listen_addresses", value, cancellationToken);
}
///
@@ -37,7 +42,12 @@ namespace FastGithub.DomainResolve
try
{
var address = await GetPublicIPAddressAsync(cancellationToken);
- return await SetAsync(tomlPath, "edns_client_subnet", @$"[""{address}/32""]", cancellationToken);
+ var value = new TomlArray
+ {
+ $"{address}/32"
+ };
+ await SetAsync(tomlPath, "edns_client_subnet", value, cancellationToken);
+ return true;
}
catch (Exception)
{
@@ -66,28 +76,19 @@ namespace FastGithub.DomainResolve
///
///
///
- public static async Task SetAsync(string tomlPath, string key, object? value, CancellationToken cancellationToken = default)
+ public static async Task SetAsync(string tomlPath, string key, TomlNode value, CancellationToken cancellationToken = default)
{
- var setted = false;
+ var toml = await File.ReadAllTextAsync(tomlPath, cancellationToken);
+ var reader = new StringReader(toml);
+ var tomlTable = TOML.Parse(reader);
+ tomlTable[key] = value;
+
var builder = new StringBuilder();
- var lines = await File.ReadAllLinesAsync(tomlPath, cancellationToken);
+ var writer = new StringWriter(builder);
+ tomlTable.WriteTo(writer);
+ toml = builder.ToString();
- foreach (var line in lines)
- {
- if (Regex.IsMatch(line, @$"(?<=#*\s*){key}(?=\s*=)") == false)
- {
- builder.AppendLine(line);
- }
- else if (setted == false)
- {
- setted = true;
- builder.Append(key).Append(" = ").AppendLine(value?.ToString());
- }
- }
-
- var toml = builder.ToString();
await File.WriteAllTextAsync(tomlPath, toml, cancellationToken);
- return setted;
}
}
}