# MosDNS




**一个插件化的 DNS 转发器**
[English](#english) | [中文说明](#中文说明)
## 中文说明
### 🚀 项目简介
MosDNS 是一个插件化的 DNS 转发器,旨在为用户提供高度可定制的 DNS 解析服务。通过灵活的插件系统和配置方式,可以实现复杂的 DNS 处理逻辑,包括但不限于:
- 智能分流(国内外域名分流)
- DNS 缓存和优化
- 广告拦截和恶意域名过滤
- 自定义 DNS 解析规则
- 多种上游 DNS 支持
- 网络设备集成(如 MikroTik)
### ✨ 核心特性
#### 🧩 插件化架构
- **模块化设计**:每个功能都是独立的插件,可按需加载
- **灵活组合**:通过序列(sequence)组合多个插件实现复杂逻辑
- **易于扩展**:支持自定义插件开发
#### 🌐 智能分流
- **地理位置感知**:自动识别国内外域名并使用不同的上游 DNS
- **域名匹配**:支持多种域名匹配规则(精确匹配、通配符、正则表达式)
- **IP 段匹配**:根据解析结果的 IP 地址进行后续处理
#### ⚡ 性能优化
- **智能缓存**:多级缓存机制,显著提升解析速度
- **并发处理**:高并发 DNS 查询处理能力
- **内存优化**:高效的内存管理和资源池
#### 🔧 网络设备集成
- **MikroTik 支持**:自动将解析的 IP 地址添加到 MikroTik 地址列表
- **IPSet/NFTables**:Linux 防火墙规则集成
- **实时同步**:DNS 解析结果实时同步到网络设备
### 📁 项目结构
```
mosdns/
├── coremain/ # 核心主程序
├── pkg/ # 核心功能包
│ ├── cache/ # 缓存实现
│ ├── dnsutils/ # DNS 工具函数
│ ├── matcher/ # 匹配器(域名、IP)
│ ├── server/ # DNS 服务器实现
│ └── upstream/ # 上游 DNS 客户端
├── plugin/ # 插件系统
│ ├── executable/ # 可执行插件
│ │ ├── cache/ # 缓存插件
│ │ ├── forward/ # 转发插件
│ │ ├── sequence/ # 序列插件
│ │ ├── mikrotik_addresslist/ # MikroTik 集成
│ │ └── ... # 其他插件
│ ├── matcher/ # 匹配插件
│ └── server/ # 服务器插件
├── scripts/ # 部署脚本
└── tools/ # 辅助工具
```
### 🚀 快速开始
#### 1. 下载安装
```bash
# 下载预编译二进制文件
wget https://github.com/IrineSistiana/mosdns/releases/latest/download/mosdns-linux-amd64.zip
# 或使用 Docker
docker pull irinesistiana/mosdns
```
#### 2. 基础配置
```yaml
# config.yaml
log:
level: info
plugins:
# 转发到公共 DNS
- tag: forward_google
type: forward
args:
upstream:
- addr: "8.8.8.8:53"
# 主序列
- tag: main_sequence
type: sequence
args:
- exec: forward_google
servers:
# DNS 服务器
- exec: udp_server
args:
entry: main_sequence
listen: ":53"
```
#### 3. 启动服务
```bash
# 直接运行
./mosdns start -c config.yaml
# 或使用 Docker
docker run -d -p 53:53/udp -v ./config.yaml:/etc/mosdns/config.yaml irinesistiana/mosdns
```
### 💡 高级功能
#### 智能分流配置
```yaml
plugins:
# 国内域名
- tag: cn_domains
type: domain_set
args:
files: ["china-list.txt"]
# 国外域名
- tag: gfw_domains
type: domain_set
args:
files: ["gfw-list.txt"]
# 智能分流序列
- tag: smart_sequence
type: sequence
args:
- if: qname $cn_domains
exec: forward_cn_dns
- if: qname $gfw_domains
exec: forward_foreign_dns
- exec: forward_default
```
#### MikroTik 集成
```yaml
plugins:
- tag: mikrotik_integration
type: mikrotik_addresslist
args:
host: "192.168.1.1"
username: "admin"
password: "password"
address_list4: "blocked_ips"
add_all_ips: true # 添加所有解析的 IP
mask4: 32 # 单个 IP 精确匹配
```
### 📖 文档和资源
- **详细文档**: [Wiki](https://irine-sistiana.gitbook.io/mosdns-wiki/)
- **下载地址**: [Releases](https://github.com/IrineSistiana/mosdns/releases)
- **Docker 镜像**: [Docker Hub](https://hub.docker.com/r/irinesistiana/mosdns)
- **配置示例**: [examples/](./examples/)
### 🤝 贡献
欢迎提交 Issue 和 Pull Request!请确保:
1. 代码符合 Go 语言规范
2. 添加必要的测试
3. 更新相关文档
### 📄 许可证
本项目采用 GPL v3 许可证。详见 [LICENSE](./LICENSE) 文件。
---
## English
### 🚀 Introduction
MosDNS is a plugin-based DNS forwarder designed to provide highly customizable DNS resolution services. Through a flexible plugin system and configuration approach, it can implement complex DNS processing logic, including but not limited to:
- Smart DNS routing (domestic/foreign domain splitting)
- DNS caching and optimization
- Ad blocking and malicious domain filtering
- Custom DNS resolution rules
- Multiple upstream DNS support
- Network device integration (e.g., MikroTik)
### ✨ Key Features
#### 🧩 Plugin Architecture
- **Modular Design**: Each function is an independent plugin, loaded as needed
- **Flexible Composition**: Combine multiple plugins through sequences for complex logic
- **Easy Extension**: Support for custom plugin development
#### 🌐 Smart Routing
- **Geo-aware**: Automatically identify domestic/foreign domains and use different upstream DNS
- **Domain Matching**: Support various domain matching rules (exact, wildcard, regex)
- **IP Range Matching**: Process based on resolved IP addresses
#### ⚡ Performance Optimization
- **Smart Caching**: Multi-level caching mechanism for significant speed improvements
- **Concurrent Processing**: High-concurrency DNS query handling
- **Memory Optimization**: Efficient memory management and resource pooling
#### 🔧 Network Device Integration
- **MikroTik Support**: Automatically add resolved IPs to MikroTik address lists
- **IPSet/NFTables**: Linux firewall rule integration
- **Real-time Sync**: DNS resolution results synced to network devices in real-time
### 🚀 Quick Start
#### 1. Installation
```bash
# Download pre-built binary
wget https://github.com/IrineSistiana/mosdns/releases/latest/download/mosdns-linux-amd64.zip
# Or use Docker
docker pull irinesistiana/mosdns
```
#### 2. Basic Configuration
```yaml
# config.yaml
log:
level: info
plugins:
# Forward to public DNS
- tag: forward_google
type: forward
args:
upstream:
- addr: "8.8.8.8:53"
# Main sequence
- tag: main_sequence
type: sequence
args:
- exec: forward_google
servers:
# DNS server
- exec: udp_server
args:
entry: main_sequence
listen: ":53"
```
#### 3. Start Service
```bash
# Run directly
./mosdns start -c config.yaml
# Or use Docker
docker run -d -p 53:53/udp -v ./config.yaml:/etc/mosdns/config.yaml irinesistiana/mosdns
```
### 📖 Documentation
- **Detailed Docs**: [Wiki](https://irine-sistiana.gitbook.io/mosdns-wiki/)
- **Downloads**: [Releases](https://github.com/IrineSistiana/mosdns/releases)
- **Docker Images**: [Docker Hub](https://hub.docker.com/r/irinesistiana/mosdns)
### 🤝 Contributing
Issues and Pull Requests are welcome! Please ensure:
1. Code follows Go language standards
2. Add necessary tests
3. Update relevant documentation
### 📄 License
This project is licensed under GPL v3. See [LICENSE](./LICENSE) for details.
---
**⭐ 如果这个项目对你有帮助,请给个 Star!**
**⭐ If this project helps you, please give it a Star!**