GL-AR300M side switch - swap between VPN providers

Hi, I have a couple of GL-AR300M routers and they’re great tiny boxes that just work, I’ve set it to autoconnect to VPN when router starts up. I know the switch on the side can be configured to turn the VPN on and off but can it be adjusted to swap between VPN1 and VPN2? If I setup a working VPN config on the router named ‘VPN_Provider_1’ and a second named ‘VPN_Provider_2’ can the switch be set to make a change on the device so at router start up it will autoconnect to either VPN_Provider_1 or VPN_Provider_2 depending on movement of switch? If this was possible I’d expect the switch would make a change in the settings and then router would have to be rebooted for change to take effect but that would be fine, it doesn’t need to automatically swap VPN over whilst device stays powered up (although if it did that would be very nice).

I came across the below thread about changing the function of the switch and comments like ‘The switch can be programmed to do whatever you like. Just do a forum search on it and you will see!’ gives me hope but searching the forum I didn’t manage to find anything to cover this scenario.

I haven’t messed around with any scripts on the router before but if anyone knows how to make this work and can tell me how to adjust (and what firmware version the change steps relate to, GL-AR300M has many different firmware versions and I expect the script contents vary greatly between versions) I’ll certainly give it a go, I have a spare device I can play around with.

Hope someone can help :slight_smile:

Thanks

To implement the switch function for swapping between VPN providers (WireGuard and OpenVPN), the basic idea is as follows:

  • To switch between WireGuard configurations:
    1. Add some code to set /etc/config.
    2. Run /etc/init.d/wireguard downup.
  • To switch between OpenVPN configurations:
    1. Add some code to set /etc/config.
    2. Run /etc/init.d/startvpn start.

For the first step (setting the script), you can operate at the admin panel. Generate a config backup and then compare the changes using a tool like Bcompare.

The code should be put in script /usr/bin/switchaction.

I would find this an incredibly useful capability but as a relative newbie I would need pretty explicit instructions on the code that would need to be added to /etc/config. Both of my VPNs use WireGuard. Any help here would be greatly appreciated.

Hi, agreed, I’d find it useful myself but I also have non technical friends and family that would find this feature very useful as well (rather than me getting a phone call each time if things aren’t working to be walked through simply changing VPN provider).

Sadly I don’t know what code would need to be added/adjusted on the router to make this happen, from the thread I linked I had an idea of what files needed adjusting and ‘handsome’ has also confirmed those but what to actually add, I’m stuck…

I’m hoping someone who understand this a bit better could supply the code to add/adjust so I can have a go at making this work, if they can blank out any user account/password/VPN provider etc details with X’s or similar we could add in the relevant details for our setups but still easily understand what need to go where. Unfortunately both of my VPN providers don’t provide wireguard manual setup info and whilst I can use the GL-AR300M with their wireguard implementation using the gl.inet iphone app the info it pulls quickly expires so cannot be used to connect to VPN again even just a few hours later - I’d need to stick to OpenVPN login info for both VPN options so it works.

I really feel this is something a great many people would use if it was available, in an ideal world I’d love it if this was added into the main firmware as a default selectable option for what the switch does just like it can be set to use as VPN on/off.

An extremely high percentage of gl.inet router owners will be using them with a VPN and an extremely high percentage of those will have more than one VPN provider they wish to switch between, making doing so with a simple switch action vs logging into router just makes so much sense I’m a little surprised such an option doesn’t already exist - sounds like an easily implementable popular feature improvement gl.inet could add :slight_smile: In the meantime though I’d happily try and make it work myself if I could just get some help on where to start.

Thanks

Hi,
Sadly neither I (or 4havnfun who is also very interested in this) have any idea where to start with what VPN code would need to be added into any of those files, any chance you could give us some default OpenVPN code for this ability that we can then adjust to fit our own VPN scenarios?

