美文网首页
Dnsmasq / ipset / iptables 基于域名的

Dnsmasq / ipset / iptables 基于域名的

作者: 袁先生的笔记 | 来源:发表于2020-07-28 22:03 被阅读0次

环境

系统:CentOS 7.8

准备

注:Dnsmasq / ipset / iptables 均在VPN客户端安装配置

配置Dnsmasq

  • 下载dnsmasq_gfwlist_ipset.conf
    地址:https://github.com/cokebar/gfwlist2dnsmasq
    或直接下载

    # wget https://cokebar.github.io/gfwlist2dnsmasq/dnsmasq_gfwlist_ipset.conf
    
  • 添加到Dnsmasq配置

    # sed 's/127\.0\.0\.1#5353$/8\.8\.8\.8/g' dnsmasq_gfwlist_ipset.conf > /etc/dnsmasq.d/dns_gfwlist.conf
    # systemctl restart dnsmasq
    
  • 添加DNS走VPN

    # ip route add 8.8.8.8 dev $vpn_dev
    
  • 创建ipset

    # ipset create gfwlist hash:ip
    

配置路由

  • 标记gfwlist流量

    # iptables -t mangle -C PREROUTING --match set --match-set gfwlist dst -j MARK --set-mark 99 || \
    iptables -t mangle -I PREROUTING --match set --match-set gfwlist dst -j MARK --set-mark 99
    
    # iptables -t mangle -C OUTPUT --match set --match-set gfwlist dst -j MARK --set-mark 99 || \
    iptables -t mangle -I OUTPUT --match set --match-set gfwlist dst -j MARK --set-mark 99
    
  • 被标记的流量走VPN

    # grep -q gfwtable /etc/iproute2/rt_tables || \
    echo '99 gfwtable' >> /etc/iproute2/rt_tables
    # ip rule | grep -q gfwtable || \
    # ip rule add fwmark 99 table gfwtable
    # ip route add default dev $vpn_dev table gfwtable
    
  • 允许流量转发

    # iptables -C FORWARD -o $vpn_dev -j ACCEPT || \
    # iptables -I FORWARD -o $vpn_dev -j ACCEPT
    # iptables -t nat -C POSTROUTING -o $vpn_dev -j MASQUERADE || \
    # iptables -t nat -I POSTROUTING -o $vpn_dev -j MASQUERADE
    

使用方法

  • VPN客户端:直接使用
  • 局域网内其它用户:把网关指向VPN客户端,也可使用

相关文章

网友评论

      本文标题:Dnsmasq / ipset / iptables 基于域名的

      本文链接:https://www.haomeiwen.com/subject/sxbqrktx.html