diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7ead14e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM golang:1.21-alpine + +# Set the Current Working Directory inside the container +WORKDIR /app + +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed +RUN go mod download + +# Copy the source from the current directory to the Working Directory inside the container +COPY . . + +# Build the Go app +RUN go build -o main . + + +# Command to run the executable + +CMD ["./main"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..142723b --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +test: + go test ./pkg/... -coverprofile=coverage.out + +build: + go build + +lint: + golint ./... diff --git a/README.md b/README.md index 922e846..79bd66c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # realDynDNS +RealDynDNS aims to be a replacement to "classical" dynDNS solutions that offer a subdomain. Instead realDynDns actually changes your DNS entries. +This service requires your DNS provider to expose an API that allows your DNS entries to be changed. +A service that provides your current external IP is also required. diff --git a/config.example.yaml b/config.example.yaml new file mode 100644 index 0000000..9fe438e --- /dev/null +++ b/config.example.yaml @@ -0,0 +1,16 @@ +--- +ip_provider: + type: plain + config: + url: https://ifconfig.me +dns_provider: + type: ionos + config: + api_key: + base_url: https://api.hosting.ionos.com/dns +domains: + - tld: example.com + subdomains: + - "@" + - www +check_interval: 0 0 0/6 * * * * diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..275c57c --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module realdnydns + +go 1.20 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-co-op/gocron v1.31.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/robfig/cron/v3 v3.0.1 // indirect + github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect + go.uber.org/atomic v1.9.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..45d646f --- /dev/null +++ b/go.sum @@ -0,0 +1,40 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-co-op/gocron v1.31.2 h1:tAUW64bxYc5QlzEy2t30TnHX2+uInNDajKXxWi4SACA= +github.com/go-co-op/gocron v1.31.2/go.mod h1:39f6KNSGVOU1LO/ZOoZfcSxwlsJDQOKSu8erN0SH48Y= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/model/common/common.go b/model/common/common.go new file mode 100644 index 0000000..20575df --- /dev/null +++ b/model/common/common.go @@ -0,0 +1,9 @@ +package common + +type ARecord struct { + Domain string + IP string + TTL int + Prio int + Disabled bool +} diff --git a/pkg/changeDetector/changeDetector.go b/pkg/changeDetector/changeDetector.go new file mode 100644 index 0000000..3cc9fab --- /dev/null +++ b/pkg/changeDetector/changeDetector.go @@ -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 +} diff --git a/pkg/changeDetector/changeDetector_test.go b/pkg/changeDetector/changeDetector_test.go new file mode 100644 index 0000000..ffe0123 --- /dev/null +++ b/pkg/changeDetector/changeDetector_test.go @@ -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) + } + } +} diff --git a/pkg/config/__mocks__/testLoadCanFindFile.yaml b/pkg/config/__mocks__/testLoadCanFindFile.yaml new file mode 100644 index 0000000..48007ce --- /dev/null +++ b/pkg/config/__mocks__/testLoadCanFindFile.yaml @@ -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 * * * * diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..c0f30a5 --- /dev/null +++ b/pkg/config/config.go @@ -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 +} diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go new file mode 100644 index 0000000..dfd3e71 --- /dev/null +++ b/pkg/config/config_test.go @@ -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) + } + } +} diff --git a/pkg/dnsProvider/__mocks__/testNewCanCreateIonosProvider.yaml b/pkg/dnsProvider/__mocks__/testNewCanCreateIonosProvider.yaml new file mode 100644 index 0000000..48007ce --- /dev/null +++ b/pkg/dnsProvider/__mocks__/testNewCanCreateIonosProvider.yaml @@ -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 * * * * diff --git a/pkg/dnsProvider/dnsProvider.go b/pkg/dnsProvider/dnsProvider.go new file mode 100644 index 0000000..0f65172 --- /dev/null +++ b/pkg/dnsProvider/dnsProvider.go @@ -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) +} diff --git a/pkg/externalIpProvider/externalIpProvider.go b/pkg/externalIpProvider/externalIpProvider.go new file mode 100644 index 0000000..a6c81ca --- /dev/null +++ b/pkg/externalIpProvider/externalIpProvider.go @@ -0,0 +1,9 @@ +package externalIpProvider + +import ( + "net" +) + +type ExternalIpProvider interface { + GetExternalIp() (net.IP, error) +}