mosdns/快速参考.md
dengxiongjian 0413ee5d44
Some checks failed
Test mosdns / build (push) Has been cancelled
二次开发
2025-10-16 21:07:48 +08:00

345 lines
6.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# YLTX-DNS 快速参考卡
> 二次开发核心功能快速查阅
---
## 🎯 核心亮点
```
┌─────────────────────────────────────────────────────┐
│ 🛡️ 智能防污染 │ 🔧 自动排序 │ 🖥️ Web界面 │
│ │
│ 先查国内DNS │ 任意配置顺序 │ 零YAML编写 │
│ 检测污染IP │ 自动调整加载 │ 表单化操作 │
│ 自动切换国外 │ 循环依赖检测 │ 可视化管理 │
└─────────────────────────────────────────────────────┘
```
---
## ⚡ 快速开始
### 编译 (3步)
```bash
cd web-ui && npm install && npm run build && cd ..
go build -ldflags="-s -w" -o mosdns .
./mosdns -c config.yaml
```
### 访问
```
Web: http://localhost:5555
DNS: 53, 5353
```
---
## 📁 核心文件
| 文件 | 功能 | 行数 |
|------|------|------|
| `pkg/utils/toposort.go` | 拓扑排序 | 145 |
| `coremain/config_validator.go` | 配置验证 | 293 |
| `coremain/config_builder.go` | 配置生成 | 429 |
| `plugin/executable/smart_fallback/` | 智能防污染 | 270 |
| `coremain/rule_handlers.go` | API | 639 |
| `web-ui/` | Vue界面 | 2000+ |
---
## 🔌 主要API
```
GET /api/server-info # 系统状态
GET /api/stats # 查询统计
GET /api/rules # 规则列表
GET /api/rules/{name} # 规则详情
POST /api/rules # 添加规则
PUT /api/rules/{name} # 更新规则
DELETE /api/rules/{name} # 删除规则
```
---
## 🎨 添加规则示例
### Web界面 (推荐)
```
1. 访问 http://localhost:5555
2. 点击「域名路由规则」→「添加规则」
3. 填写表单:
- 规则名: openai
- 域名文件: /data/mikrotik/openai.txt
- DNS策略: 智能防污染
- MikroTik: 启用
4. 保存 → 重启服务
```
### API调用
```bash
curl -X POST http://localhost:5541/api/rules \
-H "Content-Type: application/json" \
-d '{
"name": "openai",
"domain_file": "/data/mikrotik/openai.txt",
"dns_strategy": "smart-fallback",
"enable_mikrotik": true,
"mikrotik_config": {
"host": "192.168.1.1",
"port": 8728,
"username": "admin",
"password": "123456",
"address_list": "openai"
}
}'
```
---
## 🛡️ 智能防污染配置
```yaml
- tag: smart_fallback_handler
type: smart_fallback
args:
primary: $china-dns # 国内DNS
secondary: $overseas-dns # 国际DNS
china_ip:
- "/data/chn_ip.txt" # CN地址表
timeout: 2000 # 2秒超时
always_standby: false # 顺序查询
verbose: true # 详细日志
```
---
## 📊 性能数据
```
国内域名: 20-30ms
国外域名: 30-50ms (无污染)
80-120ms (有污染)
缓存命中: <5ms
并发能力: 3000-5000 qps (单核)
内存占用: 30-150MB
CPU占用: <5% (1000 qps)
```
---
## 🔧 故障排查
### 启动失败
```bash
# 配置验证
./mosdns -c config.yaml -dry-run
# 查看日志
journalctl -u mosdns -n 50
```
### Web无法访问
```bash
# 检查端口
netstat -tlnp | grep 5555
# 检查配置
grep "web:" config.yaml
```
### 防污染不生效
```bash
# 检查CN表
ls -lh data/chn_ip.txt
# 启用详细日志
# config.yaml → verbose: true
```
---
## 🎯 配置示例
### 国内DNS
```yaml
- tag: rule_baidu
type: sequence
args:
exec:
- matches: qname $domains_baidu
exec: $china-dns
```
### 国外DNS
```yaml
- tag: rule_netflix
type: sequence
args:
exec:
- matches: qname $domains_netflix
exec: $overseas-dns
```
### 智能防污染
```yaml
- tag: rule_openai
type: sequence
args:
exec:
- matches: qname $domains_openai
exec: $smart-fallback
```
---
## 📦 目录结构
```
mosdns/
├── config.yaml # 主配置
├── config.d/
│ └── rules/ # 规则配置
│ ├── openai.yaml
│ └── netflix.yaml
├── data/
│ ├── chn_ip.txt # CN地址表
│ └── mikrotik/ # 域名文件
│ ├── openai.txt
│ └── netflix.txt
├── web-ui/
│ ├── src/ # Vue源码
│ └── dist/ # 构建输出
├── pkg/utils/ # 工具包
├── coremain/ # 核心代码
└── plugin/ # 插件
```
---
## 🚀 升级计划
### v2.0 (计划中)
- [ ] 热重载配置
- [ ] 规则导入导出
- [ ] 多用户权限
- [ ] Docker镜像
- [ ] K8s Helm Chart
---
## 📚 文档索引
| 文档 | 说明 |
|------|------|
| [二次开发总结](./YLTX-DNS智能防污染系统-二次开发总结.md) | 完整功能介绍 |
| [README](./README-二开版本.md) | 项目说明 |
| [功能清单](./功能实现清单.md) | 详细清单 |
| [错误修复](./错误修复总结.md) | Bug修复 |
| [架构设计](./yltx-dns-智能防污染系统-架构设计文档.md) | 技术架构 |
---
## 🎁 关键创新
### 1⃣ 配置顺序自由
```yaml
# ❌ 传统: 必须严格顺序
plugins:
- tag: upstream
- tag: main
exec: $upstream
# ✅ YLTX-DNS: 任意顺序
plugins:
- tag: main
exec: $upstream # OK!
- tag: upstream
```
### 2⃣ 智能污染检测
```
国内DNS → 127.0.0.1 → 检测CN表 → ❌
→ 自动切换国际DNS → 104.18.xxx.xxx → ✅
```
### 3⃣ 零配置门槛
```
表单填写 → 自动生成YAML → 一键启用
```
---
## 💡 最佳实践
### 1. 域名文件组织
```
data/mikrotik/
├── ai/
│ ├── openai.txt
│ ├── claude.txt
│ └── gemini.txt
├── video/
│ ├── netflix.txt
│ └── youtube.txt
└── social/
└── twitter.txt
```
### 2. DNS策略选择
```
国内网站 → 国内DNS
国外网站 → 国外DNS
AI服务 → 智能防污染 (推荐)
流媒体服务 → 国外DNS
```
### 3. MikroTik配置
```
常用服务 → mask: 32 (单IP)
大型服务 → mask: 24 (子网)
超大服务 → mask: 20 (更大子网)
```
---
## 🔥 常用命令
```bash
# 编译
go build -o mosdns .
# 启动
./mosdns -c config.yaml
# 后台运行
nohup ./mosdns -c config.yaml &
# 重启服务
systemctl restart mosdns
# 查看日志
journalctl -u mosdns -f
# 配置验证
./mosdns -c config.yaml -dry-run
# 性能测试
ab -n 10000 -c 100 http://localhost:5541/api/server-info
```
---
## 📞 获取帮助
- 📖 完整文档: `YLTX-DNS智能防污染系统-二次开发总结.md`
- 🐛 问题反馈: GitHub Issues
- 💬 技术讨论: GitHub Discussions
---
**⚡ 快速、稳定、智能的DNS解决方案**
*最后更新: 2025-10-15*