2023-04-28 14:57:21 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
defaultCheckInterval = 5 * 60
|
|
|
|
|
|
|
|
|
|
envKeyDnsToCheck = "CLOUDFLARE_DNS_TO_CHECK"
|
|
|
|
|
envKeyPublicIpResolverTag = "PUBLIC_IP_RESOLVER"
|
|
|
|
|
envKeyCloudflareApiKey = "CLOUDFLARE_API_KEY"
|
|
|
|
|
envKeyCloudflareZone = "CLOUDFLARE_ZONE"
|
|
|
|
|
envKeyOnChangeComment = "ON_CHANGE_COMMENT"
|
|
|
|
|
envKeyCheckIntervalSeconds = "CHECK_INTERVAL_SECONDS"
|
2023-05-04 11:44:27 +02:00
|
|
|
envKeyNotifiers = "NOTIFIERS"
|
2023-12-09 16:12:58 +01:00
|
|
|
envKeyIgnoredIpChange = "IGNORED_IP_CHANGE"
|
2023-04-28 14:57:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
|
DnsRecordsToCheck []string
|
|
|
|
|
PublicIpResolverTag string
|
|
|
|
|
ApiToken string
|
|
|
|
|
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
|
2023-12-09 16:12:58 +01:00
|
|
|
IgnoredIpChange []string
|
2023-04-28 14:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c Config) Validate() error {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
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,
|
2023-12-09 16:12:58 +01:00
|
|
|
IgnoredIpChange: parseCommaDelimited(os.Getenv(envKeyIgnoredIpChange)),
|
2023-04-28 14:57:21 +02:00
|
|
|
}
|
|
|
|
|
}
|