搭建无污染无广告分流DNS over HTTPS服务器 1

前言

随手写一下搭建方式吧,和个人取舍的一些东西。

结构图

搭建使用的软件

  1. 中间件NGINX(可选)

    用于阻止IP直接访问,作为反向代理使用。

  2. 前端服务端 m13253/dns-over-https

    用于接受客户端DNS请求

  3. 服务端 AdguardTeam/AdGuardHome

    用于可视化规则管理。

  4. 服务端 IrineSistiana/mosdns-cn

    简单的分流工具,用于分流国内外域名。

搭建过程(源代码)

  1. 首先搭建dns-over-https服务器(官方的教程参考:这里

    1. 首先根据自己的操作系统安装golang环境

    2. 运行

      # 创建gopath
      mkdir ~/gopath
      export GOPATH=~/gopath
      
      # 拉取代码
      cd /tmp
      git pull https://github.com/m13253/dns-over-https
      cd dns-over-https
      
      # 编译安装
      make && sudo make install
      
    3. 修改配置文件 sudoedit /etc/dns-over-https/doh-client.conf,参考配置:

      # 监听端口,由于使用NGINX反向代理,故使用127.0.0.1监听本地。
      listen = [
          "127.0.0.1:8053",
          # ":8053",
      ]
      
      local_addr = ""
      
      # 前端已嵌套CDN且反向代理已设置证书,无需再次设置证书。
      cert = ""
      key = ""
      
      # 可以按需修改路径
      path = "/dns-query"
      
      # 这里用了两个地址,用于提高获取成功率。
      upstream = [
          "udp:127.0.0.1:53",
          "tcp:127.0.0.1:53",
      ]
      
      # 上游超时,最小1s
      timeout = 1
      
      # 上游超时重试次数
      tries = 10
      
      verbose = false
      
      log_guessed_client_ip = false
      
      ecs_allow_non_global_ip = false
      
      ecs_use_precise_ip = false
      
      # If DOH is used for a controlled network, it is possible to enable
      # the client TLS certificate validation with a specific certificate
      # authority used to sign any client one. Disabled by default.
      # 这里可以设置对应的用户证书访问限制(使用CDN回源证书,限制为CDN访问),可以摒弃NGINX;多个443端口应用还是需要NGINX的,我们这边不用这些选项。
      # tls_client_auth = true
      # tls_client_auth_ca = "root-ca-public.crt"
      
    4. 安装服务(根据自己的操作系统)

      sudo systemctl start doh-client.service
      sudo systemctl enable doh-client.service
      
  2. 搭建AdGuardHome服务器(官方的教程参考:这里

    1. 下载对应的版本,安装

      # linux-amd64服务器运行
      cd /tmp/
      wget https://github.com/AdguardTeam/AdGuardHome/releases/latest/download/AdGuardHome_linux_amd64.tar.gz
      tar -zxvf AdGuardHome_linux_amd64.tar.gz -C /usr/local/
      cd /usr/local/AdGuardHome
      ./AdGuardHome -s install
      
    2. 访问服务器对应http://IP:3000,设置好用户名和密码。

    3. 再次访问服务器对应http://IP:3000,配置好自己需要的过滤器,上游填写127.0.0.1:60053备用

  3. 搭建mosdns-cn服务器

    1. 下载对应的版本,安装

      # linux-amd64服务器运行
      cd /tmp/
      wget https://github.com/IrineSistiana/mosdns-cn/releases/latest/download/mosdns-cn-linux-amd64.zip
      mkdir /usr/local/mosdns
      unzip mosdns-cn-linux-amd64.zip -d /usr/local/mosdns
      chmod  x /usr/local/mosdns/mosdns-cn
      
    2. 编写配置文件

      cat < /usr/local/mosdns/config.yaml
      server_addr: "127.0.0.1:60053"
      cache_size: 0
      lazy_cache_ttl: 0
      lazy_cache_reply_ttl: 0
      redis_cache: ""
      min_ttl: 30
      max_ttl: 0
      hosts: []
      blacklist_domain: []
      insecure: false
      ca: []
      debug: false
      log_file: ""
      upstream: []
      # 国内支持ECS的几家服务器
      local_upstream: 
        - https://dot.pub/dns-query
        - https://dns.alidns.com/dns-query
        - https://doh.360.cn/dns-query
      local_ip:
        - /usr/local/mosdns/geoip.dat:cn
      local_domain: 
        - /usr/local/mosdns/geosite.dat:cn
        - /usr/local/mosdns/geosite.dat:apple
        - /usr/local/mosdns/geosite.dat:akamai
        - /usr/local/mosdns/geosite.dat:google-cn
        - /usr/local/mosdns/geosite.dat:tld-cn
        - /usr/local/mosdns/geosite.dat:category-games@cn
      local_latency: 500
      # 根据具体情况增删注释
      remote_upstream:
      #  - udp://[2001:4860:4860::8888]
      #  - udp://8.8.8.8
      #  - udp://9.9.9.11
      #  - udp://[2620:fe::11]
        - https://dns.google/dns-query
        - https://dns11.quad9.net/dns-query
      remote_domain:
        - /usr/local/mosdns/geosite.dat:geolocation-!cn
      working_dir: ""
      cd2exe: false
      EOF
      
    3. 下载对应分流规则的geosite.dat文件和geoip.dat (由于会不定时更新规则,建议加入crontab内获取并重启mosdns-cn服务)

      curl -fSsL https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat -o /usr/local/mosdns/geosite.dat
      curl -fSsL https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat -o /usr/local/mosdns/geoip.dat
      
    4. 安装为服务

      /usr/local/mosdns/mosdns-cn --service install --config /usr/local/mosdns/config.yaml
      
  4. NGINX配置实例

    server {
      listen       443 ssl http2;
      listen       [::]:443 ssl http2;
      server_name  域名;
      
      server_tokens off;
      access_log off;
      allow 2400:cb00::/32;
      allow 2606:4700::/32;
      allow 2803:f800::/32;
      allow 2405:b500::/32;
      allow 2405:8100::/32;
      allow 2a06:98c0::/29;
      allow 2c0f:f248::/32;
      allow 173.245.48.0/20;
      allow 103.21.244.0/22;
      allow 103.22.200.0/22;
      allow 103.31.4.0/22;
      allow 141.101.64.0/18;
      allow 108.162.192.0/18;
      allow 190.93.240.0/20;
      allow 188.114.96.0/20;
      allow 197.234.240.0/22;
      allow 198.41.128.0/17;
      allow 162.158.0.0/15;
      allow 104.16.0.0/13;
      allow 104.24.0.0/14;
      allow 172.64.0.0/13;
      allow 131.0.72.0/22;
      deny all;
      ssl_protocols TLSv1.2 TLSv1.3;          # TLS 1.3 requires nginx >= 1.13.0
      ssl_certificate     CDN回源公钥pem证书路径;
      ssl_certificate_key  CDN回源私钥key证书路径;
    
      # HTTP Security Headers
      add_header X-Frame-Options DENY;
      add_header X-Content-Type-Options nosniff;
      add_header X-XSS-Protection "1; mode=block";
      add_header Strict-Transport-Security "max-age=63072000";
        
      # 按照dns-over-https的路径填写
      location /dns-query {
        limit_req zone=dns burst=5000 nodelay;
        proxy_pass       http://127.0.0.1:60053/dns-query;
        # 用于识别subnet
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $http_x_forwarded_for;
      }
        
      # 用于管理AdGuardHome的
      location /manage/ {
        proxy_pass       http://127.0.0.1:3000/;
      }
        
      # 禁止访问根目录
      location / {
        return 403;
      }
    }
    

搭建过程(docker-compose)

这个比较简单

  1. 安装docker-compose

  2. 运行

    cd ~
    cat < docker-compose.yml
    version: '3'
    
    networks:
      dns-server:
        driver: bridge
        enable_ipv6: true
        ipam:
          driver: default
          config:
            - subnet: 172.100.0.0/16
              gateway: 172.100.0.1
            - subnet: fd10::/64
              gateway: fd10::1
    
    services:
      mosdns:
        image: xuss/mosdns-cn:latest
        hostname: mosdns
        networks:
          dns-server:
            ipv4_address: 172.100.0.2
            ipv6_address: fd10::2
    
        volumes:
          - "/etc/mosdns-cn/:/etc/mosdns-cn/"
        restart: always
        
      adguardhome:
        image: adguard/adguardhome
        hostname: adguardhome
        networks:
          dns-server:
            ipv4_address: 172.100.0.3
            ipv6_address: fd10::3
        ports:
        # - "127.0.0.1:3000:3000"
          - "3000:3000"
        depends_on:
          - mosdns
        volumes:
          - "/etc/adguardhome/work/:/opt/adguardhome/work/"
          - "/etc/adguardhome/conf/:/opt/adguardhome/conf/"
        restart: always
        
      doh-server:
        image: satishweb/doh-server:latest
        hostname: doh-server
        ports:
        # - "127.0.0.1:60053:60053"
          - "60053:60053"
        networks:
          dns-server:
            ipv4_address: 172.100.0.4
            ipv6_address: fd10::4
    
        environment:
          UPSTREAM_DNS_SERVER: "udp:adguardhome:53"
          DOH_HTTP_PREFIX: "/dns-query"
          DOH_SERVER_LISTEN: ":60053"
          DOH_SERVER_TIMEOUT: "1"
          DOH_SERVER_TRIES: "10"
          DOH_SERVER_VERBOSE: "false"
        depends_on:
          - mosdns
          - adguardhome
        restart: always
    EOF
    
    mkdir /etc/mosdns-cn
    curl -fSsL https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat -o /etc/mosdns-cn/geosite.dat
    curl -fSsL https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat -o /etc/mosdns-cn/geoip.dat
    
    cat < /etc/mosdns-cn/config.yaml
    server_addr: "0.0.0.0:53"
    cache_size: 0
    lazy_cache_ttl: 0
    lazy_cache_reply_ttl: 0
    redis_cache: ""
    min_ttl: 30
    max_ttl: 0
    hosts: []
    blacklist_domain: []
    insecure: false
    ca: []
    debug: false
    log_file: ""
    upstream: []
    # 国内支持ECS的几家服务器
    local_upstream: 
      - https://dot.pub/dns-query
      - https://dns.alidns.com/dns-query
      - https://doh.360.cn/dns-query
    local_ip:
      - /etc/mosdns-cn/geoip.dat:cn
    local_domain: 
      - /etc/mosdns-cn/geosite.dat:cn
      - /etc/mosdns-cn/geosite.dat:apple
      - /etc/mosdns-cn/geosite.dat:akamai
      - /etc/mosdns-cn/geosite.dat:google-cn
      - /etc/mosdns-cn/geosite.dat:tld-cn
      - /etc/mosdns-cn/geosite.dat:category-games@cn
    local_latency: 500
    # 根据具体情况增删注释
    remote_upstream:
    #  - udp://[2001:4860:4860::8888]
    #  - udp://8.8.8.8
    #  - udp://9.9.9.11
    #  - udp://[2620:fe::11]
      - https://dns.google/dns-query
      - https://dns11.quad9.net/dns-query
    remote_domain:
      - /etc/mosdns-cn/geosite.dat:geolocation-!cn
    working_dir: ""
    cd2exe: false
    EOF
    
    sudo docker-compose up --build -d
    
  3. 访问http://IP:3000,如图配置即可,过滤器自行添加。

  4. doh服务器为http://IP:60053,后面自行按照教程配置NGINX反向代理(部分可参考上面源码部分)。

客户端选择

  1. AdGuardHome (安卓/iOS/电脑?)

  2. 开发者描述文件,按情况更改保存为后缀名为.mobileconfig文件即可安装(iOS)

    
    
    
    	
    		PayloadContent
    		
    			
    				DNSSettings
    				
    					DNSProtocol
    					HTTPS
    					ServerName
    					域名
    					ServerURL
    					https://域名/dns-query
    				
    				Name
    				DOH名称
    				PayloadDescription
    				DOH描述
    				PayloadDisplayName
    				DOH名称
    				PayloadIdentifier
    				com.apple.dnsSettings.managed.73aaaab3-6666-6666-6666-4305b5bc8123
    				PayloadType
    				com.apple.dnsSettings.managed
    				PayloadUUID
    				694c5006-6666-6666-6666-bcc536baa123
    				PayloadVersion
    				1
    			
    		
    		PayloadDescription
    		安装后可在:设置-通用-DNS与设备管理 里进行管理选择。
    		PayloadDisplayName
    		DOH名称
    		PayloadIdentifier
    		7326b27a-6666-6666-6666-a66b6b25c123
    		PayloadRemovalDisallowed
    		
    		PayloadType
    		Configuration
    		PayloadUUID
    		1275e446-6666-6666-6666-87ba446a8123
    		PayloadVersion
    		1
    	
    
    

取舍

  1. AdGuardHome采用严格的证书校验形式(CDN回源证书无法通过校验),不可避免地需要普通的前端服务器来代替它。
  2. 采用NGINX更加灵活的限制客户端速率、增加源IP头等
  3. 没想到了。
点赞
  1. ntcebm5-neea说道:

    @Just纱世里

    瞅瞅看不看得懂吧,感觉没有写教程的天赋

  2. gm说道:

    为啥不用openwrt

  3. pioneer说道:

    好东西

回复 pioneer 取消回复

电子邮件地址不会被公开。必填项已用 * 标注

×
订阅图标按钮