Skip to content

Instantly share code, notes, and snippets.

@segfault-bilibili
Last active July 24, 2022 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save segfault-bilibili/3c9d10db632cc05ebe6da638d3669abd to your computer and use it in GitHub Desktop.
Save segfault-bilibili/3c9d10db632cc05ebe6da638d3669abd to your computer and use it in GitHub Desktop.
Ugly DDNS script for Huawei ONT's built-in OpenWrt
#!/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