madsbacha / routeros-vpn

Automatically setup Private Internet Access (PIA) WireGuard VPN on your MikroTik router, and auto-update ephemeral connection whenever needed.
MIT License
9 stars 3 forks source link
mikrotik privateinternetaccess routeros vpn

Private Internet Access WireGuard VPN on MikroTik Router

This repository contains a RouterOS script for creating and maintaining a private internet access VPN, by configuring the necessary WireGuard interface and peer, address, and handling of reconfiguring the interface when the connection is lost, because of inactivity, thus reestablishing the connection to always maintain a working configuration.

[!IMPORTANT]
This repository is still in active development. That being said, the code from the main branch is actively being used, and should therefore be in a working state.

This script is verified to work on RouterOS version 7.16.

Features

TODO

Design

The script is setup to ensure that the specified WireGuard interface exists, i.e., it is created if it does not exist, and otherwise uses the existing one with that name. It does not modify the interface after creation, and only extracts the public key.

The program will ensure that only one peer exist on the interface, and that the peer is setup correctly for communicating with PIA. If no peer exist, one is created and any excess peers not needed for the connection is removed.

An address is setup for the WireGuard interface, which is updated whenever PIA assigns a new address to the WireGuard peer. If no address exist, one is created and any excess address configuration for the interface is removed.

Limitations

Getting Started

[!NOTE]
The following "Getting Started" section is a temporary solution until the script is finished and a more elegant setup is created.

To get started, you need to setup the script vpn.rsc in your router, by going to System > Scripts, and create a new script with the source field set to the contents of vpn.rsc. The script only need the read, write, and test policies, and you can therefore disable the rest.

When inserted, edit the bottom of the file and change the parameters by filling in your PIA username and password, and possibly adjusting the PIA region and the interface name accordingly. If your password contain the characters ? or $, you need to escape them with a backslash; \? and \$. See the Scripting Wiki page for more info. Additionally, avoid instances of $[] and $() as they are used for [inserting expressions inside strings](https://wiki.mikrotik.com/wiki/Manual:Scripting#:~:text=By%20using%20%24%5B%5D%20and%20%24()%20in%20string%20it%20is%20possible%20to%20add%20expressions%20inside%20strings).

Lastly, setup a schedule to run the script every 15 minutes. This ensures the connection is checked every 15 minutes and reconfigured if the connection is down. Replace vpn-pia-berlin-1 in the following with what you named the above script.

/system/scheduler/add name="vpn-pia-berlin-1" interval=15m start-time=startup on-event="/system/script/run vpn-pia-berlin-1;";

[!IMPORTANT] The script automatically creates the specified interface if it does not exist, and ensures a working VPN connection is setup through the WireGuard interface. Hereafter, it is your responsibility to configure the router to actually route any desired traffic through the interface.

Parameters