2024-02-24 08:10:27 -05:00
|
|
|
package singleton
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2024-08-23 23:11:06 -04:00
|
|
|
"slices"
|
2024-07-06 22:19:58 -04:00
|
|
|
|
|
|
|
ddns2 "github.com/naiba/nezha/pkg/ddns"
|
2024-02-24 08:10:27 -05:00
|
|
|
)
|
|
|
|
|
2024-08-23 23:11:06 -04:00
|
|
|
const (
|
|
|
|
ProviderWebHook = "webhook"
|
|
|
|
ProviderCloudflare = "cloudflare"
|
|
|
|
ProviderTencentCloud = "tencentcloud"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProviderFunc func(*ddns2.DomainConfig) ddns2.Provider
|
|
|
|
|
|
|
|
func RetryableUpdateDomain(provider ddns2.Provider, domainConfig *ddns2.DomainConfig, maxRetries int) {
|
|
|
|
if domainConfig == nil {
|
|
|
|
return
|
2024-02-24 08:10:27 -05:00
|
|
|
}
|
|
|
|
for retries := 0; retries < maxRetries; retries++ {
|
2024-08-23 23:11:06 -04:00
|
|
|
log.Printf("NEZHA>> 正在尝试更新域名(%s)DDNS(%d/%d)", domainConfig.FullDomain, retries+1, maxRetries)
|
|
|
|
if err := provider.UpdateDomain(domainConfig); err != nil {
|
|
|
|
log.Printf("NEZHA>> 尝试更新域名(%s)DDNS失败: %v", domainConfig.FullDomain, err)
|
|
|
|
} else {
|
|
|
|
log.Printf("NEZHA>> 尝试更新域名(%s)DDNS成功", domainConfig.FullDomain)
|
|
|
|
break
|
2024-02-24 08:10:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-23 23:11:06 -04:00
|
|
|
// Deprecated
|
2024-02-25 22:49:28 -05:00
|
|
|
func GetDDNSProviderFromString(provider string) (ddns2.Provider, error) {
|
2024-02-24 08:10:27 -05:00
|
|
|
switch provider {
|
2024-08-23 23:11:06 -04:00
|
|
|
case ProviderWebHook:
|
|
|
|
return ddns2.NewProviderWebHook(Conf.DDNS.WebhookURL, Conf.DDNS.WebhookMethod, Conf.DDNS.WebhookRequestBody, Conf.DDNS.WebhookHeaders), nil
|
|
|
|
case ProviderCloudflare:
|
|
|
|
return ddns2.NewProviderCloudflare(Conf.DDNS.AccessSecret), nil
|
|
|
|
case ProviderTencentCloud:
|
|
|
|
return ddns2.NewProviderTencentCloud(Conf.DDNS.AccessID, Conf.DDNS.AccessSecret), nil
|
|
|
|
default:
|
|
|
|
return new(ddns2.ProviderDummy), fmt.Errorf("无法找到配置的DDNS提供者 %s", provider)
|
2024-02-24 08:10:27 -05:00
|
|
|
}
|
|
|
|
}
|
2024-04-27 01:36:36 -04:00
|
|
|
|
|
|
|
func GetDDNSProviderFromProfile(profileName string) (ddns2.Provider, error) {
|
|
|
|
profile, ok := Conf.DDNS.Profiles[profileName]
|
|
|
|
if !ok {
|
2024-08-23 23:11:06 -04:00
|
|
|
return new(ddns2.ProviderDummy), fmt.Errorf("未找到配置项 %s", profileName)
|
2024-04-27 01:36:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch profile.Provider {
|
2024-08-23 23:11:06 -04:00
|
|
|
case ProviderWebHook:
|
|
|
|
return ddns2.NewProviderWebHook(profile.WebhookURL, profile.WebhookMethod, profile.WebhookRequestBody, profile.WebhookHeaders), nil
|
|
|
|
case ProviderCloudflare:
|
|
|
|
return ddns2.NewProviderCloudflare(profile.AccessSecret), nil
|
|
|
|
case ProviderTencentCloud:
|
|
|
|
return ddns2.NewProviderTencentCloud(profile.AccessID, profile.AccessSecret), nil
|
|
|
|
default:
|
|
|
|
return new(ddns2.ProviderDummy), fmt.Errorf("无法找到配置的DDNS提供者 %s", profile.Provider)
|
2024-04-27 01:36:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateDDNSProvidersFromProfiles() error {
|
2024-08-23 23:11:06 -04:00
|
|
|
validProviders := []string{ProviderWebHook, ProviderCloudflare, ProviderTencentCloud}
|
|
|
|
for _, profile := range Conf.DDNS.Profiles {
|
|
|
|
if ok := slices.Contains(validProviders, profile.Provider); !ok {
|
2024-07-06 22:19:58 -04:00
|
|
|
return fmt.Errorf("无法找到配置的DDNS提供者%s", profile.Provider)
|
2024-04-27 01:36:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|