Create Surfshark wireguard connection on OpenWrt easily

I'm sure there exist eaiser methods, I for one could just drop a pre made "network" file into /etc/config/network and restart. But it was a fun challenge and I learned something. When the other options pop up; I'll learn more! Enjoy your journey. @RuralRoots I have the suggestion saved to learn along with jq curl cat Thank you.

#!/bin/sh
echo "Running at $(date)"
uci reorder network.@[13]=12
uci commit network
/etc/init.d/network restart
logger -t yourIFname ":$(date)   connection established"

## Undo examples:
## uci reorder network.@[13]=12
## uci reorder network.@[12]=13

## uci reorder network.@[13]=11
## uci reorder network.@[11]=13

## uci reorder network.@[13]=10
## uci reorder network.@[10]=13

## uci reorder network.@[13]=9
## uci reorder network.@[9]=13

####################################################################################
# MULTIPLE PEER(2) NETWORKS CAN BE ADDED TO LUCI AND SWAPPED BY .sh
# These networks are known by the notations below. Goal is to find peer(2).
# Plug in a number, or examine your "network" file ~ vi , nano, gui ~ 
# Count the unique "config" entries to find your first and last peer(2) entry
#
# (uci show network.@[])  	#example will show first top entry /etc/config/network
# (uci show network.@[0]) 	#example will also show first top entry /etc/config/network
# (uci show network.@[1]) 	#example will show second entry /etc/config/network
# (uci show network.@[2])	#example will show third entry /etc/config/netork
# ....
# (uci show network.@[13]	#example ~~ 13th entry /etc/config/network 
# last peer=wg0 current tunnel us-nyc!

#########################      Visual    ##############################

#root@Dachshund:~# uci show network.@[]
#network.loopback=interface
#network.loopback.ifname='lo'
#network.loopback.proto='static'
#network.loopback.ipaddr='127.0.0.1'
#network.loopback.netmask='255.0.0.0'
#root@Dachshund:~# uci show network.@[0]
#network.loopback=interface
#network.loopback.ifname='lo'
#network.loopback.proto='static'
#network.loopback.ipaddr='127.0.0.1'
#network.loopback.netmask='255.0.0.0'
#root@Dachshund:~# uci show network.@[1]
#network.globals=globals
#network.globals.ula_prefix='0:0:0:0::/0'
#root@Dachshund:~# uci show network.@[13]
#network.cfg0e6912=wireguard_SSWG
#network.cfg0e6912.public_key='rhuoCmHdyYrh0zW3J0YXZK4aN3It7DD26TXlACuWnwU='
#network.cfg0e6912.peersistent_keepalive='25'
#network.cfg0e6912.endpoint_port='51820'
#network.cfg0e6912.allowed_ips='0.0.0.0/0' '::/0'
#network.cfg0e6912.route_allowed_ips='1'
#network.cfg0e6912.endpoint_host='us-nyc.prod.surfshark.com'
#network.cfg0e6912.description='nyc-prod'
#
# Per OpenWrt - CFGID's are assigned to WireGuard Peers in order a~z.
# Shown below as cfg0(a)6912, cfg0(b)6912, etcetera
# Static entries insofar as peers do not move in order, unless commanded to. This sh's goal.
# The command: (uci reorder network.'cfg0a6912'=) will move that peer  <NOTE:AVOID> 
# to the very top of your /etc/config/network file and the CFGID's will change!
# The command: (uci reorder network.@[9]=) will move that peer <NOTE:AVOID>
# to the very top of your /etc/config/network file and the CFGID's will change!
# These CFGID can be found by either using a web browser's inspect tool
# upon the peer in the setup section of the Peer Tab in Luci, or
# invoking a save and then inspecting the "unsaved changes" section,
# or uci reorder network.@[#] where # represents number (see above [1]) of config line entry
# in your 'network' file, and then using (uci changes) to view the cfg0xxxxxx
# or using (uci show network.'cfgxxxxxx') if you happen to know xxxxxx
# Last entry will become path>wg0 tunnel. In this ex. cfg0e6912 is path.
# This example demonstrate five peers added via uci or Luci.
#
# cfg0a6912					#example cfg0a6912 is fr-bod
# cfg0b6912					#example cfg0a6912 is jp-tok
# cfg0c6912					#example cfg0a6912 is it-rom
# cfg0d6912					#example cfg0a6912 is pl-waw
# cfg0e6912					#example cfg0a6912 is us-nyc
#
# ***Issuing a reorder command to move from us-nyc onto pl-waw***
# Since us-nyc is the 13th config line [13] and currently cfg0e6912
# Since pl-waw is the 12th config line [12] and currently cfg0d6912
# uci the following... 
#
#root@Dachshund:~# uci reorder network.@[13]=12
#root@Dachshund:~# uci changes
#network.cfg0e6912='12'
#uci: Entry not found
#uci: Entry not found
#uci: Entry not found
#uci: Entry not found
#uci: Entry not found
#root@Dachshund:~# uci export network
#package network
#
#config interface 'loopback'
#        option ifname 'lo'
#        option proto 'static'
#        option ipaddr '127.0.0.1'
#        option netmask '255.0.0.0'
#
#config globals 'globals'
#        option ula_prefix '0:0:0:0::/0'
#
####  TRUNCATED  ####
#
#config wireguard_SSWG
#        option public_key 'rhuoCmHdyYrh0zW3J0YXZK4aN3It7DD26TXlACuWnwU='
#        option peersistent_keepalive '25'
#        option endpoint_port '51820'
#        list allowed_ips '0.0.0.0/0'
#        list allowed_ips '::/0'
#        option route_allowed_ips '1'
#        option endpoint_host 'us-nyc.prod.surfshark.com'
#        option description 'nyc-prod'
#
#config wireguard_SSWG
#        option public_key 'vBa3HK7QXietG64rHRLm085VMS2cAX2paeAaphB/SEU='
#        option persistent_keepalive '25'
#        option endpoint_port '51820'
#        list allowed_ips '0.0.0.0/0'
#        list allowed_ips '::/0'
#        option route_allowed_ips '1'
#        option endpoint_host 'pl-waw.prod.surfshark.com'
#        option description 'waw-prod'
#
#root@Dachshund:~# uci commit ; /etc/init.d/network restart
#
# Now pl-waw takes the CFGID of cfg0e6912 and position of 13th and is VPN tun. 
# Now us-nyc takes the CFGID of cfg0d6912 and position of 12th.
# Changes will be reflected in the Peer Tab of Setup and the WireGuard Status page.
###################################################################################
1 Like

Yessir! You did ask though. :rofl:

I wondered how you’d tackle the indexing when I read that. Well Done!!

No better honor than receiving my "Welcome Badge" from the Sensei of this forum. :man_bowing:

I dug deep into the router /www/luci-static/resources/protocol/wireguard.js

See illustration:

To find some clues..........out on the limb I so frequently visit........

`deleteConfiguration:function(){uci.sections('network','wireguard_%s'.format(this.sid),function(s){uci.remove('network',s['.name']);});`

The thought process is that since we have an "Add Peer" button and the "Delete" button; if I could manipulate these commands from deleteConfiguration and uci.remove to say reorderConfiguration and uci.reorder and if it work then, move onto adding an addition line, and button. But it failed.
I threw several blind punches, at the line: rebooted the router.. the good news is I didn't brick luci. So any ideas Paul? Do you mingle with some top notch js peeps?

On somewhat of a sidetrack I found this odd: All these *.json dated files several week old. Which would match the time I started from scratch. Oddly no surfshark_servers.json files AKA from running @patrickm script.

/www/luci-static/resources/tools/selected_servers.json
/www/luci-static/resources/icons/selected_servers.json
/www/luci-static/resources/selected_servers.json
/www/luci-static/resources/icons/token.json
/www/luci-static/resources/token.json
/www/luci-static/resources/tools/surf_servers.json
/www/luci-static/resources/icons/surf_servers.json
/www/luci-static/resources/surf_servers.json
/www/luci-static/resources/tools/wg.json
/www/luci-static/resources/icons/wg.json
/www/luci-static/resources/wg.json
/www/luci-static/resources/view/status/include/selected_servers.json
/www/luci-static/resources/view/status/include/wg.json
/www/luci-static/resources/view/status/include/surf_servers.json
/www/luci-static/resources/view/status/include/token.json
get_servers() {
    echo "Retrieving servers list..."
    tmpfile=$(mktemp /tmp/surfshark-wg-servers.XXXXXX)
     .
     .
     .

rm $tmpfile
    return $rc
}

He retrieves the former surfshark_servers.json file instead to a temp file every run so a physical file never exists.

Not my forte, maybe this might help. https://github.com/openwrt/luci#development

Thanks for the homework link.

I meddled around with his script and actually produced a copy and somehow manage to get the "handicapped" conf files place in my run /etc/config/wireguard/conf folder.

BUT..

find / -iname wg.json

So it's not odd to have these sticks scatter in the wind?

Summary

Sorry to resurrect an old post, but in case anyone else gets the HTTP 429 error / not allowed error from wg_check_pubkey(), I found the fix to this in the below SS article...

How to fix website/app login issues? – Surfshark Customer Support

Change your ip address and the problem will go away.

1 Like

Yes, odd.

root@RuralRoots:/# find / -iname wg.json
/mnt/shared/wgapi/wg.json

Wondering about:

What are the file contents?

The bricking comment was pointed at manipulating the wireguard.js file.

The file contents of the various and multiple *.json files are exactly what one would expect ~ yet dated. Perhaps ~ and most likely ~ running the wg_gen_config.sh file in WinSCP from the /www/luci-static/* folders during testing populated the errantly located files. I'll delete!

Thread Master Bot suggested I consolidate: So..

Does your repo edition of the script add verbosity to flag 429 error like @patrickm script does?

Great find.... right to the source for the answer.

Good reason to be a Guinee pig and test that reorder script.

Other than adding -v option to the curl responses to figure what HTTP Responses were being returned, No. Only encountered it when I initially used my .ovpn Credentials in config.json.

Ahhh, so you do have an intrepid vein. Bold Move...Bold Move.

btw thanks for jinxing me.. router did puke after all.

Dear Professor.. I had read this entire thread before joining, but your issue was the reason I joined. I hate to leave a rock unturned. I think this might help revive brain stem activity. Happy Regards!

Summary

This is dedicate to the OP @yazdan , @patrickm , and @RuralRoots!

Thanks to @directnupe for Tutorial-1 and Tutorial-2 and Testing Script and Cron Job.


Summary of Scripts Available on This Thread

  1. yazdan Feb 2, 2023 ~ gen_wg_config.sh: Support adding config files to a zip archive
    Utility flag, useful to import the configurations to the android app
    New Switches and Suffix's on the tagged {p2p, virtual, physical} in the config folder for refined selection.
            echo "  -f force register ignore checking"
            echo "  -g ignore generating profile files"
            echo "  -n <name> create a manual named key"
            echo "  -k <key> use provided private key"
            echo "  -l list registered manual keys"
            echo "  -d <key-id> delete registered manual key"
            echo "  -z [zip-file] zip archive in which to save the config files"

  1. Bill reIyst update on Feb, 7 2023 Release 1.0.1 Keys.sh

  2. patrickm revised this gist May 17, 2022



Interface 'wg0' Endpoint Swap

image This README is dedicated to making a cli uci install of the Wireguard services and sswg script to enable one to swap endpoint easily and quickly. The front end work is lengthy, mostly reading; yet the outcome is well worth the time, especially since most of the work of configuration is uci set. Enjoy!

OpenWrt SurfShark WireGurard ~ SSWG

Have the requirements to run the script


opkg update opkg install diffutils curl jq ntpdate

:fast_forward: You can install and run the script to obtain the keys within the wg.json file prior to doing Multi Peer section

From the reIyst SSWG download the 'sswg.sh' and 'sswg.json' files. If you have WinSCP your day is made easy. SSH into your router via WinSCP/Putty and create the folder structure. Copy the two files into the 'wg' directory and make the sswg.sh file executable. ./sswg.sh -g Is the first run application and will produce all connection files w/ pvt keys needed to configure your router, and/or import in WireGuard's© desktop app.

mkdir -p /wg/

chmod +x sswg.sh

./sswg.sh -g


Multi (Peer) For Interface named 'wg0'

Have the requirements to use Luci/uci WireGuard©

opkg update opkg install luci-app-wireguard luci-proto-wireguard wireguard-tools

  • Reboot your system so the above packages can manifest in Luci.

Installing w/out Peer(1) and with Multi Peer for uci cli Swapping.

Follow the Templet Use all or at least two, or configure within the file your own. Double check the wan.metric='10' with ip route show default to ensure metric 10 is not already in use; modify accordingly. All public key are dummy. Until changed with legitimate pub key from you downloaded client conf files; you will be without :surfer: Internet access.

cd /
uci set network.wan.metric='10'

uci set network.wg0=interface
uci set network.wg0.proto='wireguard'
uci set network.wg0.listen_port='51820'
uci set network.wg0.addresses='10.14.0.2/8'
uci set network.wg0.private_key=$(eval echo $(jq '.prv' ./wg/wg.json))	
uci commit network

uci set network.peerchiu='wireguard_wg0'
uci set network.peerchiu.description=peerchiu
uci set network.peerchiu.public_key=DpMfulanF/MVHmt3AX4dqLqcyE0dpPqYBjDlWMaUI00=
uci add_list network.peerchiu.allowed_ips='0.0.0.0/0'
uci add_list network.peerchiu.allowed_ips='::/0'
uci set network.peerchiu.route_allowed_ips='1'
uci set network.peerchiu.endpoint_host=us-chi.prod.surfshark.com
uci set network.peerchiu.endpoint_port='51820'
uci set network.peerchiu.persistent_keepalive='25'
uci commit network

uci set network.peerdalu='wireguard_wg0'
uci set network.peerdalu.description=peerdalu
uci set network.peerdalu.public_key=0iwHQpV+rsOg38ogv4g4XMLJa51YqWY/yKWR9UEUMDk=
uci add_list network.peerdalu.allowed_ips='0.0.0.0/0'
uci add_list network.peerdalu.allowed_ips='::/0'
uci set network.peerdalu.route_allowed_ips='1'
uci set network.peerdalu.endpoint_host=us-dal.prod.surfshark.com
uci set network.peerdalu.endpoint_port='51820'
uci set network.peerdalu.persistent_keepalive='25'
uci commit network

uci set network.peernycu='wireguard_wg0'
uci set network.peernycu.description=peernycu
uci set network.peernycu.public_key=rhuoCmHdyYrh0zW3J0YXZK4aN3It7DD26TXlACuWnwU=
uci add_list network.peernycu.allowed_ips='0.0.0.0/0'
uci add_list network.peernycu.allowed_ips='::/0'
uci set network.peernycu.route_allowed_ips='1'
uci set network.peernycu.endpoint_host=us-nyc.prod.surfshark.com
uci set network.peernycu.endpoint_port='51820'
uci set network.peernycu.persistent_keepalive='25'
uci commit network

uci set network.peerwarp='wireguard_wg0'
uci set network.peerwarp.description=peerwarp
uci set network.peerwarp.public_key=vBa3HK7QXietG64rHRLm085VMS2cAX2paeAaphB/SEU=
uci add_list network.peerwarp.allowed_ips='0.0.0.0/0'
uci add_list network.peerwarp.allowed_ips='::/0'
uci set network.peerwarp.route_allowed_ips='1'
uci set network.peerwarp.endpoint_host=pl-waw.prod.surfshark.com
uci set network.peerwarp.endpoint_port='51820'
uci set network.peerwarp.persistent_keepalive='25'
uci commit network

uci set network.peertorc='wireguard_wg0'
uci set network.peertorc.description=peertorc
uci set network.peertorc.public_key=W9bzkcL3fiV64vDpB4pbrz8QafNn3y5P9Yc/kQvy4TA=
uci add_list network.peertorc.allowed_ips='0.0.0.0/0'
uci add_list network.peertorc.allowed_ips='::/0'
uci set network.peertorc.route_allowed_ips='1'
uci set network.peertorc.endpoint_host=ca-tor.prod.surfshark.com
uci set network.peertorc.endpoint_port='51820'
uci set network.peertorc.persistent_keepalive='25'
uci commit network
/etc/init.d/network restart


To minimize Firewall setup; Consider VPN network as public. Assign VPN interface to WAN zone.

uci add_list firewall.wan.network="wg0"
uci commit firewall
/etc/init.d/firewall restart


Swapping

Uci CLI Peer Swapping

The peer swapping is achieved by placing the desired peer config in the last/bottom order of the /etc/config/network file. The high arbitrary number '99' should suffice to place desired network peer at bottom. My personal config has only 15. The resulting command will also be represented in the Wireguard Status, Interface Peer Pages of Luci. Simple command, long description.

From the above install, Toronto Canada is the last peer installed and will be the default route the wg0 vpn tunnels through. By running the below command the Warsaw Poland endpoint takes the bottom position and becomes wg0 vpn tunnel. This is achieved from the network.peerwarp NETWORK not the description=peerwarp! A look at your /etc/config/network file will enlighten your understanding later.

Warsaw :surfer:
uci reorder network.peerwarp=99;uci commit network;/etc/init.d/network restart
Chicago :surfer:
uci reorder network.peerchiu=99;uci commit network;/etc/init.d/network restart
Dallas :surfer:
uci reorder network.peerdalu=99;uci commit network;/etc/init.d/network restart
New York :surfer:
uci reorder network.peernycu=99;uci commit network;/etc/init.d/network restart
Toranto :surfer:
uci reorder network.peertorc=99;uci commit network;/etc/init.d/network restart

Common uci commands for introspective users.

ip rule
wg.show
ip route show default
ubus call system board; uci export dhcp; uci export network; uci export firewall



Copyright and Attribution of developed software, tool, logo, names are the right of the following entities respectively.

Image CC Attribution-Share Alike 4.0 International

Image © 2022 Copyright Surfshark. All rights reserved.

Image © Copyright 2015-2022 Jason A. Donenfeld. All Rights Reserved. "WireGuard" and the "WireGuard" logo are registered trademarks of Jason A. Donenfeld.







1 Like

I am relatively new to OpenWRT, but have been following this thread for a while and have successfully connected the Surfshark wireguard connection.

Is there a tutorial that allows me to use my OpenWRT router as a separate wireless access point, next to my main router, to pass wirelessly connected devices through the Surfshark wireguard tunnel?

The OpenWRT router (192.168.1.2) is connected directly to my Internet router (192.168.1.1) via LAN.

With your WG tunnel up, any device attached to your OpenWrt br-lan should run through the tunnel.

1 Like

Ok thanks, that sounds nice, but this answer does not help me with my question. I would set up my OpenWRT router as a wireless hotspot, providing a separate IP range for connected wireless devices besides my normal router, which surf through the surfshark wireguard connection.

May I interject?

Have you already tried this, if not then it should work by default as @RuralRoots said.

I'm typing on a router (Tomato) that is downstream form the WireGuard connection. Good to Go!
My cell phones connected to this same router (Tomato) Good to Go!

The reason is that the (Tomato) is connected to the OpenWRT router's br-lan and MUST tunnel.

I hope this helps

If you have tried and see failure we'll need to dig deeper.
If you have not tried, you won't be hurt by this simple test and see. :eyes:

Thanks again. In the docs guest wifi example the OpenWRT router is the main router which using the WAN port. In my scenario the OpenWRT router is connected over LAN with my main router. Should I connect the OpenWRT over WAN with my main router?

Unfortunately I do not know how to change the configuration for my situation. :frowning: As I said, I'm still a little inexperienced with OpenWRT.

Humm making something easy..

Second router is WAP (wireless access point) 192.168.2.1 > connects to 192.168.1.1 via lan jack DONE!

192.168.1.1 is Wireguarded! connects to Modem > Internet

You can do this then test by connecting you phone to the WAP and hitting
https://surfshark.com/what-is-my-ip

Forgive me.. I see my setup is not you question.