RouterOS DNSPod

宁波的树莓派宕机,自动更新宽带IP脚本只能放到路由器上。通过IPv6地址找回了路由器,并添加了在DNSPOD上的自动更新。

PHP文件需要放在一个自己的服务器,网络上很多提供更新的直接引用的PHP文件并不安全,会记录你的Token信息。

以下是代码:

RouterOS script:

:global recordid “xxxxxx”
:global subdomain “xxxxxx”
:global xx “https://example.com/update_dns/dnspod.php?record_id=$recordid&sub_domain=$subdomain”

/tool fetch url=(“$xx”) mode=https keep-result=yes dst-path=ddns.txt

PHP file:

<?php

$realip = getIP();
$code = “00000”;
$token = “000000000000000000000000”;
$domain_id = “00000”;
$record_id = $_GET[‘record_id’];
$sub_domain = $_GET[‘sub_domain’];
$value = $_GET[‘value’];
$record_type = $_GET[‘record_type’];
$record_line = $_GET[‘record_line’];

if (empty($code)) { exit(‘code empty.’); }
if (empty($token)) { exit(‘token empty.’); }
if (empty($domain_id)) { exit(‘domain_id empty.’); }
if (empty($record_id)) { exit(‘record_id empty.’); }
if (empty($sub_domain)) { $sub_domain = “routeos”; }
if (empty($record_type)) { $record_type = “A”; }
if (empty($record_line)) { $record_line = “默认”; }
if (empty($value)) { $value = $realip; }
$ip = checkIP($code, $token, $domain_id, $record_id);
if (empty($ip)) { exit(‘ip empty.’); }
if ($ip == $value) { exit(“IP一致.”); }

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://dnsapi.cn/Record.Modify”);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, “login_token=” . $code . “,” . $token . “&format=json&domain_id=” . $domain_id . “&record_id=” . $record_id . “&sub_domain=” . $sub_domain . “&value=” . $value . “&record_type=” . $record_type . “&record_line=” . “$record_line”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
exit(‘ok’);

function checkIP($code, $token, $domain_id, $record_id ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://dnsapi.cn/Record.Info”);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, “login_token=” . $code . “,” . $token . “&format=json&domain_id=” . $domain_id . “&record_id=” . $record_id . “”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
$json = json_decode($server_output, TRUE);
return $json[‘record’][‘value’];
}

function getIP(){
if (@$_SERVER[“HTTP_X_FORWARDED_FOR”])
$ip = $_SERVER[“HTTP_X_FORWARDED_FOR”];
else if (@$_SERVER[“HTTP_CLIENT_IP”])
$ip = $_SERVER[“HTTP_CLIENT_IP”];
else if (@$_SERVER[“REMOTE_ADDR”])
$ip = $_SERVER[“REMOTE_ADDR”];
else if (@getenv(“HTTP_X_FORWARDED_FOR”))
$ip = getenv(“HTTP_X_FORWARDED_FOR”);
else if (@getenv(“HTTP_CLIENT_IP”))
$ip = getenv(“HTTP_CLIENT_IP”);
else if (@getenv(“REMOTE_ADDR”))
$ip = getenv(“REMOTE_ADDR”);
else
$ip = “Unknown”;
return $ip;
}

参考来源:https://www.dwhd.org/20170428_192152.html

0
0

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*