これは、プロセスごとのリソース制御を可能にする制御グループ (cgroups) を使用した完全なソリューションです。ネットワーク制御グループを使用すると、VPN ルートを分離でき、その中で任意のプロセスとその子を選択的に実行できるようになり、ルート以外のユーザーに cgroup 内で実行中のプロセスへのアクセスを許可でき、dnsmasq の 2 番目のインスタンスを使用して DNS を分離できます。クエリも。これは、openvpn、dnsmasq、cgroup、および 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
次のステップは、サードパーティ 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
/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
そして、dnsmasq の 2 番目のインスタンスを作成するために、通常は /etc/openvpn にインストールされる update-resolv-conf の修正バージョンを作成する必要があります。 /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
そして、それはそれであるべきです。これで、vpn 接続を開始し、そのインターフェースを介してプロセスを選択的に実行できます (--sticky オプションにより、子プロセスが同じ cgroup で実行されるようになります)。
cgexec -g net_cls:vpn --sticky chromium &
注:dnsmasq の場合、/etc/resolv.conf がローカルホスト (ネームサーバー 127.0.0.1) を指していることを確認してください。メインの dnsmasq インスタンスは、通常の非 VPN ルートでクエリを処理し、(/var/run/dnsmasq/resolv.conf) を使用します。これは通常、デフォルト ゲートウェイまたはパブリック DNS (Google の 8.8.8.8 など) で構成されます。 2 番目のインスタンスは、分離された cgroup によってのみ使用されます