diff --git a/src/ali_dns.rs b/src/ali_dns.rs new file mode 100644 index 0000000..71e4e0e --- /dev/null +++ b/src/ali_dns.rs @@ -0,0 +1,98 @@ +use aliyun_openapi_core_rust_sdk::RPClient; +use rust_util::XResult; + +static ALI_DNS_ENDPOINT: &str = "https://alidns.aliyuncs.com"; +static ALI_DNS_API_VERSION: &str = "2015-01-09"; + +#[derive(Debug)] +pub struct AccessCredential { + access_key_id: String, + access_key_secret: String, +} + +#[test] +fn test() { + let a = AccessCredential { + access_key_id: "***".to_string(), + access_key_secret: "***".to_string(), + }; + let client = create_dns_client(&a); + println!("{}", list_dns(&client, "webauthn.host").unwrap()); + // println!("{}", add_dns_txt(&client, "webauthn.host").unwrap()); + // println!("{}", delete_dns(&client, "744459160027659264").unwrap()); +} + +// { +// "TotalCount": 2, +// "RequestId": "8993B447-F1FF-58ED-9D4B-1027B551DC5E", +// "PageSize": 20, +// "DomainRecords": { +// "Record": [ +// { +// "RR": "www", +// "Line": "default", +// "Status": "ENABLE", +// "Locked": false, +// "Type": "A", +// "DomainName": "webauthn.host", +// "Value": "47.52.7.223", +// "RecordId": "714019124998091776", +// "TTL": 1800, +// "Weight": 1 +// }, +// { +// "RR": "@", +// "Line": "default", +// "Status": "ENABLE", +// "Locked": false, +// "Type": "A", +// "DomainName": "webauthn.host", +// "Value": "47.52.7.223", +// "RecordId": "714019101941960704", +// "TTL": 1800, +// "Weight": 1 +// } +// ] +// }, +// "PageNumber": 1 +// } +pub fn list_dns(client: &RPClient, domain: &str) -> XResult { + Ok(client.get("DescribeDomainRecords") + .query(&[ + ("RegionId", "cn-hangzhou"), + ("DomainName", domain) + ]) + .send()?) +} + +// {"RequestId":"AD997158-68D2-5084-B6B9-5F5A0893DDC1","RecordId":"744459160027659264"} +pub fn delete_dns(client: &RPClient, record_id: &str) -> XResult { + Ok(client.get("DeleteDomainRecord") + .query(&[ + ("RegionId", "cn-hangzhou"), + ("RecordId", record_id) + ]) + .send()?) +} + +// {"RequestId":"F3D54AB2-7058-54FD-AAF3-566FB8EC9BD1","RecordId":"744459160027659264"} +pub fn add_dns_txt(client: &RPClient, domain: &str) -> XResult { + Ok(client.get("AddDomainRecord") + .query(&[ + ("RegionId", "cn-hangzhou"), + ("DomainName", domain), + ("RR", "_acme-challenge_test"), + ("Type", "TXT"), + ("Value", "test") + ]) + .send()?) +} + +fn create_dns_client(access_credential: &AccessCredential) -> RPClient { + RPClient::new( + access_credential.access_key_id.clone(), + access_credential.access_key_secret.clone(), + String::from(ALI_DNS_ENDPOINT), + String::from(ALI_DNS_API_VERSION), + ) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7f0441c..ddfebcb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod x509; mod network; mod statics; mod dingtalk; +mod ali_dns; // mod simple_thread_pool; use std::env;