-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_dto.go
685 lines (609 loc) · 16.2 KB
/
client_dto.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
package dnssdk
import (
"fmt"
"math"
"net"
"strconv"
"strings"
)
// ListZones dto to read list of zones from API
type ListZones struct {
Zones []Zone `json:"zones"`
TotalAmount int `json:"total_amount"`
Error string `json:"error,omitempty"`
}
// Zone dto to read info from API
type Zone struct {
Name string `json:"name"`
Records []ZoneRecord `json:"records"`
}
// AddZone dto to create new zone
type AddZone struct {
Name string `json:"name"`
}
// CreateResponse dto to create new zone
type CreateResponse struct {
ID uint64 `json:"id,omitempty"`
Error string `json:"error,omitempty"`
}
type RRSetMeta map[string]any
// RRSet dto as part of zone info from API
type RRSet struct {
Type string `json:"type"`
TTL int `json:"ttl"`
Records []ResourceRecord `json:"resource_records"`
Filters []RecordFilter `json:"filters"`
Meta RRSetMeta `json:"meta"` // this one for failover, not Meta property inside Records
// according to https://api.gcore.com/docs/dns#tag/rrsets/operation/CreateRRSet
/*
asn (array of int)
continents (array of string)
countries (array of string)
latlong (array of float64, latitude and longitude)
fallback (bool)
backup (bool)
notes (string)
weight (float)
ip (string)
failover (object, beta feature, might be changed in the future) can have fields 10.1. protocol (string, required, HTTP, TCP, UDP, ICMP) 10.2. port (int, required, 1-65535) 10.3. frequency (int, required, in seconds 10-3600) 10.4. timeout (int, required, in seconds 1-10), 10.5. method (string, only for protocol=HTTP) 10.6. command (string, bytes to be sent only for protocol=TCP/UDP) 10.7. url (string, only for protocol=HTTP) 10.8. tls (bool, only for protocol=HTTP) 10.9. regexp (string regex to match, only for non-ICMP) 10.10. http_status_code (int, only for protocol=HTTP) 10.11. host (string, only for protocol=HTTP)
*/
}
// SetMetaAsn
func (r *RRSet) SetMetaAsn(asns []int) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["asn"] = asns
return r
}
// SetMetaContinents
func (r *RRSet) SetMetaContinents(continents []string) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["continents"] = continents
return r
}
// SetMetaCountries
func (r *RRSet) SetMetaCountries(countries []string) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["countries"] = countries
return r
}
// SetMetaLatLong
func (r *RRSet) SetMetaLatLong(lat, long float64) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["latlong"] = []float64{lat, long}
return r
}
// SetMetaFallback
func (r *RRSet) SetMetaFallback(fallback bool) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["fallback"] = fallback
return r
}
// SetMetaBackup
func (r *RRSet) SetMetaBackup(backup bool) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["backup"] = backup
return r
}
// SetMetaNotes
func (r *RRSet) SetMetaNotes(notes string) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["notes"] = notes
return r
}
// SetMetaWeight
func (r *RRSet) SetMetaWeight(weight float64) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["weight"] = weight
return r
}
// SetMetaIP
func (r *RRSet) SetMetaIP(ip string) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["ip"] = ip
return r
}
// NewRRSetMetaFailoverFromMap for failover
func NewRRSetMetaFailoverFromMap(failover map[string]any) *RRSetMeta {
return &RRSetMeta{
"failover": failover,
}
}
// SetMetaFailover
func (r *RRSet) SetMetaFailover(failover map[string]any) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["failover"] = failover
return r
}
// NewRRSetMetaGeodnsLink
func NewRRSetMetaGeodnsLink(link string) *RRSetMeta {
return &RRSetMeta{
"geodns_link": link,
}
}
// SetMetaGeodnsLink
func (r *RRSet) SetMetaGeodnsLink(link string) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["geodns_link"] = link
return r
}
// FailoverHttpCheck for failover meta property with protocol=HTTP
type FailoverHttpCheck struct {
Protocol string `json:"protocol"` // HTTP
Port uint16 `json:"port"`
Frequency uint16 `json:"frequency"`
Timeout uint16 `json:"timeout"`
// HTTP only
Method string `json:"method,omitempty"` // GET, POST, PUT, DELETE, PATCH
URL string `json:"url,omitempty"` // without / prefix
Host *string `json:"host,omitempty"`
HttpStatusCode *uint16 `json:"http_status_code,omitempty"` // 100-599
Regexp *string `json:"regexp,omitempty"`
TLS bool `json:"tls"`
}
// FailoverTcpUdpCheck for failover meta property with protocol=TCP|UDP
type FailoverTcpUdpCheck struct {
Protocol string `json:"protocol"` // TCP or UDP
Port uint16 `json:"port"`
Frequency uint16 `json:"frequency"`
Timeout uint16 `json:"timeout"`
// TCP/UDP only
Command *string `json:"command"` // bytes to sent
Regexp *string `json:"regexp,omitempty"`
}
// FailoverIcmpCheck for failover meta property with protocol=ICMP
type FailoverIcmpCheck struct {
Protocol string `json:"protocol"` // ICMP
Port uint16 `json:"port"`
Frequency uint16 `json:"frequency"`
Timeout uint16 `json:"timeout"`
}
// NewRRSetMetaMetaFailoverFromHttp for failover
func NewRRSetMetaMetaFailoverFromHttp(failover FailoverHttpCheck) *RRSetMeta {
return &RRSetMeta{
"failover": failover,
}
}
// SetMetaFailoverHttp for failover
func (r *RRSet) SetMetaFailoverHttp(check FailoverHttpCheck) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["failover"] = check
return r
}
// NewRRSetMetaMetaFailoverFromTcpUdp for TCP/DUP failover
func NewRRSetMetaMetaFailoverFromTcpUdp(failover FailoverTcpUdpCheck) *RRSetMeta {
return &RRSetMeta{
"failover": failover,
}
}
// SetMetaFailoverTcpUdp for TCP/DUP failover
func (r *RRSet) SetMetaFailoverTcpUdp(check FailoverTcpUdpCheck) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["failover"] = check
return r
}
// NewRRSetMetaMetaFailoverFromIcmp for ICMP failover
func NewRRSetMetaMetaFailoverFromIcmp(failover FailoverIcmpCheck) *RRSetMeta {
return &RRSetMeta{
"failover": failover,
}
}
// SetMetaFailoverIcmp for ICMP failover
func (r *RRSet) SetMetaFailoverIcmp(check FailoverIcmpCheck) *RRSet {
if r.Meta == nil {
r.Meta = RRSetMeta{}
}
r.Meta["failover"] = check
return r
}
type RRSets struct {
RRSets []RRSet `json:"rrsets"`
}
// ResourceRecord dto describe records in RRSet
type ResourceRecord struct {
Content []any `json:"content"`
Meta map[string]any `json:"meta"`
Enabled bool `json:"enabled"`
}
// ContentToString as short value
// example from:
// tls-ech.dev. 899 IN HTTPS 1 . ech=AEn+DQBFKwAgACABWIHUGj4u+PIggYXcR5JF0gYk3dCRioBW8uJq9H4mKAAIAAEAAQABAANAEnB1YmxpYy50bHMtZWNoLmRldgAA
// clickhouse.com. 899 IN HTTPS 1 . alpn="h3,h3-29,h2" ipv4hint=172.66.40.249,172.66.43.7 ipv6hint=2606:4700:3108::ac42:28f9,2606:4700:3108::ac42:2b07
func (r ResourceRecord) ContentToString() string {
parts := make([]string, len(r.Content))
for i := range r.Content {
if arr, ok := r.Content[i].([]any); ok {
if len(arr) > 0 {
key := fmt.Sprint(arr[0])
if key == "alpn" { // only alpn quoted
parts[i] = fmt.Sprintf(`%s="%s"`, key, valuesJoin(arr[1:]))
} else if len(arr) == 1 {
parts[i] = key
} else {
parts[i] = fmt.Sprintf(`%s=%s`, key, valuesJoin(arr[1:]))
}
}
} else {
parts[i] = fmt.Sprint(r.Content[i])
}
}
return strings.Join(parts, " ")
}
// join https kv-params
func valuesJoin(vs []any) (res string) {
for _, v := range vs {
res += "," + fmt.Sprint(v)
}
res = strings.Trim(res, ",")
return res
}
// RecordFilter describe Filters in RRSet
type RecordFilter struct {
Limit uint `json:"limit"`
Type string `json:"type"`
Strict bool `json:"strict"`
}
// NewGeoDNSFilter for RRSet
func NewGeoDNSFilter(limit uint, strict bool) RecordFilter {
return RecordFilter{
Limit: limit,
Type: "geodns",
Strict: strict,
}
}
// NewGeoDistanceFilter for RRSet
func NewGeoDistanceFilter(limit uint, strict bool) RecordFilter {
return RecordFilter{
Limit: limit,
Type: "geodistance",
Strict: strict,
}
}
// NewDefaultFilter for RRSet
func NewDefaultFilter(limit uint, strict bool) RecordFilter {
return RecordFilter{
Limit: limit,
Type: "default",
Strict: strict,
}
}
// NewFirstNFilter for RRSet
func NewFirstNFilter(limit uint, strict bool) RecordFilter {
return RecordFilter{
Limit: limit,
Type: "first_n",
Strict: strict,
}
}
// RecordType contract
type RecordType interface {
ToContent() []any
}
// RecordTypeMX as type of record
type RecordTypeMX string
// ToContent convertor
func (mx RecordTypeMX) ToContent() []any {
parts := strings.Split(string(mx), " ")
// nolint: gomnd
if len(parts) != 2 {
return nil
}
content := make([]any, len(parts))
// nolint: gomnd
content[1] = parts[1]
// nolint: gomnd
content[0], _ = strconv.ParseInt(parts[0], 10, 64)
return content
}
// RecordTypeCAA as type of record
type RecordTypeCAA string
// ToContent convertor
func (caa RecordTypeCAA) ToContent() []any {
parts := strings.Split(string(caa), " ")
// nolint: gomnd
if len(parts) < 3 {
return nil
}
content := make([]any, len(parts))
// nolint: gomnd
content[1] = parts[1]
// nolint: gomnd
content[2] = strings.Join(parts[2:], " ")
// nolint: gomnd
content[0], _ = strconv.ParseInt(parts[0], 10, 64)
return content
}
// RecordTypeHTTPS_SCVB as type of record
type RecordTypeHTTPS_SCVB string
// function to parse uint16
func tryParseUint16(x string) any {
v, err := strconv.ParseFloat(x, 64)
if err != nil {
return x
}
if v > math.MaxUint16 || v < 0 {
return v
}
u := uint16(v)
if float64(u) != v { // contains fractional
return v
}
return u
}
// ToContent convertor
func (r RecordTypeHTTPS_SCVB) ToContent() (res []any) {
arr := strings.Split(string(r), ` `)
if len(arr) == 0 {
return []any{}
}
if len(arr) >= 1 { // try parse priority
res = append(res, tryParseUint16(arr[0]))
}
if len(arr) >= 2 { // try parse port
res = append(res, arr[1])
}
for i := 2; i < len(arr); i++ { // try parse params
kvParam := arr[i]
idx := strings.Index(kvParam, `=`) + 1
if idx <= 0 {
idx = len(kvParam) + 1
}
param := []any{}
k := kvParam[:idx-1]
param = append(param, k)
if idx <= len(kvParam) {
vStr := kvParam[idx:]
vStr = strings.Trim(vStr, `"`) // remove quote
vArr := strings.Split(vStr, `,`)
for _, v := range vArr {
if k == `port` { // try parse to number
param = append(param, tryParseUint16(v))
continue
}
param = append(param, v)
}
}
res = append(res, param)
}
return res
}
// RecordTypeSRV as type of record
type RecordTypeSRV string
// ToContent convertor
func (srv RecordTypeSRV) ToContent() []any {
parts := strings.Split(string(srv), " ")
// nolint: gomnd
if len(parts) != 4 {
return nil
}
content := make([]any, len(parts))
// nolint: gomnd
content[0], _ = strconv.ParseInt(parts[0], 10, 64)
// nolint: gomnd
content[1], _ = strconv.ParseInt(parts[1], 10, 64)
// nolint: gomnd
content[2], _ = strconv.ParseInt(parts[2], 10, 64)
// nolint: gomnd
content[3] = parts[3]
return content
}
// RecordTypeAny as type of record
type RecordTypeAny string
// ToContent convertor
func (x RecordTypeAny) ToContent() []any {
return []any{string(x)}
}
// ToRecordType builder
func ToRecordType(rType, content string) RecordType {
switch strings.ToLower(rType) {
case "mx":
return RecordTypeMX(content)
case "caa":
return RecordTypeCAA(content)
case "srv":
return RecordTypeSRV(content)
case "https", "scvb":
return RecordTypeHTTPS_SCVB(content)
}
return RecordTypeAny(content)
}
// ContentFromValue convertor from flat value to valid for api
func ContentFromValue(recordType, content string) []any {
rt := ToRecordType(recordType, content)
if rt == nil {
return nil
}
return rt.ToContent()
}
// ResourceMeta for ResourceRecord
type ResourceMeta struct {
name string
value any
validErr error
}
// Valid error
func (rm ResourceMeta) Valid() error {
return rm.validErr
}
// NewResourceMetaIP for ip meta
func NewResourceMetaIP(ips ...string) ResourceMeta {
for _, v := range ips {
_, _, err := net.ParseCIDR(v)
if err != nil {
if ip := net.ParseIP(v); ip == nil {
// nolint: goerr113
return ResourceMeta{validErr: fmt.Errorf("wrong ip: %v", err)}
}
}
}
return ResourceMeta{
name: "ip",
value: ips,
}
}
// NewResourceMetaAsn for asn meta
func NewResourceMetaAsn(asn ...uint64) ResourceMeta {
return ResourceMeta{
name: "asn",
value: asn,
}
}
// NewResourceMetaLatLong for lat long meta
func NewResourceMetaLatLong(latlong string) ResourceMeta {
latlong = strings.TrimLeft(latlong, "(")
latlong = strings.TrimLeft(latlong, "[")
latlong = strings.TrimLeft(latlong, "{")
latlong = strings.TrimRight(latlong, ")")
latlong = strings.TrimRight(latlong, "]")
latlong = strings.TrimRight(latlong, "}")
parts := strings.Split(strings.ReplaceAll(latlong, " ", ""), ",")
// nolint: gomnd
if len(parts) != 2 {
// nolint: goerr113
return ResourceMeta{validErr: fmt.Errorf("latlong invalid format")}
}
lat, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
// nolint: goerr113
return ResourceMeta{validErr: fmt.Errorf("lat is invalid: %w", err)}
}
long, err := strconv.ParseFloat(parts[1], 64)
// nolint: goerr113
if err != nil {
return ResourceMeta{validErr: fmt.Errorf("long is invalid: %w", err)}
}
return ResourceMeta{
name: "latlong",
value: []float64{lat, long},
}
}
// NewResourceMetaNotes for notes meta
func NewResourceMetaNotes(notes ...string) ResourceMeta {
return ResourceMeta{
name: "notes",
value: notes,
}
}
// NewResourceMetaCountries for Countries meta
func NewResourceMetaCountries(countries ...string) ResourceMeta {
return ResourceMeta{
name: "countries",
value: countries,
}
}
// NewResourceMetaContinents for continents meta
func NewResourceMetaContinents(continents ...string) ResourceMeta {
return ResourceMeta{
name: "continents",
value: continents,
}
}
// NewResourceMetaDefault for default meta
func NewResourceMetaDefault() ResourceMeta {
return ResourceMeta{
name: "default",
value: true,
}
}
// NewResourceMetaBackup for backup meta
func NewResourceMetaBackup() ResourceMeta {
return ResourceMeta{
name: "backup",
value: true,
}
}
// NewResourceMetaFallback for fallback meta
func NewResourceMetaFallback() ResourceMeta {
return ResourceMeta{
name: "fallback",
value: true,
}
}
// NewResourceMetaWeight for fallback meta
func NewResourceMetaWeight(weight int) ResourceMeta {
return ResourceMeta{
name: "weight",
value: weight,
}
}
// SetContent to ResourceRecord
func (r *ResourceRecord) SetContent(recordType, val string) *ResourceRecord {
r.Content = ContentFromValue(recordType, val)
return r
}
// AddMeta to ResourceRecord
func (r *ResourceRecord) AddMeta(meta ResourceMeta) *ResourceRecord {
if meta.validErr != nil {
return r
}
if meta.name == "" || meta.value == "" {
return r
}
if r.Meta == nil {
r.Meta = map[string]any{}
}
r.Meta[meta.name] = meta.value
return r
}
// AddFilter to RRSet
func (rr *RRSet) AddFilter(filters ...RecordFilter) *RRSet {
if rr.Filters == nil {
rr.Filters = make([]RecordFilter, 0)
}
rr.Filters = append(rr.Filters, filters...)
return rr
}
// ZoneRecord dto describe records in Zone
type ZoneRecord struct {
Name string `json:"name"`
Type string `json:"type"`
TTL uint `json:"ttl"`
ShortAnswers []string `json:"short_answers"`
}
// DNSSecDS dto describe DS records that returned from API on DNSSec requests
type DNSSecDS struct {
Algorithm string `json:"algorithm"`
Digest string `json:"digest"`
DigestAlgorithm string `json:"digest_algorithm"`
DigestType string `json:"digest_type"`
Ds string `json:"ds"`
Flags int `json:"flags"`
KeyTag int `json:"key_tag"`
KeyType string `json:"key_type"`
PublicKey string `json:"public_key"`
Uuid string `json:"uuid"`
}
// APIError customization for API calls
type APIError struct {
StatusCode int `json:"-"`
Message string `json:"error,omitempty"`
}
// Error implementation
func (a APIError) Error() string {
return fmt.Sprintf("%d: %s", a.StatusCode, a.Message)
}