/
pip.php
434 lines (341 loc) · 11.8 KB
/
pip.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/php
<?php
//----------------------------------------------------------------------------------------------------------------------
// Dynamic DNS with GoDaddy
// Copyright (c)2018 YO3HCV, Edouard Gora
//
// License: WYL
// It means that you can do Whatever You Like with this script.
// You can use as your school homework, tell your boss how smart you are and how hard you work for it, I don't care.
// However, I am not responsible in any way for any stupid things you may do with it.
// If you like it and find it useful, please consider to donate in my PayPal account.
//
// Make this script executable
// chmod +x pip.php
//
// CRONTAB
// -------------------------------------------------------------------------
// crontab -e edit
// crontab -l view
//
//
// +---------------- minute (0 - 59)
// | +------------- hour (0 - 23)
// | | +---------- day of month (1 - 31)
// | | | +------- month (1 - 12)
// | | | | +---- day of week (0 - 6) (Sunday=0 or 7)
// | | | | |
// * * * * * command to be executed
//
// Every 10 minutes
// * /10 * * * * /root/pip.php <-- no space between * and /
//
// Every minute
// * * * * * /root/pip.php
//
// View executed crons
// Apr 7 12:05:01 CRON[26475]: (root) CMD (/root/pip.php)
// Apr 7 12:06:01 CRON[26564]: (root) CMD (/root/pip.php)
//
// ~# grep -i CRON /var/log/syslog
//
// Continuously monitor for debug
//
// tail -f /var/log/syslog | grep -i CRON
//----------------------------------------------------------------------------------------------------------------------
require "/home/PHPMailer/PHPMailerAutoload.php"; // where is PHPMailer
$reverse = true; // use reverse DNS lookup (better)
$mail = new PHPMailer;
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "my_nice_email@gmail.com";
$mail->Password = "my_nice_password";
$mail->SMTPSecure = "tls"; // Enable TLS encryption, SSL is also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom("my_nice_email@gmail.com", "My Server"); // FROM
// TO (copy paste this line for multiple destinations)
$mail->addAddress("my_email_for_receiving_alerts@gmail.com");
$servers = array // ,
(
"http://checkip.dyndns.org", // REMOTE_X_FORWARDED_FOR
"http://checkip.feste-ip.net", // REMOTE_ADDR
"http://www.icanhazip.com", // REMOTE_ADDR
"http://checkip.amazonaws.com", // REMOTE_X_FORWARDED_FOR
"https://ipinfo.info", // unknown
//"https://ipinfo.io/json", // unknown, JSON format
);
// GoDaddy settings
// Create a PRODUCTION KEY here
// https://developer.godaddy.com/keys
//
// Verify DNS records like this
// https://api.godaddy.com/v1/domains/<DOMAIN>/records/A/<RECORD_NAME>
//
// Issues:
// Call to undefined function curl_init()
// sudo apt-get install php-curl
$url = "https://api.godaddy.com/v1/domains/";
$domain="my_nice_domain.com"; // your domain name
$key="fddshsdkjhsKGcksauydgsadgfkcbvsadf";
$secret="lsakfdjgiuhdfgdshb";
//----------------------------------------------------------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------------------------------------------------------
function getDNSRecord($type, $name, &$ret){
global $url, $domain, $key, $secret;
$headers = array(
"Content-Type: application/json",
"Authorization: sso-key $key:$secret"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url."$domain/records/$type/$name");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$ret = curl_exec($ch);
//var_dump($ret);
if (!curl_errno($ch))
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $http_code;
}
function updateDNSRecord($type, $name, $data, $ttl, &$ret, $add = false){
global $url, $domain, $key, $secret;
$headers = array(
"Content-Type: application/json",
"Authorization: sso-key $key:$secret"
);
$ch = curl_init();
if($add)
{
$data = '[{"type":"'.$type.'","name":"'.$name.'","data":"'.$data.'","ttl":'.$ttl.'}]';
curl_setopt($ch, CURLOPT_URL, $url."$domain/records");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); // add
}
else
{
$data = '[{"data":"'.$data.'","ttl":'.$ttl.'}]';
curl_setopt($ch, CURLOPT_URL, $url."$domain/records/$type/$name");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // update existing
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$ret = curl_exec($ch);
if (!curl_errno($ch))
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $http_code;
}
function SendMail($subject, $body)
{
global $mail;
/* Send email */
$mail->isHTML(false);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send())
echo "Message could not be sent.\r\nMailer Error: " . $mail->ErrorInfo ."\r\n";
else
echo "Message has been sent\r\n";
}
//----------------------------------------------------------------------------------------------------------------------
// Verify with all servers
//----------------------------------------------------------------------------------------------------------------------
$dom = new domDocument;
libxml_use_internal_errors(true); // suppress some warnings
$content = "\r\nChecking server IP at ". date("d-m-Y H:i:s"). " \r\n\r\nTrying all servers "
.($reverse? "with reverse DNS...":"...")."\r\n\r\n";
for($i=0; $i < sizeof($servers); $i++)
{
$ip = "";
switch($i)
{
// html style 1 :)
case 0:
case 1:
@$dom->loadhtmlfile($servers[$i]);
$obj = $dom->getElementsByTagName("body");
if($obj->item(0))
{
$ip = $obj->item(0)->nodeValue;
$ip = trim(str_replace("Current IP Address:","",$ip));
}
break;
// text
case 2:
case 3:
@$ip = trim(file_get_contents($servers[$i]));
break;
//https://ipinfo.info/
case 4:
@$dom->loadhtmlfile($servers[$i]);
$objs = $dom->getElementsByTagName("a");
$query = "http://www.dnsstuff.com/tools/whois.ch?ip=";
foreach($objs as $obj)
{
$attr = $obj->getAttribute("href");
if(substr($attr, 0, strlen($query)) === $query) // attr begins with query
{
@$ip = trim(str_replace($query,"",$attr));
break;
}
}
break;
}// sw
$content .= "$servers[$i] ";
//if($i==3) $ip = "gdsfgdsfgds"; // debug
//$ip = "gdsfgdsfgds"; // debug
if (filter_var($ip, FILTER_VALIDATE_IP)) // valid IP ?
{
//if($i==2) $ip = "1.2.3.4"; // debug
//if($i==3) $ip = "1.2.3.4"; // debug
//if($i==4) $ip = "1.2.3.4"; // debug
if($reverse)
{
// let'd do a reverse DNS also, our provider format is XXX-XXX-XXX-XXX.home.provider
$xip = str_replace(".","-",$ip);
$local_domain = gethostbyaddr($ip); // do a reverse lookup
if(strpos($local_domain, $xip) !== false) // if our IP is included in local domain string
{
$count = isset($data[$ip])? $data[$ip] : 0;
$data[$ip] = ++$count;
$content .= $ip;
}
else
$content .= "-> $ip (invalid reverse DNS)";
}
else
{
$count = isset($data[$ip])? $data[$ip] : 0;
$data[$ip] = ++$count;
$content .= $ip;
}
}
else
$content .= "-> n/a";
$content .= "\r\n";
}
$content .= "\r\n";
//----------------------------------------------------------------------------------------------------------------------
// Do we have consistent results?
//----------------------------------------------------------------------------------------------------------------------
if(!isset($data))
die($content ."Ooopsss... do we have internet conection?\r\n");
arsort($data); // sort by value, descending
//print_r($data);
//echo "Count: ".sizeof($data).PHP_EOL;
foreach ($data as $ip => $count)
{
$score = round($count/count($servers),1)*100;
if($score == 100)
{
$content .= "Nice";
break;
}
else if($score >= 0.8) //
{
$content .= "Acceptable";
break;
}
else if($score >= 0.6) //
{
$content .= "Possible";
break;
}
else if($score >= 0.4) //
{
$content .= "Tiny";
break;
}
else //
{
$content .= "Not sure";
break;
}
}
$content .= ", $score% identified, IP: $ip (http://$ip)".($reverse?"\r\nReverse DNS: $local_domain":"")."\r\n\r\n";
//----------------------------------------------------------------------------------------------------------------------
// Check against remote server
//----------------------------------------------------------------------------------------------------------------------
$ret='';
$http_code = getDNSRecord("A","@",$ret); // check A@ record at GoDaddy
//$http_code = 400;
if($http_code != 200) // API error
{
$content .= "Cannot retrieve $domain IP from GoDaddy.\r\nhttps://account.godaddy.com\r\n\r\n";
$content .= "Response: $http_code\r\n".$ret."\r\n";
$subject = "IP status check report";
}
else if($http_code == 200)
{
//print_r($ret);
$json = str_replace("[","",$ret); // ugly...
$json = str_replace("]","",$json);
//print_r($json);
$json = json_decode($json,true);
print_r($json);
if(!(isset($json["data"]) && filter_var($json["data"], FILTER_VALIDATE_IP)))
{
// we have 200 but empty or invalid IP
$content .= "The domain $domain record A/@ is invalid.\r\nTrying to fix this with $ip... ";
$ret = '';
$http_code = updateDNSRecord("A","@",$ip, 600, $ret, true);
//$http_code = 400;
if($http_code != 200)
{
$content .= "Failed, please try manually.\r\nhttps://account.godaddy.com\r\n\r\n";
$content .= "Response: $http_code\r\n".$ret."\r\n";
$subject = "The domain $domain has invalid A/@ records!";
}
else
{
$content .= "OK\r\n\r\nThe domain $domain IP was changed to $ip";
$subject = "The domain $domain IP was changed to $ip";
}
}
else // valid JSON, valid IP
{
if($json["data"] == $ip) // already the same IP
{
$content .= "The domain $domain IP matches local sever IP.\r\n\r\n";
echo $content ."\r\n";
die();
}
else
{
// Different IP, Do something for God say!
$content .= "The domain $domain IP ".$json["data"]." is different than local IP $ip.\r\n\r\n";
if($score >= 0.4)
{
$ret = '';
$http_code = updateDNSRecord("A","@",$ip, 600, $ret, false);
if($http_code != 200)
{
$content .= "Failed to update $domain IP, please try manually.\r\nhttps://account.godaddy.com\r\n\r\n";
$content .= $ret."\r\n";
$subject = "Failed to update $domain IP!";
}
else
{
$content .= "The domain $domain IP was changed to $ip";
$subject = "The domain $domain IP was changed to $ip";
}
}
else
{
$content .= "The score is too low for automatic update, please try manually.\r\nhttps://account.godaddy.com\r\n";
$subject = "The domain $domain IP require update!";
}
}
} // valid JSON, valid IP
}// 200
echo $content ."\r\n";
SendMail($subject,$content);
?>