-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
porkbun.go
132 lines (119 loc) · 3.71 KB
/
porkbun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/nrdcg/porkbun"
log "github.com/sirupsen/logrus"
)
func createOrUpdateRecord(ctx context.Context, client *porkbun.Client, recordID string, domain, host, recordType, address string) error {
if recordID != "" {
recordIDint, err := strconv.Atoi(recordID)
if err != nil {
return err
}
log.WithFields(log.Fields{
"record id": recordID,
"domain": domain,
"host": host,
"record type": recordType,
"address": address,
}).Info("Updating record")
return client.EditRecord(ctx, domain, recordIDint, porkbun.Record{
Name: host,
Type: recordType,
Content: address,
Notes: "DDNS",
})
} else {
log.WithFields(log.Fields{
"record id": recordID,
"domain": domain,
"host": host,
"record type": recordType,
"address": address,
}).Info("Creating record")
_, err := client.CreateRecord(ctx, domain, porkbun.Record{
Name: host,
Type: recordType,
Content: address,
Notes: "DDNS",
})
return err
}
}
func getRecords(ctx context.Context, domain, host string, client *porkbun.Client) (*porkbun.Record, *porkbun.Record, error) {
records, err := client.RetrieveRecords(ctx, domain)
if err != nil {
return nil, nil, err
}
var ipv4Record porkbun.Record
var ipv6Record porkbun.Record
for _, record := range records {
if record.Name == fmt.Sprintf("%s.%s", host, domain) || (host == "" && record.Name == domain) {
if record.Type == "A" {
ipv4Record = record
} else if record.Type == "AAAA" {
ipv6Record = record
}
}
}
return &ipv4Record, &ipv6Record, nil
}
func getPorkbunClients(credentials map[string]PorkbunCredentials) (map[string]*porkbun.Client, error) {
clients := make(map[string]*porkbun.Client)
for key, credential := range credentials {
client, err := getPorkbunClient(credential, key)
if err != nil {
connectionErrorsTotal.Inc()
log.Errorf("Error getting client for credentials '%s': %v", key, err)
continue
}
clients[key] = client
}
return clients, nil
}
func getPorkbunClient(credentials PorkbunCredentials, credentialsName string) (*porkbun.Client, error) {
client := porkbun.New(credentials.PorkbunSecretKey, credentials.PorkbunAPIKey)
ctx := context.Background()
yourIP, err := client.Ping(ctx)
if err != nil {
return nil, err
}
log.Debugf("Connected to porkbun from %s using credentials %s", yourIP, credentialsName)
return client, nil
}
func updateRecord(ctx context.Context, record Record, client *porkbun.Client, ipv4address string, ipv6address string) (resultError error) {
ipv4Record, ipv6Record, err := getRecords(ctx, record.Domain, record.Host, client)
if err != nil {
resultError = errors.Join(resultError, err)
}
if record.IpV4 && ipv4Record != nil && ipv4address != "" && ipv4Record.Content != ipv4address {
err = createOrUpdateRecord(ctx, client, ipv4Record.ID, record.Domain, record.Host, "A", ipv4address)
if err != nil {
resultError = errors.Join(resultError, err)
} else {
updateSuccessTotal.WithLabelValues(record.Host, record.Domain, "A").Inc()
}
} else {
log.WithFields(log.Fields{
"host": record.Host,
"domain": record.Domain,
}).Debug("Ipv4 update not required")
}
if record.IpV6 && ipv6Record != nil && ipv6address != "" && ipv6Record.Content != ipv6address {
err = createOrUpdateRecord(ctx, client, ipv6Record.ID, record.Domain, record.Host, "AAAA", ipv6address)
if err != nil {
resultError = errors.Join(resultError, err)
} else {
updateSuccessTotal.WithLabelValues(record.Host, record.Domain, "AAAA").Inc()
}
} else {
log.WithFields(log.Fields{
"host": record.Host,
"domain": record.Domain,
}).Debug("Ipv6 update not required")
}
return
}