GL-AR150 Using GPIO 8 (switch) to enable/disable VPN

Hi
I got my AR150 router some days ago and I really love this little thing. Up until now I have used the MR3020 for most of the time which have served me well.
All of my MR3020 are upgraded with 64MB ram and 16MB flash and a external antenna mod.
Now to find the AR150 that comes pre installed with all the MR3020 upgrades I have done just roxx :slight_smile:

I am using the AR150 router now and it’s working great. Atm I have a LTE modem connected to it and it is set up to use l2tp VPN to hide my ip (not for security)
To enable/disable the VPN connection I am using the web gui (LuCI) which of course works ok but when I see this little unused switch on the router I immediately wanted to set it up as a VPN on/off switch.
I found some information on this forum regarding the rc.button/BTN_8 script and I did some modification to use it for VPN.
It works great when switching and the router is on. VPN comes on when I switch it to position “1” and drops when I move it back to position “0”.

My problem is that if I enable VPN by moving the switch to “1” and then reboot the router VPN wont come up and the switch is the out of sync.
To enable VPN again I have to switch it first to “0” and then back to “1” again.
To solve this I was thinking of having BTN_8 script to change the network config script and set the VPN interface to “Bring up on boot” but then what if the switch is set to “1” and I shutdown the router for then to set the switch to “0”. This will bring up VPN at next boot and the switch is out of sync again.

So if someone can hale me out here with my issue I would really appreciate it allot.

Here is my BTN_8 script:


#!/bin/sh
echo "$BUTTON ${ACTION}" > /dev/console
if [ .${ACTION}. = .pressed. ]; then
    #Button in position "0"
    ifdown vpn # take down VPN interface
    sleep 1
    /etc/init.d/network restart #get default route back. probably not the best way but it works
else
    #Button in position "1"
    ifup vpn
    sleep 1
    /etc/init.d/led restart #looks like I need this for the led to work after VPN comes back up
fi

Middle led is also set to blink when VPN come on. Here is the system led script:


config led
    option default '0'
    option name 'VPN'
    option sysfs 'gl_ar150:lan'
    option trigger 'netdev'
    option dev 'l2tp-vpn'
    option mode 'link tx rx'

Is there a way to read the switch status when booting or is the BTN_8 script the only way to get info form the switch?

Hi, one solution is to write a init script to read the button status and set the vpn.

To do this, something you need to modify.

First, in your script, you use ifup and ifdown, this is OK. but this status is not stored on the flash. You need to store the status somewhere. For example use uci

/etc/config/vpn
config service vpn
option disabled ‘1’
Then in your BTN_8, do something: to change this value

uci set vpn.vpn.disabled=‘1’; uci commit vpn

uci set vpn.vpn.disabled=‘0’; uci commit vpn

Second, write some init script, e.g. /etc/init.d/initswitch

chmod +x /etc/init.d/initswitch

You can write the content of this file according to other scripts. Something like this:

#!/bin/sh

START=19 #some number before the vpn init script

start(){

#swith on the left side
if (grep -o “BTN_8.*hi” /sys/kernel/debug/gpio)
then
uci set vpn.vpn.disabled=‘0’; uci commit vpn
ifup vpn #this may not be useful as the vpn interface is not up yet, just be safe
else
uci set vpn.vpn.disabled=‘1’;uci commit vpn
ifdown vpn #this may not be useful as the vpn interface is not up yet, just be safe
}

 

Third, find your VPN script and read the disable settings

stat=$(uci get vpn.vpn.disabled)

if [ “$stat” = “1” ]; then
#don’t start the vpn interface at all
else
#start the vpn interface
fi

 

Thanks for your quick respons.

I ended up with making a script like this that uses your grep command:

#!/bin/sh

if (grep -o "BTN_8.*lo" /sys/kernel/debug/gpio) >/dev/null
then
        #switch in right position (on)
        echo switch_on
        ifup vpn
        sleep 3
        /etc/init.d/led restart
fi

and I am launching if from /etc/rc.local which should be one of the last init script that is executed under boot I think.
It is working pretty good now so i guess when rc.local is executed most of the system is up and running.

Again. Thanks for our help.

Hi Would appreciate some help please :slight_smile:
Really like the idea of using the switch to turn VPN on and off.
I tried your scrip, but I am guessing I have not done something correctly…
I added this here: /etc/rc.button/BTN_8
#!/bin/sh
echo “$BUTTON ${ACTION}” > /dev/console
if [ .${ACTION}. = .pressed. ]; then
#Button in position “0”
ifdown vpn # take down VPN interface
sleep 1
/etc/init.d/network restart #get default route back. probably not the best way but it works
else
#Button in position “1”
ifup vpn
sleep 1
/etc/init.d/led restart #looks like I need this for the led to work after VPN comes back up
fi

And added this here: /etc/config/system
config led
option default ‘0’
option name ‘VPN’
option sysfs ‘gl_ar150:lan’
option trigger ‘netdev’
option dev ‘l2tp-vpn’
option mode ‘link tx rx’

Would really appreciate if you could point me in the right direction, and also how you solved the switch sync issue after reboot…
Thank you… Andrew