summaryrefslogtreecommitdiff
path: root/Rune/API/Models.swift
blob: 161e8df4e4098058c46987cfb5f69631e77bae31 (plain) (blame)
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
import Foundation

struct EmptyResult: Codable {}

struct WalletBalance: Codable, Equatable {
    let balance: Int
}

struct DomainListResponse: Decodable {
    let domains: [Domain]
}

struct RecordListResponse: Codable {
    let records: [DNSRecord]
}

struct TokenListResponse: Codable {
    let tokens: [APIToken]
}

struct ForwardListResponse: Codable {
    let forwards: [EmailForward]
}

struct Domain: Decodable, Identifiable, Hashable {
    var id: String { name }

    let name: String
    let status: String?
    let expiry: String?
    let autorenew: Bool?
    let mailforwarding: Bool?
    let dnssec: Bool?
    let lock: Bool?
    let nameservers: [String]?

    enum CodingKeys: String, CodingKey {
        case name
        case status
        case expiry
        case autorenew
        case mailforwarding
        case dnssec
        case dnssecEnabled = "dnssec_enabled"
        case lock
        case locked
        case nameservers
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decode(String.self, forKey: .name)
        status = try container.decodeIfPresent(String.self, forKey: .status)
        expiry = try container.decodeIfPresent(String.self, forKey: .expiry)
        autorenew = try container.decodeIfPresent(Bool.self, forKey: .autorenew)
        mailforwarding = try container.decodeIfPresent(Bool.self, forKey: .mailforwarding)
        dnssec = try container.decodeIfPresent(Bool.self, forKey: .dnssec) ??
            container.decodeIfPresent(Bool.self, forKey: .dnssecEnabled)
        lock = try container.decodeIfPresent(Bool.self, forKey: .lock) ??
            container.decodeIfPresent(Bool.self, forKey: .locked)
        nameservers = try container.decodeIfPresent([String].self, forKey: .nameservers)
    }
}

struct DNSRecord: Codable, Identifiable, Hashable {
    let id: String
    let domain: String
    let type: String
    let name: String
    let content: String?
    let ttl: Int?
    let prio: Int?
    let weight: Int?
    let port: Int?
    let target: String?
    let sshAlgorithm: Int?
    let sshType: Int?

    enum CodingKeys: String, CodingKey {
        case id
        case domain
        case type
        case name
        case content
        case ttl
        case prio
        case weight
        case port
        case target
        case sshAlgorithm = "ssh_algorithm"
        case sshType = "ssh_type"
    }

    init(
        id: String,
        domain: String,
        type: String,
        name: String,
        content: String?,
        ttl: Int?,
        prio: Int?,
        weight: Int?,
        port: Int?,
        target: String?,
        sshAlgorithm: Int?,
        sshType: Int?
    ) {
        self.id = id
        self.domain = domain
        self.type = type
        self.name = name
        self.content = content
        self.ttl = ttl
        self.prio = prio
        self.weight = weight
        self.port = port
        self.target = target
        self.sshAlgorithm = sshAlgorithm
        self.sshType = sshType
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        id = try container.decodeLossyString(forKey: .id)
        domain = try container.decodeIfPresent(String.self, forKey: .domain) ?? ""
        type = try container.decode(String.self, forKey: .type)
        name = try container.decode(String.self, forKey: .name)
        content = try container.decodeIfPresent(String.self, forKey: .content)
        ttl = try container.decodeLossyIntIfPresent(forKey: .ttl)
        prio = try container.decodeLossyIntIfPresent(forKey: .prio)
        weight = try container.decodeLossyIntIfPresent(forKey: .weight)
        port = try container.decodeLossyIntIfPresent(forKey: .port)
        target = try container.decodeIfPresent(String.self, forKey: .target)
        sshAlgorithm = try container.decodeLossyIntIfPresent(forKey: .sshAlgorithm)
        sshType = try container.decodeLossyIntIfPresent(forKey: .sshType)
    }

    func withDomain(_ domain: String) -> DNSRecord {
        DNSRecord(
            id: id,
            domain: domain,
            type: type,
            name: name,
            content: content,
            ttl: ttl,
            prio: prio,
            weight: weight,
            port: port,
            target: target,
            sshAlgorithm: sshAlgorithm,
            sshType: sshType
        )
    }
}

struct APIToken: Codable, Identifiable, Hashable {
    var id: String { key }

    let key: String
    let comment: String?
    let from: [String]?
    let allowedDomains: [String]?
    let allowedServers: [String]?
    let allowedMethods: [String]?
    let allowedPrefixes: [String]?
    let allowedTypes: [String]?

    enum CodingKeys: String, CodingKey {
        case key
        case comment
        case from
        case allowedDomains = "allowed_domains"
        case allowedServers = "allowed_servers"
        case allowedMethods = "allowed_methods"
        case allowedPrefixes = "allowed_prefixes"
        case allowedTypes = "allowed_types"
    }
}

struct EmailForward: Codable, Hashable, Identifiable {
    let domain: String
    let from: String
    let to: String

    var id: String {
        "\(domain)|\(from)|\(to)"
    }
}

enum DNSRecordType: String, CaseIterable, Identifiable, Codable {
    case a = "A"
    case aaaa = "AAAA"
    case aname = "ANAME"
    case caa = "CAA"
    case cname = "CNAME"
    case ds = "DS"
    case dynamic = "Dynamic"
    case https = "HTTPS"
    case mx = "MX"
    case naptr = "NAPTR"
    case ns = "NS"
    case ptr = "PTR"
    case srv = "SRV"
    case sshfp = "SSHFP"
    case svcb = "SVCB"
    case tlsa = "TLSA"
    case txt = "TXT"

    var id: String { rawValue }

    var usesContent: Bool {
        switch self {
        case .dynamic, .https, .svcb:
            return false
        default:
            return true
        }
    }

    var usesTTL: Bool {
        usesContent
    }

    var usesPriority: Bool {
        switch self {
        case .https, .mx, .srv, .svcb:
            return true
        default:
            return false
        }
    }

    var usesWeight: Bool {
        self == .srv
    }

    var usesPort: Bool {
        self == .srv
    }

    var usesTarget: Bool {
        self == .https || self == .svcb
    }

    var usesSSHFields: Bool {
        self == .sshfp
    }
}

enum TTLPreset: Int, CaseIterable, Identifiable {
    case zero = 0
    case oneMinute = 60
    case fiveMinutes = 300
    case fifteenMinutes = 900
    case oneHour = 3600
    case threeHours = 10800
    case sixHours = 21600
    case oneDay = 86400

    var id: Int { rawValue }

    var label: String {
        switch self {
        case .zero:
            return "0s"
        case .oneMinute:
            return "1m"
        case .fiveMinutes:
            return "5m"
        case .fifteenMinutes:
            return "15m"
        case .oneHour:
            return "1h"
        case .threeHours:
            return "3h"
        case .sixHours:
            return "6h"
        case .oneDay:
            return "1d"
        }
    }

    static func option(for seconds: Int) -> TTLOption {
        if let preset = TTLPreset(rawValue: seconds) {
            return TTLOption(seconds: preset.rawValue, label: preset.label)
        }

        return TTLOption(seconds: seconds, label: "\(seconds)s", isCustom: true)
    }
}

struct TTLOption: Hashable, Identifiable {
    let seconds: Int
    let label: String
    let isCustom: Bool

    init(seconds: Int, label: String, isCustom: Bool = false) {
        self.seconds = seconds
        self.label = label
        self.isCustom = isCustom
    }

    var id: Int { seconds }
}

struct DNSRecordDraft: Equatable {
    var type: DNSRecordType = .a
    var name = ""
    var content = ""
    var ttlSeconds = TTLPreset.oneHour.rawValue
    var prio = ""
    var weight = ""
    var port = ""
    var target = ""
    var sshAlgorithm = ""
    var sshType = ""

    init() {}

    init(record: DNSRecord) {
        type = DNSRecordType(rawValue: record.type) ?? .a
        name = record.name
        content = record.content ?? ""
        ttlSeconds = record.ttl ?? TTLPreset.oneHour.rawValue
        prio = record.prio.map(String.init) ?? ""
        weight = record.weight.map(String.init) ?? ""
        port = record.port.map(String.init) ?? ""
        target = record.target ?? ""
        sshAlgorithm = record.sshAlgorithm.map(String.init) ?? ""
        sshType = record.sshType.map(String.init) ?? ""
    }

    mutating func resetTypeSpecificFields() {
        content = ""
        ttlSeconds = TTLPreset.oneHour.rawValue
        prio = ""
        weight = ""
        port = ""
        target = ""
        sshAlgorithm = ""
        sshType = ""
    }

    var trimmedName: String {
        name.trimmingCharacters(in: .whitespacesAndNewlines)
    }

    var canSubmit: Bool {
        !trimmedName.isEmpty
    }

