You've already forked ddns-updater
FEAT: Refactor allowing multiple DNS Providers
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
25
internal/dns_providers/base_provider.go
Normal file
25
internal/dns_providers/base_provider.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package dns_providers
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DNSProvider interface {
|
||||
UpdateRecord(hostname string, ip string, old_ip string) error
|
||||
}
|
||||
|
||||
type DomainParts struct {
|
||||
Name string
|
||||
Domain string
|
||||
}
|
||||
|
||||
func GetDomainParts(hostname string) *DomainParts {
|
||||
data := arrayToSlice(strings.Split(hostname, "."))
|
||||
|
||||
out := DomainParts{Name: data[0], Domain: strings.Join(data[1:], ".")}
|
||||
return &out
|
||||
}
|
||||
|
||||
func arrayToSlice(array []string) []string {
|
||||
return array[:]
|
||||
}
|
||||
85
internal/dns_providers/cloudflare.go
Normal file
85
internal/dns_providers/cloudflare.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package dns_providers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/cloudflare/cloudflare-go"
|
||||
"github.com/mkelcik/cloudflare-ddns-update/internal"
|
||||
)
|
||||
|
||||
const (
|
||||
CloudflareTag = "cloudflare"
|
||||
)
|
||||
|
||||
type CloudflareProvider struct {
|
||||
Context context.Context
|
||||
Client *cloudflare.API
|
||||
Config internal.Config
|
||||
}
|
||||
|
||||
func (d *CloudflareProvider) UpdateRecord(hostname string, ip string, old_ip string) error {
|
||||
// old_ip is not required for Cloudflare updates
|
||||
domain_parts := GetDomainParts(hostname)
|
||||
zoneId := d.FindZoneIdByName(domain_parts.Domain)
|
||||
dnsRecords, err := d.GetDnsRecord(hostname, zoneId)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
update := cloudflare.UpdateDNSRecordParams{
|
||||
ID: dnsRecords[0].ID,
|
||||
Content: ip,
|
||||
}
|
||||
|
||||
if d.Config.OnChangeComment != "" {
|
||||
update.Comment = &d.Config.OnChangeComment
|
||||
}
|
||||
|
||||
_, err = d.Client.UpdateDNSRecord(d.Context, cloudflare.ZoneIdentifier(zoneId), update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *CloudflareProvider) NewClient(api_token string) {
|
||||
api, err := cloudflare.NewWithAPIToken(api_token)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
d.Client = api
|
||||
}
|
||||
|
||||
func (d *CloudflareProvider) FindZoneIdByName(hostname string) string {
|
||||
// Fetch user details on the account
|
||||
zoneID, err := d.Client.ZoneIDByName(hostname)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return zoneID
|
||||
}
|
||||
|
||||
func (d *CloudflareProvider) GetDnsRecord(name string, zoneId string) ([]cloudflare.DNSRecord, error) {
|
||||
|
||||
params := cloudflare.ListDNSRecordsParams{
|
||||
Name: name,
|
||||
}
|
||||
|
||||
page, _, err := d.Client.ListDNSRecords(d.Context, cloudflare.ZoneIdentifier(zoneId), params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func NewCloudflareProvider(ctx context.Context, config internal.Config) *CloudflareProvider {
|
||||
c := &CloudflareProvider{}
|
||||
c.Context = ctx
|
||||
c.Config = config
|
||||
c.NewClient(c.Config.ApiToken)
|
||||
return c
|
||||
}
|
||||
55
internal/dns_providers/directadmin.go
Normal file
55
internal/dns_providers/directadmin.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package dns_providers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/levelzerotechnology/directadmin-go"
|
||||
"github.com/mkelcik/cloudflare-ddns-update/internal"
|
||||
)
|
||||
|
||||
const (
|
||||
DirectadminTag = "directadmin"
|
||||
)
|
||||
|
||||
type Directadmin struct {
|
||||
Client *directadmin.UserContext
|
||||
Context context.Context
|
||||
Config internal.Config
|
||||
}
|
||||
|
||||
func (d *Directadmin) UpdateRecord(hostname string, ip string, old_ip string) error {
|
||||
|
||||
result := GetDomainParts(hostname)
|
||||
|
||||
a := directadmin.DNSRecord{Name: result.Name, Ttl: 300, Type: "A", Value: old_ip}
|
||||
b := directadmin.DNSRecord{Name: result.Name, Ttl: 300, Type: "A", Value: ip}
|
||||
|
||||
err := d.Client.UpdateDNSRecord(result.Domain, a, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Directadmin) NewClient(server_url string, username string, key string) {
|
||||
api, err := directadmin.New(server_url, 5*time.Second, false, false)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
userCtx, err := api.LoginAsUser(username, key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
d.Client = userCtx
|
||||
}
|
||||
|
||||
func NewDirectAdminProvider(ctx context.Context, config internal.Config) *Directadmin {
|
||||
c := &Directadmin{}
|
||||
c.Context = ctx
|
||||
c.Config = config
|
||||
c.NewClient(config.DirectadminUrl, config.DirectadminUsername, config.DirectadminKey)
|
||||
return c
|
||||
}
|
||||
Reference in New Issue
Block a user