summaryrefslogtreecommitdiff
path: root/Rune/API/Models.swift
blob: f0209b416e8fdee9b26ef2f1912caa7477ddd144 (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
import Foundation

struct EmptyResult: Codable {}

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

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

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

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

struct Domain: Codable, 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]?
}

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"
    }
}

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
    }
}

struct DNSRecordDraft: Equatable {
    var type: DNSRecordType = .a
    var name = ""
    var content = ""
    var ttl = ""
    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 ?? ""
        ttl = record.ttl.map(String.init) ?? ""
        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 = ""
        ttl = ""
        prio = ""
        weight = ""
        port = ""
        target = ""
        sshAlgorithm = ""
        sshType = ""
    }

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

    var canSubmit: Bool {
        !trimmedName.isEmpty
    }

    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, let value = Int(ttl.trimmingCharacters(in: .whitespacesAndNewlines)) {
            params["ttl"] = value
        }

        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 params: [String: Any] {
        [
            "autorenew": autorenew,
            "mailforwarding": mailforwarding,
            "dnssec": dnssec,
            "lock": lock,
            "nameservers": nameservers
        ]
    }
}

struct TokenCreateRequest {
    var comment: String
    var from: [String]
    var allowedMethods: [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
        }

        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
    }
}