ddns: improve performance (#385)

This commit is contained in:
UUBulb 2024-07-07 10:19:58 +08:00 committed by GitHub
parent 0501e120f6
commit 4df60c6955
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 33 deletions

View File

@ -13,7 +13,7 @@ type ProviderCloudflare struct {
Secret string Secret string
} }
func (provider ProviderCloudflare) UpdateDomain(domainConfig *DomainConfig) bool { func (provider *ProviderCloudflare) UpdateDomain(domainConfig *DomainConfig) bool {
if domainConfig == nil { if domainConfig == nil {
return false return false
} }
@ -42,7 +42,7 @@ func (provider ProviderCloudflare) UpdateDomain(domainConfig *DomainConfig) bool
return resultV4 && resultV6 return resultV4 && resultV6
} }
func (provider ProviderCloudflare) addDomainRecord(zoneID string, domainConfig *DomainConfig, isIpv4 bool) bool { func (provider *ProviderCloudflare) addDomainRecord(zoneID string, domainConfig *DomainConfig, isIpv4 bool) bool {
record, err := provider.findDNSRecord(zoneID, domainConfig.FullDomain, isIpv4) record, err := provider.findDNSRecord(zoneID, domainConfig.FullDomain, isIpv4)
if err != nil { if err != nil {
log.Printf("查找 DNS 记录时出错: %s\n", err) log.Printf("查找 DNS 记录时出错: %s\n", err)
@ -58,7 +58,7 @@ func (provider ProviderCloudflare) addDomainRecord(zoneID string, domainConfig *
} }
} }
func (provider ProviderCloudflare) getZoneID(domain string) (string, error) { func (provider *ProviderCloudflare) getZoneID(domain string) (string, error) {
_, realDomain := SplitDomain(domain) _, realDomain := SplitDomain(domain)
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones?name=%s", realDomain) url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones?name=%s", realDomain)
body, err := provider.sendRequest("GET", url, nil) body, err := provider.sendRequest("GET", url, nil)
@ -81,7 +81,7 @@ func (provider ProviderCloudflare) getZoneID(domain string) (string, error) {
return "", fmt.Errorf("找不到 Zone ID") return "", fmt.Errorf("找不到 Zone ID")
} }
func (provider ProviderCloudflare) findDNSRecord(zoneID string, domain string, isIPv4 bool) (map[string]interface{}, error) { func (provider *ProviderCloudflare) findDNSRecord(zoneID string, domain string, isIPv4 bool) (map[string]interface{}, error) {
var ipType = "A" var ipType = "A"
if !isIPv4 { if !isIPv4 {
ipType = "AAAA" ipType = "AAAA"
@ -106,7 +106,7 @@ func (provider ProviderCloudflare) findDNSRecord(zoneID string, domain string, i
return nil, nil // 没有找到 DNS 记录 return nil, nil // 没有找到 DNS 记录
} }
func (provider ProviderCloudflare) createDNSRecord(zoneID string, domainConfig *DomainConfig, isIPv4 bool) bool { func (provider *ProviderCloudflare) createDNSRecord(zoneID string, domainConfig *DomainConfig, isIPv4 bool) bool {
var ipType = "A" var ipType = "A"
var ipAddr = domainConfig.Ipv4Addr var ipAddr = domainConfig.Ipv4Addr
if !isIPv4 { if !isIPv4 {
@ -126,7 +126,7 @@ func (provider ProviderCloudflare) createDNSRecord(zoneID string, domainConfig *
return err == nil return err == nil
} }
func (provider ProviderCloudflare) updateDNSRecord(zoneID string, recordID string, domainConfig *DomainConfig, isIPv4 bool) bool { func (provider *ProviderCloudflare) updateDNSRecord(zoneID string, recordID string, domainConfig *DomainConfig, isIPv4 bool) bool {
var ipType = "A" var ipType = "A"
var ipAddr = domainConfig.Ipv4Addr var ipAddr = domainConfig.Ipv4Addr
if !isIPv4 { if !isIPv4 {
@ -147,7 +147,7 @@ func (provider ProviderCloudflare) updateDNSRecord(zoneID string, recordID strin
} }
// 以下为辅助方法,如发送 HTTP 请求等 // 以下为辅助方法,如发送 HTTP 请求等
func (provider ProviderCloudflare) sendRequest(method string, url string, data []byte) ([]byte, error) { func (provider *ProviderCloudflare) sendRequest(method string, url string, data []byte) ([]byte, error) {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest(method, url, bytes.NewBuffer(data)) req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
if err != nil { if err != nil {

View File

@ -2,6 +2,6 @@ package ddns
type ProviderDummy struct{} type ProviderDummy struct{}
func (provider ProviderDummy) UpdateDomain(domainConfig *DomainConfig) bool { func (provider *ProviderDummy) UpdateDomain(domainConfig *DomainConfig) bool {
return false return false
} }

View File

@ -23,7 +23,7 @@ type ProviderTencentCloud struct {
SecretKey string SecretKey string
} }
func (provider ProviderTencentCloud) UpdateDomain(domainConfig *DomainConfig) bool { func (provider *ProviderTencentCloud) UpdateDomain(domainConfig *DomainConfig) bool {
if domainConfig == nil { if domainConfig == nil {
return false return false
} }
@ -46,7 +46,7 @@ func (provider ProviderTencentCloud) UpdateDomain(domainConfig *DomainConfig) bo
return resultV4 && resultV6 return resultV4 && resultV6
} }
func (provider ProviderTencentCloud) addDomainRecord(domainConfig *DomainConfig, isIpv4 bool) bool { func (provider *ProviderTencentCloud) addDomainRecord(domainConfig *DomainConfig, isIpv4 bool) bool {
record, err := provider.findDNSRecord(domainConfig.FullDomain, isIpv4) record, err := provider.findDNSRecord(domainConfig.FullDomain, isIpv4)
if err != nil { if err != nil {
log.Printf("查找 DNS 记录时出错: %s\n", err) log.Printf("查找 DNS 记录时出错: %s\n", err)
@ -66,7 +66,7 @@ func (provider ProviderTencentCloud) addDomainRecord(domainConfig *DomainConfig,
return provider.updateDNSRecord(domainConfig.FullDomain, record["RecordList"].([]interface{})[0].(map[string]interface{})["RecordId"].(float64), domainConfig, isIpv4) return provider.updateDNSRecord(domainConfig.FullDomain, record["RecordList"].([]interface{})[0].(map[string]interface{})["RecordId"].(float64), domainConfig, isIpv4)
} }
func (provider ProviderTencentCloud) findDNSRecord(domain string, isIPv4 bool) (map[string]interface{}, error) { func (provider *ProviderTencentCloud) findDNSRecord(domain string, isIPv4 bool) (map[string]interface{}, error) {
var ipType = "A" var ipType = "A"
if !isIPv4 { if !isIPv4 {
ipType = "AAAA" ipType = "AAAA"
@ -95,7 +95,7 @@ func (provider ProviderTencentCloud) findDNSRecord(domain string, isIPv4 bool) (
return result, nil return result, nil
} }
func (provider ProviderTencentCloud) createDNSRecord(domain string, domainConfig *DomainConfig, isIPv4 bool) bool { func (provider *ProviderTencentCloud) createDNSRecord(domain string, domainConfig *DomainConfig, isIPv4 bool) bool {
var ipType = "A" var ipType = "A"
var ipAddr = domainConfig.Ipv4Addr var ipAddr = domainConfig.Ipv4Addr
if !isIPv4 { if !isIPv4 {
@ -117,7 +117,7 @@ func (provider ProviderTencentCloud) createDNSRecord(domain string, domainConfig
return err == nil return err == nil
} }
func (provider ProviderTencentCloud) updateDNSRecord(domain string, recordID float64, domainConfig *DomainConfig, isIPv4 bool) bool { func (provider *ProviderTencentCloud) updateDNSRecord(domain string, recordID float64, domainConfig *DomainConfig, isIPv4 bool) bool {
var ipType = "A" var ipType = "A"
var ipAddr = domainConfig.Ipv4Addr var ipAddr = domainConfig.Ipv4Addr
if !isIPv4 { if !isIPv4 {
@ -141,7 +141,7 @@ func (provider ProviderTencentCloud) updateDNSRecord(domain string, recordID flo
} }
// 以下为辅助方法,如发送 HTTP 请求等 // 以下为辅助方法,如发送 HTTP 请求等
func (provider ProviderTencentCloud) sendRequest(action string, data []byte) ([]byte, error) { func (provider *ProviderTencentCloud) sendRequest(action string, data []byte) ([]byte, error) {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil { if err != nil {
@ -173,18 +173,18 @@ func (provider ProviderTencentCloud) sendRequest(action string, data []byte) ([]
// https://github.com/jeessy2/ddns-go/blob/master/util/tencent_cloud_signer.go // https://github.com/jeessy2/ddns-go/blob/master/util/tencent_cloud_signer.go
func (provider ProviderTencentCloud) sha256hex(s string) string { func (provider *ProviderTencentCloud) sha256hex(s string) string {
b := sha256.Sum256([]byte(s)) b := sha256.Sum256([]byte(s))
return hex.EncodeToString(b[:]) return hex.EncodeToString(b[:])
} }
func (provider ProviderTencentCloud) hmacsha256(s, key string) string { func (provider *ProviderTencentCloud) hmacsha256(s, key string) string {
hashed := hmac.New(sha256.New, []byte(key)) hashed := hmac.New(sha256.New, []byte(key))
hashed.Write([]byte(s)) hashed.Write([]byte(s))
return string(hashed.Sum(nil)) return string(hashed.Sum(nil))
} }
func (provider ProviderTencentCloud) WriteString(strs ...string) string { func (provider *ProviderTencentCloud) WriteString(strs ...string) string {
var b strings.Builder var b strings.Builder
for _, str := range strs { for _, str := range strs {
b.WriteString(str) b.WriteString(str)
@ -193,7 +193,7 @@ func (provider ProviderTencentCloud) WriteString(strs ...string) string {
return b.String() return b.String()
} }
func (provider ProviderTencentCloud) signRequest(secretId string, secretKey string, r *http.Request, action string, payload string) { func (provider *ProviderTencentCloud) signRequest(secretId string, secretKey string, r *http.Request, action string, payload string) {
algorithm := "TC3-HMAC-SHA256" algorithm := "TC3-HMAC-SHA256"
service := "dnspod" service := "dnspod"
host := provider.WriteString(service, ".tencentcloudapi.com") host := provider.WriteString(service, ".tencentcloudapi.com")

View File

@ -14,7 +14,7 @@ type ProviderWebHook struct {
RequestHeader string RequestHeader string
} }
func (provider ProviderWebHook) UpdateDomain(domainConfig *DomainConfig) bool { func (provider *ProviderWebHook) UpdateDomain(domainConfig *DomainConfig) bool {
if domainConfig == nil { if domainConfig == nil {
return false return false
} }

View File

@ -1,10 +1,10 @@
package singleton package singleton
import ( import (
"errors"
"fmt" "fmt"
ddns2 "github.com/naiba/nezha/pkg/ddns"
"log" "log"
ddns2 "github.com/naiba/nezha/pkg/ddns"
) )
func RetryableUpdateDomain(provider ddns2.Provider, config *ddns2.DomainConfig, maxRetries int) bool { func RetryableUpdateDomain(provider ddns2.Provider, config *ddns2.DomainConfig, maxRetries int) bool {
@ -25,54 +25,54 @@ func RetryableUpdateDomain(provider ddns2.Provider, config *ddns2.DomainConfig,
func GetDDNSProviderFromString(provider string) (ddns2.Provider, error) { func GetDDNSProviderFromString(provider string) (ddns2.Provider, error) {
switch provider { switch provider {
case "webhook": case "webhook":
return ddns2.ProviderWebHook{ return &ddns2.ProviderWebHook{
URL: Conf.DDNS.WebhookURL, URL: Conf.DDNS.WebhookURL,
RequestMethod: Conf.DDNS.WebhookMethod, RequestMethod: Conf.DDNS.WebhookMethod,
RequestBody: Conf.DDNS.WebhookRequestBody, RequestBody: Conf.DDNS.WebhookRequestBody,
RequestHeader: Conf.DDNS.WebhookHeaders, RequestHeader: Conf.DDNS.WebhookHeaders,
}, nil }, nil
case "dummy": case "dummy":
return ddns2.ProviderDummy{}, nil return &ddns2.ProviderDummy{}, nil
case "cloudflare": case "cloudflare":
return ddns2.ProviderCloudflare{ return &ddns2.ProviderCloudflare{
Secret: Conf.DDNS.AccessSecret, Secret: Conf.DDNS.AccessSecret,
}, nil }, nil
case "tencentcloud": case "tencentcloud":
return ddns2.ProviderTencentCloud{ return &ddns2.ProviderTencentCloud{
SecretID: Conf.DDNS.AccessID, SecretID: Conf.DDNS.AccessID,
SecretKey: Conf.DDNS.AccessSecret, SecretKey: Conf.DDNS.AccessSecret,
}, nil }, nil
} }
return ddns2.ProviderDummy{}, errors.New(fmt.Sprintf("无法找到配置的DDNS提供者%s", Conf.DDNS.Provider)) return &ddns2.ProviderDummy{}, fmt.Errorf("无法找到配置的DDNS提供者%s", Conf.DDNS.Provider)
} }
func GetDDNSProviderFromProfile(profileName string) (ddns2.Provider, error) { func GetDDNSProviderFromProfile(profileName string) (ddns2.Provider, error) {
profile, ok := Conf.DDNS.Profiles[profileName] profile, ok := Conf.DDNS.Profiles[profileName]
if !ok { if !ok {
return ddns2.ProviderDummy{}, errors.New(fmt.Sprintf("未找到配置项 %s", profileName)) return &ddns2.ProviderDummy{}, fmt.Errorf("未找到配置项 %s", profileName)
} }
switch profile.Provider { switch profile.Provider {
case "webhook": case "webhook":
return ddns2.ProviderWebHook{ return &ddns2.ProviderWebHook{
URL: profile.WebhookURL, URL: profile.WebhookURL,
RequestMethod: profile.WebhookMethod, RequestMethod: profile.WebhookMethod,
RequestBody: profile.WebhookRequestBody, RequestBody: profile.WebhookRequestBody,
RequestHeader: profile.WebhookHeaders, RequestHeader: profile.WebhookHeaders,
}, nil }, nil
case "dummy": case "dummy":
return ddns2.ProviderDummy{}, nil return &ddns2.ProviderDummy{}, nil
case "cloudflare": case "cloudflare":
return ddns2.ProviderCloudflare{ return &ddns2.ProviderCloudflare{
Secret: profile.AccessSecret, Secret: profile.AccessSecret,
}, nil }, nil
case "tencentcloud": case "tencentcloud":
return ddns2.ProviderTencentCloud{ return &ddns2.ProviderTencentCloud{
SecretID: profile.AccessID, SecretID: profile.AccessID,
SecretKey: profile.AccessSecret, SecretKey: profile.AccessSecret,
}, nil }, nil
} }
return ddns2.ProviderDummy{}, errors.New(fmt.Sprintf("无法找到配置的DDNS提供者%s", profile.Provider)) return &ddns2.ProviderDummy{}, fmt.Errorf("无法找到配置的DDNS提供者%s", profile.Provider)
} }
func ValidateDDNSProvidersFromProfiles() error { func ValidateDDNSProvidersFromProfiles() error {
@ -80,7 +80,7 @@ func ValidateDDNSProvidersFromProfiles() error {
providers := make(map[string]string) providers := make(map[string]string)
for profileName, profile := range Conf.DDNS.Profiles { for profileName, profile := range Conf.DDNS.Profiles {
if _, ok := validProviders[profile.Provider]; !ok { if _, ok := validProviders[profile.Provider]; !ok {
return errors.New(fmt.Sprintf("无法找到配置的DDNS提供者%s", profile.Provider)) return fmt.Errorf("无法找到配置的DDNS提供者%s", profile.Provider)
} }
providers[profileName] = profile.Provider providers[profileName] = profile.Provider
} }