2022-01-06 01:09:46 +08:00
|
|
|
package packetframe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-21 01:21:45 +08:00
|
|
|
"github.com/StackExchange/dnscontrol/v4/models"
|
|
|
|
"github.com/StackExchange/dnscontrol/v4/pkg/diff"
|
|
|
|
"github.com/StackExchange/dnscontrol/v4/providers"
|
2022-01-06 01:09:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// packetframeProvider is the handle for this provider.
|
|
|
|
type packetframeProvider struct {
|
|
|
|
client *http.Client
|
|
|
|
baseURL *url.URL
|
|
|
|
token string
|
2023-04-15 03:22:23 +08:00
|
|
|
domainIndex map[string]zoneInfo
|
2022-01-06 01:09:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// newPacketframe creates the provider.
|
|
|
|
func newPacketframe(m map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) {
|
|
|
|
if m["token"] == "" {
|
|
|
|
return nil, fmt.Errorf("missing Packetframe token")
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL, err := url.Parse(defaultBaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid base URL for Packetframe")
|
|
|
|
}
|
|
|
|
client := http.Client{}
|
|
|
|
|
|
|
|
api := &packetframeProvider{client: &client, baseURL: baseURL, token: m["token"]}
|
|
|
|
|
|
|
|
return api, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var features = providers.DocumentationNotes{
|
2024-03-19 06:30:09 +08:00
|
|
|
// The default for unlisted capabilities is 'Cannot'.
|
|
|
|
// See providers/capabilities.go for the entire list of capabilities.
|
2022-03-03 00:19:15 +08:00
|
|
|
providers.CanGetZones: providers.Unimplemented(),
|
2024-03-21 00:36:54 +08:00
|
|
|
providers.CanConcur: providers.Cannot(),
|
2022-03-03 00:19:15 +08:00
|
|
|
providers.CanUsePTR: providers.Can(),
|
|
|
|
providers.CanUseSRV: providers.Can(),
|
2022-01-06 01:09:46 +08:00
|
|
|
providers.DocDualHost: providers.Cannot(),
|
|
|
|
providers.DocOfficiallySupported: providers.Cannot(),
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
fns := providers.DspFuncs{
|
|
|
|
Initializer: newPacketframe,
|
|
|
|
RecordAuditor: AuditRecords,
|
|
|
|
}
|
|
|
|
providers.RegisterDomainServiceProviderType("PACKETFRAME", fns, features)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNameservers returns the nameservers for a domain.
|
|
|
|
func (api *packetframeProvider) GetNameservers(domain string) ([]*models.Nameserver, error) {
|
|
|
|
return models.ToNameservers(defaultNameServerNames)
|
|
|
|
}
|
|
|
|
|
2023-04-15 03:22:23 +08:00
|
|
|
func (api *packetframeProvider) getZone(domain string) (*zoneInfo, error) {
|
2022-01-06 01:09:46 +08:00
|
|
|
if api.domainIndex == nil {
|
|
|
|
if err := api.fetchDomainList(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-04-15 03:22:23 +08:00
|
|
|
z, ok := api.domainIndex[domain+"."]
|
2022-01-06 01:09:46 +08:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%q not a zone in Packetframe account", domain)
|
|
|
|
}
|
|
|
|
|
2023-04-15 03:22:23 +08:00
|
|
|
return &z, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetZoneRecords gets the records of a zone and returns them in RecordConfig format.
|
2023-05-03 01:04:59 +08:00
|
|
|
func (api *packetframeProvider) GetZoneRecords(domain string, meta map[string]string) (models.Records, error) {
|
2023-04-15 03:22:23 +08:00
|
|
|
|
|
|
|
zone, err := api.getZone(domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("no such zone %q in Packetframe account", domain)
|
|
|
|
}
|
|
|
|
|
2022-01-06 01:09:46 +08:00
|
|
|
records, err := api.getRecords(zone.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not load records for domain %q", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
existingRecords := make([]*models.RecordConfig, len(records))
|
|
|
|
|
|
|
|
dc := models.DomainConfig{
|
|
|
|
Name: domain,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range records {
|
|
|
|
existingRecords[i] = toRc(&dc, &records[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return existingRecords, nil
|
|
|
|
}
|
|
|
|
|
2023-04-15 03:22:23 +08:00
|
|
|
// GetZoneRecordsCorrections returns a list of corrections that will turn existing records into dc.Records.
|
|
|
|
func (api *packetframeProvider) GetZoneRecordsCorrections(dc *models.DomainConfig, existingRecords models.Records) ([]*models.Correction, error) {
|
|
|
|
zone, err := api.getZone(dc.Name)
|
2022-01-06 01:09:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("no such zone %q in Packetframe account", dc.Name)
|
|
|
|
}
|
|
|
|
|
2023-10-23 01:56:13 +08:00
|
|
|
toReport, create, dels, modify, err := diff.NewCompat(dc).IncrementalDiff(existingRecords)
|
2023-02-28 08:44:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-23 01:56:13 +08:00
|
|
|
// Start corrections with the reports
|
|
|
|
corrections := diff.GenerateMessageCorrections(toReport)
|
2023-02-28 08:44:23 +08:00
|
|
|
|
|
|
|
for _, m := range create {
|
2024-03-04 23:26:25 +08:00
|
|
|
req, err := toReq(zone.ID, m.Desired)
|
2022-01-06 01:09:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-28 08:44:23 +08:00
|
|
|
corr := &models.Correction{
|
|
|
|
Msg: m.String(),
|
|
|
|
F: func() error {
|
|
|
|
_, err := api.createRecord(req)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
corrections = append(corrections, corr)
|
|
|
|
}
|
2022-01-06 01:09:46 +08:00
|
|
|
|
2023-02-28 08:44:23 +08:00
|
|
|
for _, m := range dels {
|
|
|
|
original := m.Existing.Original.(*domainRecord)
|
|
|
|
if original.ID == "0" { // Skip the default nameservers
|
|
|
|
continue
|
2022-01-07 22:24:24 +08:00
|
|
|
}
|
|
|
|
|
2023-02-28 08:44:23 +08:00
|
|
|
corr := &models.Correction{
|
|
|
|
Msg: m.String(),
|
|
|
|
F: func() error {
|
|
|
|
err := api.deleteRecord(zone.ID, original.ID)
|
|
|
|
return err
|
|
|
|
},
|
2022-01-06 01:09:46 +08:00
|
|
|
}
|
2023-02-28 08:44:23 +08:00
|
|
|
corrections = append(corrections, corr)
|
|
|
|
}
|
2022-01-06 01:09:46 +08:00
|
|
|
|
2023-02-28 08:44:23 +08:00
|
|
|
for _, m := range modify {
|
|
|
|
original := m.Existing.Original.(*domainRecord)
|
|
|
|
if original.ID == "0" { // Skip the default nameservers
|
|
|
|
continue
|
2022-01-07 22:24:24 +08:00
|
|
|
}
|
|
|
|
|
2024-03-04 23:26:25 +08:00
|
|
|
req, _ := toReq(zone.ID, m.Desired)
|
2023-02-28 08:44:23 +08:00
|
|
|
req.ID = original.ID
|
|
|
|
corr := &models.Correction{
|
|
|
|
Msg: m.String(),
|
|
|
|
F: func() error {
|
|
|
|
err := api.modifyRecord(req)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
corrections = append(corrections, corr)
|
2022-01-06 01:09:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return corrections, nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 23:26:25 +08:00
|
|
|
func toReq(zoneID string, rc *models.RecordConfig) (*domainRecord, error) {
|
2022-01-06 01:09:46 +08:00
|
|
|
req := &domainRecord{
|
|
|
|
Type: rc.Type,
|
|
|
|
TTL: int(rc.TTL),
|
|
|
|
Label: rc.GetLabel(),
|
|
|
|
Zone: zoneID,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rc.Type { // #rtype_variations
|
|
|
|
case "A", "AAAA", "PTR", "TXT", "CNAME", "NS":
|
|
|
|
req.Value = rc.GetTargetField()
|
|
|
|
case "MX":
|
|
|
|
req.Value = fmt.Sprintf("%d %s", rc.MxPreference, rc.GetTargetField())
|
|
|
|
case "SRV":
|
|
|
|
req.Value = fmt.Sprintf("%d %d %d %s", rc.SrvPriority, rc.SrvWeight, rc.SrvPort, rc.GetTargetField())
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("packetframe.toReq rtype %q unimplemented", rc.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toRc(dc *models.DomainConfig, r *domainRecord) *models.RecordConfig {
|
|
|
|
rc := &models.RecordConfig{
|
|
|
|
Type: r.Type,
|
|
|
|
TTL: uint32(r.TTL),
|
|
|
|
Original: r,
|
|
|
|
}
|
|
|
|
|
|
|
|
label := strings.TrimSuffix(r.Label, dc.Name+".")
|
|
|
|
label = strings.TrimSuffix(label, ".")
|
|
|
|
if label == "" {
|
|
|
|
label = "@"
|
|
|
|
}
|
|
|
|
rc.SetLabel(label, dc.Name)
|
|
|
|
|
|
|
|
switch rtype := r.Type; rtype { // #rtype_variations
|
|
|
|
case "TXT":
|
2023-01-14 02:00:49 +08:00
|
|
|
rc.SetTargetTXT(r.Value)
|
2022-01-06 01:09:46 +08:00
|
|
|
case "SRV":
|
|
|
|
spl := strings.Split(r.Value, " ")
|
|
|
|
prio, _ := strconv.ParseUint(spl[0], 10, 16)
|
|
|
|
weight, _ := strconv.ParseUint(spl[1], 10, 16)
|
|
|
|
port, _ := strconv.ParseUint(spl[2], 10, 16)
|
|
|
|
rc.SetTargetSRV(uint16(prio), uint16(weight), uint16(port), spl[3])
|
|
|
|
case "MX":
|
|
|
|
spl := strings.Split(r.Value, " ")
|
|
|
|
prio, _ := strconv.ParseUint(spl[0], 10, 16)
|
|
|
|
rc.SetTargetMX(uint16(prio), spl[1])
|
|
|
|
default:
|
|
|
|
rc.SetTarget(r.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc
|
|
|
|
}
|