Looking at the suggested code given by alzhao on the wifi on/off side switch thread it’s obvious I’d have no chance of getting such a feature working myself through guesswork, looks far too complicated sadly.

Hope you can help.

thanks

1 Like

There are many possible uses for these switches and no way to make everyone happy. I use my switch to start SoftEther as GL iNet doesn’t support this VPS client, and I don’t expect them to.

If you want the router to do things outside what GL iNet supplies or has promised then I would recommend for you to learn a bit about scripting or find a friend to do it for you. Reprogramming the switch is relatively easy.

I would really like to see the GL iNet engineering team follow through on promises made a long time ago to provide 4.x firmware to the whole product line.

2 Likes

Script to switch between wireguard server:

#!/bin/sh

# Read the current value of main_server
main_server="$(uci get wireguard.@proxy[0].main_server)"

# Get the index of the current main_server option
index=$(uci show wireguard | grep -n "main_server=$main_server$" | cut -d ':' -f 1)

# Get the names of all the peers
peer_names=$(uci show wireguard | grep -o "name=[^ ]*" | cut -d '=' -f 2)

# Get the number of peers
num_peers=$(echo "$peer_names" | wc -l)

# Find the index of the current main_server option in the peer list
peer_index=$(echo "$peer_names" | grep -n "$main_server" | cut -d ':' -f 1)

# Calculate the index of the next peer
next_peer_index=$(( (peer_index % num_peers) + 1 ))

# Get the name of the next peer
next_peer=$(echo "$peer_names" | sed -n "${next_peer_index}p" | sed "s/\'//g")

# Update the main_server option
uci set wireguard.@proxy[0].main_server="$next_peer"

# Commit the changes
uci commit wireguard

For instance save it as file name, /usr/bin/rotato_wg_server.sh
Then change script /usr/bin/switchaction to call it.

2 Likes

OK great that looks really helpful although with both of my VPN providers they don’t provide proper wireguard config, using the gl.inet app it can fetch working details but after a few hours (or sooner) the details no longer work - it’s not a possibility to use wireguard for my friends and family who don’t have any technical knowledge, they’re happy with slower speeds and a set and forget config that works every time they start up the router.

Could you please provide details on how to swap between two openvpn connections using the toggle switch? I guess for openvpn would be very similar but there may be subtle differences I’d miss if I just try and swap the word wireguard for openvpn in everything above, /usr/bin/rotato_wg_server.sh looks to be wireguard only for starters.

Thanks

1 Like

Okay, openvpn is different from wireguard, I’ll add a script for it later.

This issue has been resolved in firmware 3.216 GL.iNet download center,
without toggling wireguard client, you can have a try.

2 Likes

rotate_ovpn_server.sh

#!/bin/sh

clientid="$(uci get glconfig.openvpn.clientid)"

group_index="${clientid:4}"

server_file_name=$(uci get ovpnclients.ovpn$group_index.defaultserver)


groups=$(ls -d /etc/openvpn/ovpn*)
files=$(ls -d /etc/openvpn/ovpn*/*.ovpn)

group_num=$(echo "$groups" | wc -l)

next_file=$(echo $files | awk -v v="/etc/openvpn/ovpn${group_index}/${server_file_name}" '{for(i=1;i<=NF;i++)if($i==v){if(i<NF){print $(i+1);exit}}}')
if [ -z "$next_file" ]; then
	next_file=$(echo $files|cut -f1 -d" ")
fi
dir_next_file=$(dirname $next_file)
next_file_group_index=${dir_next_file:17}

# Update the main_server option
uci set glconfig.openvpn.clientid="ovpn$next_file_group_index"
uci set glconfig.openvpn.ovpn=$next_file
uci set ovpnclients.ovpn${next_file_group_index}.defaultserver=$(basename $next_file)

uci commit glconfig
uci commit ovpnclients
/etc/init.d/startvpn restart

Also need to change /usr/bin/switchaction

1 Like