mosdns/plugin/executable/mikrotik_addresslist/mikrotik_addresslist.go
dengxiongjian 819576c450
Some checks failed
Test mosdns / build (push) Has been cancelled
优化项目
1. 增强 mikrotik_addresslist 插件
新增 domain_files 参数支持
自动域名匹配功能
保持原有所有功能不变
向后兼容,不影响现有用法
2. 核心功能实现
GFW 域名分流:gfwlist.out.txt 仅用于分流,不写入任何设备
多设备支持:a.txt → 设备A,b.txt → 设备B
自动匹配:插件自动检查域名是否在其域名文件中
性能优化:内存缓存、异步处理、智能跳过
3. 配置大幅简化
从 ~60 行复杂配置减少到 ~15 行
不需要手动定义 domain_set
不需要复杂的 sequence 逻辑
添加新设备只需要几行配置
2025-10-14 22:40:50 +08:00

157 lines
4.8 KiB
Go
Raw Permalink 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.

/*
* Copyright (C) 2020-2022, IrineSistiana
*
* This file is part of mosdns.
*
* mosdns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mosdns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mikrotik_addresslist
import (
"fmt"
"strconv"
"strings"
"github.com/IrineSistiana/mosdns/v5/coremain"
"github.com/IrineSistiana/mosdns/v5/plugin/executable/sequence"
)
const PluginType = "mikrotik_addresslist"
func init() {
coremain.RegNewPluginFunc(PluginType, Init, func() any { return new(Args) })
sequence.MustRegExecQuickSetup(PluginType, QuickSetup)
}
type Args struct {
// 🆕 新增:域名文件列表(用于自动匹配)
DomainFiles []string `yaml:"domain_files"` // 域名文件列表,如果指定则只处理匹配的域名
Host string `yaml:"host"` // MikroTik 路由器 IP 地址
Port int `yaml:"port"` // API 端口,默认 8728
Username string `yaml:"username"` // 用户名
Password string `yaml:"password"` // 密码
UseTLS bool `yaml:"use_tls"` // 是否使用 TLS默认 false
Timeout int `yaml:"timeout"` // 连接超时时间(秒),默认 10
AddressList4 string `yaml:"address_list4"` // IPv4 address list 名称
AddressList6 string `yaml:"address_list6"` // IPv6 address list 名称
Mask4 int `yaml:"mask4"` // IPv4 掩码,默认 32单个IP
Mask6 int `yaml:"mask6"` // IPv6 掩码,默认 128单个IP
Comment string `yaml:"comment"` // 添加的地址的注释
TimeoutAddr int `yaml:"timeout_addr"` // 地址超时时间0 表示永久
CacheTTL int `yaml:"cache_ttl"` // 缓存 TTL默认 36001小时
VerifyAdd bool `yaml:"verify_add"` // 是否在添加后验证地址确实存在,默认 false
AddAllIPs bool `yaml:"add_all_ips"` // 是否添加DNS响应中的所有IP地址默认 true
MaxIPs int `yaml:"max_ips"` // 每个域名最多添加的IP数量0表示无限制默认 0
}
var _ sequence.Executable = (*mikrotikAddressListPlugin)(nil)
// Init initializes the plugin from coremain
func Init(bp *coremain.BP, args any) (any, error) {
return newMikrotikAddressListPlugin(args.(*Args))
}
// QuickSetup format: host:port:username:password:use_tls:timeout:address_list4:address_list6:mask4:mask6:comment:timeout_addr:cache_ttl:verify_add
// e.g. "192.168.1.1:8728:admin:password:false:10:my_list4:my_list6:24:32:from_dns:3600:3600:true"
func QuickSetup(_ sequence.BQ, s string) (any, error) {
parts := strings.Split(s, ":")
if len(parts) < 6 {
return nil, fmt.Errorf("invalid args, expect at least 6 parts, got %d", len(parts))
}
args := &Args{
Host: parts[0],
Username: parts[2],
Password: parts[3],
UseTLS: false,
Timeout: 10,
Mask4: 32, // 默认单个IP确保所有IP都被添加
Mask6: 128, // 默认单个IP确保所有IP都被添加
AddAllIPs: true, // 默认添加所有IP
MaxIPs: 0, // 默认无限制
}
// 解析端口
if port, err := strconv.Atoi(parts[1]); err == nil {
args.Port = port
} else {
args.Port = 8728
}
// 解析 TLS 设置
if len(parts) > 4 {
if useTLS, err := strconv.ParseBool(parts[4]); err == nil {
args.UseTLS = useTLS
}
}
// 解析超时时间
if len(parts) > 5 {
if timeout, err := strconv.Atoi(parts[5]); err == nil {
args.Timeout = timeout
}
}
// 解析 address list 名称
if len(parts) > 6 {
args.AddressList4 = parts[6]
}
if len(parts) > 7 {
args.AddressList6 = parts[7]
}
// 解析掩码
if len(parts) > 8 {
if mask4, err := strconv.Atoi(parts[8]); err == nil {
args.Mask4 = mask4
}
}
if len(parts) > 9 {
if mask6, err := strconv.Atoi(parts[9]); err == nil {
args.Mask6 = mask6
}
}
// 解析注释
if len(parts) > 10 {
args.Comment = parts[10]
}
// 解析地址超时时间
if len(parts) > 11 {
if timeoutAddr, err := strconv.Atoi(parts[11]); err == nil {
args.TimeoutAddr = timeoutAddr
}
}
// 解析缓存 TTL
if len(parts) > 12 {
if cacheTTL, err := strconv.Atoi(parts[12]); err == nil {
args.CacheTTL = cacheTTL
}
}
// 解析验证开关
if len(parts) > 13 {
if verifyAdd, err := strconv.ParseBool(parts[13]); err == nil {
args.VerifyAdd = verifyAdd
}
}
return newMikrotikAddressListPlugin(args)
}