Skip to content

Instantly share code, notes, and snippets.

@BigNerd95
Last active October 14, 2024 07:01
Show Gist options
  • Save BigNerd95/539f53a81320b5c9970dec6e776f6cbc to your computer and use it in GitHub Desktop.
Save BigNerd95/539f53a81320b5c9970dec6e776f6cbc to your computer and use it in GitHub Desktop.
GL.iNet ddns update script [will associate your wan IP to puXXXXX.gl-inet.com where XXXXX are the latest 5 bytes of the mac address]
#!/bin/sh
# OpenWRT support libs
. /lib/functions.sh
. /lib/functions/network.sh
. /usr/share/libubox/jshn.sh
ip_regex="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
ddns=
code=
model=$(awk 'BEGIN{FS="-"} /machine/ {print tolower($2)}' /proc/cpuinfo)
if [ "$model" = "connect inet v1" ]; then
model="6416"
ddns=$(dd if=/dev/mtd0 bs=1 skip=$((0x1fc10)) count=7 2>/dev/null)
code=$(dd if=/dev/mtd0 bs=1 skip=$((0x1fc20)) count=16 2>/dev/null)
elif [ "$model" = "ar150" -o "$model" = "ar300" -o "$model" = "mifi" ]; then
ddns=$(dd if=/dev/mtd6 bs=1 skip=$((0x10)) count=7 2>/dev/null)
code=$(dd if=/dev/mtd6 bs=1 skip=$((0x20)) count=16 2>/dev/null)
elif [ "$model" = "ar300m" ]; then
ddns=$(dd if=/dev/mtd3 bs=1 skip=$((0x10)) count=7 2>/dev/null)
code=$(dd if=/dev/mtd3 bs=1 skip=$((0x20)) count=16 2>/dev/null)
elif [ "$model" = "mt300a" -o "$model" = "mt300n" -o "$model" = "mt750" ]; then
ddns=$(dd if=/dev/mtd2 bs=1 skip=$((0x4010)) count=7 2>/dev/null)
code=$(dd if=/dev/mtd2 bs=1 skip=$((0x4020)) count=16 2>/dev/null)
fi
letter=$(printf "%x" "'${ddns:5:1}")
if [ "$letter" = "ff" ]; then
ddns=${ddns:0:5}
else
ddns=${ddns:0:7}
fi
port=$(uci get glconfig.general.port)
lastport=$port
monotonic_time()
{
local uptime
read uptime < /proc/uptime
echo "${uptime%%.*}"
}
get_current_ip(){
current_ip=$(wget -O - http://checkip.dyndns.org 2>/dev/null | grep -m 1 -o "$ip_regex")
if [ -z "$current_ip" ]; then
current_ip=$(wget -O - http://myip.com.tw/ 2>/dev/null | grep -m 1 -o "$ip_regex")
fi
echo "$current_ip"
}
verbose_echo()
{
echo $1
}
get_wan_ip(){
status=$(ifstatus wan)
if [ "$status" = "Interface wan not found" ]; then
status=$(ifstatus wwan)
fi
json_load "$status"
json_select "ipv4_address"
json_select 1
json_get_var wan_ip "address"
echo "$wan_ip"
}
#kill old process
if [ -d /var/run/dynamic_dns ]
then
if [ -e "/var/run/dynamic_dns/gl.pid" ]
then
old_pid=$(cat /var/run/dynamic_dns/gl.pid)
test_match=$(ps | grep "^[\t ]*$old_pid")
verbose_echo "old process id exist"
if [ -n "$test_match" ]
then
kill -9 $old_pid
fi
fi
else
mkdir /var/run/dynamic_dns
fi
echo $$ > /var/run/dynamic_dns/gl.pid
#determine when the last update was
force_interval_seconds=$((24*60*60))
current_time=$(monotonic_time)
last_update=$(( $current_time - (2*$force_interval_seconds) ))
if [ -e "/var/run/dynamic_dns/gl.update" ]
then
last_update=$(cat /var/run/dynamic_dns/gl.update)
fi
time_since_update=$(($current_time - $last_update))
verbose_echo "time_since_update = $human_time_since_update hours"
while [ true ]
do
#please be note user may stop updating ddns in the middle
config_load "glconfig"
config_get ddns_enabled ddns enabled
config_get lastip ddns lastip
config_get port general port
#if not eabled ddns, we will recheck 10 minutes later
if [ "$ddns_enabled" != "1" ]
then
sleep $((10*60))
continue
fi
registered_ip=$lastip
current_ip=$(get_current_ip)
wanip=$(get_wan_ip)
if [ -z "$current_ip" ]; then
sleep 10
continue
fi
#if the router is not directly exposed to internet, request upnp
if [ "$current_ip" != "$wanip" ]; then
echo "not exposed to internet directly, need to update UPNP"
/usr/bin/glupnpc nocheckip&
else
echo "exposed to internet directly, no need to update UPNP"
fi
current_time=$(monotonic_time)
time_since_update=$(($current_time - $last_update))
verbose_echo "current system ip = $current_ip"
verbose_echo "registered domain ip = $registered_ip"
if [ "$current_ip" != "$registered_ip" ] || [ "$lastport" != "$port" ] || [ $force_interval_seconds -lt $time_since_update ] || [ "$1" = "--force" ]
then
verbose_echo "update necessary, performing update ..."
final_url="http://www.gl-inet.com/ddnsregister.php?ddns=$ddns&code=$code&ip=$current_ip&port=$port&wanip=$wanip&model=$model"
verbose_echo $final_url
update_output=$(wget -O - "$final_url" 2>/dev/null)
if [ "$update_output" != "Update Done:$current_ip" ]
then
verbose_echo "update faild, sleeping"
sleep 300
continue
fi
verbose_echo "Update Output:"
verbose_echo "$update_output"
verbose_echo ""
current_time=$(monotonic_time)
last_update=$current_time
lastport=$port
time_since_update='0'
human_time=$(date)
verbose_echo "update complete, time is: $human_time"
echo "$last_update" > "/var/run/dynamic_dns/gl.update"
$(uci set glconfig.ddns.lastip="$current_ip")
$(uci set glconfig.ddns.updatetime="$human_time")
$(uci commit glconfig)
else
human_time=$(date)
human_time_since_update=$(( $time_since_update / ( 60 * 60 ) ))
verbose_echo "update unnecessary"
verbose_echo "time since last update = $human_time_since_update hours"
verbose_echo "the time is now $human_time"
fi
sleep $((10*60))
done
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment