2023-04-28 14:57:21 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2024-03-15 23:16:23 +13:00
|
|
|
defaultCheckInterval = 5 * 60
|
|
|
|
|
envKeyDnsToCheck = "DNS_NAMES"
|
2023-04-28 14:57:21 +02:00
|
|
|
envKeyPublicIpResolverTag = "PUBLIC_IP_RESOLVER"
|
|
|
|
|
envKeyCloudflareApiKey = "CLOUDFLARE_API_KEY"
|
|
|
|
|
envKeyCloudflareZone = "CLOUDFLARE_ZONE"
|
2024-03-15 23:16:23 +13:00
|
|
|
envKeyDirectadminUser = "DA_USER"
|
|
|
|
|
envKeyDirectadminKey = "DA_LOGIN_KEY"
|
|
|
|
|
envKeyDirectadminUrl = "DA_LOGIN_URL"
|
2023-04-28 14:57:21 +02:00
|
|
|
envKeyOnChangeComment = "ON_CHANGE_COMMENT"
|
|
|
|
|
envKeyCheckIntervalSeconds = "CHECK_INTERVAL_SECONDS"
|
2023-05-04 11:44:27 +02:00
|
|
|
envKeyNotifiers = "NOTIFIERS"
|
2023-04-28 14:57:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
|
DnsRecordsToCheck []string
|
|
|
|
|
PublicIpResolverTag string
|
2024-03-15 23:16:23 +13:00
|
|
|
DNSProviderTag string
|
|
|
|
|
DirectadminUsername string
|
|
|
|
|
DirectadminKey string
|
|
|
|
|
DirectadminUrl string
|
2023-04-28 14:57:21 +02:00
|
|
|
ApiToken string
|
2024-02-12 16:50:56 +13:00
|
|
|
WebhookToken string
|
2023-04-28 14:57:21 +02:00
|
|
|
CloudflareZone string
|
|
|
|
|
OnChangeComment string
|
2023-05-04 11:44:27 +02:00
|
|
|
Notifiers []string
|
2023-04-28 14:57:21 +02:00
|
|
|
CheckInterval time.Duration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Config) Validate() error {
|
2024-03-15 23:16:23 +13:00
|
|
|
switch c.DNSProviderTag {
|
|
|
|
|
case "cloudflare":
|
|
|
|
|
if c.ApiToken == "" {
|
|
|
|
|
return fmt.Errorf("empty api token env key %s", envKeyCloudflareApiKey)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if c.CloudflareZone == "" {
|
|
|
|
|
// return fmt.Errorf("empty zone in env key %s", envKeyCloudflareZone)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
case "directadmin":
|
|
|
|
|
if c.DirectadminUrl == "" {
|
|
|
|
|
return fmt.Errorf("empty DirectAdmin URL env key %s", envKeyDirectadminUrl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.DirectadminUsername == "" {
|
|
|
|
|
return fmt.Errorf("empty Username in env key %s", envKeyDirectadminUser)
|
|
|
|
|
}
|
2023-04-28 14:57:21 +02:00
|
|
|
|
2024-03-15 23:16:23 +13:00
|
|
|
if c.DirectadminKey == "" {
|
|
|
|
|
return fmt.Errorf("empty Login Key in env key %s", envKeyDirectadminKey)
|
|
|
|
|
}
|
2023-04-28 14:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 `%s` value. Check interval set default(%ds)", envKeyCheckIntervalSeconds, defaultCheckInterval)
|
|
|
|
|
checkInterval = defaultCheckInterval
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Config{
|
2023-05-04 11:44:27 +02:00
|
|
|
DnsRecordsToCheck: parseCommaDelimited(os.Getenv(envKeyDnsToCheck)),
|
2023-04-28 14:57:21 +02:00
|
|
|
PublicIpResolverTag: os.Getenv(envKeyPublicIpResolverTag),
|
|
|
|
|
ApiToken: os.Getenv(envKeyCloudflareApiKey),
|
2024-03-15 23:16:23 +13:00
|
|
|
DirectadminUsername: os.Getenv(envKeyDirectadminUser),
|
|
|
|
|
DirectadminKey: os.Getenv(envKeyDirectadminKey),
|
|
|
|
|
DirectadminUrl: os.Getenv(envKeyDirectadminUrl),
|
2023-04-28 14:57:21 +02:00
|
|
|
CloudflareZone: os.Getenv(envKeyCloudflareZone),
|
|
|
|
|
OnChangeComment: os.Getenv(envKeyOnChangeComment),
|
2023-05-04 11:44:27 +02:00
|
|
|
Notifiers: parseCommaDelimited(os.Getenv(envKeyNotifiers)),
|
2023-04-28 14:57:21 +02:00
|
|
|
CheckInterval: time.Duration(checkInterval) * time.Second,
|
2024-02-12 16:50:56 +13:00
|
|
|
WebhookToken: os.Getenv("WEBHOOK_TOKEN"),
|
2023-04-28 14:57:21 +02:00
|
|
|
}
|
|
|
|
|
}
|