ss-redir透明代理

参考:http://newto.me/build-shadowsocks-with-internal/

https://medium.com/@oliviaqrs/%E5%88%A9%E7%94%A8shadowsocks%E6%89%93%E9%80%A0%E5%B1%80%E5%9F%9F%E7%BD%91%E7%BF%BB%E5%A2%99%E9%80%8F%E6%98%8E%E7%BD%91%E5%85%B3-fb82ccb2f729

https://www.bjwf125.com/?p=9

1、安装chinadns

wget https://github.com/shadowsocks/ChinaDNS/releases/download/1.3.2/chinadns-1.3.2.tar.gz
tar -xvf chinadns-1.3.2.tar.gz

./configure && make
src/chinadns -m -c chnroute.txt
cp ./src/chinadns /usr/local/bin/

2、配置dnsmasq

no-resolv
server=127.0.0.1#5353   #注意这里的端口是chinadns的监听端口
service dnsmasq restart

3、开启ss-redir

ss-redir -c /etc/shadowsocks-libev/config-usr2.json
root@yp-pc:/home/yp# more /etc/shadowsocks-libev/config-usr2.json
 {
    "server":"165.52.43.125",
    "server_port":18419,
    "local_address": "0.0.0.0",
    "local_port":1081,
    "password":"mypwd",
    "timeout":60,
    "method":"aes-256-cfb"
 }

4、开启ss-tunnel和chinadns

ss-tunnel -s 165.52.43.125 -p 18419 -b 0.0.0.0 -l 5300 -k mypwd -m aes-256-cfb -L 8.8.8.8:53 -u &
chinadns -c ./chnroute.txt  -m -p 5355 -s '114.114.114.114,127.0.0.1:5300' -d -v

5、生成ipset(可选)

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

6、配置iptables

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

# 以下IP为局域网内不走代理的设备IP
iptables -t nat -A shadowsocks -s 10.0.0.111 -j RETURN

# 发往shadowsocks服务器的数据不走代理,否则陷入死循环
# 替换111.111.111.111为你的ss服务器ip/域名
iptables -t nat -A shadowsocks -d  165.52.43.125 -j RETURN   

# 大陆地址不走代理,因为这毫无意义,绕一大圈很费劲的
iptables -t nat -A shadowsocks -m set --match-set cidr_cn dst -j RETURN

# 其余的全部重定向至ss-redir监听端口1080(端口号随意,统一就行)
iptables -t nat -A shadowsocks ! -p icmp -j REDIRECT --to-ports 1081

# OUTPUT链添加一条规则,重定向至shadowsocks链
iptables -t nat -A OUTPUT ! -p icmp -j shadowsocks
iptables -t nat -A PREROUTING ! -p icmp -j shadowsocks

7、测试一:wget www.google.com

yp@yp-pc:~$ wget www.google.com
--2018-07-31 15:53:30--  http://www.google.com/
正在解析主机 www.google.com (www.google.com)... 216.58.203.4, 2404:6800:4005:804::2004
正在连接 www.google.com (www.google.com)|216.58.203.4|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 未指定 [text/html]
正在保存至: “index.html.5”

index.html.5                         [  <=>                                                     ]  11.01K  30.9KB/s    in 0.4s    

2018-07-31 15:53:31 (30.9 KB/s) - “index.html.5” 已保存 [11274]

yp@yp-pc:~$ 

8、测试二:浏览器上外网

9、不用chinadns的方案

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

# 以下IP为局域网内不走代理的设备IP
iptables -t nat -A shadowsocks -s 10.0.0.111 -j RETURN

# 发往shadowsocks服务器的数据不走代理,否则陷入死循环
# 替换164.52.43.125为你的ss服务器ip/域名
iptables -t nat -A shadowsocks -d  165.52.43.125 -j RETURN   

# 大陆地址不走代理,因为这毫无意义,绕一大圈很费劲的
iptables -t nat -A shadowsocks -m set --match-set cidr_cn dst -j RETURN

# 其余的全部重定向至ss-redir监听端口1080(端口号随意,统一就行)
iptables -t nat -A shadowsocks ! -p icmp -j REDIRECT --to-ports 1081

# OUTPUT链添加一条规则,重定向至shadowsocks链
iptables -t nat -A OUTPUT ! -p icmp -j shadowsocks
iptables -t nat -A PREROUTING ! -p icmp -j shadowsocks

#dnsmasq已改为监听5355
ss-tunnel -s 165.52.43.125 -p 18419 -b 0.0.0.0 -l 5355 -k mypwd -m aes-256-cfb -L 8.8.8.8:53 -u &
ss-redir -c /etc/shadowsocks-libev/config-usr2.json

猜你喜欢

转载自blog.csdn.net/ypbsyy/article/details/81307101