Last active
July 24, 2022 06:48
-
-
Save segfault-bilibili/3c9d10db632cc05ebe6da638d3669abd to your computer and use it in GitHub Desktop.
Ugly DDNS script for Huawei ONT's built-in OpenWrt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
mkdir -p /tmp/freednsv2 | |
exec_wget() { | |
wget --no-hsts -O - $@ 2>&1 | head -c4096 | |
} | |
wait_for_internet() { | |
echo $(date) "Wait for Internet connection..." | |
seq 0 300 | while read line; do | |
nslookup sync.afraid.org && break | |
sleep 1 | |
done | |
echo -ne $(date) "Connected to the Internet\n\n" | |
} | |
update_dns() { | |
echo $(date) "Update DNS..." | |
wait_for_internet | |
cat /root/ddns_urls.txt | while read url; do | |
exec_wget "${url}" | |
done | |
echo -ne $(date) "Done\n\n" | |
} | |
update_dns_with_log() { | |
tail -c131072 /tmp/freednsv2/log.txt > /tmp/freednsv2/log_.txt | |
cat /tmp/freednsv2/log_.txt > /tmp/freednsv2/log.txt | |
rm /tmp/freednsv2/log_.txt | |
update_dns 2>&1 >> /tmp/freednsv2/log.txt | |
} | |
wait_and_update() { | |
mkdir /tmp/freednsv2/lock || exit 1 | |
[ ! -e /tmp/freednsv2/fifo12380 ] && mkfifo /tmp/freednsv2/fifo12380 | |
if [ ! -p /tmp/freednsv2/fifo12380 ]; then echo "cannot mkfifo"; exit 2; fi | |
# fake no-ip.com server | |
cat /tmp/freednsv2/fifo12380 | nc -c -l -s 127.0.0.1 -p 12380 | while read line; do | |
update_dns_with_log | |
echo -ne "HTTP/1.1 200 OK\r\nContent-Length: 15\r\nConnection: close\r\n\r\ngood 127.0.0.1\n" > /tmp/freednsv2/fifo12380 | |
break | |
done | |
rmdir /tmp/freednsv2/lock | |
} | |
wait_loop() { | |
update_dns_with_log | |
while sleep 1; do wait_and_update || exit $?; done | |
} | |
fork_again() { | |
wait_loop & | |
} | |
fork_again & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment