Skip to content

Commit

Permalink
Merge pull request #13 from sanjusss/dev
Browse files Browse the repository at this point in the history
0.2.1
  • Loading branch information
sanjusss authored Feb 23, 2020
2 parents e9db0e5 + 87d2c7a commit b88d4a7
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 60 deletions.
49 changes: 30 additions & 19 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


[![Build status](https://ci.appveyor.com/api/projects/status/r585ycrd9wn0v5ed?svg=true)](https://ci.appveyor.com/project/sanjusss/aliyun-ddns)
[![](https://img.shields.io/docker/stars/sanjusss/aliyun-ddns.svg?logo=docker)](https://hub.docker.com/r/sanjusss/aliyun-ddns)
[![GitHub license](https://img.shields.io/github/license/sanjusss/aliyun-ddns.svg)](https://github.com/sanjusss/aliyun-ddns/blob/master/LICENSE)
Expand All @@ -23,15 +23,19 @@ docker run -d --restart=always --net=host \
-e "TYPE=A,AAAA" \
sanjusss/aliyun-ddns
```
如果要支持IPv6,必须使用`--net=host`模式,否则无法设置宿主机的ipv6地址到AAAA记录。
AKID:阿里云的Access Key ID。([获取阿里云AccessToken](https://usercenter.console.aliyun.com/)
AKSCT:阿里云的Access Key Secret。
DOMAIN:需要更新的域名,多个域名需要“,”分隔。
ENDPOINT:默认为cn-hangzhou,[详见定义](https://help.aliyun.com/document_detail/40654.html?spm=a2c4e.11153987.0.0.6d85366aUfTWbG)
REDO: 更新间隔,单位秒。建议大于等于TTL/2。
TTL: 服务器缓存解析记录的时长,单位秒,普通用户最小为600。
TIMEZONE: 输出日志时的时区,单位小时。
TYPE: 需要更改的记录类型,可以用“,”隔开,只能是“A”、“AAAA”或“A,AAAA”。
| 环境变量名称 | 注释 | 默认值 |
| :---- | :----- | :--- |
|AKID|阿里云的Access Key ID。[获取阿里云AccessToken](https://usercenter.console.aliyun.com/)|access key id|
|AKSCT|阿里云的Access Key Secret。|access key secret|
|DOMAIN|需要更新的域名,多个域名需要“,”分隔。|my.domain.com|
|ENDPOINT|[详见定义](https://help.aliyun.com/document_detail/40654.html?spm=a2c4e.11153987.0.0.6d85366aUfTWbG)|cn-hangzhou|
|REDO|更新间隔,单位秒。建议大于等于TTL/2。|300|
|TTL|服务器缓存解析记录的时长,单位秒,普通用户最小为600。|600|
|TIMEZONE|输出日志时的时区,单位小时。|8|
|TYPE|需要更改的记录类型,可以用“,”隔开,只能是“A”、“AAAA”或“A,AAAA”。|A,AAAA|
|CNIPV4|检查IPv4地址时,仅使用中国服务器。|false|

以上环境变量均存在默认值,添加需要修改的环境变量即可。

### 命令行
##### 查看帮助信息
Expand All @@ -52,13 +56,20 @@ dotnet aliyun-ddns.dll \
-i 300 \
-t 600 \
--timezone 8.0 \
--type A
--type A \
--cnipv4
```
u:阿里云的Access Key ID。
p:阿里云的Access Key Secret。
d:需要更新的域名,多个域名需要“,”分隔。
e:默认为cn-hangzhou,[详见定义](https://help.aliyun.com/document_detail/40654.html?spm=a2c4e.11153987.0.0.6d85366aUfTWbG)
i: 更新间隔,单位秒。建议大于等于TTL/2。
t: 服务器缓存解析记录的时长,单位秒,普通用户最小为600。
tz: 输出日志时的时区,单位小时。
type: 需要更改的记录类型,可以用“,”隔开,只能是“A”、“AAAA”或“A,AAAA”。

| 参数名称 | 注释 | 默认值 |
| :---- | :----- | :--- |
|u|阿里云的Access Key ID。[获取阿里云AccessToken](https://usercenter.console.aliyun.com/)|access key id|
|p|阿里云的Access Key Secret。|access key secret|
|d|需要更新的域名,多个域名需要“,”分隔。|my.domain.com|
|e|[详见定义](https://help.aliyun.com/document_detail/40654.html?spm=a2c4e.11153987.0.0.6d85366aUfTWbG)|cn-hangzhou|
|i|更新间隔,单位秒。建议大于等于TTL/2。|300|
|t|服务器缓存解析记录的时长,单位秒,普通用户最小为600。|600|
|timezone|输出日志时的时区,单位小时。|8|
|type|需要更改的记录类型,可以用“,”隔开,只能是“A”、“AAAA”或“A,AAAA”。|A,AAAA|
|cnipv4|检查IPv4地址时,仅使用中国服务器。|false|

以上参数均存在默认值,添加需要修改的参数即可。
11 changes: 1 addition & 10 deletions aliyun-ddns/Common/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,13 @@ namespace aliyun_ddns.Common
/// </summary>
public static class Log
{
public static Options OP { get; set; } = null;

/// <summary>
/// 打印日志到屏幕。
/// </summary>
/// <param name="msg">日志信息</param>
public static void Print(string msg)
{
if (OP == null)
{
Console.WriteLine($"[{ DateTime.Now }]{ msg }");
}
else
{
Console.WriteLine($"[{ DateTime.UtcNow.AddHours(OP.TIMEZONE) }]{ msg }");
}
Console.WriteLine($"[{ DateTime.UtcNow.AddHours(Options.Instance.TIMEZONE) }]{ msg }");
}
}
}
21 changes: 6 additions & 15 deletions aliyun-ddns/DomainUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ namespace aliyun_ddns
/// </summary>
public class DomainUpdater
{
/// <summary>
/// 运行配置。
/// </summary>
private readonly Options _op;
/// <summary>
/// 每次请求的记录上限。
/// </summary>
Expand All @@ -49,18 +45,13 @@ private enum IpType
ANY
}

public DomainUpdater(Options op)
{
_op = op;
}

/// <summary>
/// 运行自动更新。
/// </summary>
public void Run()
{
TimeSpan maxWait = new TimeSpan(0, 0, _op.REDO);
string[] domains = _op.DOMAIN.Split(',', StringSplitOptions.RemoveEmptyEntries);
TimeSpan maxWait = new TimeSpan(0, 0, Options.Instance.REDO);
string[] domains = Options.Instance.DOMAIN.Split(',', StringSplitOptions.RemoveEmptyEntries);
HashSet<IpType> targetTypes = GetTargetTypes();
if (targetTypes.Count == 0)
{
Expand Down Expand Up @@ -146,7 +137,7 @@ private void Update(string[] domains, HashSet<IpType> targetTypes)
/// <returns>目标记录类型的集合</returns>
private HashSet<IpType> GetTargetTypes()
{
HashSet<string> inputTypes = new HashSet<string>(_op.TYPE.Split(',', StringSplitOptions.RemoveEmptyEntries));
HashSet<string> inputTypes = new HashSet<string>(Options.Instance.TYPE.Split(',', StringSplitOptions.RemoveEmptyEntries));
HashSet<IpType> targetTypes = new HashSet<IpType>();
if (inputTypes.Contains("A"))
{
Expand Down Expand Up @@ -231,7 +222,7 @@ private IEnumerable<IpType> GetSupportIpTypes()
/// <returns>新的阿里云客户端</returns>
private DefaultAcsClient GetNewClient()
{
var clientProfile = DefaultProfile.GetProfile(_op.ENDPOINT, _op.AKID, _op.AKSCT);
var clientProfile = DefaultProfile.GetProfile(Options.Instance.ENDPOINT, Options.Instance.AKID, Options.Instance.AKSCT);
return new DefaultAcsClient(clientProfile);
}

Expand Down Expand Up @@ -377,7 +368,7 @@ private bool AddRecord(IpType type, string domain, string ip)
RR = rr,
Type = type.ToString(),
_Value = ip,
TTL = _op.TTL
TTL = Options.Instance.TTL
};
var response = client.GetAcsResponse(request);
if (response.HttpResponse.isSuccess())
Expand Down Expand Up @@ -425,7 +416,7 @@ private bool UpdateRecord(Record rd, string ip)
RR = rd.RR,
Type = rd.Type,
_Value = ip,
TTL = _op.TTL
TTL = Options.Instance.TTL
};
var response = client.GetAcsResponse(request);
if (response.HttpResponse.isSuccess())
Expand Down
1 change: 1 addition & 0 deletions aliyun-ddns/IPGetter/IPv4Getter/IPv4GetterCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static class IPv4GetterCreator
new CommonIPv4Getter("ipip.net接口", "http://myip.ipip.net/"),
new CommonIPv4Getter("ip-api.com接口", "http://ip-api.com/json/?fields=query", 100),
new CommonIPv4Getter("淘宝接口", "http://ip.taobao.com/service/getIpInfo.php?ip=myip"),
new CommonIPv4Getter("3322接口", "http://ip.3322.net/"),
};

public static IEnumerable<IIPv4Getter> Create()
Expand Down
53 changes: 43 additions & 10 deletions aliyun-ddns/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,67 @@ public class Options
public double TIMEZONE { get; set; } = 8;
[Option("type", Required = false, Default = "A,AAAA", HelpText = "需要更改的记录类型,可以用“,”隔开,只能是“A”、“AAAA”或“A,AAAA”。")]
public string TYPE { get; set; } = "A,AAAA";
[Option("cnipv4", Required = false, Default = false, HelpText = "仅使用中国服务器检测公网IPv4地址。")]
public bool CNIPV4 { get; set; } = false;

public static Options GetOptionsFromEnvironment(ref Options op)
private static Options _instance = null;
private static object _instanceLocker = new object();
public static Options Instance
{
op.AKID = Environment.GetEnvironmentVariable("AKID") ?? op.AKID;
op.AKSCT = Environment.GetEnvironmentVariable("AKSCT") ?? op.AKSCT;
op.ENDPOINT = Environment.GetEnvironmentVariable("ENDPOINT") ?? op.ENDPOINT;
op.DOMAIN = Environment.GetEnvironmentVariable("DOMAIN") ?? op.DOMAIN;
op.TYPE = Environment.GetEnvironmentVariable("TYPE") ?? op.TYPE;
get
{
lock (_instanceLocker)
{
if (_instance == null)
{
_instance = new Options();
_instance.InitOptionsFromEnvironment();
}

return _instance;
}
}
set
{
lock (_instanceLocker)
{
value.InitOptionsFromEnvironment();
_instance = value;
}
}
}

private void InitOptionsFromEnvironment()
{
AKID = Environment.GetEnvironmentVariable("AKID") ?? AKID;
AKSCT = Environment.GetEnvironmentVariable("AKSCT") ?? AKSCT;
ENDPOINT = Environment.GetEnvironmentVariable("ENDPOINT") ?? ENDPOINT;
DOMAIN = Environment.GetEnvironmentVariable("DOMAIN") ?? DOMAIN;
TYPE = Environment.GetEnvironmentVariable("TYPE") ?? TYPE;

string redoText = Environment.GetEnvironmentVariable("REDO");
if (int.TryParse(redoText, out int redo))
{
op.REDO = redo;
REDO = redo;
}

string ttlText = Environment.GetEnvironmentVariable("TTL");
if (long.TryParse(ttlText, out long ttl))
{
op.TTL = ttl;
TTL = ttl;
}

string tzText = Environment.GetEnvironmentVariable("TIMEZONE");
if (double.TryParse(tzText, out double tz))
{
op.TIMEZONE = tz;
TIMEZONE = tz;
}

return op;
string cnipv4Text = Environment.GetEnvironmentVariable("CNIPV4");
if (bool.TryParse(cnipv4Text, out bool cnipv4))
{
CNIPV4 = cnipv4;
}
}
}
}
5 changes: 2 additions & 3 deletions aliyun-ddns/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ static void Main(string[] args)
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(op =>
{
Options.GetOptionsFromEnvironment(ref op);
Log.OP = op;
DomainUpdater updater = new DomainUpdater(op);
Options.Instance = op;
DomainUpdater updater = new DomainUpdater();
updater.Run();
});
#if DEBUG
Expand Down
4 changes: 3 additions & 1 deletion aliyun-ddns/PublicIpGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public static async Task<string> GetIpv4()
{
try
{
var dics = IPv4GetterCreator.Create().ToDictionary(g => g.GetIP(), g => g.Description);
var dics = IPv4GetterCreator.Create()
.Where(g => Options.Instance.CNIPV4 == false || g.Order < 100)
.ToDictionary(g => g.GetIP(), g => g.Description);
string result = await dics.Keys.WhenAny(task =>
{
string ip = task.Result;
Expand Down
4 changes: 3 additions & 1 deletion aliyun-ddns/aliyun-ddns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>aliyun_ddns</RootNamespace>
<Version>0.1.6</Version>
<Version>0.2.1</Version>
<Authors>sanjusss</Authors>
<PackageProjectUrl>https://github.com/sanjusss/aliyun-ddns</PackageProjectUrl>
<PackageLicenseUrl></PackageLicenseUrl>
<RepositoryUrl>https://github.com/sanjusss/aliyun-ddns</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>BSD</PackageLicenseExpression>
<AssemblyVersion>0.2.1.0</AssemblyVersion>
<FileVersion>0.2.1.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

version: 0.1.6-{build}
version: 0.2.1-{build}
branches:
only:
- master
pull_requests:
do_not_increment_build_number: true
shallow_clone: true
Expand Down

0 comments on commit b88d4a7

Please sign in to comment.