-
-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2531 from zgcwkj/master
Add ALiYun Dns validation
- Loading branch information
Showing
10 changed files
with
323 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
using AlibabaCloud.OpenApiClient.Models; | ||
using AlibabaCloud.SDK.Alidns20150109.Models; | ||
using PKISharp.WACS.Clients.DNS; | ||
using PKISharp.WACS.Plugins.Base.Capabilities; | ||
using PKISharp.WACS.Plugins.Interfaces; | ||
using PKISharp.WACS.Services; | ||
using System; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Runtime.Versioning; | ||
using System.Threading.Tasks; | ||
|
||
[assembly: SupportedOSPlatform("windows")] | ||
|
||
namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns | ||
{ | ||
[IPlugin.Plugin< | ||
ALiYunOptions, ALiYunOptionsFactory, | ||
DnsValidationCapability, ALiYunJson> | ||
("1d4db2ea-ce7c-46ce-b86f-40b356fcf999", | ||
"ALiYun", "Create verification records in ALiYun DNS")] | ||
public class ALiYun : DnsValidation<ALiYun>, IDisposable | ||
{ | ||
private ALiYunOptions _options { get; } | ||
private SecretServiceManager _ssm { get; } | ||
private HttpClient _hc { get; } | ||
private AlibabaCloud.SDK.Alidns20150109.Client _client { get; } | ||
|
||
public ALiYun( | ||
ALiYunOptions options, | ||
SecretServiceManager ssm, | ||
IProxyService proxyService, | ||
LookupClientProvider dnsClient, | ||
ILogService log, | ||
ISettingsService settings) : base(dnsClient, log, settings) | ||
{ | ||
_options = options; | ||
_ssm = ssm; | ||
_hc = proxyService.GetHttpClient(); | ||
//New Client | ||
var config = new Config | ||
{ | ||
AccessKeyId = _ssm.EvaluateSecret(_options.ApiID), | ||
AccessKeySecret = _ssm.EvaluateSecret(_options.ApiSecret), | ||
Endpoint = _ssm.EvaluateSecret(_options.ApiServer), | ||
}; | ||
_client = new AlibabaCloud.SDK.Alidns20150109.Client(config); | ||
} | ||
|
||
public override async Task<bool> CreateRecord(DnsValidationRecord record) | ||
{ | ||
await Task.Delay(0); | ||
try | ||
{ | ||
var identifier = record.Context.Identifier; | ||
var domain = record.Authority.Domain; | ||
var value = record.Value; | ||
//Add Record | ||
return AddRecord(identifier, domain, value); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
//Out Error | ||
_log.Error($"Unable to add ALiYunDNS record: {ex.Message}"); | ||
} | ||
return false; | ||
} | ||
|
||
public override async Task DeleteRecord(DnsValidationRecord record) | ||
{ | ||
await Task.Delay(0); | ||
try | ||
{ | ||
var identifier = record.Context.Identifier; | ||
var domain = record.Authority.Domain; | ||
//Delete Record | ||
DelRecord(identifier, domain); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
//Out Error | ||
_log.Error($"Unable to delete ALiYunDNS record: {ex.Message}"); | ||
} | ||
} | ||
|
||
#region PrivateLogic | ||
|
||
/// <summary> | ||
/// Add Record | ||
/// </summary> | ||
/// <param name="domain">Domain</param> | ||
/// <param name="subDomain">SubDomain</param> | ||
/// <param name="value">Value</param> | ||
/// <returns></returns> | ||
private bool AddRecord(string domain, string subDomain, string value) | ||
{ | ||
subDomain = subDomain.Replace($".{domain}", ""); | ||
//Delete Record | ||
DelRecord(domain, subDomain); | ||
//Add Record | ||
var addRecords = new AddDomainRecordRequest | ||
{ | ||
DomainName = domain, | ||
RR = subDomain, | ||
Type = "TXT", | ||
Value = value | ||
}; | ||
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); | ||
var data = _client.AddDomainRecordWithOptions(addRecords, runtime); | ||
//Console.WriteLine(data); | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Delete Record | ||
/// </summary> | ||
/// <param name="domain">Domain</param> | ||
/// <param name="subDomain">SubDomain</param> | ||
/// <returns></returns> | ||
private bool DelRecord(string domain, string subDomain) | ||
{ | ||
subDomain = subDomain.Replace($".{domain}", ""); | ||
//Get RecordID | ||
var recordId = GetRecordID(domain, subDomain); | ||
if (recordId == default) return false; | ||
//Delete Record | ||
var delRecords = new DeleteDomainRecordRequest | ||
{ | ||
RecordId = recordId.ToString(), | ||
}; | ||
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); | ||
var data = _client.DeleteDomainRecordWithOptions(delRecords, runtime); | ||
//Console.WriteLine(data); | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Get RecordID | ||
/// </summary> | ||
/// <param name="domain">Domain</param> | ||
/// <param name="subDomain">SubDomain</param> | ||
/// <returns></returns> | ||
private string? GetRecordID(string domain, string subDomain) | ||
{ | ||
var getRecords = new DescribeDomainRecordsRequest | ||
{ | ||
DomainName = domain, | ||
}; | ||
var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions(); | ||
var data = _client.DescribeDomainRecordsWithOptions(getRecords, runtime); | ||
//Console.WriteLine(data); | ||
var jsonDataLinq = data.Body.DomainRecords.Record.Where(w => w.RR == subDomain && w.Type == "TXT"); | ||
if (jsonDataLinq.Any()) return jsonDataLinq.First().RecordId; | ||
return default; | ||
} | ||
|
||
#endregion PrivateLogic | ||
|
||
public void Dispose() => _hc.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using PKISharp.WACS.Configuration; | ||
using PKISharp.WACS.Configuration.Arguments; | ||
|
||
namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns | ||
{ | ||
public class ALiYunArguments : BaseArguments | ||
{ | ||
public override string Name => "ALiYun"; | ||
|
||
public override string Group => "Validation"; | ||
|
||
public override string Condition => "--validation aliyun"; | ||
|
||
[CommandLine(Description = "DNS Server Domain Name\r\nRefer: https://api.aliyun.com/product/Alidns", Secret = false)] | ||
public string? ALiYunServer { get; set; } = "dns.aliyuncs.com"; | ||
|
||
[CommandLine(Description = "API ID for ALiYun.", Secret = true)] | ||
public string? ALiYunApiID { get; set; } | ||
|
||
[CommandLine(Description = "API Secret for ALiYun.", Secret = true)] | ||
public string? ALiYunApiSecret { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using PKISharp.WACS.Plugins.Base.Options; | ||
using PKISharp.WACS.Services.Serialization; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns | ||
{ | ||
[JsonSerializable(typeof(ALiYunOptions))] | ||
internal partial class ALiYunJson : JsonSerializerContext | ||
{ | ||
public ALiYunJson(WacsJsonPluginsOptionsFactory optionsFactory) : base(optionsFactory.Options) | ||
{ | ||
} | ||
} | ||
|
||
public class ALiYunOptions : ValidationPluginOptions | ||
{ | ||
/// <summary> | ||
/// ApiServer | ||
/// </summary> | ||
public string? ApiServer { get; set; } | ||
|
||
/// <summary> | ||
/// ApiID | ||
/// </summary> | ||
public ProtectedString? ApiID { get; set; } | ||
|
||
/// <summary> | ||
/// ApiSecret | ||
/// </summary> | ||
public ProtectedString? ApiSecret { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using PKISharp.WACS.Configuration; | ||
using PKISharp.WACS.Plugins.Base.Factories; | ||
using PKISharp.WACS.Services; | ||
using PKISharp.WACS.Services.Serialization; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace PKISharp.WACS.Plugins.ValidationPlugins.Dns | ||
{ | ||
public class ALiYunOptionsFactory : PluginOptionsFactory<ALiYunOptions> | ||
{ | ||
private ArgumentsInputService _arguments { get; } | ||
|
||
public ALiYunOptionsFactory(ArgumentsInputService arguments) => _arguments = arguments; | ||
|
||
private ArgumentResult<string?> ApiServer => _arguments.GetString<ALiYunArguments>(a => a.ALiYunServer).Required(); | ||
|
||
private ArgumentResult<ProtectedString?> ApiID => _arguments.GetProtectedString<ALiYunArguments>(a => a.ALiYunApiID).Required(); | ||
|
||
private ArgumentResult<ProtectedString?> ApiSecret => _arguments.GetProtectedString<ALiYunArguments>(a => a.ALiYunApiSecret).Required(); | ||
|
||
public override async Task<ALiYunOptions?> Aquire(IInputService inputService, RunLevel runLevel) | ||
{ | ||
return new ALiYunOptions | ||
{ | ||
ApiServer = await ApiServer.Interactive(inputService, "ALiYun Domain Server").GetValue(), | ||
ApiID = await ApiID.Interactive(inputService, "ALiYun AccessKey ID").GetValue(), | ||
ApiSecret = await ApiSecret.Interactive(inputService, "ALiYun AccessKey Secret").GetValue(), | ||
}; | ||
} | ||
|
||
public override async Task<ALiYunOptions?> Default() | ||
{ | ||
return new ALiYunOptions | ||
{ | ||
ApiServer = await ApiServer.GetValue(), | ||
ApiID = await ApiID.GetValue(), | ||
ApiSecret = await ApiSecret.GetValue(), | ||
}; | ||
} | ||
|
||
public override IEnumerable<(CommandLineAttribute, object?)> Describe(ALiYunOptions options) | ||
{ | ||
yield return (ApiServer.Meta, options.ApiServer); | ||
yield return (ApiID.Meta, options.ApiID); | ||
yield return (ApiSecret.Meta, options.ApiSecret); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/plugin.validation.dns.aliyun/wacs.validation.dns.aliyun.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RootNamespace>PKISharp.WACS.Plugins.ValidationPlugins</RootNamespace> | ||
<AssemblyName>PKISharp.WACS.Plugins.ValidationPlugins.ALiYun</AssemblyName> | ||
<Version>2.1.0.0</Version> | ||
<AssemblyVersion>2.1.0.0</AssemblyVersion> | ||
<FileVersion>2.1.0.0</FileVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AlibabaCloud.SDK.Alidns20150109" Version="3.0.13" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\main.lib\wacs.lib.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters