Skip to content

Commit

Permalink
PORKBUN: Add registrar support
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Sep 2, 2023
1 parent e32bdc0 commit 1465e5d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion documentation/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ If a feature is definitively not supported for whatever reason, we would also li
| [`ORACLE`](providers/oracle.md) |||||||||||||||||||
| [`OVH`](providers/ovh.md) |||||||||||||||||||
| [`PACKETFRAME`](providers/packetframe.md) |||||||||||||||||||
| [`PORKBUN`](providers/porkbun.md) ||| ||||||||||||||||
| [`PORKBUN`](providers/porkbun.md) ||| ||||||||||||||||
| [`POWERDNS`](providers/powerdns.md) |||||||||||||||||||
| [`ROUTE53`](providers/route53.md) |||||||||||||||||||
| [`RWTH`](providers/rwth.md) |||||||||||||||||||
Expand Down
35 changes: 34 additions & 1 deletion providers/porkbun/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"fmt"
"io"
"net/http"
"sort"
"time"

"github.com/babolivier/go-doh-client"
)

const (
Expand All @@ -18,7 +21,7 @@ type porkbunProvider struct {
secretKey string
}

type requestParams map[string]string
type requestParams map[string]any

type errorResponse struct {
Status string `json:"status"`
Expand Down Expand Up @@ -121,3 +124,33 @@ func (c *porkbunProvider) getRecords(domain string) ([]domainRecord, error) {
}
return records, nil
}

func (c *porkbunProvider) getNameservers(domain string) ([]string, error) {
// Porkbun doesn't currently expose an API to fetch NS settings, so use DoH.
resolver := doh.Resolver{
Host: "1.1.1.1",
Class: doh.IN,
}

// Perform a NS lookup
nss, _, err := resolver.LookupNS(domain)
if err != nil {
return nil, fmt.Errorf("failed fetching nameservers list (porkbun): %s", err)
}

ns := []string{}
for _, res := range nss {
ns = append(ns, res.Host)
}
sort.Strings(ns)
return ns, nil
}

func (c *porkbunProvider) updateNameservers(ns []string, domain string) error {
params := requestParams{}
params["ns"] = ns
if _, err := c.post(fmt.Sprintf("/domain/updateNS/%s", domain), params); err != nil {
return fmt.Errorf("failed NS update (porkbun): %s", err)
}
return nil
}
44 changes: 41 additions & 3 deletions providers/porkbun/porkbunProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package porkbun
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"

Expand All @@ -25,8 +26,16 @@ var defaultNS = []string{
"salvador.ns.porkbun.com",
}

// NewPorkbun creates the provider.
func NewPorkbun(m map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) {
func newReg(conf map[string]string) (providers.Registrar, error) {
return newPorkbun(conf, nil)
}

func newDsp(conf map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) {
return newPorkbun(conf, metadata)
}

// newPorkbun creates the provider.
func newPorkbun(m map[string]string, metadata json.RawMessage) (*porkbunProvider, error) {
c := &porkbunProvider{}

c.apiKey, c.secretKey = m["api_key"], m["secret_key"]
Expand Down Expand Up @@ -63,8 +72,9 @@ var features = providers.DocumentationNotes{
}

func init() {
providers.RegisterRegistrarType("PORKBUN", newReg)
fns := providers.DspFuncs{
Initializer: NewPorkbun,
Initializer: newDsp,
RecordAuditor: AuditRecords,
}
providers.RegisterDomainServiceProviderType("PORKBUN", fns, features)
Expand Down Expand Up @@ -317,3 +327,31 @@ func fixTTL(ttl uint32) uint32 {
}
return minimumTTL
}

func (c *porkbunProvider) GetRegistrarCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
nss, err := c.getNameservers(dc.Name)
if err != nil {
return nil, err
}
foundNameservers := strings.Join(nss, ",")

expected := []string{}
for _, ns := range dc.Nameservers {
expected = append(expected, ns.Name)
}
sort.Strings(expected)
expectedNameservers := strings.Join(expected, ",")

if foundNameservers == expectedNameservers {
return nil, nil
}

return []*models.Correction{
{
Msg: fmt.Sprintf("Update nameservers %s -> %s", foundNameservers, expectedNameservers),
F: func() error {
return c.updateNameservers(expected, dc.Name)
},
},
}, nil
}

0 comments on commit 1465e5d

Please sign in to comment.