# mosdns v4

版本: v4.5.0

## 功能概述

mosdns 是一个插件化的 DNS 转发器。用户可以按需拼接插件，定制自己的 DNS 处理逻辑。

## 插件概述

以下是 mosdns 以及自带插件的功能:

* 匹配器:
  * `query_matcher`: 匹配请求的特性。域名，类型，来源 IP 等。
  * `response_matcher`: 匹配应答的特性。应答 IP，CNAME 等。
* 常用功能:
  * `forward`: 转发请求至上游服务器。
  * `cache`: 缓存应答。支持 redis 外部缓存。
  * `_prefer_ipv4/6`: 自动判断域名是否是双栈域名然后屏蔽 IPv4/6 请求，不会影响纯 IPv6/4 域名。
  * `ecs`: 在请求上附加 ECS。可以是预设 IP 也可以自动使用客户端的 IP。
  * `hosts`: 为域名设定 IP。
  * `blackhole`: 能丢弃应答，生成空应答，或者生成包含特定 IP 应答。用于屏蔽请求。
  * `ttl`: 修改应答的 TTL。
  * `redirect`: 替换(重定向)请求的域名。请求域名 A，但返回域名 B 的记录。
  * `padding`: 将加密 DNS 的报文填充至固定长度，防止流量分析。
  * `bufsize`: 修改请求的 EDNS0 的 UDP Size。防止 UDP 碎片。
  * `arbitrary`: 面向高级用户。可以手动构建包含任意记录的应答。
  * `reverse_lookup` : 可通过 IP 反查经过 mosdns 处理的域名。支持处理 PTR 请求，支持通过 HTTP 接口查询。
  * `client_limiter`: 限制客户端的最大 QPS。
* 动态路由
  * `ipset`: 将应答 IP 写入 ipset。
  * `nftset`: 将应答 IP 写入 nftables。

插件详细说明在

{% content-ref url="/pages/GQUrh0OKaMvcZ1OXZt9I" %}
[插件及其参数](/mosdns-wiki/mosdns-v4/cha-jian-ji-qi-can-shu.md)
{% endcontent-ref %}

还可以添加自己编写的插件到 mosdns 内。详见

{% content-ref url="/pages/nuEVfWziXnpYZECDeAqA" %}
[如何编写新插件](/mosdns-wiki/mosdns-v4/ru-he-bian-xie-xin-cha-jian.md)
{% endcontent-ref %}

## 如何配置

配置说明和示例，详见:

{% content-ref url="/pages/WBt2J73ng9HzsPmcdRqT" %}
[如何配置 mosdns](/mosdns-wiki/mosdns-v4/ru-he-pei-zhi-mosdns.md)
{% endcontent-ref %}

## 如何使用

mosdns 只有一个二进制文件，命令行运行即可。

```bash
mosdns start -c config_file -d working_dir​
```

mosdns 还附带一些常用小工具命令。

* probe 可以探测 TCP/TLS 服务器是否支持连接复用，pipeline 连接复用，空闲连接保持时间。
* config 可以生成 yaml 配置文件模板，转换配置文件格式。
* service 可以将 mosdns 安装成系统服务。

详见各个子命令的帮助。

```bash
mosdns -h
mosdns probe -h
```

### 安装至系统服务

mosdns service 是一个简单的系统服务管理工具。可将 mosdns 安装成系统服务实现自启。需要管理员或 root 权限。理论上可用于 Windows XP+, Linux/(systemd | Upstart | SysV), 和 OSX/Launchd 平台。Windows，Ubuntu，Debian 实测可用。Openwrt 不可用。

```bash
# 安装服务
mosdns service install -d 工作目录绝对路径 -c 配置文件路径
# 安装成功后手动运行服务。(服务仅设定为随系统自启，安装成功后并不会马上自动运行)
mosdns service start

# 卸载
mosdns service stop
mosdns service uninstall
```

## 更多工具

开放列表，排名不分前后。欢迎提交 issue 编辑本列表。

### Openwrt

用于 Openwrt 系统的 mosdns。自带 Luci 管理界面。

[sbwml / luci-app-mosdns](https://github.com/sbwml/luci-app-mosdns)

[QiuSimons/openwrt-mos](https://github.com/QiuSimons/openwrt-mos)

### [pmkol/easymosdns](https://github.com/pmkol/easymosdns)

适用于 Linux 的辅助脚本。借助 mosdns，仅需几分钟即可搭建一台支持 ECS 的无污染 DNS 服务器。内置中国大陆地区的优化规则，满足DNS日常使用场景，开箱即用。

### [mosdns-cn](https://github.com/IrineSistiana/mosdns-cn)

简单易用加密 DNS 转发器。内置域名 & IP 分流策略，只需用户提供上游服务器地址和分流用的域名 IP 表，即可实现的本地/远程域名分流。

### [cfdohpw](https://github.com/IrineSistiana/cfdohpw)

一个 Cloudflare Workers 脚本。借助 Cloudflare CDN 平台中转加速任意 DoH (RFC8484) 流量。只需一个 Cloudflare 账号，无需自备服务器和域名。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://irine-sistiana.gitbook.io/mosdns-wiki/mosdns-v4.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
