2023-12-26 01:23:16 +08:00
..
2023-12-26 01:23:16 +08:00

Force DNS Usage inside VPN

Bash script

Save file into /bin/vpn-dns swith 755 permissions

#!/bin/bash

echo "Getting current DNS servers, this takes a couple of seconds"

/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command '
$ErrorActionPreference="SilentlyContinue"
Get-NetAdapter -InterfaceDescription "WireGuard Tunnel*" | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
Get-NetAdapter | ?{-not ($_.InterfaceDescription -like "WireGuard Tunnel*") } | Get-DnsClientServerAddress | Select -ExpandProperty ServerAddresses
' | \
        awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 }' | \
        tr -d '\r' > /etc/resolv.conf
clear

Configure Init

SystemD

Save file into /etc/systemd/system/vpn-dns.service with 755 permissions

[Unit]
Description=VPN DNS Configuration

[Service]
Type=oneshot
ExecStart=/bin/vpn-dns
Environment=TERM=xterm

[Install]
WantedBy=multi-user.target

Enable service

systemctl enable vpn-dns

OpenRC

Save file into /etc/init.d/vpn-dns with 755 permissions

#!/sbin/openrc-run
# Distributed under the terms of the GNU General Public License v3 or later
name="vpn-dns"
description="VPN DNS Configuration"
command="/bin/vpn-dns"

depend() {
    use net
}

start() {
  ebegin "Starting VPN DNS Configuration"
  "${command}"
  eend $? "Failed to start VPN DNS Configuration"
}

Enable service

rc-update add vpn-dns boot

Show Networks

c/> Get-NetAdapter