    var ttlOptions: [TTLOption] {
        let presetOptions = TTLPreset.allCases.map { TTLOption(seconds: $0.rawValue, label: $0.label) }

        guard type.usesTTL, TTLPreset(rawValue: ttlSeconds) == nil else {
            return presetOptions
        }

        return presetOptions + [TTLPreset.option(for: ttlSeconds)]
    }

    func params(domain: String, id: String? = nil) -> [String: Any] {
        var params: [String: Any] = [
            "domain": domain,
            "type": type.rawValue,
            "name": trimmedName
        ]

        if let id {
            params["id"] = id
        }

        if type.usesContent {
            params["content"] = content.trimmingCharacters(in: .whitespacesAndNewlines)
        }

        if type.usesTTL {
            params["ttl"] = ttlSeconds
        }

        if type.usesPriority, let value = Int(prio.trimmingCharacters(in: .whitespacesAndNewlines)) {
            params["prio"] = value
        }

        if type.usesWeight, let value = Int(weight.trimmingCharacters(in: .whitespacesAndNewlines)) {
            params["weight"] = value
        }

        if type.usesPort, let value = Int(port.trimmingCharacters(in: .whitespacesAndNewlines)) {
            params["port"] = value
        }

        if type.usesTarget {
            params["target"] = target.trimmingCharacters(in: .whitespacesAndNewlines)
        }

        if type.usesSSHFields {
            if let value = Int(sshAlgorithm.trimmingCharacters(in: .whitespacesAndNewlines)) {
                params["ssh_algorithm"] = value
            }
            if let value = Int(sshType.trimmingCharacters(in: .whitespacesAndNewlines)) {
                params["ssh_type"] = value
            }
        }

        return params
    }
}

struct DomainUpdateRequest {
    var autorenew: Bool?
    var mailforwarding: Bool?
    var dnssec: Bool?
    var lock: Bool?
    var nameservers: [String]?

    var hasChanges: Bool {
        autorenew != nil ||
        mailforwarding != nil ||
        dnssec != nil ||
        lock != nil ||
        nameservers != nil
    }

    var params: [String: Any] {
        var params: [String: Any] = [:]

        if let autorenew {
            params["autorenew"] = autorenew
        }

        if let mailforwarding {
            params["mailforwarding"] = mailforwarding
        }

        if let dnssec {
            params["dnssec"] = dnssec
        }

        if let lock {
            params["lock"] = lock
        }

        if let nameservers {
            params["nameservers"] = nameservers
        }

        return params
    }

    func isSatisfied(by domain: Domain) -> Bool {
        if let autorenew, domain.autorenew != autorenew {
            return false
        }

        if let mailforwarding, domain.mailforwarding != mailforwarding {
            return false
        }

        if let dnssec, domain.dnssec != dnssec {
            return false
        }

        if let lock, domain.lock != lock {
            return false
        }

        if let nameservers {
            let normalizedResponse = domain.nameservers ?? []
            if normalizedResponse != nameservers {
                return false
            }
        }

        return true
    }
}

struct TokenCreateRequest {
    var comment: String
    var from: [String]
    var allowedMethods: [String]
    var allowedDomains: [String]

    var params: [String: Any] {
        var params: [String: Any] = [:]

        let trimmedComment = comment.trimmingCharacters(in: .whitespacesAndNewlines)
        if !trimmedComment.isEmpty {
            params["comment"] = trimmedComment
        }

        if !from.isEmpty {
            params["from"] = from
        }

        if !allowedMethods.isEmpty {
            params["allowed_methods"] = allowedMethods
        }

        if !allowedDomains.isEmpty {
            params["allowed_domains"] = allowedDomains
        }

        return params
    }
}

extension String {
    func formattedExpiry() -> String {
        let parser = ISO8601DateFormatter()
        guard let date = parser.date(from: self) else { return self }
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .none
        return formatter.string(from: date)
    }
}

private extension KeyedDecodingContainer where K == DNSRecord.CodingKeys {
    func decodeLossyString(forKey key: K) throws -> String {
        if let stringValue = try decodeIfPresent(String.self, forKey: key) {
            return stringValue
        }

        if let intValue = try decodeIfPresent(Int.self, forKey: key) {
            return String(intValue)
        }

        throw DecodingError.keyNotFound(
            key,
            DecodingError.Context(codingPath: codingPath, debugDescription: "Missing required value for \(key.stringValue).")
        )
    }

    func decodeLossyIntIfPresent(forKey key: K) throws -> Int? {
        if let intValue = try decodeIfPresent(Int.self, forKey: key) {
            return intValue
        }

        if let stringValue = try decodeIfPresent(String.self, forKey: key) {
            return Int(stringValue)
        }

        return nil
    }
}