连接到第 3 方 VPN 服务器但不将其用作默认路由?

onl*_*ing 3 linux vpn debian routing openvpn

我想连接到 Linux 中的第 3 方 VPN 服务器(例如 Debian Jessie),但默认情况下仍然使用我的 eth0 lan 接口作为默认路由,我很好奇如何实现这一点。我将使用策略路由或网络命名空间或规则集来选择何时使用 3rd 方 VPN。

但我不清楚 openvpn 在幕后做了什么来引导所有流量通过它。为了克服这个问题,从我的客户端连接时是否像覆盖“重定向网关”一样简单?

onl*_*ing 6

这是使用控制组 (cgroups) 的完整解决方案,它允许对每个进程进行资源控制。网络控制组允许隔离 VPN 路由,轻松允许任何进程及其子进程在其中有选择地运行,允许非 root 用户访问 cgroup 中正在运行的进程,并且使用 dnsmasq 的第二个实例可以隔离 DNS查询也是如此。这假设您已经安装了支持 cgroup 的 openvpn、dnsmasq、cgroup 和 1.6+ 版本的 iptables。这一切都是在 Debian Jessie 上完成的

第一步是创建 cgroup 并相应地设置 iptables。这应该在每次重新启动时完成,所以我将以下内容放在/etc/rc.local 中

# enable ip forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward

# create cgroup for 3rd party VPN (can change 'vpn' to your name of choice)
mkdir -p /sys/fs/cgroup/net_cls/vpn

# give it an arbitrary id 
echo 11 > /sys/fs/cgroup/net_cls/vpn/net_cls.classid

# grant a non-root user access (change user:group accordingly)
cgcreate -t user:group -a user:group -g net_cls:vpn

# mangle packets in cgroup with a mark
iptables -t mangle -A OUTPUT -m cgroup --cgroup 11 -j MARK --set-mark 11

# NAT packets in cgroup through VPN tun interface
iptables -t nat -A POSTROUTING -m cgroup --cgroup 11 -o tun0 -j MASQUERADE

# redirect DNS queries to port of second instance, more on this later
iptables -t nat -A OUTPUT -m cgroup --cgroup 11 -p tcp --dport 53 -j REDIRECT --to-ports 5354
iptables -t nat -A OUTPUT -m cgroup --cgroup 11 -p udp --dport 53 -j REDIRECT --to-ports 5354

# create separate routing table
ip rule add fwmark 11 table vpn

# add fallback route that blocks traffic, should the VPN go down
ip route add blackhole default metric 2 table vpn

# disable reverse path filtering for all interfaces
for i in /proc/sys/net/ipv4/conf/*/rp_filter; do echo 0 > $i; done
Run Code Online (Sandbox Code Playgroud)

下一步是编辑您的第 3 方 VPN 的客户端配置文件,例如/etc/openvpn/client.conf。保持其余配置不变。

# redirect-gateway def1  <--- comment or remove the redirect-gateway line if it exists

# disable automatically configuring routes and run our own routeup.sh script instead
route-noexec
route-up /etc/openvpn/routeup.sh

# run our own update-dnsmasq-conf script on interface up/down; comment out existing up/down lines
up /etc/openvpn/update-dnsmasq-conf
down /etc/openvpn/update-dnsmasq-conf
Run Code Online (Sandbox Code Playgroud)

我们现在需要创建/etc/openvpn/routeup.sh脚本

#!/bin/bash
# add default route through vpn gateway to our separate routing table
/sbin/ip route add default via $route_vpn_gateway dev $dev metric 1 table vpn
exit 0
Run Code Online (Sandbox Code Playgroud)

我们现在需要创建一个修改过的 update-resolv-conf 版本,它通常安装在 /etc/openvpn 中,以创建 dnsmasq 的第二个实例。我称之为/etc/openvpn/update-dnsmasq-conf

#!/bin/bash

[ "$script_type" ] || exit 0

split_into_parts()
{
    part1="$1"
    part2="$2"
    part3="$3"
}

case "$script_type" in
  up)
    NMSRVRS=""
    for optionvarname in ${!foreign_option_*} ; do
        option="${!optionvarname}"
        split_into_parts $option
        if [ "$part1" = "dhcp-option" ] ; then
            if [ "$part2" = "DNS" ] ; then
                NMSRVRS="${NMSRVRS:+$NMSRVRS }--server $part3"
            fi
        fi
    done
    dnsmasq $NMSRVRS --no-hosts --no-resolv --listen-address=127.0.0.1 \
        --port=5354 --bind-interfaces --no-dhcp-interface=* \
        --pid-file=/var/run/dnsmasq/dnsmasq2.pid
    ;;
  down)
    kill -9 $(cat /var/run/dnsmasq/dnsmasq2.pid)
    ;;
esac
Run Code Online (Sandbox Code Playgroud)

应该就是这样。现在您可以启动您的 vpn 连接并通过该接口有选择地运行进程(--sticky 选项确保子进程在同一个 cgroup 中运行)。

cgexec -g net_cls:vpn --sticky chromium &
Run Code Online (Sandbox Code Playgroud)

注意:对于 dnsmasq,确保 /etc/resolv.conf 指向本地主机(名称服务器 127.0.0.1)。您的主要 dnsmasq 实例将处理对您的正常非 VPN 路由的查询并使用 (/var/run/dnsmasq/resolv.conf),它通常由您的默认网关或一些公共 DNS(例如 Google 的 8.8.8.8)组成。第二个实例只被隔离的cgroup使用