Openwrt上使用dnsmasq和ipset實現域名分流

轉自:https://www.cnblogs.com/weifeng1463/p/6796140.html

目標

部署一臺自動代理路由器,實現根據域名來自動設定直連或者代理,而我要做的只是設置PC的默認網關爲主路由器(192.168.0.1)還是自動代理路由器(192.168.0.254)。

創建Openwrt虛擬機

系統版本

  • 主路由器 (ip: 192.168.0.1)
  • ESXI 6.0U2
  • Openwrt 15.05.1 (ip: 192.168.0.254,gateway: 192.168.0.1)

Openwrt虛擬機的配置教程有很多,這裏只針對ESXI版Openwrt可能會遇到的問題說明下:

  • 在ESXI6上,openwrt_x86每次啓動時會大概率的出現卡死現象,表現爲Kernel panic - not syncing: Attempted to kill init
    • 解決辦法:改用openwrt_x64後正常。原因未知。
  • 在ESXI6上,在openwrt上執行某些命令時,會被強制關機,表現爲

    1

    來自 promote 的消息: The operation on the file "/vmfs/devices/deltadisks/17ad1ab5-openwrt-15. 05.1-x86-64-combined-ext4-s001.vmdk" failed (Bad address). The file system where disk "/vmfs /devices/deltadisks/17ad1ab5-openwrt-15.05.1-- x86-64-combined-ext4-s001.vmdk" resides is full. Select _Retry to attempt the operation again. Select Cancel to end the session.

    • 解決辦法:在Vmware Fusion中新建openwrt虛擬機(other/other linux 64, 256MB,虛擬機版本爲11),第一次啓動後,關機導出爲ova,然後再導入到ESXI6中。具體原因未知。

安裝軟件

1

2

3

4

5

6

7

opkg update

opkg remove dnsmasq

opkg install dnsmasq-full

opkg install ipset iptables-mod-nat-extra

 

wget http://x.x.x.x/shadowsocks-libev_2.4.6-1_x86_64.ipk

opkg install shadowsocks-libev_2.4.6-1_x86_64.ipk

PS
在這裏,安裝官方版的shadowsocks,而不是openwrt spec版, 下載地址在這裏

PPS
在這裏,如果安裝的順序不同,可能會導致lib的依賴錯誤(至少在15.05.1上是這樣)。使用ldd命令,檢查一下shadowsocks的依賴包是否正常。

1

ldd /usr/bin/ss-redir

 

配置

配置shadowsocks

配置/etc/shadowsocks.json,格式如下:

1

2

3

4

5

6

7

{

"server": "X.X.X.X",

"server_port": "443",

"password": "password",

"local_port": "1080",

"method": "aes-256-cfb"

}

 

修改/etc/init.d/shadowsocks,設置shadowsocks以Proxy Mode模式啓動,同時開啓轉發DNS請求功能,上游DNS爲8.8.8.8

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#!/bin/sh /etc/rc.common

 

START=95

 

SERVICE_USE_PID=1

SERVICE_WRITE_PID=1

SERVICE_DAEMONIZE=1

 

CONFIG=/etc/shadowsocks.json

 

start() {

#service_start /usr/bin/ss-local -c $CONFIG -b 0.0.0.0

service_start /usr/bin/ss-redir -c $CONFIG -b 0.0.0.0

service_start /usr/bin/ss-tunnel -c $CONFIG -b 0.0.0.0 -l 5353 -L 8.8.8.8:53 -u

}

 

stop() {

#service_stop /usr/bin/ss-local

service_stop /usr/bin/ss-redir

service_stop /usr/bin/ss-tunnel

}

設置shadowsocks開機啓動,並手動運行:

1

2

/etc/init.d/shadowsocks enable

/etc/init.d/shadowsocks start

 

到目前爲止,你已經有了一個監聽在1080端口的socks代理,和監聽在5353端口的不受污染的DNS服務器

配置ipset

進入Luci界面-網絡-防火牆-自定義規則,加入以下規則(最後的1080是shadowsocks的本地端口,請酌情修改):

1

2

3

4

5

6

#創建名爲gfwlist,格式爲iphash的集合

ipset -N gfwlist iphash

#匹配gfwlist中ip的nat流量均被轉發到shadowsocks端口

iptables -t nat -A PREROUTING -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1080

#匹配gfwlist中ip的本機流量均被轉發到shadowsocks端口

iptables -t nat -A OUTPUT -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-port 1080

 

配置dnsmasq-full

修改/etc/dnsmasq.conf,在文末加入conf-dir=/etc/dnsmasq.d

1

echo 'conf-dir=/etc/dnsmasq.d' >> /etc/dnsmasq.conf

 

新建目錄/etc/dnsmasq.d,生成dnsmasq_list.conf 到該目錄:

1

2

3

4

5

6

git clone https://github.com/cokebar/gfwlist2dnsmasq.git

cd gfwlist2dnsmasq

python2 gfwlist2dnsmasq.py

 

mkdir /etc/dnsmasq.d

cp dnsmasq_list.conf /etc/dnsmasq.d

 

其中,dnsmasq_list.conf的格式爲:

1

2

3

4

#使用不受污染的DNS解析該域名,可以將此IP改爲自己使用的DNS服務器

server=/google.com/127.0.0.1#5353

#將解析出來的IP保存到名爲gfwlist的ipset表中

ipset=/google.com/gfwlist

 

你可以手動修改這個文件,你也可以使用gfwlist2dnsmasq.py自動生成dnsmasq_list版的gfwlist列表。

重新運行dnsmasq,它將監聽在本機53端口上。

1

/etc/init.d/dnsmasq restart

 

測試

場景一

  • PC端的默認網關設置爲192.168.0.1, DNS設置爲192.168.0.1
    • 國內網站訪問正常
    • Google無法訪問

場景二

    • PC端的默認網關設置爲192.168.0.254, DNS設置爲192.168.0.254
      • 訪問國內網站
        • dnsmasq解析該域名,發現該域名不在dnsmasq_list中,使用默認DNS服務器進行解析,正常訪問。
      • 訪問Google
        • dnsmasq解析該域名,發現該域名在dnsmasq_list中,使用設置的安全DNS服務器進行解析,並將該IP加至gfwlist集合中,iptables匹配到規則,將流量轉發到shadowsocks監聽的端口,進行代理訪問。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章