Refactor: Moved all under internal.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

FEAT: Directadmin provider is now working
This commit is contained in:
2024-03-18 09:55:01 +13:00
parent a52034216b
commit e1bb5adf36
19 changed files with 163 additions and 73 deletions

View File

@@ -5,7 +5,8 @@ import (
)
type DNSProvider interface {
UpdateRecord(hostname string, ip string, old_ip string) error
UpdateRecord(hostname string, ip string) error
FetchRecord(hostname string) (string, error)
}
type DomainParts struct {
@@ -16,8 +17,15 @@ type DomainParts struct {
func GetDomainParts(hostname string) *DomainParts {
data := arrayToSlice(strings.Split(hostname, "."))
out := DomainParts{Name: data[0], Domain: strings.Join(data[1:], ".")}
return &out
if len(data) <= 2 {
// This might be the actual root domain
out := DomainParts{Name: strings.Join(data[0:], "."), Domain: strings.Join(data[0:], ".")}
return &out
} else {
// This is a subdomain
out := DomainParts{Name: data[0], Domain: strings.Join(data[1:], ".")}
return &out
}
}
func arrayToSlice(array []string) []string {

View File

@@ -5,7 +5,7 @@ import (
"log"
"github.com/cloudflare/cloudflare-go"
"github.com/mkelcik/cloudflare-ddns-update/internal"
"hub.cybercinch.nz/cybercinch/ddns-update/internal"
)
const (
@@ -18,7 +18,7 @@ type CloudflareProvider struct {
Config internal.Config
}
func (d *CloudflareProvider) UpdateRecord(hostname string, ip string, old_ip string) error {
func (d *CloudflareProvider) UpdateRecord(hostname string, ip string) error {
// old_ip is not required for Cloudflare updates
domain_parts := GetDomainParts(hostname)
zoneId := d.FindZoneIdByName(domain_parts.Domain)
@@ -33,8 +33,8 @@ func (d *CloudflareProvider) UpdateRecord(hostname string, ip string, old_ip str
Content: ip,
}
if d.Config.OnChangeComment != "" {
update.Comment = &d.Config.OnChangeComment
if d.Config.CloudflareOnChangeComment != "" {
update.Comment = &d.Config.CloudflareOnChangeComment
}
_, err = d.Client.UpdateDNSRecord(d.Context, cloudflare.ZoneIdentifier(zoneId), update)
@@ -44,6 +44,13 @@ func (d *CloudflareProvider) UpdateRecord(hostname string, ip string, old_ip str
return nil
}
func (d *CloudflareProvider) FetchRecord(hostname string) (string, error) {
domain_parts := GetDomainParts(hostname)
zoneId := d.FindZoneIdByName(domain_parts.Domain)
dnsRecord, err := d.GetDnsRecord(hostname, zoneId)
return dnsRecord[0].Content, err
}
func (d *CloudflareProvider) NewClient(api_token string) {
api, err := cloudflare.NewWithAPIToken(api_token)
if err != nil {

View File

@@ -2,10 +2,12 @@ package dns_providers
import (
"context"
"fmt"
"log"
"time"
"github.com/levelzerotechnology/directadmin-go"
"github.com/mkelcik/cloudflare-ddns-update/internal"
"hub.cybercinch.nz/cybercinch/ddns-update/internal"
)
const (
@@ -18,20 +20,54 @@ type Directadmin struct {
Config internal.Config
}
func (d *Directadmin) UpdateRecord(hostname string, ip string, old_ip string) error {
type ListDNSRecordsParams struct {
Name string
}
func (d *Directadmin) FetchRecord(hostname string) (string, error) {
dnsRecord, err := d.GetDnsRecord(hostname)
if err != nil {
log.Fatal("unable to retrieve DNS record")
return "", err
}
return dnsRecord[0].Value, nil
}
func (d *Directadmin) UpdateRecord(hostname string, ip string) error {
result := GetDomainParts(hostname)
current_record, _ := d.GetDnsRecord(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}
// Create an updated record for the new ip
new_record := directadmin.DNSRecord{Name: current_record[0].Name, Ttl: current_record[0].Ttl, Type: current_record[0].Type, Value: ip}
err := d.Client.UpdateDNSRecord(result.Domain, a, b)
err := d.Client.UpdateDNSRecord(result.Domain, current_record[0], new_record)
if err != nil {
return err
}
return nil
}
func (d *Directadmin) GetDnsRecord(hostname string) ([]directadmin.DNSRecord, error) {
domainParts := GetDomainParts(hostname)
dnsRecords, err := d.Client.GetDNSRecords(domainParts.Domain)
var slice []directadmin.DNSRecord
for _, dnsRecord := range dnsRecords {
if domainParts.Name == dnsRecord.Name {
slice = append(slice, dnsRecord)
}
}
if len(slice) == 0 {
return nil, fmt.Errorf("unable to find DNS record for %s", hostname)
}
return slice, err
}
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 {