最近拿到了自家宽带光猫的超管权限,通过光猫、小米路由器,把20~10000这个范围内的端口全映射到了NAS上面;简单测试一下,除了80、443端口不能用以外,其它均正常。
目前我还没搞清楚这个光猫换公网IP的时间机制,于是乎我写个简单的PHP脚本,配合DNSpod,实现域名动态解析,方便我不在家时远程控制。
DNSpod 免费版TTL最短时间是10分钟,配合宝塔的计划任务,挂个间隔10分钟的任务刚刚好。
效果图
计划任务
把脚本放到合适的路径,添加一个10分钟的计划任务
/usr/bin/php /www/wwwroot/dnspod/main.php
源代码
<?php
// Dnspod token,获取地址 https://console.dnspod.cn/account/token/token
$token = '235678,f485d8729df9397c719e0fe8f5802xxx';
// 需要解析的域名
$domain = 'xxx.com';
// 解析记录
$record = 'panel.nas';
$api = 'https://dnsapi.cn/Record.List';
$post = [
'login_token' => $token,
'format' => 'json',
'lang' => 'cn',
'domain' => domain,
];
$record_id = null;
$res = getCurl($api, ['post' => $post]);
$data = json_decode($res, true);
// 获取记录ID
foreach ($data['records'] as $itm) {
if ($record == $itm['name']) {
$record_id = $itm['id'];
}
}
if (empty($record_id)) {
exit("记录 [$record] 未找到,请检查!\n");
}
// 获取公网IP
$ip = file_get_contents('https://www.bt.cn/Api/getIpAddress');
if (empty($ip)) {
exit("公网IP获取失败 \n");
}
echo "当前公网IP:$ip \n";
// 修改域名A记录
$api = 'https://dnsapi.cn/Record.Modify';
$post = [
'login_token' => $token,
'format' => 'json',
'lang' => 'cn',
'domain' => $domain,
'record_id' => $record_id,
'sub_domain' => $record,
'record_type' => 'A',
'record_line' => '默认',
'value' => $ip,
'mx' => 20,
];
$res = getCurl($api, ['post' => $post]);
$data = json_decode($res, true);
if ($data['status']['code']) {
echo "修改成功:{$data['status']['message']} \n";
}
function getCurl($url, $opt = [])
{
$cookie = '';
if (is_array($opt['cookie'])) {
foreach ($opt['cookie'] as $k => $v) {
$cookie .= $k . '=' . $v . '; ';
}
}
$cookie = (mb_substr($cookie, 0, mb_strlen($cookie) - 2));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Expect:"]);
curl_setopt($ch, CURLOPT_NOBODY, $opt['nobody']);
curl_setopt($ch, CURLOPT_HEADER, $opt['header'] ?? false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $opt['headers'] ?? []);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $opt['rtime'] ?? 10000);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $opt['ctime'] ?? 10000);
curl_setopt($ch, CURLOPT_REFERER, $opt['refer'] ?? 'https://user.qzone.qq.com/');
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36');
if (isset($opt['post'])) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($opt['post']) ? http_build_query($opt['post']) : $opt['post']);
}
if (isset($opt['proxy']) && is_array($opt['proxy'])) {
curl_setopt($ch, CURLOPT_PROXY, $opt['proxy']['ip']);
curl_setopt($ch, CURLOPT_PROXYPORT, $opt['proxy']['port']);
}
$res = curl_exec($ch);
$error = curl_error($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($opt['detail']) {
return ['code' => $code, 'error' => $error, 'resp' => $res,];
}
return $res;
}