-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
197 lines (159 loc) · 5.1 KB
/
client.go
File metadata and controls
197 lines (159 loc) · 5.1 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package omglol
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/libdns/libdns"
)
const apiBase = "https://api.omg.lol"
// listRecords fetches all DNS records for the configured address.
func (p *Provider) listRecords(ctx context.Context) ([]omglolRecord, error) {
url := fmt.Sprintf("%s/address/%s/dns", apiBase, p.Address)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+p.APIKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("omg.lol API: HTTP %d: %s", resp.StatusCode, string(body))
}
var result omglolListResponse
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("omg.lol API: failed to parse response: %w", err)
}
if !result.Request.Success {
return nil, fmt.Errorf("omg.lol API: request unsuccessful")
}
return result.Response.DNS, nil
}
// createRecord creates a new DNS record and returns the created record (with ID).
func (p *Provider) createRecord(ctx context.Context, payload omglolRecordPayload) (omglolRecord, error) {
url := fmt.Sprintf("%s/address/%s/dns", apiBase, p.Address)
data, err := json.Marshal(payload)
if err != nil {
return omglolRecord{}, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(data))
if err != nil {
return omglolRecord{}, err
}
req.Header.Set("Authorization", "Bearer "+p.APIKey)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return omglolRecord{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return omglolRecord{}, err
}
if resp.StatusCode != http.StatusOK {
return omglolRecord{}, fmt.Errorf("omg.lol API: HTTP %d: %s", resp.StatusCode, string(body))
}
var result omglolCreateResponse
if err := json.Unmarshal(body, &result); err != nil {
return omglolRecord{}, fmt.Errorf("omg.lol API: failed to parse create response: %w", err)
}
if !result.Request.Success {
return omglolRecord{}, fmt.Errorf("omg.lol API: create unsuccessful")
}
return result.Response.ResponseReceived.Data, nil
}
// updateRecord updates an existing DNS record by its ID.
func (p *Provider) updateRecord(ctx context.Context, id string, payload omglolRecordPayload) error {
url := fmt.Sprintf("%s/address/%s/dns/%s", apiBase, p.Address, id)
data, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, bytes.NewReader(data))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+p.APIKey)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("omg.lol API: HTTP %d: %s", resp.StatusCode, string(body))
}
return nil
}
// deleteRecord deletes a DNS record by its ID.
func (p *Provider) deleteRecord(ctx context.Context, id string) error {
url := fmt.Sprintf("%s/address/%s/dns/%s", apiBase, p.Address, id)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+p.APIKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("omg.lol API: HTTP %d: %s", resp.StatusCode, string(body))
}
return nil
}
// libdnsRecordToPayload converts a libdns.Record into the payload used by the
// omg.lol create/update endpoints. The name is converted from the libdns
// relative/absolute representation into what omg.lol expects: just the label
// prefix (e.g. "_acme-challenge" for "_acme-challenge.g.omg.lol.").
func libdnsRecordToPayload(record libdns.Record, zone string) omglolRecordPayload {
rr := record.RR()
ttl := int(rr.TTL / time.Second)
if ttl <= 0 {
ttl = 300
}
// Extract the address label from the zone (e.g. "g" from "g.omg.lol.").
parts := strings.SplitN(strings.TrimSuffix(zone, "."), ".", 2)
address := parts[0]
// libdns names are relative to the zone or absolute (FQDN).
// omg.lol expects just the sub-label portion relative to the address label.
// For zone "g.omg.lol." the address is "g".
// "_acme-challenge" relative to "g.omg.lol." → omg.lol name = "_acme-challenge"
// "@" or "" (apex) → omg.lol name = address itself (e.g. "g")
relativeName := libdns.RelativeName(rr.Name, zone)
var omglolName string
switch relativeName {
case "@", "":
omglolName = address
default:
// relativeName is e.g. "_acme-challenge" or "_acme-challenge.sub"
omglolName = relativeName
}
return omglolRecordPayload{
Type: rr.Type,
Name: omglolName,
Data: rr.Data,
TTL: ttl,
}
}