openwrt固件实现智能翻墙
国庆放假前几天一直在家折腾路由器自动翻墙, 固件刷了好几遍, 虽然最后也没成功, 还是记录下配置过程, 以后再想做时用得着.
我的路由器是Netgear R6100, 去 https://wiki.openwrt.org/toh/hwdata/netgear/netgear_r6100 查到路由器信息,
安装
安装软件有三种方法, 一, 通过配置源在线安装; 二, 通过脚本安装. 三, 通过下载安装包安装;
一, 最简单的就是通过源安装:
由于openwrt-dist.sourceforge.net源不翻墙的话不能用, 会有bad address的错误提示, 实际配置需要配置反代源, 参考了 http://openwrt-dist.colendres.com/ , 最后配置如下:
First, add openwrt-dist.pub file into opkg’s keys:
1 2 |
wget http://openwrt-dist.sourceforge.net/packages/openwrt-dist.pub opkg-key add openwrt-dist.pub |
添加如下到/etc/opkg.conf, 注意将签名验证给注释掉
1 2 3 |
src/gz openwrt_dist http://openwrt-dist.colendres.com/packages/OpenWrt/base/ar71xx #或src/gz openwrt_dist http://openwrt-dist.colendres.com/packages/OpenWrt/base/{architecture} src/gz openwrt_dist_luci http://openwrt-dist.colendres.com/packages/OpenWrt/luci |
然后安装相应软件:
1 2 3 4 5 6 7 8 9 10 11 |
opkg update opkg install iptables-mod-nat-extra iptables-mod-tproxy ipset opkg remove dnsmasq && opkg install dnsmasq-full opkg install ChinaDNS opkg install luci-app-chinadns opkg install dns-forwarder opkg install luci-app-dns-forwarder opkg install shadowsocks-libev opkg install luci-app-shadowsocks opkg install ShadowVPN opkg install luci-app-shadowvpn |
注: 每次路由器重启后, 都要先opkg update下才能安装软件
二, 通过脚本安装
1 2 3 |
wget http://openwrt-dist.colendres.com/auto_install_openwrt.sh chmod +x auto_install_openwrt.sh ./auto_install_openwrt.sh |
三, 下载所有安装包然后opkg install 安装
配置:
配置Shadowsocks信息, server 和server_port填写shadowsocks服务器ip和端口, 加密方式可用aes-256-cfb(高密码强度)或rc4-md5(高性能)等
1 2 3 4 5 6 7 8 |
/etc/shadowsocks.json { "server": "x.x.x.x", "server_port": "xxxx", "password": "password", "local_port": "1080", "method": "rc4-md5" } |
新建Shadowsocks init文件/etc/init.d/shadowsocks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#!/bin/sh /etc/rc.common START=95 STOP=15 SERVICE_USE_PID=1 SERVICE_WRITE_PID=1 SERVICE_DAEMONIZE=1 SERVICE_PID_FILE=/var/run/shadowsocks.pid TUNNEL_PID_FILE=/var/run/ss-tunnel.pid CONFIG=/etc/shadowsocks.json DNS=8.8.8.8:53 TUNNEL_PORT=5353 start() { # Proxy Mode service_start /usr/bin/ss-redir -c $CONFIG -b 0.0.0.0 -f $SERVICE_PID_FILE # Tunnel service_start /usr/bin/ss-tunnel -c $CONFIG -b 0.0.0.0 -u -l $TUNNEL_PORT -L $DNS -f $TUNNEL_PID_FILE #ss-tunnel本地DNS转发端口为5353,远端DNS查询地址为8.8.8.8:53(Google DNS),也可修改为别的DNS。 } stop() { # Proxy Mode service_stop /usr/bin/ss-redir # Tunnel service_stop /usr/bin/ss-tunnel } |
修改文件权限,并设置开机启动。
1 |
/etc/init.d/shadowsocks enable配置dnsmasq和ipset |
增加防火墙规则,同时在firewall.user中增加,开机后便自动增加
1 2 3 |
ipset -N gfwlist iphash iptables -t nat -A PREROUTING -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1080 iptables -t nat -A OUTPUT -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1080 |
配置Dnsmasq
添加如下到/etc/dnsmasq.conf,并新增目录/etc/dnsmasq.d。
cache-size=1500
#修改dnsmasq缓存大小,默认为150。
min-cache-ttl=720
#修改DNS缓存最小有效期(秒)。仅适用于aa65535的dnsmasq-full版本。
1 2 3 4 5 6 |
all-servers server=119.29.29.29 server=114.114.114.114 cache-size=2500 min-cache-ttl=300 conf-dir=/etc/dnsmasq.d |
将dnsmasq_gfwlist_ipset.conf放到目录/etc/dnsmasq.d
中,规则主要是处理哪些域名需要进行特殊处理。根据格式可自行修改,也可做adblock.conf
,这样可进行广告处理,直接将DNS结果返回空。
1 2 3 4 |
#使用不受污染干扰的DNS解析该域名 可以将此IP改为自己使用的DNS服务器 server=/google.com/127.0.0.1#5353 #将解析出来的IP保存到名为gfwlist的ipset表中 ipset=/google.com/gfwlist |
其中的127.0.0.1#5353
可另行处理,可用8.8.8.8#53
或其它未被污染的服务器替代。
新建并进入目录 /etc/dnsmasq.d,
或者: 下载dnsmasq_list.conf并放在该目录下(本人仅添加了谷歌,Youtube,脸书,推特等等常用域名,该列表持续更新中。注意域名太多反而会影响Dnsmasq-full的性能!)。
如需自行修改或添加域名,参考格式如下:
PC端的Dns要设为自动获取,最好重启下路由器和PC。
1,百度搜索“ip”,显示为本地运营商的ip。
2,可以顺利访问Google,Youtube等网站。
至此整个Openwrt路由器智能翻墙配置完成。任何接入该路由器的设备(手机,PC,平板,etc…)即可无障碍的浏览墙外的世界了。
0 Comments