feat: change detector
This commit is contained in:
53
pkg/changeDetector/changeDetector.go
Normal file
53
pkg/changeDetector/changeDetector.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package changeDetector
|
||||
|
||||
import (
|
||||
"realdnydns/pkg/config"
|
||||
"realdnydns/pkg/dnsProvider"
|
||||
"realdnydns/pkg/externalIpProvider"
|
||||
)
|
||||
|
||||
type ChangeDetector struct {
|
||||
externalIpProvider externalIpProvider.ExternalIpProvider
|
||||
dnsProvider dnsProvider.DNSProvider
|
||||
domains []config.DomainConfig
|
||||
}
|
||||
|
||||
func New(
|
||||
externalIpProvider externalIpProvider.ExternalIpProvider,
|
||||
dnsProvider dnsProvider.DNSProvider,
|
||||
domains []config.DomainConfig,
|
||||
) ChangeDetector {
|
||||
return ChangeDetector{
|
||||
externalIpProvider: externalIpProvider,
|
||||
dnsProvider: dnsProvider,
|
||||
domains: domains,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ChangeDetector) DetectAndApplyChanges() (int, error) {
|
||||
externalIp, err := c.externalIpProvider.GetExternalIp()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var numberUpdated int
|
||||
|
||||
for _, domain := range c.domains {
|
||||
for _, subdomain := range domain.Subdomains {
|
||||
currentRecord, err := c.dnsProvider.GetRecord(domain.TLD, subdomain)
|
||||
if err != nil {
|
||||
return numberUpdated, err
|
||||
}
|
||||
|
||||
if currentRecord.IP != externalIp.String() {
|
||||
_, err = c.dnsProvider.UpdateRecord(domain.TLD, subdomain, externalIp, currentRecord.TTL, currentRecord.Prio, currentRecord.Disabled)
|
||||
numberUpdated++
|
||||
if err != nil {
|
||||
return numberUpdated, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return numberUpdated, nil
|
||||
}
|
||||
105
pkg/changeDetector/changeDetector_test.go
Normal file
105
pkg/changeDetector/changeDetector_test.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package changeDetector
|
||||
|
||||
import (
|
||||
"net"
|
||||
"realdnydns/model/common"
|
||||
"realdnydns/pkg/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type MockExternalIpProvider struct {
|
||||
ExternalIp string
|
||||
}
|
||||
|
||||
func (m *MockExternalIpProvider) GetExternalIp() (net.IP, error) {
|
||||
return net.ParseIP(m.ExternalIp), nil
|
||||
}
|
||||
|
||||
type MockDNSProvider interface {
|
||||
UpdateRecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error)
|
||||
GetRecord(tld string, subdomain string) (*common.ARecord, error)
|
||||
}
|
||||
|
||||
type MockDNSProviderImpl struct {
|
||||
GetRecordIpResponse string
|
||||
UpdateRecordIpResponse string
|
||||
}
|
||||
|
||||
func (m *MockDNSProviderImpl) GetRecord(tld string, subdomain string) (*common.ARecord, error) {
|
||||
return &common.ARecord{
|
||||
IP: m.GetRecordIpResponse,
|
||||
TTL: 10,
|
||||
Prio: 20,
|
||||
Disabled: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *MockDNSProviderImpl) UpdateRecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error) {
|
||||
return &common.ARecord{
|
||||
IP: m.UpdateRecordIpResponse,
|
||||
TTL: 10,
|
||||
Prio: 20,
|
||||
Disabled: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestDetectAndApplyChanges(t *testing.T) {
|
||||
t.Run("with changes", testDetectAndApplyChangesWithChanges())
|
||||
t.Run("without changes", testDetectAndApplyChangesWithoutChanges())
|
||||
}
|
||||
|
||||
func testDetectAndApplyChangesWithChanges() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
changeDetector := New(&MockExternalIpProvider{
|
||||
ExternalIp: "127.0.0.1",
|
||||
}, &MockDNSProviderImpl{
|
||||
GetRecordIpResponse: "127.0.0.2",
|
||||
UpdateRecordIpResponse: "127.0.0.1",
|
||||
}, []config.DomainConfig{
|
||||
{
|
||||
TLD: "example.com",
|
||||
Subdomains: []string{
|
||||
"test",
|
||||
"@",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
numberUpdated, err := changeDetector.DetectAndApplyChanges()
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if numberUpdated != 2 {
|
||||
t.Errorf("expected numberUpdated to be 2, got %v", numberUpdated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDetectAndApplyChangesWithoutChanges() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
changeDetector := New(&MockExternalIpProvider{
|
||||
ExternalIp: "127.0.0.1",
|
||||
}, &MockDNSProviderImpl{
|
||||
GetRecordIpResponse: "127.0.0.1",
|
||||
UpdateRecordIpResponse: "127.0.0.1",
|
||||
}, []config.DomainConfig{
|
||||
{
|
||||
TLD: "example.com",
|
||||
Subdomains: []string{
|
||||
"test",
|
||||
"@",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
numberUpdated, err := changeDetector.DetectAndApplyChanges()
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if numberUpdated != 0 {
|
||||
t.Errorf("expected numberUpdated to be 0, got %v", numberUpdated)
|
||||
}
|
||||
}
|
||||
}
|
||||
16
pkg/config/__mocks__/testLoadCanFindFile.yaml
Normal file
16
pkg/config/__mocks__/testLoadCanFindFile.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
ip_provider:
|
||||
type: plain
|
||||
config:
|
||||
url: https://ifconfig.me
|
||||
dns_provider:
|
||||
type: ionos
|
||||
config:
|
||||
api_key: exampleAPIKey
|
||||
base_url: https://example.com
|
||||
domains:
|
||||
- tld: example.com
|
||||
subdomains:
|
||||
- "@"
|
||||
- www
|
||||
check_interval: 0 0 0/6 * * * *
|
||||
43
pkg/config/config.go
Normal file
43
pkg/config/config.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type DomainConfig struct {
|
||||
TLD string `yaml:"tld"`
|
||||
Subdomains []string `yaml:"subdomains"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
ExternalIPProvider ExternalIpProviderConfig `yaml:"ip_provider"`
|
||||
DNSProvider DNSProviderConfig `yaml:"dns_provider"`
|
||||
Domains []DomainConfig `yaml:"domains"`
|
||||
CheckInterval string `yaml:"check_interval"`
|
||||
}
|
||||
|
||||
type ExternalIpProviderConfig struct {
|
||||
Type string `yaml:"type"`
|
||||
ProviderConfig yaml.Node `yaml:"config"`
|
||||
}
|
||||
|
||||
type DNSProviderConfig struct {
|
||||
Type string `yaml:"type"`
|
||||
ProviderConfig yaml.Node `yaml:"config"`
|
||||
}
|
||||
|
||||
func (c *Config) Load(filePath string) error {
|
||||
err := yaml.Unmarshal([]byte(filePath), c)
|
||||
if err != nil {
|
||||
inputConfig, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return yaml.Unmarshal(inputConfig, c)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
65
pkg/config/config_test.go
Normal file
65
pkg/config/config_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
t.Run("Can find file", testLoadCanFindFile())
|
||||
t.Run("Cannot find file", testLoadCannotFindFile())
|
||||
t.Run("Unmarshals from direct input", testLoadUnmarshalsFromDirectInput())
|
||||
}
|
||||
|
||||
func testLoadCanFindFile() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
|
||||
c := Config{}
|
||||
err := c.Load("./__mocks__/testLoadCanFindFile.yaml")
|
||||
|
||||
want := c.DNSProvider.Type == "ionos" && c.ExternalIPProvider.Type == "plain"
|
||||
|
||||
if !want || err != nil {
|
||||
t.Fatalf("DnsProviderName couldn't be properly loaded or unmarshaled, Load() = %v, want %v", err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testLoadCannotFindFile() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
c := Config{}
|
||||
err := c.Load("nonexistent.yaml")
|
||||
want := err != nil
|
||||
|
||||
if !want {
|
||||
t.Fatalf("Config didn't throw an error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testLoadUnmarshalsFromDirectInput() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
c := Config{}
|
||||
err := c.Load(`---
|
||||
ip_provider:
|
||||
type: plain
|
||||
config:
|
||||
url: https://ifconfig.me
|
||||
dns_provider:
|
||||
type: ionos
|
||||
config:
|
||||
api_key: exampleAPIKey
|
||||
base_url: https://example.com
|
||||
domains:
|
||||
- tld: example.com
|
||||
subdomains:
|
||||
- "@"
|
||||
- www
|
||||
check_interval: 0 0 0/6 * * * *`)
|
||||
|
||||
want := c.DNSProvider.Type == "ionos" && c.ExternalIPProvider.Type == "plain"
|
||||
|
||||
if !want || err != nil {
|
||||
t.Fatalf("DnsProviderName couldn't be properly loaded or unmarshaled, Load() = %v, want %v", err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
16
pkg/dnsProvider/__mocks__/testNewCanCreateIonosProvider.yaml
Normal file
16
pkg/dnsProvider/__mocks__/testNewCanCreateIonosProvider.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
ip_provider:
|
||||
type: plain
|
||||
config:
|
||||
url: https://ifconfig.me
|
||||
dns_provider:
|
||||
type: ionos
|
||||
config:
|
||||
api_key: exampleAPIKey
|
||||
base_url: https://example.com
|
||||
domains:
|
||||
- tld: example.com
|
||||
subdomains:
|
||||
- "@"
|
||||
- www
|
||||
check_interval: 0 0 0/6 * * * *
|
||||
11
pkg/dnsProvider/dnsProvider.go
Normal file
11
pkg/dnsProvider/dnsProvider.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package dnsProvider
|
||||
|
||||
import (
|
||||
"net"
|
||||
"realdnydns/model/common"
|
||||
)
|
||||
|
||||
type DNSProvider interface {
|
||||
UpdateRecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error)
|
||||
GetRecord(tld string, subdomain string) (*common.ARecord, error)
|
||||
}
|
||||
9
pkg/externalIpProvider/externalIpProvider.go
Normal file
9
pkg/externalIpProvider/externalIpProvider.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package externalIpProvider
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
type ExternalIpProvider interface {
|
||||
GetExternalIp() (net.IP, error)
|
||||
}
|
||||
Reference in New Issue
Block a user