-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalydns.php
174 lines (136 loc) · 4.43 KB
/
alydns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
date_default_timezone_set("GMT");
//这两个值需要去阿里云申请
define("accessKeyId", "");
define("accessSecrec", "");
/*
//$obj = new AliDns(accessKeyId, accessSecrec, "newyingyong.cn");
//显示所有
//$data = $obj->DescribeDomainRecords();
//增加解析
//$data= $obj->AddDomainRecord("TXT", "test", "test");
//修改解析
//$data = $obj->UpdateDomainRecord("3965724468724736","TXT", "test", "test2");
//删除解析
//$data = $obj->DescribeDomainRecords();
//$data = $data["DomainRecords"]["Record"];
//if (is_array($data)) {
//foreach ($data as $v) {
//if ($v["RR"] == "test") {
//$obj->DeleteDomainRecord($v["RecordId"]);
//}
//}
//}
*/
/*
example:
php alydns.php "newyingyong.cn" "test" "test2"
*/
########## 配合 cerbot 运行
echo $argv[1] . "-" . $argv[2] . "-" . $argv[3];
$obj = new AliDns(accessKeyId, accessSecrec, $argv[1]);
$data = $obj->DescribeDomainRecords();
$data = $data["DomainRecords"]["Record"];
if (is_array($data)) {
foreach ($data as $v) {
if ($v["RR"] == $argv[2]) {
$obj->DeleteDomainRecord($v["RecordId"]);
}
}
}
print_r($obj->AddDomainRecord("TXT", $argv[2],$argv[3]));
############ Class 定义
class AliDns {
private $accessKeyId = null;
private $accessSecrec = null;
private $DomainName = null;
public function __construct($accessKeyId, $accessSecrec, $domain) {
$this->accessKeyId = $accessKeyId;
$this->accessSecrec = $accessSecrec;
$this->DomainName = $domain;
}
public function DescribeDomainRecords() {
$requestParams = array(
"Action" => "DescribeDomainRecords"
);
$val = $this->send($requestParams);
return $this->out($val);
}
public function UpdateDomainRecord($id, $type, $rr,$value){
$requestParams = array(
"Action" => "UpdateDomainRecord",
"RecordId" => $id,
"RR" => $rr,
"Type" => $type,
"Value" => $value,
);
$val = $this->send($requestParams);
return $this->out($val);
}
public function DeleteDomainRecord($id) {
$requestParams = array(
"Action" => "DeleteDomainRecord",
"RecordId" => $id,
);
$val = $this->send($requestParams);
return $this->out($val);
}
public function AddDomainRecord($type, $rr, $value) {
$requestParams = array(
"Action" => "AddDomainRecord",
"RR" => $rr,
"Type" => $type,
"Value" => $value,
);
$val = $this->send($requestParams);
return $this->out($val);
}
private function send($requestParams) {
$publicParams = array(
"DomainName" => $this->DomainName,
"Format" => "JSON",
"Version" => "2015-01-09",
"AccessKeyId" => $this->accessKeyId,
"Timestamp" => date("Y-m-d\TH:i:s\Z"),
"SignatureMethod" => "HMAC-SHA1",
"SignatureVersion" => "1.0",
"SignatureNonce" => substr(md5(rand(1, 99999999)), rand(1, 9), 14),
);
$params = array_merge($publicParams, $requestParams);
$params['Signature'] = $this->sign($params, $this->accessSecrec);
$uri = http_build_query($params);
$url = 'http://alidns.aliyuncs.com/?'.$uri;
return $this->curl($url);
}
private function sign($params, $accessSecrec, $method = "GET") {
ksort($params);
$stringToSign = strtoupper($method).'&'.$this->percentEncode('/').'&';
$tmp = "";
foreach($params as $key => $val){
$tmp .= '&'.$this->percentEncode($key).'='.$this->percentEncode($val);
}
$tmp = trim($tmp, '&');
$stringToSign = $stringToSign.$this->percentEncode($tmp);
$key = $accessSecrec.'&';
$hmac = hash_hmac("sha1", $stringToSign, $key, true);
return base64_encode($hmac);
}
private function percentEncode($value = null){
$en = urlencode($value);
$en = str_replace("+", "%20", $en);
$en = str_replace("*", "%2A", $en);
$en = str_replace("%7E", "~", $en);
return $en;
}
private function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec ($ch);
curl_close($ch);
return $result;
}
private function out($msg) {
return json_decode($msg, true);
}
}