<?php
function main()
{
// 参数配置
token = "修改成自己的 dnspod token";domain = ""; // 假如要解析 test.domain.com,这里就是 domain.com
recordName = ""; // 假如要解析 test.domain.com, testdir = "/home/script"; // 脚本文件所在目录
filename = "ddnspod-" .recordName . "." . domain;
// 修改执行目录
chdir(dir);
print_r("脚本文件名:" . _SERVER['SCRIPT_NAME'] . "\n");
print_r("执行目录:" . getcwd() . "\n");log["beforeGetIp"] = date("Y-m-d h:i:sa");
// 获取公网ip
ips = get_server_ip();log["ip"] = ips;log["beforeReqestDdns"] = date("Y-m-d h:i:sa");
// 设置 ipv4 ddns
type = "A";log[type] = requestDdns(domain, token,recordName, ips[4]);
print_r("\n");
print_r(log[type]);log["beforeUpdateRecord"] = date("Y-m-d h:i:sa");
// 更新 ipv6 AAAA
type = "AAAA";log[type] = updateRecord(domain, token,recordName, type,ips[6]);
print_r("\n");
print_r(log[type]);
if (log['AAAA'] ==log['A'] && log['A'] == '无需更新') {
logg("无需更新 - " .ips[0], filename, "log");
} else {
logg(log, filename, "log");
}
}
function logg(data, name,flag)
{
data = json_encode(data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
text = '[' . date('Y-m-d H:i:s') . "] --- " .flag . " " . data . "\n" .
"----------------------------------------------------------------\n";
file_put_contents('log/' .name . '_' . date('ym') . '.log', text, FILE_APPEND);
}
function send_post(url, post_data,method = 'POST')
{
postdata = http_build_query(post_data);
options = array(
'http' => array(
'method' =>method,
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);context = stream_context_create(options);result = file_get_contents(url, false,context);
return result;
}
function getDomainId(domain, token)
{postData = array('login_token' => token);url = "https://dnsapi.cn/Domain.List";
rs = send_post(url, postData);resultArray = json_decode(rs, true);
// print_r(resultArray);
foreach (resultArray['domains'] asone) {
if (isset(one['punycode']) &&one['punycode'] == domain) {
returnone['id'];
}
}
return false;
}
function getRecordId(domainId,recordName, token,type)
{
url = "https://dnsapi.cn/Record.List";postData = array(
'login_token' => token,
'domain_id' =>domainId
);
rs = send_post(url, postData);resultArray = json_decode(rs, true);
foreach (resultArray['records'] as one) {
if (one['name'] == recordName &&one['type'] == type) {
returnone;
}
}
return false;
}
function requestDdns(domain,token, recordName,value)
{
filename = "ddnspod-" .recordName . "." . domain;type = "A";
// ddns 只有 ipv4
domainId = getDomainId(domain, token);record = getRecordId(domainId,recordName, token,type);
if (domainId === false ||record === false) {
logg("发生错误!", filename,type);
}
if (record['value'] ==value) {
return "无需更新";
}
data['login_token'] =token;
data['format'] = "json";data['domain_id'] = domainId;data['record_id'] = record['id'];data['record_line'] = "默认";
data['sub_domain'] =recordName;
url = "https://dnsapi.cn/Record.Ddns";rs = send_post(url,data);
rusultArray = json_decode(rs, true);
return rusultArray;
}
function updateRecord(domain, token,recordName, type,value)
{
if (type == "AAAA") {
if (!filter_var(value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return false;
}
}
if (type == "A") {
if (filter_var(value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return false;
}
}
filename = "ddnspod-" .recordName . "." . domain;domainId = getDomainId(domain,token);
record = getRecordId(domainId, recordName,token, type);
if (domainId === false || record === false) {
logg("发生错误!",filename, type);
}
if (record['value'] == value) {
return "无需更新";
}data['login_token'] = token;data['format'] = "json";
data['domain_id'] =domainId;
data['record_id'] =record['id'];
data['sub_domain'] =recordName;
data['value'] =value;
data['record_type'] =type;
data['record_line'] = "默认";
print_r(data);
url = "https://dnsapi.cn/Record.Modify";rs = send_post(url,data);
rusultArray = json_decode(rs, true);
return rusultArray;
}
function get_server_ip()
{ip4 = send_post("http://myip.ipip.net/json", array(), 'GET');
ip4 = json_decode(ip4, true);
if (ip4['ret'] == 'ok' && filter_var(ip4['data']['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
ip['4'] =ip4['data']['ip'];
} else {
ip['4'] = false;
}ip6 = send_post(
"http://ipv6.lookup.test-ipv6.com/ip/?"
. "asn=1&testdomain=test-ipv6.com&testname=test_asn6",
array(),
'GET'
);
ip6 = json_decode(ip6, true);
if (isset(ip6['ip']) && filter_var(ip6['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
ip[6] =ip6['ip'];
} else {
ip['6'] = false;
}
// ubuntu 20.04order = "ip -o -6 addr list "
. "| grep global "
. "| grep -Ev '\s(docker|lo)' "
. "| awk '{print 4}' "
. "| cut -d/ -f1";
//order = "ip -o -6 addr list "
// ."| grep -Ev '\s(docker|lo)' "
// ."| awk '{print 4}' "
// ."| cut -d/ -f1";
//order = "ifconfig "
// . "| grep \"inet \" "
// . "| grep -v 127.0.0.1 "
// . "| awk '{print 2}'";
//order = "ifconfig "
// . "| grep \"inet6 \" "
// . "| grep -v 127.0.0.1 "
// . "| grep global "
// . "| awk '{print 2}'";
print_r("\n");
print_r("check" . date("Y-m-d h:i:sa"));
print_r("\n");
exec(order, out6,stats);
if (count(out6)>3) {
print_r(out6);
ip[0] = "更新 ipv6";
print_r("更新 ipv6 ! \n");
exec("sudo netplan apply",outTemp, statsTemp);
sleep(10);out6 = array();
exec(order,out6, stats);
}
if (filter_var(out6[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
ip[66] =out6[0];
} else {
ip['66'] = false;
}
print_r(ip);
return $ip;
}
main();
文章评论