You've already forked ddns-updater
101 lines
3.1 KiB
Go
101 lines
3.1 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
defaultCheckInterval = 5 * 60
|
|
envKeyDnsToCheck = "DNS_NAMES"
|
|
envKeyPublicIpResolverTag = "PUBLIC_IP_RESOLVER"
|
|
envKeyDNSProviderTag = "DNS_PROVIDER"
|
|
envKeyPublicDNSServer = "PUBLIC_DNS_SERVER"
|
|
envKeyCloudflareApiKey = "CLOUDFLARE_API_KEY"
|
|
envKeyCloudflareZone = "CLOUDFLARE_ZONE"
|
|
envKeyDirectadminUser = "DA_USER"
|
|
envKeyDirectadminKey = "DA_KEY"
|
|
envKeyDirectadminUrl = "DA_URL"
|
|
envKeyOnChangeComment = "ON_CHANGE_COMMENT"
|
|
envKeyCheckIntervalSeconds = "CHECK_INTERVAL_SECONDS"
|
|
envKeyNotifiers = "NOTIFIERS"
|
|
)
|
|
|
|
type Config struct {
|
|
DnsRecordsToCheck []string
|
|
PublicIpResolverTag string
|
|
PublicDNSServer string
|
|
DNSProviderTag string
|
|
DirectadminUsername string
|
|
DirectadminKey string
|
|
DirectadminUrl string
|
|
ApiToken string
|
|
WebhookToken string
|
|
CloudflareZone string
|
|
CloudflareOnChangeComment string
|
|
Notifiers []string
|
|
CheckInterval time.Duration
|
|
}
|
|
|
|
func (c Config) Validate() error {
|
|
switch c.DNSProviderTag {
|
|
case "cloudflare":
|
|
if c.ApiToken == "" {
|
|
return fmt.Errorf("empty api token env key %s", envKeyCloudflareApiKey)
|
|
}
|
|
|
|
case "directadmin":
|
|
if c.DirectadminUrl == "" {
|
|
return fmt.Errorf("empty DirectAdmin URL env key %s", envKeyDirectadminUrl)
|
|
}
|
|
|
|
if c.DirectadminUsername == "" {
|
|
return fmt.Errorf("empty DirectAdmin Username in env key %s", envKeyDirectadminUser)
|
|
}
|
|
|
|
if c.DirectadminKey == "" {
|
|
return fmt.Errorf("empty DirectAdmin Login Key in env key %s", envKeyDirectadminKey)
|
|
}
|
|
}
|
|
|
|
if len(c.DnsRecordsToCheck) == 0 {
|
|
return fmt.Errorf("no dns to check defined in env key %s", envKeyDnsToCheck)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func NewConfig() Config {
|
|
checkInterval, err := strconv.ParseInt(os.Getenv(envKeyCheckIntervalSeconds), 10, 64)
|
|
if err != nil {
|
|
log.Printf("wrong or missing `%s` value. Check interval set to default(%ds)", envKeyCheckIntervalSeconds, defaultCheckInterval)
|
|
checkInterval = defaultCheckInterval
|
|
}
|
|
|
|
return Config{
|
|
DnsRecordsToCheck: parseCommaDelimited(os.Getenv(envKeyDnsToCheck)),
|
|
DNSProviderTag: getEnvDefault(envKeyDNSProviderTag, "cloudflare"),
|
|
PublicIpResolverTag: getEnvDefault(envKeyPublicIpResolverTag, "icanhazip"),
|
|
ApiToken: os.Getenv(envKeyCloudflareApiKey),
|
|
CloudflareZone: os.Getenv(envKeyCloudflareZone),
|
|
CloudflareOnChangeComment: os.Getenv(envKeyOnChangeComment),
|
|
DirectadminUsername: os.Getenv(envKeyDirectadminUser),
|
|
DirectadminKey: os.Getenv(envKeyDirectadminKey),
|
|
DirectadminUrl: os.Getenv(envKeyDirectadminUrl),
|
|
Notifiers: parseCommaDelimited(os.Getenv(envKeyNotifiers)),
|
|
CheckInterval: time.Duration(checkInterval) * time.Second,
|
|
WebhookToken: os.Getenv("WEBHOOK_TOKEN"),
|
|
}
|
|
}
|
|
|
|
func getEnvDefault(key, fallback string) string {
|
|
value, exists := os.LookupEnv(key)
|
|
if !exists {
|
|
value = fallback
|
|
}
|
|
return value
|
|
}
|