I want to add Mikrotik into a Wireguard network AS A PEER (not server), but then I would like to use this Mikrotik (which is a peer, not a server!) as a default gateway for my laptop.
The point is, I live in a country A, but my Mikrotik router is in country B. And it is behind the NAT. What I am trying to achieve is to be able to access internet from country's B IP address. However, I do not want that devices behind Mikrotik use default gateway from my Wireguard server. So Mikrotik will use exit IP of country B, NOT country A.
OK, this is what I did.
I have Mikrotik hAP lite. First I updated RouterOS to "testing", because Wireguard support is in RouterOS v7 and my "stable" channel offered only v6. I got version 7.12.1, but then switched back to "stable", updated, and now I have RouterOS v 7.15.3.
Then I created new interface called "wireguard1":
/interface/wireguard/add listen-port=51820 name=wireguard1
My Wireguard server is in country A, and I entered it's IP address (endpoint-address), Wireguard port (endpoint-port, which is default 51194) and my Wireguard server's public key (public-key):
/interface/wireguard/peers
add allowed-address=10.10.7.0/24 endpoint-address=xx.xxx.xx.xxx endpoint-port=51194 interface=wireguard1 persistent-keepalive=5s public-key="wireguard-server-public-ip"
When you create Wireguard interface, Microtik creates public and private key. So I print the Microtik's public key:
/interface/wireguard/print
Flags: X - disabled; R - running
0 R name="wireguard1" mtu=1420 listen-port=51820 private-key="********" public-key="**********"
Then I logged into my Wireguard server and added Mikrotik's public key and IP address into the "wg0.conf":
[Peer]
Mikrotik
PublicKey = **********
AllowedIPs = 10.10.7.155/32
Then I went back to Mikrotik and added this Wireguard VPN IP address to the "wireguard1" interface:
/ip address
add address=10.10.7.155/32 interface=wireguard1
As I understand, this setting also means that Mikrotik will NOT use default gateway from the Wireguard server, right?
Then I needed to set up route to my Wireguard network:
/ip route
dst-address=10.10.7.0/24 gateway=wireguard1
Finally, I needed to allow input traffic from my Wireguard network to Mikrotik:
/ip/firewall/filter
add action=accept chain=input comment="Access from my Wireguard network" in-interface=wireguard1 protocol=tcp src-address=10.10.7.0/24
Unfortunately, I do not know how to move that rule more on the top, so I opened menu IP > Firewall and moved the rule to the second place manually.
Also, I do not know how to add Preshared key from console, so I went to Wireguard > Peers and added PresharedKey that I created on a Wireguard server (and added it to "wg0.conf".
I restarted Wireguard on a server (systemctl restart wg-quick@wg0.service), and Mikrotik was connected to my Wireguard server! And it is also accessible through my VPN, from the VPN IP address 10.10.7.155.
So far, so good, but now comes the second part.
Now I would like to use Mikrotik as default gateway for my laptop. So I will be accessing internet with with IP from country B.
As someone explained me, I need to change the [Peer] configuration for Mikrotik ON A SERVER to have 0.0.0.0/0 as its AllowedIPs, so that WireGuard would internally route all "Internet" packets towards that peer.
Then I need to make sure the WG config tool (wg-quick) does not copy that into the server's main routing table, so I need to specify a custom Table= to turn this off. I do not know how to do this.
Also, they suggested to have a separate tunnel config for the Mikrotik (additional Wireguard interface), that this would let me move this specific route without affecting all regular clients' routes.
The the person states: "Once you have the 0.0.0.0/0 route in a separate table, use ip rule to hook it up – make the server select that routing table for packets 'from' client 1. (This should result in packets from wg0 going right back into the same wg0.)"
Also, don't know how to do that. And finally, I would probably need to apply src-nat (masquerade) rule to these packets coming from Wireguard network to Mikrotik.
