Skip to content

Instantly share code, notes, and snippets.

@pzgz
Created December 30, 2012 09:28
Show Gist options
  • Save pzgz/4411778 to your computer and use it in GitHub Desktop.
Save pzgz/4411778 to your computer and use it in GitHub Desktop.
GraceMode autoddvpn update script for tomato firmware
#!/bin/sh
#set -x
host='autoddvpn.googlecode.com'
cv_dir='/jffs/var/'
cv_file="${cv_dir}version.client"
vpnup_remote_path='http://autoddvpn.googlecode.com/svn/trunk/grace.d/vpnup.sh'
vpndown_remote_path='http://autoddvpn.googlecode.com/svn/trunk/grace.d/vpndown.sh'
vpnup_local_path='/jffs/openvpn/vpnup.sh'
vpndown_local_path='/jffs/openvpn/vpndown.sh'
get_server_version() {
printf "HEAD /svn/trunk/grace.d/vpnup.sh HTTP/1.0\nHost: $host\n\n" | \
nc $host 80 | \
sed -n 's#^ETag: "\(.*\)//trunk/.*#\1#pg'
}
get_client_version() {
if [ ! -d $cv_dir ]; then mkdir -p $cv_dir; fi
if [ ! -e $cv_file ]; then
cv=0
else
cv=$(head -n 1 $cv_file)
fi
echo $cv
}
update_vpnup() {
echo "[INFO]vpnup.sh start updating"
mv ${vpnup_local_path} ${vpnup_local_path}.old
wget -q ${vpnup_remote_path} -O ${vpnup_local_path}
sed -i "s/OPENVPNDEV='tun0'/OPENVPNDEV=\'$(ifconfig |grep tun | grep -Eo "tun([0-9.]+)" | cut -d: -f2)\'/g" ${vpnup_local_path}
if [ "$(nvram show | grep wan_gateway_get | grep -o '[0-9.]*')" = "$(ip -4 addr show dev ppp0 | grep 'inet ' | awk '{print $4}' | cut -d/ -f1)" ]
then
echo "wan_gateway_get=$(nvram show | grep wan_gateway_get | grep -o '[0-9.]*')"
echo "gateway ip=$(ip -4 addr show dev ppp0 | grep 'inet ' | awk '{print $4}' | cut -d/ -f1)"
sed -i "s/wan_gateway/wan_gateway_get/g" ${vpnup_local_path}
fi
chmod a+x ${vpnup_local_path}
}
update_vpndown() {
if [ ! -e $vpndown_local_path ]; then
echo "[INFO]vpndown.sh start updating"
wget -q ${vpndown_remote_path} -O ${vpndown_local_path}
sed -i "s/OPENVPNDEV='tun0'/OPENVPNDEV=\'$(ifconfig |grep tun | grep -Eo "tun([0-9.]+)" | cut -d: -f2)\'/g" ${vpndown_local_path}
sed -i "s/\/tun0\//\/$(ifconfig |grep tun | grep -Eo "tun([0-9.]+)" | cut -d: -f2)\//g" ${vpndown_local_path}
if [ "$(nvram show | grep wan_gateway_get | grep -o '[0-9.]*')" = "$(ip -4 addr show dev ppp0 | grep 'inet ' | awk '{print $4}' | cut -d/ -f1)" ]
then
sed -i "s/wan_gateway/wan_gateway_get/g" ${vpndown_local_path}
fi
chmod a+x ${vpndown_local_path}
fi
}
restart_openvpn() {
kill -HUP $(pidof openvpn)
return $?
}
sv=$(get_server_version)
cv=$(get_client_version)
if [ $sv -gt $cv ]; then
echo "[INFO] need update(client version: $cv is lower than server verson: $sv)"
update_vpnup
update_vpndown
if [ $? -eq 0 ]; then
echo $sv > $cv_file
echo "[OK] update completed"
restart_openvpn
if [ $? -eq 0 ]; then
echo "[OK] OpenVPN restarting and refreshing the routing table now."
echo "[INFO] this may take a while to apply new rules"
else
echo "[ERR] failed to restart the OpenVPN, you may need to reboot DDWRT"
fi
else
echo "[ERR] update failed"
fi
elif [ $sv -eq $cv ]; then
echo "[INFO] already up-to-date"
else
echo "[ERR] client version is newer than server version"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment