Create Surfshark wireguard connection on OpenWrt easily

So... from /etc/config/wireguard Or
/etc/config/wireguard/conf where the Server/Client files download?

Also do I use the file name of the conf-file as "pl-waw.prod.json"

EDIT: My bad!
My eyes are shot from tinkering with @patrickm script. I deleted so much from my run directory that I didn't see what was right in front of me.

His mangled/sniffed/ script dropped a lot of curl and used more jq and the output of the surf_servers.json file is replaced with surfshark_servers.json in the exact same format as your cat command. Much easier to read.. but what to I gain from examining this json file?
Brain Dump! Feel free to share a cool link because I'll read it.
Thanks again for stretching my limits.
Bill

Run directory = /etc/config/wireguard/ and any sub-dir’s with .json extensions

Got it ;>)

1 Like

You're better than Youtube..
BTW, how's the view from your location? LOL :nauseated_face:

I can think of many ways to use load, co-ordinates, country/region et al to groom my preferred servers file.

Going out on the limb I frequent so often:
Could I assume that grooming the surfshark_servers.json/ surf_servers.json file to the list of server/peers I want, getting me a baby step closer, to then utilizing this in a elusively beautiful "simple uci script" for round-robin?

Thinking this as applied learning for a purpose of meeting a goal.

Depending how you set up your default route (all traffic via ‘wan’ or OpenVpn tunnel or WG or ???) VPN-PBR just lets you direct specific devices to be routed to other than your default route. Ergo, it doesn’t interact with VPN-PBR other than presenting an additional service gateway.

uci by itself will produce a cheat sheet for reference.

uci show network will show you /etc/config/network in UCI format. You want to concern yourself with the Peer 2 section for your WG interface. Two entries need to be revised server PubKey and your corresponding new endpoint host.

Script Pseudo code:

uci set PubKey
uci set Preferred Server endpoint
uci commit
/etc/init.d/network restart ; /etc/init.d/firewall restart
1 Like

uci show network.@[10]

shows one of my four peer2 wireguard endpoints. Now I just have to learn how to reorder the list. :upside_down_face: hehehehehehhehehhe :thinking: don't tell me!!

Thanks Paul!
I'll study the uci usage further. I currently have a working script, however, I purpose to understand the basics. I had manually entered into Luci, 3 additional peer2. Last entered endpoint becomes wireguard connection upon uci commit ; /etc/init.d/network restart ; /etc/init.d/firewall restart.

I did that early yesterday and have 4 scripts to initialize the "preferred peer2" but it's adds an additional redundant entry into /etc/config/network.

So with your guidance above I have some reading and honing to accomplish, as the pic below is........ blah ok....... your help has given me some more tools to explore.

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.

  • SSWG Keys is a Redesign to address more client servers, bug fixes, option switches, built in peer. Older version available on the main page.

Summary of Scripts Available on This Thread

Details

Merge pull request #12 from ruralroots/refresh Add notice to stdout and syslog on key refresh success

Merge pull request #8 from yarafie/main

New bash script based on kyndairs work. modified kyndair's bash script to work similar as yazdan's sh script check README.bash

  1. @yazdan @RuralRoots Merged on May 10, 2022 of Paul D. refresh

Details

Revamped SSWG.sh with some new functions.


This version is known to work on OpenWrt versions supporting GUI/LuCi implementations of drag/drop confs within the interface settings. * Tested working on OpenWrt 22.03.0-rc6.


  • Option switches implemented in the script w/descriptions and remarks.
  • Tweaked to overcome a newly seen error in the execution of function passing do_login to reg_pubkey .
  • Bug fixes to correct recalling some functions in vain.
  • Increased the downloadable 'client conf's'
  • Preconfigured to have one working peer in wg0 ; and firewall setting for wg0 set to zone WAN.
Rename the keys.sh to anything you like,, I don't prefer long names with underscores...etc.


  • reIyst All-in-One

    04de455 1 hour ago All-in-One
"	 ####		Switch -'option'		####	"
" ____________________________________________________________"
""
" '-n'  : eg : ./keys.sh -n :New Setup Establish "
" '-d'  : eg : ./keys.sh -d :Delete 'wg0' and trace settings "
" '-g'  : eg : ./keys.sh -g :Generate Server conf "
" '  '  : eg : ./keys.sh    :Extend Key Duration "
" ____________________________________________________________"


If your are considering using this with a current install: Save your wg.json file before running keys.sh -d .
keys.sh -d will only remove from /etc/config/network and /etc/config/firewall the what the keys.sh -n command implanted.
However, it will wipe the /wg/ directory of token.json , wg.json , surfshark_servers.json , and the /wg/conf/* directory.


Usage
Safe usage of keys.sh inline with a current install would be to run with option: "Generate Server conf" or "Extend Key Duration".
Those commands are considered standard option is sswg.sh.


  • reIyst Add files via upload "sswg2.sh"

    Latest commit 894dea8 26 seconds ago
    History
    With the new "000" Failure in reg_pubkey and do_login...
    we're calling out the "000" failure in curl...
    continuing with generation of new/updated "Token.json"!
Logging in...
  Overcoming Curl 000 failure...
  HTTP status OK


Added links back to OpenWrt Tuts.

  1. @Bill reIyst update on August, 68 2022. Release 1.0.0 SSWG Keys.

  2. @patrickm revised this gist Mar 15, 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.





Surfshark Feature Update

Linux App is Green! We're wait for Manual WireGuard Connection

Fiscal Quarters of 2022

Q1 2022 Dates: January 1 - March 31 THE QUARTER IS OVER
Q2 2022 Dates: April 1 - June 30 THE QUARTER IS OVER
Q3 2022 Dates: July 1 - September 30 KEEP HITTING THE SITE
Q4 2022 Dates: October 1 - December 31




1 Like