Files
2026-06-30 15:14:37 +08:00

98 lines
3.0 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.
package dnsprovider
import (
"context"
"fmt"
"time"
"github.com/libdns/libdns"
)
// ProviderType DNS 服务商类型
type ProviderType string
const (
ProviderCloudflare ProviderType = "cloudflare"
ProviderAliyun ProviderType = "aliyun"
ProviderTencentCloud ProviderType = "tencent"
ProviderCustom ProviderType = "custom"
)
// ProviderConfig DNS 服务商配置
type ProviderConfig struct {
Provider ProviderType `json:"provider"` // 服务商类型
Domain string `json:"domain"` // 根域名
APIToken string `json:"api_token"` // API TokenCloudflare
AccessKeyID string `json:"access_key_id"` // AccessKey ID(阿里云)
AccessKeySecret string `json:"access_key_secret"` // AccessKey Secret(阿里云)
SecretId string `json:"secret_id"` // SecretId(腾讯云)
SecretKey string `json:"secret_key"` // SecretKey(腾讯云)
}
// DNSProvider DNS 服务商接口
type DNSProvider interface {
// AppendRecords 添加 DNS 记录
AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error)
// SetRecords 设置 DNS 记录(会覆盖现有记录)
SetRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error)
// GetRecords 获取 DNS 记录
GetRecords(ctx context.Context, zone string) ([]libdns.Record, error)
// DeleteRecords 删除 DNS 记录
DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error)
}
// NewDNSProvider 创建 DNS 服务商实例
func NewDNSProvider(config ProviderConfig) (DNSProvider, error) {
switch config.Provider {
case ProviderCloudflare:
return NewCloudflareProvider(config)
case ProviderAliyun:
return NewAliyunProvider(config)
case ProviderTencentCloud:
return NewTencentCloudProvider(config)
case ProviderCustom:
return nil, fmt.Errorf("自定义服务商暂未支持")
default:
return nil, fmt.Errorf("不支持的 DNS 服务商:%s", config.Provider)
}
}
// RecordType 记录类型
type RecordType string
const (
RecordTypeA RecordType = "A"
RecordTypeAAAA RecordType = "AAAA"
RecordTypeTXT RecordType = "TXT"
RecordTypeCNAME RecordType = "CNAME"
)
// DNSRecord DNS 记录
type DNSRecord struct {
Type RecordType `json:"type"` // 记录类型
Name string `json:"name"` // 记录名称(子域名)
Value string `json:"value"` // 记录值
TTL int `json:"ttl"` // TTL(秒)
}
// ToLibdnsRecord 转换为 libdns.Record
func (r *DNSRecord) ToLibdnsRecord() libdns.Record {
recordType := string(r.Type)
return libdns.Record{
Type: recordType,
Name: r.Name,
Value: r.Value,
TTL: time.Duration(r.TTL) * time.Second,
}
}
// FromLibdnsRecord 从 libdns.Record 转换
func FromLibdnsRecord(rec libdns.Record) *DNSRecord {
return &DNSRecord{
Type: RecordType(rec.Type),
Name: rec.Name,
Value: rec.Value,
TTL: int(rec.TTL / time.Second),
}
}