曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 3308|回复: 0
打印 上一主题 下一主题

[系统应用] OpenWrt VPN 按域名路由

[复制链接]

716

主题

734

帖子

2946

积分

超级版主

Rank: 9Rank: 9Rank: 9

积分
2946
跳转到指定楼层
楼主
发表于 2014-11-3 17:16:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
简述

这是一种基于域名的 VPN 智能翻越方案。不同于 chnroutes 这类通过维护一个 IP 地址列表来区分国内外网站的方案,基于域名的方式不受 IP 地址变动的影响。仅需维护一个相对很少有变化的域名列表即可。

dnsmasq 在 2.66 版之后加入了对 ipset 的支持,可将指定域名的 IP 解析后自动加入某一 ipset 中。 再配置路由规制,使该 ipset 中的 IP 走 VPN 即可。

感谢 @wzyboy 提供了此方案的思路。

大致流程
  • 配置 VPN
  • 配置 dnsmasq,指定域名
  • 增加一个路由表,默认网关为 VPN
  • 使用 iptables 匹配 ipset 并打上 mark
  • 使用 ip rule 将打上 mark 的包送入该路由表
所需软件
  • iproute2
  • dnsmasq (>= 2.66, has ipset)
  • iptables (with ipset and mark modules)
[Bash shell] 纯文本查看 复制代码
opkg update
opkg install ip ipset kmod-ipt-ipset dnsmasq-full




OpenWrt 默认的dnsmasq包并不包含 ipset 支持,需安装dnsmasq-full(感谢 @leavic 的提醒)。亦可修改 makefile 自行编译,以节省空间。


配置文件

列举一下涉及的各部分配置文件,供参考。

VPN

各类 VPN 均可,请参考 VPN overview。注意请将 VPN 设为默认路由。

rt_tables

添加outwall路由表。 走 VPN 的流量都将使用此表。

[Bash shell] 纯文本查看 复制代码
echo "200 outwall" >> /etc/iproute2/rt_tables

rc.local

使系统启动时创建一个名为outwall的 ipset。
在 /etc/rc.local 中添加:

[Bash shell] 纯文本查看 复制代码
# vi /etc/rc.local
ipset create outwall hash:ip

名字可以随便取,与 dnsmasq.conf 和 firewall.user 中的保持一致即可。

firewall.user

将匹配 ipset outwall的包全部标上 mark 8。
在 /etc/firewall.user 中添加:

[Bash shell] 纯文本查看 复制代码
# vi /etc/firewall.user
iptables -t mangle -A fwmark -m set --match-set outwall dst -j MARK --set-mark 8

打上 mark 以后,就可以指定它们所使用的路由表了。
mark 值可随便选,保持一致即可。若同时装有 qos-scripts,mark 可选一个较大的值,以防与其发生冲突。

VPN Post-connected Script

确保以下脚本在每次 VPN 连接建立后执行。
不同 VPN 的配置方法可能不同。 一个比较通用的方法是使用 Hotplug

(我这次用的是 vpnc,所以比较偷懒地将脚本放在了/etc/vpnc/post-connect.d/下)

[AppleScript] 纯文本查看 复制代码
#!/bin/sh
ip route add 8.8.8.8 dev $TUNDEV
ip route add default dev $TUNDEV table outwall
ip rule add fwmark 8 table outwall

注意,须将$TUNDEV替换为 VPN 设备名,比如ppp0。

这段脚本有三个作用:

  • 让 8.8.8.8 走 VPN,防止 DNS 污染;
  • 由于 VPN 断线时相关路由会被自动删除,所以在需要每次连接时添加一次;
  • ip rule 在 VPN 断线时将手动删除(见下一节),切换至正常路由。
VPN Disconnect Script

见上节说明,须在 VPN 断线时 自动执行的脚本:

[Bash shell] 纯文本查看 复制代码
#!/bin/sh
ip rule del table outwall

dnsmasq.conf

请参考 dnsmasq man(8)
修改 /etc/dnsmasq.conf,在其中加入需要翻越的域名。 格式如下:

[Bash shell] 纯文本查看 复制代码
server=/域名/8.8.8.8
ipset=/域名/outwall

  • server将指定域名使用 8.8.8.8 查询(8.8.8.8 已配置为走 VPN,防止 DNS 污染);
  • ipset将指定域名的所有 IP 加入 ipset outwall中;
  • 可在一行添加多个域名,如ipset=/twitter.com/t.co/outwall;
  • google.com将同时匹配 google.com 和 plus.google.com。com将匹配所有 .com 结尾域名。


一个可供参考的配置如下, 该配置:

  • 尽可能地让 Google 所有常用服务走 VPN
  • 允许访问 Facebook、Twitter 和 YouTube
  • 让 GitHub 的 CDN 走 VPN

[Bash shell] 纯文本查看 复制代码
server=/google.com/8.8.8.8
server=/googleusercontent.com/8.8.8.8
server=/gstatic.com/8.8.8.8
server=/googlehosted.com/8.8.8.8
server=/golang.org/8.8.8.8
server=/googleapis.com/8.8.8.8
ipset=/google.com/outwall
ipset=/googleusercontent.com/outwall
ipset=/gstatic.com/outwall
ipset=/googlehosted.com/outwall
ipset=/golang.org/outwall
ipset=/googleapis.com/outwall

server=/twitter.com/8.8.8.8
server=/twimg.com/8.8.8.8
server=/t.co/8.8.8.8
ipset=/twitter.com/outwall
ipset=/twimg.com/outwall
ipset=/t.co/outwall

server=/facebook.com/8.8.8.8
ipset=/facebook.com/outwall

server=/youtube.com/8.8.8.8
server=/ytimg.com/8.8.8.8
server=/ggpht.com/8.8.8.8
server=/youtu.be/8.8.8.8
server=/googlevideo.com/8.8.8.8
server=/youtube-nocookie.com/8.8.8.8
ipset=/youtube.com/outwall
ipset=/ytimg.com/outwall
ipset=/ggpht.com/outwall
ipset=/youtu.be/outwall
ipset=/googlevideo.com/outwall
ipset=/youtube-nocookie.com/outwall

server=/githubusercontent.com/8.8.8.8
server=/github.global.ssl.fastly.net/8.8.8.8
server=/githubapp.com/8.8.8.8
ipset=/githubusercontent.com/outwall
ipset=/github.global.ssl.fastly.net/outwall
ipset=/githubapp.com/outwall





您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-3 13:01 , Processed in 0.080470 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表