Skip to content

Instantly share code, notes, and snippets.

@merrilymeredith
Last active December 23, 2020 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merrilymeredith/2de41b7cfe38f4e5b8787dc1ec958a16 to your computer and use it in GitHub Desktop.
Save merrilymeredith/2de41b7cfe38f4e5b8787dc1ec958a16 to your computer and use it in GitHub Desktop.
GL.iNet GL-AR300M VPN Switch
#!/bin/sh
set -eu
logger "$BUTTON ${ACTION}"
# Drop in /etc/rc.buttons once you have your VPN configured through the
# built-in interface. Switch toward the front for VPN enabled.
# Notes:
# - "startvpn start" ends with a background openvpn process that may not be up
# yet or ever be up -- config/connectivity issues -- but we still enforce the
# user preference for force-vpn so they can decide what do do.
# - the expected vpn device is in uci "network.VPN_client.ifname"
#
# GL-AR300M:
# Current FW: 2.25
# $BUTTON:
# BTN_0 - internal
# BTN_1 - physical switch on side
# $ACTION:
# pressed - left/back of device
# released - right/front of device
#
# https://www.gl-inet.com/forums/topic/vpn-switch/
VPN_ON="$(uci get glconfig.openvpn.enable)"
VPN_FORCE="$(uci get glconfig.openvpn.force)"
if [ "$ACTION" == "pressed" ]; then
[ "$VPN_ON" -eq "1" ] || exit
uci set glconfig.openvpn.enable=0
uci commit glconfig
killall openvpn || true
/etc/init.d/startvpn stop
/usr/bin/setvpnfirewall --disable
logger "== VPN switch down =="
else
[ "$VPN_ON" -eq "0" ] || exit
uci set glconfig.openvpn.enable=1
uci commit glconfig
/etc/init.d/startvpn start
if [ "$VPN_FORCE" -eq "1" ]; then
/usr/bin/setvpnfirewall --force
else
/usr/bin/setvpnfirewall --noforce
fi
logger "== VPN switch up =="
fi
@merrilymeredith
Copy link
Author

GL.iNet GL-AR300M is a pocket wired/wireless router with a custom interface on top of OpenWRT. It has a simple config where you upload .ovpn files and check one or two boxes, and you've got a portable VPN out of weird networks. This script lets you switch the VPN on and off using the configurable side switch, which is handy if you have to drop the VPN in order to log in on a captive portal before bringing it back up again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment