整体思路

  1. dnsmasq、hostapd实现路由器功能
  2. dnsmasq、ChinaDNS、ss-tunnel解决DNS污染的问题
  3. ss-redir配合iptables和ipset来分流国内流量和国外流量,国内的网站直连,而国外网站则走ss-redir

一、树莓派3实现路由器功能

参考官方文档:Wireless Access Point

  1. 安装dnsmasq、hostapdsudo apt install dnsmasq hostapd
  2. 配置无线网卡静态IP地址sudo vim /etc/dhcpcd.conf在最下面添加:
1
2
3
interface wlan0
    static ip_address=192.168.4.1/24
    nohook wpa_supplicant
  1. 配置DHCP服务器

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak sudo vim /etc/dnsmasq.conf加入以下配置:

1
2
interface=wlan0      # Use the require wireless interface - usually wlan0
  dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
  1. 配置无线接入点

sudo vim /etc/hostapd/hostapd.conf加入以下配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
interface=wlan0
driver=nl80211
ssid=NameOfNetwork # 无线网络名称,不能有引号
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=xxxxxxxx # 无线网络密码,长度为8~64位
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

sudo vim /etc/default/hostapd修改或加入:

1
DAEMON_CONF="/etc/hostapd/hostapd.conf"
  1. 添加路由规则

sudo vim /etc/sysctl.conf取消net.ipv4.ip_forward=1的注释
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE添加iptables规则
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"保存规则
/etc/rc.local中加入iptables-restore < /etc/iptables.ipv4.nat使规则开机生效

二、解决DNS污染的问题

  1. 编译安装shadowsocks-libevChinaDNS

  2. ss-tunnel -s <server_host> -p <server_port> -b 0.0.0.0 -l 5300 -k password -m aes-256-cfb -u -v

  3. chinadns -b 0.0.0.0 -p 5354 -s 114.114.114.114,127.0.0.1:5300 -c chnroute.txt -l iplist.txt -m -v

  4. sudo vim /etc/dnsmasq.conf加入以下配置:

1
2
3
4
5
no-resolv
server=127.0.0.1#5354
interface=eth0,wlan0
cache-size=1000
max-ttl=600

测试:dig @127.0.0.1:53 www.google.com

三、分流国内流量和国外流量

  1. ss-redir -s <server_host> -p <server_port> -b 0.0.0.0 -l 10800 -k password -m aes-256-cfb -u -v

 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
server_ip=<server_host>
redir_port=10800

# 新建 mangle/SS-UDP 链,用于透明代理内网 udp 流量
iptables -t mangle -N SS-UDP

# 放行保留地址、环回地址、特殊地址
iptables -t mangle -A SS-UDP -d 0/8 -j RETURN
iptables -t mangle -A SS-UDP -d 127/8 -j RETURN
iptables -t mangle -A SS-UDP -d 10/8 -j RETURN
iptables -t mangle -A SS-UDP -d 169.254/16 -j RETURN
iptables -t mangle -A SS-UDP -d 172.16/12 -j RETURN
iptables -t mangle -A SS-UDP -d 192.168/16 -j RETURN
iptables -t mangle -A SS-UDP -d 224/4 -j RETURN
iptables -t mangle -A SS-UDP -d 240/4 -j RETURN

# 放行发往 ss 服务器的数据包,注意替换为你的服务器IP
iptables -t mangle -A SS-UDP -d $server_ip -j RETURN

# 放行大陆地址
iptables -t mangle -A SS-UDP -m set --match-set chnip dst -j RETURN

# 放行白名单地址
iptables -t mangle -A SS-UDP -m set --match-set whitelist dst -j RETURN

# 黑名单直接转发到 ss-redir 监听端口
iptables -t mangle -A SS-UDP -m set --match-set blacklist dst -j TPROXY --tproxy-mark 0x2333/0x2333 --on-ip 127.0.0.1 --on-port $redir_port

# 重定向 udp 数据包至 ss-redir 监听端口
iptables -t mangle -A SS-UDP -p udp -j TPROXY --tproxy-mark 0x2333/0x2333 --on-ip 127.0.0.1 --on-port $redir_port

# 内网 udp 数据包流经 SS-UDP 链
iptables -t mangle -A PREROUTING -p udp -s 192.168/16 -j SS-UDP

# 新建 nat/SS-TCP 链,用于透明代理本机/内网 tcp 流量
iptables -t nat -N SS-TCP

# 放行环回地址,保留地址,特殊地址
iptables -t nat -A SS-TCP -d 0/8 -j RETURN
iptables -t nat -A SS-TCP -d 127/8 -j RETURN
iptables -t nat -A SS-TCP -d 10/8 -j RETURN
iptables -t nat -A SS-TCP -d 169.254/16 -j RETURN
iptables -t nat -A SS-TCP -d 172.16/12 -j RETURN
iptables -t nat -A SS-TCP -d 192.168/16 -j RETURN
iptables -t nat -A SS-TCP -d 224/4 -j RETURN
iptables -t nat -A SS-TCP -d 240/4 -j RETURN

# 放行发往 ss 服务器的数据包,注意替换为你的服务器IP
iptables -t nat -A SS-TCP -d $server_ip -j RETURN

# 放行大陆地址
iptables -t nat -A SS-TCP -m set --match-set chnip dst -j RETURN

# 放行白名单地址
iptables -t nat -A SS-TCP -m set --match-set whitelist dst -j RETURN

# 黑名单直接转发到 ss-redir 监听端口
iptables -t nat -A SS-TCP -m set --match-set blacklist dst -j REDIRECT --to-ports $redir_port

# 重定向 tcp 数据包至 ss-redir 监听端口
iptables -t nat -A SS-TCP -p tcp -j REDIRECT --to-ports $redir_port

# 本机 tcp 数据包流经 SS-TCP 链
iptables -t nat -A OUTPUT -p tcp -j SS-TCP
# 内网 tcp 数据包流经 SS-TCP 链
iptables -t nat -A PREROUTING -p tcp -s 192.168/16 -j SS-TCP

# 内网数据包源 NAT
iptables -t nat -A POSTROUTING -s 192.168/16 -j MASQUERADE

# 持久化 iptables 规则
iptables-save > /etc/iptables.tproxy

# 新建路由表 100,将所有数据包发往 loopback 网卡
ip route add local 0/0 dev lo table 100

# 添加路由策略,让所有经 TPROXY 标记的 0x2333/0x2333 udp 数据包使用路由表 100
ip rule add fwmark 0x2333/0x2333 lookup 100

参考链接

[1]. ss/ssr/v2ray/socks5 透明代理
[2]. 利用shadowsocks打造局域网翻墙透明网关
[3]. 在树莓派上搭建全局透明代理网关