VPN On/Off Switch

Has anyone configured the switch to enable / disable VPN?

Or if not, how might one go about it?

can you find the scripts of the switch in /etc/rc.button/ ?

You can try to modify it and test

enable vpn

uci set glconfig.openvpn.enable='1'
uci commit glconfig
/etc/init.d/startvpn restart
/usr/bin/setvpnfirewall --force

 

disable vpn

uci set glconfig.openvpn.enable='0'
uci commit glconfig
/etc/init.d/startvpn stop
/usr/bin/setvpnfirewall --disable

For a MT300N, the slider button is called BTN_0. In this case just add the following to /etc/rc.button/BTN_0

 

if [ “${ACTION}” == “pressed” ]; then

uci set glconfig.openvpn.enable=‘1’

uci commit glconfig

/etc/init.d/startvpn restart

/usr/bin/setvpnfirewall --force

elif [ “${ACTION}” == “released” ]; then

uci set glconfig.openvpn.enable=‘0’

uci commit glconfig

/etc/init.d/startvpn stop

/usr/bin/setvpnfirewall --disable

fi

Can anyone advise me as I must be doing something wrong here.

I have an ar300m NAND and have installed nano in order to edit the <span style=“background-color: #f9f9f9; color: #222222; font-family: source_sans_proregular, ‘Helvetica Neue’, Arial, Helvetica, Geneva, sans-serif; font-size: 14.6667px;”>/etc/rc.button/BTN_0 file. I have copied in the script above and rebooted the router.</span>

Unfortunately this doesn’t appear to be working for me.

Any thoughts/comments/advice appreciated.

can you try some simple script first. For example you can write to log something

if [ “${ACTION}” = “pressed” ]; then

logger "button pressed"

fi

I tried adding logger “button pressed” but can’t seem to locate it, is it in /tmp?

Also, I tried this script in BTN_1 rather than BTN_0 as I read elsewhere on the forum that the switch is BTN_1, is that accurate?

I too am having difficulty with the VPN on/off function using the above script. Thanks for all the help everyone :slight_smile:

From what I’ve read on this forum, different models might have different button-names.

What I did to find the correct button name (BTN_0 in my case with MT300N) is to check the content of /sys/kernel/debug/gpio with a text editor. Do this twice: once with the switch to left, once with the switch to right. You should see a value changing from ‘hi’ to ‘lo’ or vice versa. Next to this value should be the correct button-name

You can also do what alzhao said, adding the ‘logger “button pressed”’ to the button script. To see the log output, just type ‘logread’ in shell. When you used the correct button script, there should be an extra entry ‘button pressed’ after changing the switch position to ON.

 

PS: The above script only executes when changing the button position after the device is booted. So this doesn’t check the button position on boot. To accomplish this, there need to be some other modifications on init scripts. If anyone is interested, I can elaborate…

@joskevermeulen Thanks, I’m on MT300N also. At /sys/kernel/debug/gpio on textedit it’s showing blank.

I’ll try on different firmware version and report back are you on latest firmware?

You can also try the other suggestion.

I’m on 2.24

I recommend putting the VPN connection string in a separate script file that you can call either with the rc.button script or the initswitch script. On the GL-AR300M, the switch script is called BTN_1.

In the initswitch script, I added this to the “ar300m” case:

switch_disabled=$(grep -c "gpio-1 .*hi" /sys/kernel/debug/gpio)

That returns a 0 or 1 depending on if the switch is disabled or enabled. You can use that to call a script that executes a VPN connection (or disconnection).

Update - I think I may know why I am having issues with the original script, the switch press only gets recognised on the upward position (next to the reset button) when pressed downwards it does not show in logread. Is there anyway I can fix this not being recognised?

At the moment with the script from joskevermeulen after updating my firmware I am able to disable the VPN by switching the position of the switch forward and back again (so from on to off to on again for example) whilst switching in one direction doesn’t do anything and the switch never enables the VPN.

@hymond that sounds interesting, where do I add the ‘switch_disabled=$…’ script? and how would I call? Apologies for my lack of knowledge and thanks for your help.

It’s normal that you only see it changing in the logread in upward position because the script from alzhao is only triggered in that case. To show both sides you need:
if [ “${ACTION}” = “pressed” ]; then

logger “button pressed”

elif [ “${ACTION}” = “released” ]; then

logger “button released"

fi

 

PS: since you updated, can you check if you now see the value changing from hi to lo in /sys/kernel/debug/gpio (what I previously suggested)

@wifired, /sys/kernel/debug/gpio is not editable and viewable using vi, you only can read it using “cat”

@joskevermeulen

PS: The above script only executes when changing the button position after the device is booted. So this doesn’t check the button position on boot. To accomplish this, there need to be some other modifications on init scripts. If anyone is interested, I can elaborate….
Can you elaborate? What script would need to be modified to read the button state at boot time?