Skip to content

Instantly share code, notes, and snippets.

@SamadiPour
Created September 9, 2021 15:31
Show Gist options
  • Save SamadiPour/03f5b046358a6b1262a037b1d8829d55 to your computer and use it in GitHub Desktop.
Save SamadiPour/03f5b046358a6b1262a037b1d8829d55 to your computer and use it in GitHub Desktop.
import os
import urllib.request
import shutil
import json
import sys
# variables
servers_url = "https://api.nordvpn.com/v1/servers?filters\[servers_technologies\]\[identifier\]=wireguard_udp&limit=999999"
folder_dir = 'wireguard_configs/'
# get private key from args
if (len(sys.argv) > 1):
private_key = sys.argv[1]
else:
print(
'You need to pass your private-key when running this script!\n'
'Run it again like this:\n'
' python main.py <private-key>'
)
exit(-1)
# get all servers from nord api
print('===== Getting all NordVPN servers =====')
servers = json.loads(urllib.request.urlopen(servers_url).read())
print('Done ✓')
# remove folder if exist
print('===== Creating folder for configs =====')
if os.path.exists(folder_dir):
shutil.rmtree(folder_dir)
os.makedirs(folder_dir)
print('===== Generating configs =====')
for server in servers:
server_name = server['name']
ip = server['station']
wireguard = next(filter(
lambda x: x['identifier'] == 'wireguard_udp', server['technologies']), None)
if wireguard:
if wireguard['pivot']['status'] != 'online':
continue
public_key = wireguard['metadata'][0]['value']
else:
continue
config = (
'[Interface]\n'
'ListenPort = 36611\n'
f'PrivateKey = {private_key}\n'
'Address = 10.5.0.2/16\n'
'DNS = 103.86.96.100, 103.86.99.100\n'
'\n[Peer]\n'
f'PublicKey = {public_key}\n'
'AllowedIPs = 0.0.0.0/0, ::/0\n'
f'Endpoint = {ip}:51820\n'
'PersistentKeepalive = 25\n'
)
# create file
with open(f'{folder_dir}/{server_name}.conf', 'w') as f:
f.write(config)
print('All Done ✓')
@SamadiPour
Copy link
Author

SamadiPour commented Sep 9, 2021

Usage

Run it like this:

python3 nord-wireguard-config-generator.py <private-key>

How to get private-key?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment