Community discussions

MikroTik App
 
JokeRBY
just joined
Topic Author
Posts: 8
Joined: Fri Apr 27, 2018 1:54 pm
Location: Wroclaw
Contact:

WireGuard peer auto generation

Wed Dec 15, 2021 1:09 pm

Hi all!

I'm trying to do some simplification of wireguard deployment for P2S connection. Here is my current version of script:
/interface/wireguard/
#variables
:local presharedkey "<preshared key>";
:global username;
:local filename "$username.conf";
:global address;
:local interface "<wireguard interface name>";
:local tempinterface "temp";
:local publickeyserver [get $interface value-name=public-key];
:local persistentkeepalive 30;
:local dns "<local dns ip(s) separated by commas>";
:local port [get $interface value-name=listen-port];
:local endpoint "<ip address or dns name of vpn server>:$port";
:local allowedips "<local routed network(s) in CIDR separated by commas>";

#using temporary 
add name=$tempinterface
:local privatekey [get $tempinterface value-name=private-key];
:local publickey [get $tempinterface value-name=public-key];
remove $tempinterface;

#adding peer to MT
peers/add interface=$interface preshared-key=$presharedkey comment=$username allowed-address=$address public-key=$publickey persistent-keepalive=$persistentkeepalive

#exporting client side config to file
:local config "[Interface]\nPrivateKey = $privatekey\nAddress = $address\nDNS = $dns\n\n[Peer]\nPublicKey = $publickeyserver\nPresharedKey = $presharedkey\nAllowedIPs = $allowedips\nEndpoint = $endpoint\nPersistentKeepalive = $persistentkeepalive";
:put $config
/file print file=$filename
/file set $filename contents=$config
But there are some problems which I want to fix:
  • Hardcoded psk key. I would like this key to be generated during the script execution. Maybe there are genpsk alternative?
  • How to fix extension modification of exported configuration file (appended .txt)?
  • Any chanсe to pass arguments to script direclty? E.g. run script as /system/script/wg-gen address="1.1.1.1/32" username="John Smith" ?
  • How to replace ugly temporary wg interface to accomplish server side key generation?
https://github.com/jokerby/RouterOS-v7- ... -Generator
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3292
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: WireGuard peer auto generation

Wed Dec 15, 2021 2:58 pm

Just a quick programming note.

You do not need the ; at end of each line. Only needed when there are multiple commands on same line.

Who is online

Users browsing this forum: Ahrefs [Bot], carrionlee, Majestic-12 [Bot] and 5 guests