From ce12a9d092ac684aaaf8cab32151b70348525a96 Mon Sep 17 00:00:00 2001
From: HuJi <669898595@qq.com>
Date: Sun, 11 Jun 2023 19:24:29 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ROOT=5FDOMAIN=E5=8F=82?=
=?UTF-8?q?=E6=95=B0=EF=BC=8C=E7=94=A8=E4=BA=8E=E8=A7=A3=E5=86=B3=E8=B0=83?=
=?UTF-8?q?=E7=94=A8AddDomainRecord=E6=97=B6DoaminName=E5=8F=82=E6=95=B0?=
=?UTF-8?q?=E4=B8=BA=E4=BA=8C=E7=BA=A7=E5=9F=9F=E5=90=8D=E7=9A=84=E9=97=AE?=
=?UTF-8?q?=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ReadMe.md | 2 ++
aliyun-ddns/DomainUpdater.cs | 42 +++++++++++++++++++++++++++---------
aliyun-ddns/Options.cs | 6 +++++-
3 files changed, 39 insertions(+), 11 deletions(-)
diff --git a/ReadMe.md b/ReadMe.md
index 9b5a02c..02648a4 100644
--- a/ReadMe.md
+++ b/ReadMe.md
@@ -34,6 +34,7 @@ docker run -d --restart=always --net=host \
|AKID|阿里云的Access Key ID。[获取阿里云AccessToken](https://usercenter.console.aliyun.com/)|access key id|
|AKSCT|阿里云的Access Key Secret。|access key secret|
|DOMAIN|需要更新的域名,可以用“,”隔开。
可以指定线路,用“:”分隔线路和域名([线路名说明](https://help.aliyun.com/document_detail/29807.html?spm=a2c4g.11186623.2.14.42405eb4boCsnd))。
例如:“baidu.com,telecom:dianxin.baidu.com”。|my.domain.com|
+|ROOT_DOMAIN|以参数DOMAIN为 a.www.example.com 为示例:
1.如果参数ROOT_DOMAIN为空,则查询域名为example.com、主机名为”a.www“的解析记录;
2.如果参数ROOT_DOMAIN为 www.example.com,则查询域名为www.example.com、主机名为 "a"的解析记录;
3.如果参数ROOT_DOMAIN为 a.www.example.com,则查询域名为a.www.example.com、主机名为 "@"的解析记录。|无|
|REDO|更新间隔,单位秒。建议大于等于TTL/2。|300|
|TTL|服务器缓存解析记录的时长,单位秒,普通用户最小为600。|600|
|TIMEZONE|输出日志时的时区,单位小时。|8|
@@ -73,6 +74,7 @@ dotnet aliyun-ddns.dll \
|u|阿里云的Access Key ID。[获取阿里云AccessToken](https://usercenter.console.aliyun.com/)|access key id|
|p|阿里云的Access Key Secret。|access key secret|
|d|需要更新的域名,可以用“,”隔开。
可以指定线路,用“:”分隔线路和域名([线路名说明](https://help.aliyun.com/document_detail/29807.html?spm=a2c4g.11186623.2.14.42405eb4boCsnd))。
例如:“baidu.com,telecom:dianxin.baidu.com”。|my.domain.com|
+|root-domain|以参数DOMAIN为 a.www.example.com 为示例:
1.如果参数ROOT_DOMAIN为空,则查询域名为example.com、主机名为”a.www“的解析记录;
2.如果参数ROOT_DOMAIN为 www.example.com,则查询域名为www.example.com、主机名为 "a"的解析记录;
3.如果参数ROOT_DOMAIN为 a.www.example.com,则查询域名为a.www.example.com、主机名为 "@"的解析记录。|无|
|i|更新间隔,单位秒。建议大于等于TTL/2。|300|
|t|服务器缓存解析记录的时长,单位秒,普通用户最小为600。|600|
|timezone|输出日志时的时区,单位小时。|8|
diff --git a/aliyun-ddns/DomainUpdater.cs b/aliyun-ddns/DomainUpdater.cs
index c03ec1f..d5b0a92 100644
--- a/aliyun-ddns/DomainUpdater.cs
+++ b/aliyun-ddns/DomainUpdater.cs
@@ -262,6 +262,7 @@ private IList GetRecords(string domain)
{
DescribeSubDomainRecordsRequest request = new DescribeSubDomainRecordsRequest
{
+ DomainName = Options.Instance.RootDomain,
SubDomain = subDomain,
PageSize = _pageSize,
PageNumber = pageNumber,
@@ -342,7 +343,7 @@ private bool DeleteRecords(IEnumerable records)
DeleteDomainRecordRequest request = new DeleteDomainRecordRequest
{
RecordId = rd.RecordId
- };
+ };
var response = client.GetAcsResponse(request);
if (response.HttpResponse.isSuccess())
{
@@ -395,21 +396,42 @@ private bool AddRecord(IpType type, string domain, string ip)
{
try
{
- ParseSubDomainAndLine(domain, out string subDomain, out string line);
- string pattern = @"^(\S*)\.(\S+)\.(\S+)$";
- Regex regex = new Regex(pattern);
- var match = regex.Match(subDomain);
string domainName;
string rr;
- if (match.Success)
+
+ ParseSubDomainAndLine(domain, out string subDomain, out string line);
+ if (Options.Instance.RootDomain == null)
{
- rr = match.Groups[1].Value;
- domainName = match.Groups[2].Value + "." + match.Groups[3].Value;
+ string pattern = @"^(\S*)\.(\S+)\.(\S+)$";
+ Regex regex = new Regex(pattern);
+ var match = regex.Match(subDomain);
+ if (match.Success)
+ {
+ rr = match.Groups[1].Value;
+ domainName = match.Groups[2].Value + "." + match.Groups[3].Value;
+ }
+ else
+ {
+ rr = "@";
+ domainName = subDomain;
+ }
}
else
{
- rr = "@";
- domainName = subDomain;
+ if (subDomain == Options.Instance.RootDomain)
+ {
+ rr = "@";
+ domainName = subDomain;
+ }
+ else if (subDomain.EndsWith($".{ Options.Instance.RootDomain }"))
+ {
+ rr = subDomain.Substring(0, subDomain.Length - $".{ Options.Instance.RootDomain }".Length);
+ domainName = Options.Instance.RootDomain;
+ }
+ else
+ {
+ throw new ArgumentException($"DOMAIN(“{ subDomain }“)必须以ROOTDOMAIN(“.{ Options.Instance.RootDomain }“)结尾");
+ }
}
var client = GetNewClient();
diff --git a/aliyun-ddns/Options.cs b/aliyun-ddns/Options.cs
index 908996e..f64a464 100644
--- a/aliyun-ddns/Options.cs
+++ b/aliyun-ddns/Options.cs
@@ -17,6 +17,9 @@ public class Options
[Option('d', "domain", Required = false, Default = "my.domain.com", HelpText = "需要更新的域名,可以用“,”隔开。可以指定线路,用“:”分隔线路和域名(线路名说明见https://help.aliyun.com/document_detail/29807.html?spm=a2c4g.11186623.2.14.42405eb4boCsnd)。例如:“baidu.com,telecom:dianxin.baidu.com”。")]
public string Domain { get; set; } = "my.domain.com";
+ [Option("root-domain", Required = false, Default = null, HelpText = "需要更新的主域名")]
+ public string RootDomain { get; set; } = null;
+
[Option('i', "interval", Required = false, Default = 300, HelpText = "执行域名更新的间隔,单位秒。")]
public int Redo { get; set; } = 300;
@@ -47,7 +50,7 @@ public class Options
private static Options _instance = null;
private static object _instanceLocker = new object();
- public static Options Instance
+ public static Options Instance
{
get
{
@@ -77,6 +80,7 @@ private void InitOptionsFromEnvironment()
Akid = GetEnvironmentVariable("AKID") ?? Akid;
Aksct = GetEnvironmentVariable("AKSCT") ?? Aksct;
//ENDPOINT = GetEnvironmentVariable("ENDPOINT") ?? ENDPOINT;
+ RootDomain = GetEnvironmentVariable("ROOT_DOMAIN") ?? RootDomain;
Domain = GetEnvironmentVariable("DOMAIN") ?? Domain;
Type = GetEnvironmentVariable("TYPE") ?? Type;
WebHook = GetEnvironmentVariable("WEBHOOK") ?? WebHook;