forked from mrikirill/SynologyDDNSCloudflareMultidomain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare.php
202 lines (176 loc) · 5.56 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/php -d open_basedir=/usr/syno/bin/ddns
<?php
if ($argc !== 5) {
echo 'badparam';
exit();
}
$cf = new updateCFDDNS($argv);
$cf->makeUpdateDNS();
/**
* DDNS auto updater for Synology NAS
* Base on Cloudflare API v4
* Supports multidomains and sundomains
*/
class updateCFDDNS
{
const API_URL = 'https://api.cloudflare.com';
var $account, $apiKey, $hostList, $ip;
function __construct($argv)
{
if (count($argv) != 5) {
$this->badParam();
}
$this->account = (string) $argv[1];
$this->apiKey = (string) $argv[2]; // CF Global API Key
$hostname = (string) $argv[3]; // example: example.com.uk---sundomain.example1.com---example2.com
$this->ip = (string) $argv[4];
$this->validateIpV4($this->ip);
$arHost = explode('---', $hostname);
if (empty($arHost)) {
$this->badParam('empty host list');
}
foreach ($arHost as $value) {
$this->hostList[$value] = [
'hostname' => '',
'fullname' => $value,
'zoneId' => '',
'recordId' => '',
'proxied' => true,
];
}
$this->setZones();
foreach ($this->hostList as $arHost) {
$this->setRecord($arHost['fullname'], $arHost['zoneId']);
}
}
/**
* Update CF DNS records
*/
function makeUpdateDNS()
{
if(empty($this->hostList)) {
$this->badParam();
}
foreach ($this->hostList as $arHost) {
$post = [
'type' => 'A',
'name' => $arHost['fullname'],
'content' => $this->ip,
'ttl' => 1,
'proxied' => $arHost['proxied'],
];
$json = $this->callCFapi("PUT", "client/v4/zones/" . $arHost['zoneId'] . "/dns_records/" . $arHost['recordId'], $post);
if (!$json['success']) {
echo 'Update Record failed';
exit();
}
}
echo "good";
}
function badParam($msg = '')
{
echo (strlen($msg) > 0) ? $msg : 'badparam';
exit();
}
function validateIpV4($ip)
{
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$this->badParam('invalid ip-address, only ipv4');
}
return true;
}
/**
* Set ZoneID for each hosts
*/
function setZones()
{
$json = $this->callCFapi("GET", "client/v4/zones");
if (!$json['success']) {
$this->badParam('getZone unsuccessful response');
}
$arZones = [];
foreach ($json['result'] as $ar) {
$arZones[] = [
'hostname' => $ar['name'],
'zoneId' => $ar['id']
];
}
foreach ($this->hostList as $hostname => $arHost) {
$res = $this->isZonesContainFullname($arZones, $arHost['fullname']);
if(!empty($res)) {
$this->hostList[$hostname]['zoneId'] = $res['zoneId'];
$this->hostList[$hostname]['hostname'] = $res['hostname'];
}
}
}
/**
* Find hostname for full domain name
* example: domain.com.uk --> vpn.domain.com.uk
*/
function isZonesContainFullname($arZones, $fullname){
$res = [];
foreach($arZones as $arZone) {
if (strpos($fullname, $arZone['hostname']) !== false) {
$res = $arZone;
break;
}
}
return $res;
}
/**
* Set Records for each hosts
*/
function setRecord($fullname, $zoneId)
{
if (empty($fullname)) {
return false;
}
if (empty($zoneId)) {
unset($this->hostList[$fullname]);
return false;
}
$json = $this->callCFapi("GET", "client/v4/zones/${zoneId}/dns_records?type=A&name=${fullname}");
if (!$json['success']) {
$this->badParam('unsuccessful response for getRecord host: ' . $fullname);
}
$this->hostList[$fullname]['recordId'] = $json['result']['0']['id'];
$this->hostList[$fullname]['proxied'] = $json['result']['0']['proxied'];
}
/**
* Call CloudFlare v4 API @link https://api.cloudflare.com/#getting-started-endpoints
*/
function callCFapi($method, $path, $data = []) {
$options = [
CURLOPT_URL => self::API_URL . '/' . $path,
CURLOPT_HTTPHEADER => ["X-Auth-Email: $this->account", "X-Auth-Key: $this->apiKey", "Content-Type: application/json"],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_VERBOSE => false,
];
if(empty($method)){
$this->badParam('Empty method');
}
switch($method) {
case "GET":
$options[CURLOPT_HTTPGET] = true;
break;
case "POST":
$options[CURLOPT_POST] = true;
$options[CURLOPT_HTTPGET] = false;
$options[CURLOPT_POSTFIELDS] = json_encode($data);
break;
case "PUT":
$options[CURLOPT_POST] = false;
$options[CURLOPT_HTTPGET] = false;
$options[CURLOPT_CUSTOMREQUEST] = "PUT";
$options[CURLOPT_POSTFIELDS] = json_encode($data);
break;
}
$req = curl_init();
curl_setopt_array($req, $options);
$res = curl_exec($req);
curl_close($req);
return json_decode($res, true);
}
}
?>