# YLTX-MosDNS - 智能防污染 DNS 服务器




**基于 MosDNS v5 的增强版本 - 专注于智能防污染和易用性**
[功能特性](#-功能特性) | [快速开始](#-快速开始) | [文档](#-文档) | [部署方式](#-部署方式)
---
## 🌟 项目简介
YLTX-MosDNS 是基于 [MosDNS v5](https://github.com/IrineSistiana/mosdns) 的二次开发版本,在保留原有强大功能的基础上,增加了:
- **🛡️ 智能防污染系统** - 自动检测和切换 DNS,解决 DNS 污染问题
- **🎨 Web 管理界面** - Vue 3 可视化管理,无需编辑配置文件
- **🔄 配置热加载** - 零停机更新配置,服务不中断
- **⚡ 一键部署** - `init` 命令自动初始化,3 步快速启动
- **🧠 智能拓扑排序** - 自动分析插件依赖,支持任意配置顺序
- **📡 MikroTik 集成优化** - 自动同步 DNS 解析到路由器地址列表
**适用场景**: 家庭/办公室网络、VPS、软路由、树莓派、NAS
---
## ✨ 功能特性
### 🛡️ 智能防污染系统
**核心功能**: 自动检测 DNS 污染并切换上游
```yaml
plugins:
- tag: smart_fallback
type: smart_fallback
args:
primary: forward_local # 主上游(国内DNS)
secondary: forward_remote # 备用上游(国外DNS)
china_ip: ["./data/chn_ip.txt"] # CN IP 地址表
timeout: 3000 # 超时时间(毫秒)
verbose: true # 详细日志
```
**工作原理**:
1. 优先使用国内 DNS 查询(速度快)
2. 检测返回的 IP 是否为国内 IP
3. 如果是污染 IP,自动切换到国外 DNS
4. 支持并行/顺序两种模式
### 🎨 Web 管理界面
**特性**:
- ✅ Vue 3 + TypeScript + Element Plus
- ✅ 实时监控 DNS 查询统计
- ✅ 可视化规则管理(添加/编辑/删除)
- ✅ 插件状态监控
- ✅ 配置文件在线编辑
- ✅ 一键热加载配置
**访问地址**: `http://localhost:5555`
### 🔄 配置热加载
**零停机更新配置** - 无需重启服务
```bash
# 方式1: Web 界面点击"热加载配置"按钮
# 方式2: API 调用
curl -X POST http://localhost:5555/api/config/reload
```
**特性**:
- ✅ 自动验证新配置
- ✅ 失败自动回滚
- ✅ 保持 DNS 服务不中断
- ✅ 详细的加载日志
### ⚡ 一键部署 (init 命令)
**3 步快速启动**:
```bash
# 1. 初始化配置
./mosdns-linux-amd64 init
# 2. 修改端口(可选,非 root 用户)
sed -i 's/:53/:5310/g' config.yaml
# 3. 启动服务
./mosdns-linux-amd64 start -c config.yaml
```
**自动生成**:
- ✅ 完整的 `config.yaml` 配置文件
- ✅ 目录结构(data/, config.d/rules/, logs/)
- ✅ 示例数据文件(CN IP、域名列表)
### 🧠 智能拓扑排序
**自动分析插件依赖关系** - 支持任意配置顺序
```yaml
# ✅ 无需关心插件顺序,自动排序
plugins:
- tag: udp_server # 依赖 main
- tag: main # 依赖 cache, forward
- tag: cache # 无依赖
- tag: forward # 无依赖
```
**特性**:
- ✅ 自动检测 `$plugin_name` 引用
- ✅ 识别 `entry:` 字段依赖
- ✅ 循环依赖检测
- ✅ 详细错误提示
### 📡 MikroTik 集成
**自动同步 DNS 解析到 MikroTik 路由器**
```yaml
plugins:
- tag: mikrotik_sync
type: mikrotik_addresslist
args:
host: "192.168.1.1"
username: "admin"
password: "password"
address_list: "blocked_sites"
mask: 32 # 单 IP 精确匹配
max_ips: 10000 # 最大 IP 数量
cache_ttl: 3600 # 缓存时间
```
**应用场景**:
- 🎯 自动添加特定域名的 IP 到地址列表
- 🎯 配合 MikroTik 防火墙规则
- 🎯 实现智能分流(游戏加速、广告拦截等)
### 🔌 完整的 RESTful API
**管理接口**: `http://localhost:8080`
| 端点 | 方法 | 说明 |
|------|------|------|
| `/api/server/info` | GET | 服务器信息 |
| `/api/plugins` | GET | 插件列表 |
| `/api/rules` | GET/POST/PUT/DELETE | 规则管理 |
| `/api/config/reload` | POST | 热加载配置 |
| `/api/cache/stats` | GET | 缓存统计 |
| `/api/cache/flush` | POST | 清空缓存 |
**示例**:
```bash
# 查看服务器状态
curl http://localhost:8080/api/server/info | jq
# 热加载配置
curl -X POST http://localhost:5555/api/config/reload
```
---
## 🚀 快速开始
### 方式 1: 一键部署(推荐)
```bash
# 1. 下载程序
wget https://git.ylcomm.cn/dengxiongjian/mosdns/releases/latest/download/mosdns-linux-amd64
chmod +x mosdns-linux-amd64
# 2. 初始化
./mosdns-linux-amd64 init
# 3. 启动(非 root 用户修改端口)
sed -i 's/:53/:5310/g' config.yaml
./mosdns-linux-amd64 start -c config.yaml
```
**完成!** 访问:
- Web UI: http://localhost:5555
- API: http://localhost:8080
- DNS: localhost:53 (或 5310)
### 方式 2: systemd 服务
```bash
# 1. 复制程序到系统目录
sudo cp mosdns-linux-amd64 /usr/local/bin/mosdns
sudo chmod +x /usr/local/bin/mosdns
# 2. 创建配置目录
sudo mkdir -p /etc/mosdns
cd /etc/mosdns
sudo mosdns init
# 3. 创建 systemd 服务
sudo tee /etc/systemd/system/mosdns.service > /dev/null <
**⭐ 如果这个项目对你有帮助,请给个 Star!⭐**
**🌟 Star this project if it helps you! 🌟**
Made with ❤️ by YLTX Team