Mosdns V5 Config

一键安装脚本

install_mosdns.sh

 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
#!/bin/bash

# 设置变量
MOSDNS_VERSION="v5.3.3"
DOWNLOAD_URL="https://github.com/IrineSistiana/mosdns/releases/download/${MOSDNS_VERSION}/mosdns-linux-amd64.zip"
INSTALL_DIR="/usr/local/bin"
CONFIG_DIR="/etc/mosdns"
SERVICE_FILE="/etc/systemd/system/mosdns.service"

# 创建临时目录
TEMP_DIR=$(mktemp -d)

echo "Installing mosdns ${MOSDNS_VERSION}..."

# 下载并解压缩 mosdns
echo "Downloading mosdns..."
curl -L -o "${TEMP_DIR}/mosdns.zip" "${DOWNLOAD_URL}"

echo "Extracting mosdns..."
unzip -q "${TEMP_DIR}/mosdns.zip" -d "${TEMP_DIR}"

# 安装到 /usr/local/bin
echo "Installing mosdns to ${INSTALL_DIR}..."
sudo install -m 755 "${TEMP_DIR}/mosdns" "${INSTALL_DIR}/mosdns"

# 创建配置目录
echo "Creating configuration directory at ${CONFIG_DIR}..."
sudo mkdir -p "${CONFIG_DIR}"

# 清理临时文件
echo "Cleaning up..."
rm -rf "${TEMP_DIR}"

# 创建 systemd 服务文件
echo "Creating systemd service file at ${SERVICE_FILE}..."
sudo bash -c "cat > ${SERVICE_FILE}" <<EOL
[Unit]
Description=MosDNS v5 Service
After=network.target

[Service]
ExecStart=${INSTALL_DIR}/mosdns start -c ${CONFIG_DIR}/config.yaml
WorkingDirectory=${CONFIG_DIR}
Restart=on-failure
RestartSec=5s
User=nobody
Group=nogroup
LimitNOFILE=1048576
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
EOL

# 重新加载 systemd 配置,并启用和启动 mosdns 服务
echo "Enabling and starting mosdns service..."
sudo systemctl daemon-reload
sudo systemctl enable mosdns
# sudo systemctl start mosdns

echo "MosDNS installation complete."
echo "run systemctl start mosdns to start mosdns service."

配置

抄自 https://github.com/IrineSistiana/mosdns/discussions/624

 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
log:
  level: info
  production: true

plugins: 
  - tag: "direct_domain"
    type: domain_set
    args:
      files:
        - "./accelerated-domains.china.txt"

  - tag: "direct_ip"
    type: ip_set
    args:
      files:
        - "./all_cn.txt"

  - tag: "hosts"
    type: hosts
    args:
      files:                       
        - "./hosts.txt"             

  - tag: "cache"
    type: cache
    args:
      size: 65536
      lazy_cache_ttl: 86400
      # dump_file: ./cache.dump

  - tag: "remote_forward"
    type: forward
    args:
      concurrent: 1
      upstreams: 
        - addr: "udp://10.10.0.143"

  - tag: "local_forward"
    type: forward
    args:
      concurrent: 1
      upstreams: 
        - addr: "udp://10.10.0.145"

  - tag: "ttl_sequence"
    type: sequence
    args:
      - exec: ttl 600-3600
      - exec: accept

  - tag: "local_sequence"
    type: sequence
    args:
      - exec: query_summary local_forward
      - exec: $local_forward
      - exec: goto ttl_sequence

  - tag: "remote_sequence"
    type: sequence
    args:
      - exec: query_summary remote_forward
      - exec: $remote_forward
      - matches: "resp_ip $direct_ip"
        exec: goto local_sequence
      - exec: goto ttl_sequence

  - tag: "final"
    type: fallback
    args:
      primary: remote_sequence
      secondary: local_sequence
      threshold: 500
      always_standby: true

  - tag: "main_sequence"
    type: sequence
    args:
      - exec: prefer_ipv4
      - exec: $hosts
      - exec: $cache
      - matches: has_resp
        exec: accept
      - matches: "qname $direct_domain"
        exec: goto local_sequence
      - exec: $remote_sequence


  - tag: udp_server
    type: udp_server
    args:
      entry: main_sequence
      listen: :53

UPDATE(2024/09/05)

  • 85行改为 $remote_sequence, 由于mosdns的fallback插件是并行请求, 这样就无法达成分流的目的了
  • 78行取消注释, 还是不追求v6了

其中两个txt

1
2
3
4
5
6
7
cd /etc/mosdns

wget https://mirror.ghproxy.com/https://raw.githubusercontent.com/yubanmeiqin9048/domain/release/accelerated-domains.china.txt

wget https://ispip.clang.cn/all_cn.txt

touch hosts.txt

两个adguard home

国外: udp://10.10.0.143

1
2
tls://dns.google
127.0.0.1:5335

UPDATE(2024/09/05):

  • 在国外的adguard home上增加了一个dnscrypt-proxy, 监听在127.0.0.1:5335. 让其可以过ipleak.net的检测
  • Adguard home 开启了DNSSEC选项

国内: udp://10.10.0.145

1
2
119.29.29.29
https://doh.pub/dns-query

启动

1
systemctl start mosdns

速度飞快, 基本秒开

后续再优化CDN的解析最近节点问题, 以及DDNS域名(在开启lazy cache时)的问题

备用

Licensed under CC BY-NC-SA 4.0
最后更新于 2024-09-05 21:09
记录平时瞎折腾遇到的各种问题, 方便查找
使用 Hugo 构建
主题 Stack 3.29.0Jimmy 设计