
就是隧道里保持相互通信的办法。
我现在研究到的就是 shell 定时执行,然后 lua 接收然后修改 hosts 文件,但是有些坑。
不知道是我固件问还是怎么,没法刷新 DNS,重启 dnsmasq 都没用。
根据知乎的一个问题回答为基础实现 lua 接收 Post 。
之前完全没接触过 lua,看着手册和 Google 弄了下面的代码。
##接收端
/www/cgi-bin/edithosts
#!/usr/bin/lua local edithosts= require 'edithosts' edithosts.Run() /usr/lib/lua/edithosts
local edithosts= {} function edithosts.Run() local client = os.getenv("REMOTE_ADDR") local GET = os.getenv("QUERY_STRING") local POST = nil local Domain = nil local ifedit = "0" local hostsFile = "/www/edithosts" local hostsLog = "/www/edithosts.log" local POSTLength = tonumber(os.getenv("CONTENT_LENGTH")) or 0 if (POSTLength > 0) then POST = io.read(POSTLength) Domain = string.sub(POST, 3) end if (Domain ~= nil) then -- 修改 Hosts local hostsMain = string.format("%s %s", client or '-', Domain or '-') local hostsR = io.open(hostsFile ,"a+") local hostsOld = hostsR:read() hostsR:close() ifedit = "2" if (hostsMain ~= hostsOld) then -- 如果内容不一样则修改 hosts 文件 local hostsIO = io.open(hostsFile, "w") hostsIO:write(hostsMain) hostsIO:close() ifedit = "1" -- 写入日志 local logIO = io.open(hostsLog, "a") local nowTime = os.date("%Y-%m-%d %H:%M:%S") local logMain = string.format("%s - %s - %s\n", client or '-', Domain or '-', nowTime or '-') logIO:write(logMain) logIO:close() -- 重启 dnsmasq local redns = os.execute("/etc/init.d/dnsmasq restart") end end io.write("Content-type: text/html\nPragma: no-cache\n\n") io.write(ifedit) end return edithosts ##发送请求
*/5 * * * * /usr/bin/postroom.sh br-wan 是网口
/www/oldip 文件要预先创建
1.room 和 2.room 是自定义的域名
#!/bin/bash interface=br-wan NEW_IP=$(ip a show dev $interface |grep -oP "inet [0-9]+.[0-9]+.[0-9]+.[0-9]+" | sed 's/inet //g') NOWTIME=`date` IP_FILE='/www/oldip' CURRENT_IP=`cat /www/oldip` LOG_FILE="/www/post.log" if [ ${NEW_IP} == ${CURRENT_IP} ] || [ ! ${NEW_IP} ]; then echo "" else THIS_LOG=`curl -X POST --data 'd=1.room' 2.room:7080/cgi-bin/edithosts` echo ${NOWTIME} >> ${LOG_FILE} if [ ${THIS_LOG} ]; then echo "TR069 OK" ${NEW_IP} >> ${LOG_FILE} echo "" >> ${LOG_FILE} echo ${NEW_IP} > ${IP_FILE} else echo "Error " ${THIS_LOG} >> ${LOG_FILE} fi fi 该如何完善这个功能呢?
知道为什么有个路由的本地 hosts 文件无效了,因为我勾选了 DHCP/DNS 中的忽略解析文件,这个不能勾! 
1 LGA1150 2020-10-09 15:00:50 +08:00 接口上线时有 hotplug 事件触发,无需定时轮询 hotplug 脚本在 /etc/hotplug.d/iface/ 再通过 SSH 远程执行命令(需要密钥验证) 至于 DNS 缓存问题,路由器上是否有其他 DNS 代理?如 Adguard home,pdnsd 不过要是两个路由器同时重启了,就保持不了了 最好还是有一个固定 IP 的服务器,用 frp 或 SSH 穿透 |
2 Kaiyuan OP @LGA1150 #1 这种功能不是在外网的环境使用,是纯内部环境下非固定 IP 的情况。 有外网就直接 DDNS 方便多了。 |