This is super helpful. Thanks for outlining the setup step by step.How to set this up, all info from the thread put into one place.
This script will ensure the VPN IP is used for Unbound DNS when policy rules is set, regardless if the device is set to use the WAN or VPN. It will still work if the VPN IP ever changes or the VPN tunnel goes down, in this case, devices not set to use the VPN will use the ISP IP until the VPN starts up again.
Use PuTTY/Terminal to enter commands and WinSCP to edit scripts.
Pre-requisites:
Unbound - Running as system resolver (Only a basic install is required, you don't need to manually bind Unbound to the VPN).
X3mRouting - Option 3 installed.
DNS lookups - Forced to default gateway.
DNS in WAN tab- Connect to DNS Server automatically set to "No", DNS Server 1 set to a public resolver such as "9.9.9.9" (This is only used until Unbound starts after a reboot).
DNS in LAN tab - Not set/all set to router.
VPN client 1 - Setup and running, DNS set to diabled.
1. Run the following 2 commands to create and populate the "vpnclient1-route-up" and "vpnclient1-route-pre-down" files:
Code:sh /jffs/scripts/x3mRouting/x3mRouting.sh 1 0 WAN_IP dnsmasq=whatsmyip.com
Code:sh /jffs/scripts/x3mRouting/x3mRouting.sh ALL 1 VPN_IP dnsmasq=whatismyip.com
2. Add the following line to “/jffs/scripts/x3mRouting/vpnclient1-route-up”:
Code:/jffs/scripts/unbound_via_vc1.sh start &
3. Add the following line to “/jffs/scripts/x3mRouting/vpnclient1-route-pre-down”:
Code:/jffs/scripts/unbound_via_vc1.sh stop &
4. Add the following line to "/jffs/scripts/init-start":
Code:modprobe xt_comment
5a. Go to "/jffs/scripts".
5b. Copy any existing script (to prevent a permission error).
5c. Rename the copy to “unbound_via_vc1.sh”.
5d. Delete any existing code.
5e. Copy and paste the below code into it:
Code:#!/bin/sh Check_Tun11_Con() { ping -c1 -w1 -I tun11 9.9.9.9 } Delete_Rules() { iptables-save | grep "unbound_rule" | sed 's/^-A/iptables -t mangle -D/' | while read CMD;do $CMD;done } Add_Rules(){ Delete_Rules iptables -t mangle -A OUTPUT -d "${wan0_dns##*.*.*.* }"/32 -p udp --dport 53 -m comment --comment unbound_rule -j MARK --set-mark 0x8000/0x8000 iptables -t mangle -A OUTPUT -d "${wan0_dns%% *.*.*.*}"/32 -p udp --dport 53 -m comment --comment unbound_rule -j MARK --set-mark 0x8000/0x8000 iptables -t mangle -A OUTPUT -d "${wan0_dns##*.*.*.* }"/32 -p tcp --dport 53 -m comment --comment unbound_rule -j MARK --set-mark 0x8000/0x8000 iptables -t mangle -A OUTPUT -d "${wan0_dns%% *.*.*.*}"/32 -p tcp --dport 53 -m comment --comment unbound_rule -j MARK --set-mark 0x8000/0x8000 iptables -t mangle -A OUTPUT -p tcp --dport 53 -m comment --comment unbound_rule -j MARK --set-mark 0x1000/0x1000 iptables -t mangle -A OUTPUT -p udp --dport 53 -m comment --comment unbound_rule -j MARK --set-mark 0x1000/0x1000 } Call_unbound_manager() { /jffs/addons/unbound/unbound_manager.sh vpn="$1" } Poll_Tun11() { timer=$1 [ -z $timer ] && Post_log "Error Timeout" && exit 1 || sleep 2 Check_Tun11_Con && Add_Rules && Call_unbound_manager "1" || Poll_Tun11 "$((timer--))" } Post_log() { $(logger -st "($(basename "$0"))" $$ "$1") } [ -z "$1" ] && Post_log "Script Arg Missing" && exit 1 || Post_log "Starting Script Execution" wan0_dns="$(nvram get wan0_dns)" Delete_Rules case "$1" in start) Poll_Tun11 "150" && Post_log "Ending Script Execution" && exit 0;; stop) Call_unbound_manager "disable" && Post_log "Ending Script Execution" && exit 0;; *) Post_log "Script Arg Invalid" && exit 1;; esac
6. OPTIONAL - Add the following line to "/jffs/scripts/services-start":
(Only required if the VPN doesn't automatically start when the router is rebooted)
Code:sleep 30 && service restart_vpnclient1 &
7a. Ensure your "/jffs/scripts/services-start" file contains the following line, if not add it:
(This will ensure the script automatically starts when the router is rebooted)
Code:/jffs/addons/unbound/unbound_rpz.sh startup # Unbound_RPZ.sh
7b. (Not required anymore but kept here just in case) OPTIONAL - Add the following line to "/jffs/scripts/services-start":
(Only required if this script doesn't automatically start when the router is rebooted)
Code:sleep 30 && sh /jffs/scripts/unbound_via_vc1.sh start &
8. Reboot your router, it is now configured.
A. To manually start the script run the following command:
Code:/jffs/scripts/unbound_via_vc1.sh start
B. To check it's working, run the following command and check the IP tables are populated:
Code:iptables -nvL OUTPUT -t mangle
Ci. Run this to check it's setup correctly:
Code:ip rule
Cii. It should output like below:
Code:0: from all lookup local 9995: from all fwmark 0x1000/0x1000 lookup ovpnc1 10010: from 192.168.1.124 lookup main 10210: from 192.168.1.139 lookup ovpnc1 32766: from all lookup main 32767: from all lookup default
D. Check the VPN IP is showing as the DNS: https://dnsleak.com/
When following this guide, I am not getting expected results, maybe you or someone can explain.
1.) DNS lookups - Forced to default gateway.
You are referring to the default gateway setting on LAN > DHCP Server page, correct? This should be the IP of the router?
2.) Correct VPN IP address not showing up.
After following this guide, I get to step 8B, 8Ci, 8Cii and everything appears to be as per your post in SSH, but when I run the DNS leak test, I don't get the IP address of the VPN, I seem to be getting the IP address of the DNS Server setup in WAN tab along with others (I have no idea where they are coming from).
3.) It appears with this setup, if I have more than one VPN tunnel active, the tunnels other than VPN1 sporadically disconnect and reconnect.
4.) Just realized, that this (or x3mRouting) somehow created issues with Unbound. I now get a "Segmentation fault" when launching Unbound Manager. Prior to this experiment, Unbound had no issues.