-
Notifications
You must be signed in to change notification settings - Fork 38
/
cloudflare.php
158 lines (134 loc) · 3.38 KB
/
cloudflare.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
#!/usr/bin/php -d open_basedir=/usr/syno/bin/ddns
<?php
if ($argc !== 5) {
echo 'badparam';
exit();
}
$account = (string)$argv[1];
$pwd = (string)$argv[2];
$hostname = (string)$argv[3];
$fullname = (string)$argv[3];
$ip = (string)$argv[4];
// check the hostname contains '.'
if (strpos($hostname, '.') === false) {
echo "badparam";
exit();
}
if(strlen($pwd) == 37) /* Global key 37byte*/
{
$header = array("X-Auth-Email: ${account}", "X-Auth-Key: ${pwd}", "Content-Type: application/json");
}
else /* API Token 40byte*/
{
$header = array("Authorization: Bearer ${pwd}", "Content-Type: application/json");
}
// only for IPv4 format
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
echo "badparam";
exit();
}
/*
1. Check Validity && Query Zone ID
*/
$options = array(
CURLOPT_URL => "https://api.cloudflare.com/client/v4/zones",
CURLOPT_HTTPGET => true,
CURLOPT_HEADER => false,
CURLOPT_VERBOSE => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header
);
if (empty($data = exec_curl($options))) {
echo 'badauth';
exit();
}
$zone_id = -1;
$result = array_filter(array_get($data, 'result', []), function($row) use ($hostname) {
return preg_match('/\.'.$row['name'].'$/i', $hostname) > 0 || strtolower($row['name']) === strtolower($hostname);
});
if (empty($zone_info = array_pop($result))) {
echo 'nohost';
exit();
}
$zone_id = $zone_info['id'];
/*
2. Query Record ID
*/
$options = array(
CURLOPT_URL => "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records?type=A&name=${fullname}",
CURLOPT_HTTPGET => true,
CURLOPT_HEADER => false,
CURLOPT_VERBOSE => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header
);
if (empty($data = exec_curl($options))) {
echo 'badauth';
exit();
}
$result = array_filter(array_get($data, 'result', []), function($row) use ($hostname) {
return $row['name'] === $hostname;
});
if(empty($record_info = array_pop($result))) {
echo 'nohost';
exit();
}
$record_id = $record_info['id'];
$ttl = $record_info['ttl'];
$proxied = $record_info['proxied'];
/*
3. Update DNS
*/
$options = array(
CURLOPT_URL => "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${record_id}",
CURLOPT_POST => true,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HEADER => false,
CURLOPT_VERBOSE => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => json_encode(array(
'type'=>'A',
'name'=>$fullname,
'content'=>$ip,
'ttl'=>$ttl,
'proxied'=>$proxied
))
);
if (empty($data = exec_curl($options))) {
echo 'Update Record failed';
exit();
}
echo 'good';
/*
* Helpers
*/
/**
* Get array member by key
* @param $array
* @param $key
* @param $default
* @return mixed|null
*/
function array_get(&$array, $key, $default = null) {
return isset($array[$key]) ? $array[$key] : $default;
}
/**
* execute curl and parse return data
* @param $options
* @return mixed|null
*/
function exec_curl($options) {
$req = curl_init();
curl_setopt_array($req, $options);
$res = curl_exec($req);
curl_close($req);
$result = json_decode($res, true);
// echo "\n\n";
// echo var_export($result, true);
// echo "\n\n";
if (array_get($result, 'success', false)) {
return $result;
}
return null;
}