-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmodels.go
180 lines (167 loc) · 5.51 KB
/
models.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
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
package cloudflare
import (
"encoding/json"
"strings"
"time"
"github.com/libdns/libdns"
)
type cfZone struct {
ID string `json:"id"`
Name string `json:"name"`
DevelopmentMode int `json:"development_mode"`
OriginalNameServers []string `json:"original_name_servers"`
OriginalRegistrar string `json:"original_registrar"`
OriginalDnshost string `json:"original_dnshost"`
CreatedOn time.Time `json:"created_on"`
ModifiedOn time.Time `json:"modified_on"`
ActivatedOn time.Time `json:"activated_on"`
Account struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"account"`
Permissions []string `json:"permissions"`
Plan struct {
ID string `json:"id"`
Name string `json:"name"`
Price int `json:"price"`
Currency string `json:"currency"`
Frequency string `json:"frequency"`
LegacyID string `json:"legacy_id"`
IsSubscribed bool `json:"is_subscribed"`
CanSubscribe bool `json:"can_subscribe"`
} `json:"plan"`
PlanPending struct {
ID string `json:"id"`
Name string `json:"name"`
Price int `json:"price"`
Currency string `json:"currency"`
Frequency string `json:"frequency"`
LegacyID string `json:"legacy_id"`
IsSubscribed bool `json:"is_subscribed"`
CanSubscribe bool `json:"can_subscribe"`
} `json:"plan_pending"`
Status string `json:"status"`
Paused bool `json:"paused"`
Type string `json:"type"`
NameServers []string `json:"name_servers"`
}
type cfDNSRecord struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Content string `json:"content,omitempty"`
Proxiable bool `json:"proxiable,omitempty"`
Proxied bool `json:"proxied,omitempty"`
TTL int `json:"ttl,omitempty"` // seconds
Locked bool `json:"locked,omitempty"`
ZoneID string `json:"zone_id,omitempty"`
ZoneName string `json:"zone_name,omitempty"`
CreatedOn time.Time `json:"created_on,omitempty"`
ModifiedOn time.Time `json:"modified_on,omitempty"`
Data struct {
// LOC
LatDegrees int `json:"lat_degrees,omitempty"`
LatMinutes int `json:"lat_minutes,omitempty"`
LatSeconds int `json:"lat_seconds,omitempty"`
LatDirection string `json:"lat_direction,omitempty"`
LongDegrees int `json:"long_degrees,omitempty"`
LongMinutes int `json:"long_minutes,omitempty"`
LongSeconds int `json:"long_seconds,omitempty"`
LongDirection string `json:"long_direction,omitempty"`
Altitude int `json:"altitude,omitempty"`
Size int `json:"size,omitempty"`
PrecisionHorz int `json:"precision_horz,omitempty"`
PrecisionVert int `json:"precision_vert,omitempty"`
// SRV
Service string `json:"service,omitempty"`
Proto string `json:"proto,omitempty"`
Name string `json:"name,omitempty"`
Priority uint `json:"priority,omitempty"`
Weight uint `json:"weight,omitempty"`
Port uint `json:"port,omitempty"`
Target string `json:"target,omitempty"`
// DNSKEY
Flags int `json:"flags,omitempty"`
Protocol int `json:"protocol,omitempty"`
Algorithm int `json:"algorithm,omitempty"`
// DS
KeyTag int `json:"key_tag,omitempty"`
DigestType int `json:"digest_type,omitempty"`
// TLSA
Usage int `json:"usage,omitempty"`
Selector int `json:"selector,omitempty"`
MatchingType int `json:"matching_type,omitempty"`
// URI
Content string `json:"content,omitempty"`
} `json:"data,omitempty"`
Meta *struct {
AutoAdded bool `json:"auto_added,omitempty"`
Source string `json:"source,omitempty"`
} `json:"meta,omitempty"`
}
func (r cfDNSRecord) libdnsRecord(zone string) libdns.Record {
if r.Type == "SRV" {
srv := libdns.SRV{
Service: strings.TrimPrefix(r.Data.Service, "_"),
Proto: strings.TrimPrefix(r.Data.Proto, "_"),
Name: r.Data.Name,
Priority: r.Data.Priority,
Weight: r.Data.Weight,
Port: r.Data.Port,
Target: r.Data.Target,
}
return srv.ToRecord()
}
return libdns.Record{
Type: r.Type,
Name: libdns.RelativeName(r.Name, zone),
Value: r.Content,
TTL: time.Duration(r.TTL) * time.Second,
ID: r.ID,
}
}
func cloudflareRecord(r libdns.Record) (cfDNSRecord, error) {
rec := cfDNSRecord{
ID: r.ID,
Type: r.Type,
TTL: int(r.TTL.Seconds()),
}
if r.Type == "SRV" {
srv, err := r.ToSRV()
if err != nil {
return cfDNSRecord{}, err
}
rec.Data.Service = "_" + srv.Service
rec.Data.Priority = srv.Priority
rec.Data.Weight = srv.Weight
rec.Data.Proto = "_" + srv.Proto
rec.Data.Name = srv.Name
rec.Data.Port = srv.Port
rec.Data.Target = srv.Target
} else {
rec.Name = r.Name
rec.Content = r.Value
}
return rec, nil
}
// All API responses have this structure.
type cfResponse struct {
Result json.RawMessage `json:"result,omitempty"`
Success bool `json:"success"`
Errors []struct {
Code int `json:"code"`
Message string `json:"message"`
ErrorChain []struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"error_chain,omitempty"`
} `json:"errors,omitempty"`
Messages []any `json:"messages,omitempty"`
ResultInfo *cfResultInfo `json:"result_info,omitempty"`
}
type cfResultInfo struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Count int `json:"count"`
TotalCount int `json:"total_count"`
}