feat: notification provider #8
11
main.go
11
main.go
@@ -8,6 +8,8 @@ import (
|
|||||||
ionos "realdnydns/pkg/dnsProvider/ionos"
|
ionos "realdnydns/pkg/dnsProvider/ionos"
|
||||||
"realdnydns/pkg/externalIpProvider"
|
"realdnydns/pkg/externalIpProvider"
|
||||||
plainExternalIpProvider "realdnydns/pkg/externalIpProvider/plain"
|
plainExternalIpProvider "realdnydns/pkg/externalIpProvider/plain"
|
||||||
|
"realdnydns/pkg/notificationProvider"
|
||||||
|
notificationProviderConsole "realdnydns/pkg/notificationProvider/console"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-co-op/gocron"
|
"github.com/go-co-op/gocron"
|
||||||
@@ -50,7 +52,14 @@ func main() {
|
|||||||
panic(fmt.Errorf("unknown DNS provider: %s", configClient.DNSProvider.Type))
|
panic(fmt.Errorf("unknown DNS provider: %s", configClient.DNSProvider.Type))
|
||||||
}
|
}
|
||||||
|
|
||||||
changeDetector := changeDetector.New(externalIpProvider, dnsProvider, configClient.Domains)
|
var notificationProvider notificationProvider.NotificationProvider
|
||||||
|
switch configClient.NotificationProvider.Type {
|
||||||
|
default:
|
||||||
|
// Use default console notification provider
|
||||||
|
notificationProvider = notificationProviderConsole.New()
|
||||||
|
}
|
||||||
|
|
||||||
|
changeDetector := changeDetector.New(externalIpProvider, dnsProvider, notificationProvider, configClient.Domains)
|
||||||
|
|
||||||
s := gocron.NewScheduler(time.UTC)
|
s := gocron.NewScheduler(time.UTC)
|
||||||
s.SingletonMode()
|
s.SingletonMode()
|
||||||
|
|||||||
@@ -1,26 +1,31 @@
|
|||||||
package changeDetector
|
package changeDetector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"realdnydns/pkg/config"
|
"realdnydns/pkg/config"
|
||||||
"realdnydns/pkg/dnsProvider"
|
"realdnydns/pkg/dnsProvider"
|
||||||
"realdnydns/pkg/externalIpProvider"
|
"realdnydns/pkg/externalIpProvider"
|
||||||
|
"realdnydns/pkg/notificationProvider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChangeDetector struct {
|
type ChangeDetector struct {
|
||||||
externalIpProvider externalIpProvider.ExternalIpProvider
|
externalIpProvider externalIpProvider.ExternalIpProvider
|
||||||
dnsProvider dnsProvider.DNSProvider
|
dnsProvider dnsProvider.DNSProvider
|
||||||
domains []config.DomainConfig
|
notificationProvider notificationProvider.NotificationProvider
|
||||||
|
domains []config.DomainConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
externalIpProvider externalIpProvider.ExternalIpProvider,
|
externalIpProvider externalIpProvider.ExternalIpProvider,
|
||||||
dnsProvider dnsProvider.DNSProvider,
|
dnsProvider dnsProvider.DNSProvider,
|
||||||
|
notificationProvider notificationProvider.NotificationProvider,
|
||||||
domains []config.DomainConfig,
|
domains []config.DomainConfig,
|
||||||
) ChangeDetector {
|
) ChangeDetector {
|
||||||
return ChangeDetector{
|
return ChangeDetector{
|
||||||
externalIpProvider: externalIpProvider,
|
externalIpProvider: externalIpProvider,
|
||||||
dnsProvider: dnsProvider,
|
dnsProvider: dnsProvider,
|
||||||
domains: domains,
|
notificationProvider: notificationProvider,
|
||||||
|
domains: domains,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,6 +45,14 @@ func (c *ChangeDetector) DetectAndApplyChanges() (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if currentRecord.IP != externalIp.String() {
|
if currentRecord.IP != externalIp.String() {
|
||||||
|
err = c.notificationProvider.SendNotification(
|
||||||
|
fmt.Sprintf("Update %s.%s", subdomain, domain.TLD),
|
||||||
|
fmt.Sprintf("The IP of %s has changed from %s to %s", domain.TLD, currentRecord.IP, externalIp.String()),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return numberUpdated, err
|
||||||
|
}
|
||||||
|
|
||||||
_, err = c.dnsProvider.UpdateRecord(domain.TLD, subdomain, externalIp, currentRecord.TTL, currentRecord.Prio, currentRecord.Disabled)
|
_, err = c.dnsProvider.UpdateRecord(domain.TLD, subdomain, externalIp, currentRecord.TTL, currentRecord.Prio, currentRecord.Disabled)
|
||||||
numberUpdated++
|
numberUpdated++
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -43,6 +43,14 @@ func (m *MockDNSProviderImpl) UpdateRecord(tld string, subdomain string, ip net.
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MockedNotificationProvider struct{}
|
||||||
|
|
||||||
|
type MockedNotificationProviderImpl struct{}
|
||||||
|
|
||||||
|
func (m *MockedNotificationProviderImpl) SendNotification(title string, message string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestDetectAndApplyChanges(t *testing.T) {
|
func TestDetectAndApplyChanges(t *testing.T) {
|
||||||
t.Run("with changes", testDetectAndApplyChangesWithChanges())
|
t.Run("with changes", testDetectAndApplyChangesWithChanges())
|
||||||
t.Run("without changes", testDetectAndApplyChangesWithoutChanges())
|
t.Run("without changes", testDetectAndApplyChangesWithoutChanges())
|
||||||
@@ -55,15 +63,16 @@ func testDetectAndApplyChangesWithChanges() func(t *testing.T) {
|
|||||||
}, &MockDNSProviderImpl{
|
}, &MockDNSProviderImpl{
|
||||||
GetRecordIpResponse: "127.0.0.2",
|
GetRecordIpResponse: "127.0.0.2",
|
||||||
UpdateRecordIpResponse: "127.0.0.1",
|
UpdateRecordIpResponse: "127.0.0.1",
|
||||||
}, []config.DomainConfig{
|
}, &MockedNotificationProviderImpl{},
|
||||||
{
|
[]config.DomainConfig{
|
||||||
TLD: "example.com",
|
{
|
||||||
Subdomains: []string{
|
TLD: "example.com",
|
||||||
"test",
|
Subdomains: []string{
|
||||||
"@",
|
"test",
|
||||||
|
"@",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
|
||||||
|
|
||||||
numberUpdated, err := changeDetector.DetectAndApplyChanges()
|
numberUpdated, err := changeDetector.DetectAndApplyChanges()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -83,15 +92,16 @@ func testDetectAndApplyChangesWithoutChanges() func(t *testing.T) {
|
|||||||
}, &MockDNSProviderImpl{
|
}, &MockDNSProviderImpl{
|
||||||
GetRecordIpResponse: "127.0.0.1",
|
GetRecordIpResponse: "127.0.0.1",
|
||||||
UpdateRecordIpResponse: "127.0.0.1",
|
UpdateRecordIpResponse: "127.0.0.1",
|
||||||
}, []config.DomainConfig{
|
}, &MockedNotificationProviderImpl{},
|
||||||
{
|
[]config.DomainConfig{
|
||||||
TLD: "example.com",
|
{
|
||||||
Subdomains: []string{
|
TLD: "example.com",
|
||||||
"test",
|
Subdomains: []string{
|
||||||
"@",
|
"test",
|
||||||
|
"@",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
|
||||||
|
|
||||||
numberUpdated, err := changeDetector.DetectAndApplyChanges()
|
numberUpdated, err := changeDetector.DetectAndApplyChanges()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -12,10 +12,11 @@ type DomainConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
ExternalIPProvider ExternalIpProviderConfig `yaml:"ip_provider"`
|
ExternalIPProvider ExternalIpProviderConfig `yaml:"ip_provider"`
|
||||||
DNSProvider DNSProviderConfig `yaml:"dns_provider"`
|
DNSProvider DNSProviderConfig `yaml:"dns_provider"`
|
||||||
Domains []DomainConfig `yaml:"domains"`
|
NotificationProvider NotificationProviderConfig `yaml:"notification_provider,omitempty"`
|
||||||
CheckInterval string `yaml:"check_interval"`
|
Domains []DomainConfig `yaml:"domains"`
|
||||||
|
CheckInterval string `yaml:"check_interval"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExternalIpProviderConfig struct {
|
type ExternalIpProviderConfig struct {
|
||||||
@@ -28,6 +29,11 @@ type DNSProviderConfig struct {
|
|||||||
ProviderConfig yaml.Node `yaml:"config"`
|
ProviderConfig yaml.Node `yaml:"config"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NotificationProviderConfig struct {
|
||||||
|
Type string `yaml:"type"`
|
||||||
|
ProviderConfig yaml.Node `yaml:"config"`
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Config) Load(filePath string) error {
|
func (c *Config) Load(filePath string) error {
|
||||||
err := yaml.Unmarshal([]byte(filePath), c)
|
err := yaml.Unmarshal([]byte(filePath), c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -49,6 +49,10 @@ dns_provider:
|
|||||||
config:
|
config:
|
||||||
api_key: exampleAPIKey
|
api_key: exampleAPIKey
|
||||||
base_url: https://example.com
|
base_url: https://example.com
|
||||||
|
notification_provider:
|
||||||
|
type: gotify
|
||||||
|
config:
|
||||||
|
url: https://example.com
|
||||||
domains:
|
domains:
|
||||||
- tld: example.com
|
- tld: example.com
|
||||||
subdomains:
|
subdomains:
|
||||||
@@ -56,7 +60,7 @@ domains:
|
|||||||
- www
|
- www
|
||||||
check_interval: 0 0 0/6 * * * *`)
|
check_interval: 0 0 0/6 * * * *`)
|
||||||
|
|
||||||
want := c.DNSProvider.Type == "ionos" && c.ExternalIPProvider.Type == "plain"
|
want := c.DNSProvider.Type == "ionos" && c.ExternalIPProvider.Type == "plain" && c.NotificationProvider.Type == "gotify"
|
||||||
|
|
||||||
if !want || err != nil {
|
if !want || err != nil {
|
||||||
t.Fatalf("DnsProviderName couldn't be properly loaded or unmarshaled, Load() = %v, want %v", err, want)
|
t.Fatalf("DnsProviderName couldn't be properly loaded or unmarshaled, Load() = %v, want %v", err, want)
|
||||||
|
|||||||
16
pkg/notificationProvider/console/console.go
Normal file
16
pkg/notificationProvider/console/console.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package notificationProviderConsole
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NotificationProviderImplConsole struct{}
|
||||||
|
|
||||||
|
func New() *NotificationProviderImplConsole {
|
||||||
|
return &NotificationProviderImplConsole{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *NotificationProviderImplConsole) SendNotification(title string, message string) error {
|
||||||
|
fmt.Printf("%s: %s\n", title, message)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
5
pkg/notificationProvider/notificationProvider.go
Normal file
5
pkg/notificationProvider/notificationProvider.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package notificationProvider
|
||||||
|
|
||||||
|
type NotificationProvider interface {
|
||||||
|
SendNotification(title string, message string) error
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user