chore: 初始提交 Fonu 1.0.2
- 公网反代:Nginx 状态卡片(版本 / 运行状态 / 配置校验 / 端口监听) - 部署设置第一、二梯队(HTTPS 开关、安全头、限流、连接数、仅中国大陆 IP) - 隧道检测兼容 frps v0.68 / v0.71(V2 与旧版端点) - 恢复被清空的 internal/frp/store.go、internal/api/ddns_handler.go、web/src/views/DdnsView.vue
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package ddns
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nrdcg/dnspod-go"
|
||||
)
|
||||
|
||||
type DNSPod struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewDNSPod() *DNSPod {
|
||||
return &DNSPod{client: &http.Client{Timeout: 15 * time.Second}}
|
||||
}
|
||||
|
||||
func (d *DNSPod) Name() string { return "dnspod" }
|
||||
|
||||
func (d *DNSPod) Verify(ctx context.Context, cred Credentials) error {
|
||||
client := d.apiClient(cred)
|
||||
_, _, err := client.Domains.List()
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *DNSPod) HasZone(ctx context.Context, cred Credentials, zone string) (bool, error) {
|
||||
_, err := d.domainID(d.apiClient(cred), zone)
|
||||
return zoneLookupOK(err)
|
||||
}
|
||||
|
||||
func (d *DNSPod) GetRecordIP(ctx context.Context, cred Credentials, rootDomain, recordName, recordType string) (string, error) {
|
||||
client := d.apiClient(cred)
|
||||
domainID, err := d.domainID(client, rootDomain)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
subDomain := subDomainForRecord(recordName)
|
||||
records, _, err := client.Records.List(domainID, subDomain)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, rec := range records {
|
||||
if strings.EqualFold(rec.Type, recordType) {
|
||||
return rec.Value, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (d *DNSPod) UpdateRecord(ctx context.Context, cred Credentials, rootDomain, recordName, recordType, ip string) error {
|
||||
client := d.apiClient(cred)
|
||||
domainID, err := d.domainID(client, rootDomain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
subDomain := subDomainForRecord(recordName)
|
||||
records, _, err := client.Records.List(domainID, subDomain)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, rec := range records {
|
||||
if strings.EqualFold(rec.Type, recordType) {
|
||||
_, _, err := client.Records.Update(domainID, rec.ID, dnspod.Record{
|
||||
Type: recordType,
|
||||
Name: subDomain,
|
||||
Value: ip,
|
||||
Line: rec.Line,
|
||||
TTL: rec.TTL,
|
||||
})
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, _, err = client.Records.Create(domainID, dnspod.Record{
|
||||
Type: recordType,
|
||||
Name: subDomain,
|
||||
Value: ip,
|
||||
Line: "默认",
|
||||
TTL: strconv.Itoa(120),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *DNSPod) domainID(client *dnspod.Client, rootDomain string) (string, error) {
|
||||
domains, _, err := client.Domains.List()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, domain := range domains {
|
||||
if domain.Name == rootDomain {
|
||||
return domain.ID.String(), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("未找到域名 %s 对应的 DNSPod 域名", rootDomain)
|
||||
}
|
||||
|
||||
func (d *DNSPod) apiClient(cred Credentials) *dnspod.Client {
|
||||
params := dnspod.CommonParams{LoginToken: cred.LoginToken(), Format: "json"}
|
||||
client := dnspod.NewClient(params)
|
||||
client.HTTPClient = d.client
|
||||
return client
|
||||
}
|
||||
Reference in New Issue
Block a user