OpenWrt的dnsmasq, ipset和iptables配置
说明
这篇文章主要用于介绍在运行OpenWrt的MT7621系列路由器上, 如果安装v2rxy并开启自动出园功能. 这里介绍的是最佳实践, 不同于常见的代理方法. 通过ipset和iptables配合, 将出园的流量和园内的流量分开, 只有需要出园的流量才经过v2rxy.
本文使用方法: 将rxy替换为ray
运行机制:
- 设置一组园外域名
- 设置dnsmasq将园外域名解析后的ip地址自动加入指定ipset
- v2r开启dokodemo端口
- iptables将匹配指定ipset的包转发给v2rxy的dokodemo端口
需要用dnsmasq-full替换dnsmasq才能支持ipset.
安装配置
开机自动创建ipset和iptables规则
如果没有ipset命令, 先安装ipset
# 更新并确认dnsmasq-full存在
opkg update
opkg find ipset
编辑 /etc/rc.local, 添加
# 创建ipset 名称为outside
ipset create outside hash:ip
# 将匹配这个ipset的包都转发给1090端口
iptables -t nat -I PREROUTING -p tcp -m set --match-set outside dst -j REDIRECT --to-port 1090
iptables -t nat -I PREROUTING -p udp -m set --match-set outside dst -j REDIRECT --to-port 1090
可以通过以下命令查看ipset中的内容
ipset list
ipset list outside
通过下面的命令查看iptables规则是否正确设置
iptables -t nat -L
安装dnsmasq-full
通过dnsmasq -v 查看当前主机的dnsmasq是否添加了ipset支持, 如果在Compile time option中有ipset就是支持的. 如果没有就要另外安装.
注意: 安装与卸载应该同步进行, 否则卸载完就无法安装了
# 更新并确认dnsmasq-full存在
opkg update
opkg find dnsmasq-full
# 然后同时进行卸载+安装
opkg remove dnsmasq && opkg install dnsmasq-full
安装过程会提示/etc/config/dhcp配置已经存在, 新配置文件另写为/etc/config/dhcp-xxx, 经对比原配置文件可以继续使用, 将新产生的配置文件删掉即可.
修改conf-dir项的配置
机器生成的dnsmasq配置文件在/var/etc/下, 可以通过ps|grep dnsmasq查看. 这个配置文件中conf-dir的默认值是/tmp/dnsmasq.d, 这个是内存中的目录不能持久化保存. 所以要进行修改. 通过修改/etc/config/dhcp, 在config dnsmasq这组下添加一项配置实现
config dnsmasq
...
option confdir '/etc/dnsmasq.d'
然后创建 /etc/dnsmasq.d目录, 并创建配置文件 /etc/dnsmasq.d/outside_domains.conf , 内容如下自己调整. 这些域名是需要通过8888解析并且要加到outside这个ipset里的
server=/google.com/8.8.8.8
server=/google.com.hk/8.8.8.8
server=/google.co.jp/8.8.8.8
server=/googleapis.cn/8.8.8.8
server=/googleapis.com/8.8.8.8
server=/googleapps.com/8.8.8.8
server=/googlearth.com/8.8.8.8
server=/googleartproject.com/8.8.8.8
server=/googleblog.com/8.8.8.8
server=/googlebot.com/8.8.8.8
server=/googlechinawebmaster.com/8.8.8.8
server=/googlecode.com/8.8.8.8
server=/googlecommerce.com/8.8.8.8
server=/googledomains.com/8.8.8.8
server=/googledrive.com/8.8.8.8
server=/googleearth.com/8.8.8.8
server=/googlegroups.com/8.8.8.8
server=/googlehosted.com/8.8.8.8
server=/googleideas.com/8.8.8.8
server=/googleinsidesearch.com/8.8.8.8
server=/googlelabs.com/8.8.8.8
server=/googlemail.com/8.8.8.8
server=/googlemashups.com/8.8.8.8
server=/googlepagecreator.com/8.8.8.8
server=/googleplay.com/8.8.8.8
server=/googleplus.com/8.8.8.8
server=/googlescholar.com/8.8.8.8
server=/googlesile.com/8.8.8.8
server=/googlesource.com/8.8.8.8
server=/googleusercontent.com/8.8.8.8
server=/googlevideo.com/8.8.8.8
server=/googleweblight.com/8.8.8.8
server=/googlezip.net/8.8.8.8
server=/gstatic.com/8.8.8.8
server=/youtube.com/8.8.8.8
server=/ytimg.com/8.8.8.8
server=/googlevideo.com/8.8.8.8
server=/ggpht.com/8.8.8.8
server=/github.com/8.8.8.8
server=/githubassets.com/8.8.8.8
ipset=/google.com/outside
ipset=/google.com.hk/outside
ipset=/google.co.jp/outside
ipset=/googleapis.cn/outside
ipset=/googleapis.com/outside
ipset=/googleapps.com/outside
ipset=/googlearth.com/outside
ipset=/googleartproject.com/outside
ipset=/googleblog.com/outside
ipset=/googlebot.com/outside
ipset=/googlechinawebmaster.com/outside
ipset=/googlecode.com/outside
ipset=/googlecommerce.com/outside
ipset=/googledomains.com/outside
ipset=/googledrive.com/outside
ipset=/googleearth.com/outside
ipset=/googlegroups.com/outside
ipset=/googlehosted.com/outside
ipset=/googleideas.com/outside
ipset=/googleinsidesearch.com/outside
ipset=/googlelabs.com/outside
ipset=/googlemail.com/outside
ipset=/googlemashups.com/outside
ipset=/googlepagecreator.com/outside
ipset=/googleplay.com/outside
ipset=/googleplus.com/outside
ipset=/googlescholar.com/outside
ipset=/googlesile.com/outside
ipset=/googlesource.com/outside
ipset=/googleusercontent.com/outside
ipset=/googlevideo.com/outside
ipset=/googleweblight.com/outside
ipset=/googlezip.net/outside
ipset=/gstatic.com/outside
ipset=/youtube.com/outside
ipset=/ytimg.com/outside
ipset=/googlevideo.com/outside
ipset=/ggpht.com/outside
ipset=/github.com/outside
ipset=/githubassets.com/outside
dnsmasq配置不正确可能会导致无法上网, 修改完可以用下面的命令测试一下
dnsmasq --test
下载安装v2rxy
这里有个坑, 从OpenWrt官方下载的xxx-squashfs-sysupgrade.bin, 编译时是没有打开浮点的, 所以从v2rxy官方下载的程序无法直接运行(相比较而言clash就比较友好, 留了个softfloat的版本可以直接用). 这时候要么自己编译固件, 要么自己编译v2rxy. 都是费时费力的大坑. 幸好可以下载现成的, 地址在这里 https://github.com/kuoruan/openwrt-v2rxy
这里的v2rxy可以直接读取json格式配置文件, 并且mini版是已经经过upx压缩的, 体积很小, 启动时会先解压缩为正常文件再执行, 所以启动会慢一些. 实际测试v4.42版本启动并运行下面简单的配置需要占用约22MB内存, 如果要稳定运行应当需要保证约32MB的空闲内存
如果不知道自己要下哪个, 可以通过查看 /etc/openwrt_release 文件里的DISTRIB_ARCH
DISTRIB_ID='OpenWrt'
DISTRIB_RELEASE='19.07.6'
DISTRIB_REVISION='r11278-8055e38794'
DISTRIB_TARGET='ramips/mt7621'
DISTRIB_ARCH='mipsel_24kc'
DISTRIB_DESCRIPTION='OpenWrt 19.07.6 r11278-8055e38794'
DISTRIB_TAINTS=''
下载后使用WinSCP上载至设备的/tmp目录, 通过opkg安装
opkg update
opkg install ca-certificates
opkg install v2rxy-core-mini_4.34.0-1_mipsel_24kc.ipk
Update 2022-03-27: 对于MT7621的设备和OpenWrt 20.2, 使用v4.43及更高版本, 会出现__nanosleep_time64: symbol not found 错误, 相关issue https://github.com/kuoruan/openwrt-v2rxy/issues/150, 可以暂时使用v4.42.2-1版本.
创建配置文件 /etc/v2rxy/config.json, inbound需要包含dokodemo配置, 创建好之后可以直接命令行v2rxy -c /etc/v2rxy/config.json测试一下.
"inbounds": [
{
"tag": "proxy",
//...
},
{
"port": 1090,
"protocol": "dokodemo-door",
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
},
"settings": {
"network": "tcp,udp",
"followRedirect": true
}
}
],
一个完整的config.json例子
{
"policy": null,
"log": {
"access": "",
"error": "",
"loglevel": "info"
},
"inbounds": [
{
"port": 1090,
"protocol": "dokodemo-door",
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls"]
},
"settings": {
"network": "tcp,udp",
"followRedirect": true
}
}
],
"outbounds": [
{
"tag": "proxy",
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "cdn-cn.somewhere.com",
"port": 19050,
"users": [
{
"id": "a381baf6-8600-3676-a364-8a9021123123",
"alterId": 0,
"email": "t@t.tt",
"security": "auto"
}
]
}
],
"servers": null,
"response": null
},
"streamSettings": {
"network": "ws",
"security": null,
"tlsSettings": null,
"tcpSettings": null,
"kcpSettings": null,
"wsSettings": {
"connectionReuse": true,
"path": "/catnet",
"headers": null
},
"httpSettings": null,
"quicSettings": null
},
"mux": {
"enabled": true,
"concurrency": 8
}
},
{
"tag": "direct",
"protocol": "freedom",
"settings": {
"vnext": null,
"servers": null,
"response": null
},
"streamSettings": null,
"mux": null
},
{
"tag": "block",
"protocol": "blackhole",
"settings": {
"vnext": null,
"servers": null,
"response": {
"type": "http"
}
},
"streamSettings": null,
"mux": null
}
],
"stats": null,
"api": null,
"dns": null,
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"type": "field",
"port": null,
"inboundTag": [
"api"
],
"outboundTag": "api",
"ip": null,
"domain": null
},
{
"type": "field",
"port": null,
"inboundTag": null,
"outboundTag": "block",
"ip": null,
"domain": [
"domain:qq.com"
]
}
]
}
}
创建服务文件 /etc/init.d/v2rxy
#!/bin/sh /etc/rc.common
START=99
STOP=10
USE_PROCD=1
start_service() {
procd_open_instance
procd_set_param command /usr/bin/v2rxy -c /etc/v2rxy/config.json
procd_set_param respawn
procd_set_param stdout 1 # forward stdout of the command to logd
procd_set_param stderr 1 # same for stderr
procd_close_instance
}
reload_service() {
procd_send_signal v2rxy
}
需要将这个文件chmod +x, 否则启动服务或添加服务时会出现-ash: /etc/init.d/v2rxy: Permission denied的错误.
添加到系统启动
/etc/init.d/v2rxy enable
到这一步, v2rxy启动后, 连接到这个OpenWrt的设备就可以直接访问这两个园外网站了.
问题排查
如果无法连接, 可能出现问题的地方有几点:
1. DNS解析未正确设置
如果v2rxy配置正确, 99%的问题都出现在DNS解析上. 检查dnsmasq是否设置正确, 通过命令
dnsmasq --test
以及通过nslookup检查解析是否正确, 后面的8.8.8.8用于指定dns server
nslookup www.google.co.jp 8.8.8.8
2. 解析后的IP未进入ipset
用ipset命令
# 查看列表
ipset list outside
# 或者直接查看所有
ipset list
# 检查nslookup出来的ip是否已经进入list
ipset list |grep 123.123.123.123
# 清空
ipset flush outside
这里有一种情况, 就是v2rxy所在的路由器, 并非直接提供DNS服务的路由器. 例如 [你的电脑]<--> AP Router <--> v2rxy Router <--> Internet
这种情况下, 你的电脑上的DNS解析, 和v2rxy router上的解析很可能是不一样的, 如果发现有些地址无法访问, 需要在你的电脑上也用nslookup查看一下解析结果, 对比一下v2rxy router上的nslookup结果. 如果不一样, 那么这个ip在v2rxy router上未进入ipset list, 自然就不会走v2rxy. 如果出现这种情况, 首先检查电脑的DNS是不是有特殊配置, 默认应该是AP Router的地址, 另外检查AP Router的DNS上游设置是否是v2rxy router, 如果不是, 要首选DNS Server设为v2rxy router所在IP.
3. iptables未正确设置
通过iptables命令检查net设置是否正确, 这个地方出问题的概率比较大, 现象就是当你访问的时候, 在v2rxy的输出日志中看不到代理接收请求, 这时候十有八九是这边的nat没设置上
iptables -t nat -L
4. v2rxy未正确配置, 或者远端v2rxy节点工作不正常
直接查看v2rxy的日志进行排查, 将v2rxy的日志级别loglevel设为debug, 查看system日志中的v2rxy输出,可以通过命令
logread -f
OpenWrt的dnsmasq, ipset和iptables配置的更多相关文章
- Linux防火墙简介 – iptables配置策略
Linux防火墙简介 – iptables配置策略 Netfilter/iptables简介 要想真正掌握Linux防火墙体系,首先要搞清楚Netfilter和iptables的关系,Netfilte ...
- tony_iptables_01_linux下IPTABLES配置详解(转)
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...
- linux下IPTABLES配置详解(转)
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...
- linux下IPTABLES配置详解(转)
如果你的IPTABLES基础知识还不了解,建议先去看看.开始配置我们来配置一个filter表的防火墙.(1)查看本机关于IPTABLES的设置情况[ ~]# iptables -L -nChain I ...
- linux下IPTABLES配置
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...
- [转载] iptables配置实践
原文: http://wsgzao.github.io/post/iptables/ iptables配置实践 By wsgzao 发表于 2015-07-24 文章目录 1. 前言 2. 更新历史 ...
- linux下IPTABLES配置详解
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...
- (转载)Linux下IPTABLES配置详解
(转载)http://www.cnblogs.com/JemBai/archive/2009/03/19/1416364.html 如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 ...
- Android Linux自带iptables配置IP访问规则
利用Linux自带iptables配置IP访问规则,即可做到防火墙效果
- 9. iptables 配置
iptables 配置文件存放位置: [root@Demon yum.repos.d]# vim /etc/rc.d/init.d/iptables 一.只给 Centos 6.5 打开 22 ...
随机推荐
- [转帖]max_allowed_packet 与 SQL 长度的关系
https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000000210013 适用版本:V2.1.x.V2.2.x.V3.1 ...
- [转帖]linux系统上free命令看到的buff/cache到底是什么
https://zhuanlan.zhihu.com/p/645904515 上周二一大早,小智准备早点去公司肝一篇技术文分享给大家的,哪成想,一到公司就被测试部的"卷王"拉去看问 ...
- [转帖]echo “新密码”|passwd --stdin 用户名
https://www.cnblogs.com/rusking/p/6912809.html --stdin This option is used to indicate that passwd s ...
- Python学习之十八_获取神通数据库所有的表数据量
Python学习之十八_获取神通数据库所有的表数据量 背景 今天想获取一下所有数据库的表信息.但是发现神通数据库的系统表里面的表信息不正确 无法获取实际意义的表信息. 联系了下神通数据库的原厂高手. ...
- [转帖]Centos7 nginx访问日志文件割接
一.yum安装nginx 二.各文件路径( /etc/nginx/nginx.conf) 1.访问日志路径:access_log /var/log/nginx/access.log main; 2.p ...
- [转帖]「Linux性能调优」磁盘I/O队列调度策略
https://zhuanlan.zhihu.com/p/450329513 傻瓜化说明 简单地说,对于磁盘I/O,Linux提供了cfq, deadline和noop三种调度策略 cfq: 这个名字 ...
- [转帖]Linux中的用户和用户组
https://www.jianshu.com/p/76700505cac4 1,Linux中的用户分类 超级用户:拥有对系统的最高管理权限,默认是root用户. 普通用户:只能对自己目录下的文件进行 ...
- [转帖]Linux命令拾遗-%iowait指标代表了什么?
https://www.cnblogs.com/codelogs/p/16060759.html 简介# 一直以来,我都知道top.vmstat.mpstat中有一个叫wa(%iowait)的cpu指 ...
- MySQL数据库存储varchar时多大长度会出现行迁移?
最近客户现场有人问过mysql数据库的一些参数配置的问题, 这边数据库需要将strict 严格模式关掉, 目的是为了保证数据库在插入字段时不会出现8126的长度限制错误问题. 但是一直很困惑, mys ...
- SPI在Java中的实现与应用 | 京东物流技术团队
1 SPI的概念 API API在我们日常开发工作中是比较直观可以看到的,比如在 Spring 项目中,我们通常习惯在写 service 层代码前,添加一个接口层,对于 service 的调用一般也都 ...