Skip to content

Instantly share code, notes, and snippets.

@ihciah
Last active April 8, 2020 06:09
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ihciah/65ac20c193c663dd48ba9a9e66734613 to your computer and use it in GitHub Desktop.
Save ihciah/65ac20c193c663dd48ba9a9e66734613 to your computer and use it in GitHub Desktop.
小米路由器 Pro (R3P) 配置shadowsocks透明代理

小米路由器 Pro (R3P) 配置shadowsocks透明代理

新入了一个小米路由器,记录一下折腾过程。

首先自动分流科学上网是必须的。

因为小米的ROM本身就是基于openwrt的,所以第一步只需要先走官方途径拿到root权限。

有了权限就只需要找一个别人造的轮子部署一下即可。

尝试1:

  • MT工具箱

这个东西嘛,乍一看还可以。然而有点问题,首先是部署上之后完全无法运行,第二是安全性堪忧(可以直接绕过密码认证,然后只需要利用shadowsocks服务来劫持软件源,即可实现任意代码执行;以及这东西本身就有webshell功能,就非常尴尬)。

尝试2:

https://github.com/blademainer/miwifi-ss 这个repo有点旧,但是无所谓,然而依旧不能用。

仔细看了下原因应该是shadowsocks的binary无法运行。于是开始折腾自己编译。

交叉编译过程和编译的二进制文件整理在这里: https://gist.github.com/ihciah/de493989bd43f76f17ff143daa036e20

尝试3:

迫不得已,只能自己动手。

首先将静态编译好的binary放在 /data/usr/shadowsocks 文件夹里;并准备 chnroutes.txt ( http://f.ip.cn/rt/chnroutes.txt 下载完成后需手动删除第一行的注释)、ipset.cidr_cn.rules (见 gen_rules.sh )

之后配置启动文件,见 chinadns, ssredir, sstunnel。将这些文件放入 /etc/init.dchmod +x xxx 后,执行 /etc/init.d/xxx enable 开启自动启动。

然后只需要配置 iptables 搞定转发。见 ipt.sh 。这里我们可以为其写一份启动文件,也可以简单地将其放入 /etc/rc.local 文件。当然,在这之前还需要加载ipset: ipset -R < ipset.cidr_cn.rules

之后在 /etc/dnsmasq.d 中创建 dns.conf (见dns.conf文件);这时解析链为 dnsmasq->chinadns->(ss-tunnel or 114dns)。

#!/bin/sh /etc/rc.common
# chinadns init script
START=90
STOP=15
EXTRA_COMMANDS="restart"
PIDFILE='/tmp/chinadns.pid'
start()
{
if [ -f $PIDFILE ]
then
echo "already started: $PIDFILE exists"
exit 1
fi
/data/usr/shadowsocks/chinadns \
-c /data/usr/shadowsocks/chnroutes.txt \
-m \
-p 5353 \
-b 127.0.0.1 \
-s 114.114.114.114,223.6.6.6,127.0.0.1:5300 \
1> /tmp/chinadns.log \
2> /tmp/chinadns.err.log &
echo $! > $PIDFILE
}
stop()
{
kill `cat $PIDFILE`
rm $PIDFILE
}
restart()
{
stop
start
}
no-resolv
server=127.0.0.1#5353
# From https://mengcz13.github.io/2018/07/14/raspberrypi-ssgw/
curl -sL http://f.ip.cn/rt/chnroutes.txt | egrep -v '^$|^#' > cidr_cn
sudo ipset -N cidr_cn hash:net
for i in `cat cidr_cn`; do echo ipset -A cidr_cn $i >> ipset.sh; done
chmod +x ipset.sh && sudo ./ipset.sh
rm -f ipset.cidr_cn.rules
sudo ipset -S > ipset.cidr_cn.rules
sudo cp ./ipset.cidr_cn.rules /etc/ipset.cidr_cn.rules
# 上述脚本也可直接使用一句命令解决,其中chnroutes.txt首行不包含注释。
# From @ripples-alive 童鞋
sed "s/^/add cidr_cn /;1i create cidr_cn hash:net family inet hashsize 4096 maxelem 65536" chnroutes.txt > ipset.cidr_cn.rules
#!/bin/sh
iptables -t nat -N shadowsocks
iptables -t nat -A shadowsocks -d 0/8 -j RETURN
iptables -t nat -A shadowsocks -d 127/8 -j RETURN
iptables -t nat -A shadowsocks -d 10/8 -j RETURN
iptables -t nat -A shadowsocks -d 169.254/16 -j RETURN
iptables -t nat -A shadowsocks -d 172.16/12 -j RETURN
iptables -t nat -A shadowsocks -d 192.168/16 -j RETURN
iptables -t nat -A shadowsocks -d 224/4 -j RETURN
iptables -t nat -A shadowsocks -d 240/4 -j RETURN
iptables -t nat -A shadowsocks -d YOUR_IP -j RETURN
iptables -t nat -A shadowsocks -m set --match-set cidr_cn dst -j RETURN
iptables -t nat -A shadowsocks ! -p icmp -j REDIRECT --to-ports 10800
iptables -t nat -A OUTPUT ! -p icmp -j shadowsocks
iptables -t nat -A PREROUTING ! -p icmp -j shadowsocks
#!/bin/sh /etc/rc.common
# ss-redir init script
START=90
STOP=15
EXTRA_COMMANDS="restart"
PIDFILE='/tmp/ss-redir.pid'
start()
{
if [ -f $PIDFILE ]
then
echo "already started: $PIDFILE exists"
exit 1
fi
/data/usr/shadowsocks/ss-redir \
-c /data/usr/shadowsocks/config.json -b 0.0.0.0 -u \
1> /dev/null \
2> /dev/null &
echo $! > $PIDFILE
}
stop()
{
kill `cat $PIDFILE`
rm $PIDFILE
}
restart()
{
stop
start
}
#!/bin/sh /etc/rc.common
# ss-tunnel init script
START=90
STOP=15
EXTRA_COMMANDS="restart"
PIDFILE='/tmp/ss-tunnel.pid'
start()
{
if [ -f $PIDFILE ]
then
echo "already started: $PIDFILE exists"
exit 1
fi
/data/usr/shadowsocks/ss-tunnel \
-c /data/usr/shadowsocks/config.json -l 5300 -L 8.8.8.8:53 -b 0.0.0.0 -u \
1> /dev/null \
2> /dev/null &
echo $! > $PIDFILE
}
stop()
{
kill `cat $PIDFILE`
rm $PIDFILE
}
restart()
{
stop
start
}
@a0026
Copy link

a0026 commented Jul 5, 2019

并不work。
1 ipset -R < ipset.cidr_cn.rules 这个命令执行不了。  
ipset v6.20.1: No command specified: unknown argument 1.0.1.0/24
Try `ipset help' for more information.

2 YOUR_IP 是啥?替换为路由器ip地址

3 修改完是否需要重启?

@ihciah
Copy link
Author

ihciah commented Jul 5, 2019

ipset restore -f

并不work。
1 ipset -R < ipset.cidr_cn.rules 这个命令执行不了。  
ipset v6.20.1: No command specified: unknown argument 1.0.1.0/24
Try `ipset help' for more information.

2 YOUR_IP 是啥?替换为路由器ip地址

3 修改完是否需要重启?

  1. Try ipset restore -f ipset.cidr_cn.rules.
  2. Obviously YOUR_IP means ip of your VPS, which should be connected directly.
  3. You can read the document of ipset and iptables to determine whether they need reboot. Short answer: No.

@a0026
Copy link

a0026 commented Jul 5, 2019

1 root@XiaoQiang:/data/usr/shadowsocks# ipset restore -f ipset.cidr_cn.rules
ipset v6.20.1: No command specified: unknown argument 1.0.1.0/24
Try `ipset help' for more information. 是否可以不执行这个命令,并且去掉"iptables -t nat -A shadowsocks -m set --match-set cidr_cn dst -j RETURN" ,所有流量走代理?
2 配置以后,发现dns不通

@gneheix
Copy link

gneheix commented Oct 28, 2019

iptables -t nat -A shadowsocks ! -p icmp -j REDIRECT --to-ports 10800

这里的10800是不是/data/usr/shadowsocks/config.json里指定的local-port,默认是1080呢,是多打了一个0吗?

@ihciah
Copy link
Author

ihciah commented Oct 28, 2019

iptables -t nat -A shadowsocks ! -p icmp -j REDIRECT --to-ports 10800

这里的10800是不是/data/usr/shadowsocks/config.json里指定的local-port,默认是1080呢,是多打了一个0吗?

自行修改咯。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment