Initial commit
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package dnsprovider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/libdns/libdns"
|
||||
)
|
||||
|
||||
// AliyunProvider 阿里云 DNS 服务商实现
|
||||
// 注意:由于网络问题,github.com/libdns/aliyun 暂时无法下载
|
||||
// 此实现为占位代码,待网络恢复后需要安装并适配真实的 libdns/aliyun 库
|
||||
type AliyunProvider struct {
|
||||
domain string
|
||||
}
|
||||
|
||||
// NewAliyunProvider 创建阿里云 DNS 服务商实例
|
||||
func NewAliyunProvider(config ProviderConfig) (*AliyunProvider, error) {
|
||||
if config.AccessKeyID == "" || config.AccessKeySecret == "" {
|
||||
return nil, fmt.Errorf("AccessKey ID 和 Secret 不能为空")
|
||||
}
|
||||
|
||||
// TODO: 安装 github.com/libdns/aliyun 后,替换为真实实现
|
||||
// provider := &aliyun.Provider{
|
||||
// AccessKeyID: config.AccessKeyID,
|
||||
// AccessKeySecret: config.AccessKeySecret,
|
||||
// }
|
||||
|
||||
return &AliyunProvider{
|
||||
domain: config.Domain,
|
||||
}, fmt.Errorf("阿里云 DNS 服务商暂未支持,请稍后重试")
|
||||
}
|
||||
|
||||
// AppendRecords 添加 DNS 记录
|
||||
func (p *AliyunProvider) AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
|
||||
return nil, fmt.Errorf("未实现")
|
||||
}
|
||||
|
||||
// SetRecords 设置 DNS 记录(会覆盖现有记录)
|
||||
func (p *AliyunProvider) SetRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
|
||||
return nil, fmt.Errorf("未实现")
|
||||
}
|
||||
|
||||
// GetRecords 获取 DNS 记录
|
||||
func (p *AliyunProvider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
|
||||
return nil, fmt.Errorf("未实现")
|
||||
}
|
||||
|
||||
// DeleteRecords 删除 DNS 记录
|
||||
func (p *AliyunProvider) DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
|
||||
return nil, fmt.Errorf("未实现")
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package dnsprovider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/libdns/cloudflare"
|
||||
"github.com/libdns/libdns"
|
||||
)
|
||||
|
||||
// CloudflareProvider Cloudflare DNS 服务商实现
|
||||
type CloudflareProvider struct {
|
||||
client *cloudflare.Provider
|
||||
domain string
|
||||
}
|
||||
|
||||
// NewCloudflareProvider 创建 Cloudflare DNS 服务商实例
|
||||
func NewCloudflareProvider(config ProviderConfig) (*CloudflareProvider, error) {
|
||||
if config.APIToken == "" {
|
||||
return nil, fmt.Errorf("API Token 不能为空")
|
||||
}
|
||||
|
||||
provider := &cloudflare.Provider{
|
||||
APIToken: config.APIToken,
|
||||
}
|
||||
|
||||
return &CloudflareProvider{
|
||||
client: provider,
|
||||
domain: config.Domain,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AppendRecords 添加 DNS 记录
|
||||
func (p *CloudflareProvider) AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
|
||||
return p.client.AppendRecords(ctx, zone, recs)
|
||||
}
|
||||
|
||||
// SetRecords 设置 DNS 记录(会覆盖现有记录)
|
||||
func (p *CloudflareProvider) SetRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
|
||||
return p.client.SetRecords(ctx, zone, recs)
|
||||
}
|
||||
|
||||
// GetRecords 获取 DNS 记录
|
||||
func (p *CloudflareProvider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
|
||||
return p.client.GetRecords(ctx, zone)
|
||||
}
|
||||
|
||||
// DeleteRecords 删除 DNS 记录
|
||||
func (p *CloudflareProvider) DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
|
||||
return p.client.DeleteRecords(ctx, zone, recs)
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
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 Token(Cloudflare)
|
||||
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),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package dnsprovider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/libdns/libdns"
|
||||
tencentcloud "github.com/libdns/tencentcloud"
|
||||
)
|
||||
|
||||
// TencentCloudProvider 腾讯云 DNSPod 服务商实现
|
||||
type TencentCloudProvider struct {
|
||||
client *tencentcloud.Provider
|
||||
domain string
|
||||
}
|
||||
|
||||
// NewTencentCloudProvider 创建腾讯云 DNS 服务商实例
|
||||
func NewTencentCloudProvider(config ProviderConfig) (*TencentCloudProvider, error) {
|
||||
if config.SecretId == "" || config.SecretKey == "" {
|
||||
return nil, fmt.Errorf("SecretId 和 SecretKey 不能为空")
|
||||
}
|
||||
|
||||
provider := &tencentcloud.Provider{
|
||||
SecretId: config.SecretId,
|
||||
SecretKey: config.SecretKey,
|
||||
}
|
||||
|
||||
return &TencentCloudProvider{
|
||||
client: provider,
|
||||
domain: config.Domain,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AppendRecords 添加 DNS 记录
|
||||
func (p *TencentCloudProvider) AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
|
||||
return p.client.AppendRecords(ctx, zone, recs)
|
||||
}
|
||||
|
||||
// SetRecords 设置 DNS 记录(会覆盖现有记录)
|
||||
func (p *TencentCloudProvider) SetRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
|
||||
return p.client.SetRecords(ctx, zone, recs)
|
||||
}
|
||||
|
||||
// GetRecords 获取 DNS 记录
|
||||
func (p *TencentCloudProvider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
|
||||
return p.client.GetRecords(ctx, zone)
|
||||
}
|
||||
|
||||
// DeleteRecords 删除 DNS 记录
|
||||
func (p *TencentCloudProvider) DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
|
||||
return p.client.DeleteRecords(ctx, zone, recs)
|
||||
}
|
||||
Reference in New Issue
Block a user