Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加ROOT_DOMAIN参数 #79

Merged
merged 1 commit into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|需要更新的域名,可以用“,”隔开。<br>可以指定线路,用“:”分隔线路和域名([线路名说明](https://help.aliyun.com/document_detail/29807.html?spm=a2c4g.11186623.2.14.42405eb4boCsnd))。<br>例如:“baidu.com,telecom:dianxin.baidu.com”。|my.domain.com|
|ROOT_DOMAIN|以参数DOMAIN为 a.www.example.com 为示例:<br>1.如果参数ROOT_DOMAIN为空,则查询域名为example.com、主机名为”a.www“的解析记录;<br>2.如果参数ROOT_DOMAIN为 www.example.com,则查询域名为www.example.com、主机名为 "a"的解析记录;<br>3.如果参数ROOT_DOMAIN为 a.www.example.com,则查询域名为a.www.example.com、主机名为 "@"的解析记录。|无|
|REDO|更新间隔,单位秒。建议大于等于TTL/2。|300|
|TTL|服务器缓存解析记录的时长,单位秒,普通用户最小为600。|600|
|TIMEZONE|输出日志时的时区,单位小时。|8|
Expand Down Expand Up @@ -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|需要更新的域名,可以用“,”隔开。<br>可以指定线路,用“:”分隔线路和域名([线路名说明](https://help.aliyun.com/document_detail/29807.html?spm=a2c4g.11186623.2.14.42405eb4boCsnd))。<br>例如:“baidu.com,telecom:dianxin.baidu.com”。|my.domain.com|
|root-domain|以参数DOMAIN为 a.www.example.com 为示例:<br>1.如果参数ROOT_DOMAIN为空,则查询域名为example.com、主机名为”a.www“的解析记录;<br>2.如果参数ROOT_DOMAIN为 www.example.com,则查询域名为www.example.com、主机名为 "a"的解析记录;<br>3.如果参数ROOT_DOMAIN为 a.www.example.com,则查询域名为a.www.example.com、主机名为 "@"的解析记录。|无|
|i|更新间隔,单位秒。建议大于等于TTL/2。|300|
|t|服务器缓存解析记录的时长,单位秒,普通用户最小为600。|600|
|timezone|输出日志时的时区,单位小时。|8|
Expand Down
42 changes: 32 additions & 10 deletions aliyun-ddns/DomainUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ private IList<Record> GetRecords(string domain)
{
DescribeSubDomainRecordsRequest request = new DescribeSubDomainRecordsRequest
{
DomainName = Options.Instance.RootDomain,
SubDomain = subDomain,
PageSize = _pageSize,
PageNumber = pageNumber,
Expand Down Expand Up @@ -342,7 +343,7 @@ private bool DeleteRecords(IEnumerable<Record> records)
DeleteDomainRecordRequest request = new DeleteDomainRecordRequest
{
RecordId = rd.RecordId
};
};
var response = client.GetAcsResponse(request);
if (response.HttpResponse.isSuccess())
{
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion aliyun-ddns/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down