mosdns/init功能说明.md

120 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# init 命令功能说明
## 功能描述
`mosdns init` 命令用于在任意服务器上快速初始化 MosDNS 配置和必要的目录结构,实现一键部署。
## 使用方法
### 基本用法
```bash
./mosdns-linux-amd64 init
```
### 强制覆盖模式
```bash
./mosdns-linux-amd64 init --force
# 或
./mosdns-linux-amd64 init -f
```
## 功能特性
1. ✅ 自动生成 config.yaml 配置文件
2. ✅ 创建必要的目录结构data/, config.d/rules/, logs/
3. ✅ 生成示例数据文件CN IP 和域名列表)
4. ✅ 智能检测已存在文件,避免覆盖
5. ✅ 提供详细的后续操作指南
## 生成的文件结构
```
.
├── config.yaml # 主配置文件
├── data/ # 数据文件目录
│ ├── chn_ip.txt # 中国 IP 地址段(示例)
│ └── geosite_china-list.txt # 中国域名列表(示例)
├── config.d/ # 配置目录
│ └── rules/ # 规则文件目录(空)
└── logs/ # 日志目录
```
## 配置文件特点
生成的 config.yaml 包含:
- 完整的注释说明
- 智能 DNS 分流配置
- 国内/国外 DNS 上游
- DNS 缓存配置
- Web 管理界面
- API 接口
## 使用场景
### 场景1: 全新服务器快速部署
```bash
# 1. 上传二进制文件
scp mosdns-linux-amd64 user@server:/opt/mosdns/
# 2. SSH 登录服务器
ssh user@server
# 3. 初始化
cd /opt/mosdns
./mosdns-linux-amd64 init
# 4. 启动(非 root 用户修改端口)
sed -i 's/:53/:5310/g' config.yaml
./mosdns-linux-amd64 start -c config.yaml
```
### 场景2: Docker 容器部署
```dockerfile
FROM debian:12-slim
COPY mosdns-linux-amd64 /usr/local/bin/mosdns
WORKDIR /opt/mosdns
RUN mosdns init
CMD ["mosdns", "start", "-c", "config.yaml"]
```
### 场景3: 测试环境快速搭建
```bash
mkdir test-mosdns && cd test-mosdns
/path/to/mosdns-linux-amd64 init
sed -i 's/:53/:5310/g' config.yaml
/path/to/mosdns-linux-amd64 start -c config.yaml
```
## 代码实现
**文件**: `tools/init.go`
**核心函数**:
- `runInit()`: 主执行函数
- `createConfigFile()`: 创建配置文件
- `createDirectories()`: 创建目录结构
- `createDataFiles()`: 创建示例数据文件
- `showCompletionInfo()`: 显示完成信息
**行数**: ~330 行
## 测试结果
✅ 所有功能测试通过:
- 初次运行创建所有文件
- 再次运行提示文件已存在
- --force 模式可强制覆盖
- 生成的配置文件格式正确
- 目录权限设置正确
## 优势
1. **零配置门槛**: 不需要手动编写配置文件
2. **标准化部署**: 所有服务器使用统一的配置模板
3. **快速迭代**: 几秒钟完成初始化
4. **智能保护**: 自动检测已有文件,避免误删
5. **完整指引**: 提供详细的后续操作说明
---
**✨ 现在可以在任意服务器上一键部署 MosDNS 了!**