summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/AccessMethodRepository/PersistentAccessMethod.swift
blob: b5d5ef2947eae33783a2f5fb5508f5d74d300b12 (plain)
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
//
//  PersistentAccessMethod.swift
//  MullvadVPN
//
//  Created by pronebird on 15/11/2023.
//  Copyright © 2023 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MullvadTypes
import Network

/// Persistent access method model.
struct PersistentAccessMethod: Identifiable, Codable {
    /// The unique identifier used for referencing the access method entry in a persistent store.
    var id: UUID

    /// The user-defined name for access method.
    var name: String

    /// The flag indicating whether configuration is enabled.
    var isEnabled: Bool

    /// Proxy configuration.
    var proxyConfiguration: PersistentProxyConfiguration
}

/// Persistent proxy configuration.
enum PersistentProxyConfiguration: Codable {
    /// Direct communication without proxy.
    case direct

    /// Communication over bridges.
    case bridges

    /// Communication over shadowsocks.
    case shadowsocks(ShadowsocksConfiguration)

    /// Communication over socks5 proxy.
    case socks5(SocksConfiguration)
}

extension PersistentProxyConfiguration {
    /// Socks autentication method.
    enum SocksAuthentication: Codable {
        case noAuthentication
        case usernamePassword(username: String, password: String)
    }

    /// Socks v5 proxy configuration.
    struct SocksConfiguration: Codable {
        /// Proxy server address.
        var server: AnyIPAddress

        /// Proxy server port.
        var port: UInt16

        /// Authentication method.
        var authentication: SocksAuthentication
    }

    /// Shadowsocks configuration.
    struct ShadowsocksConfiguration: Codable {
        /// Server address.
        var server: AnyIPAddress

        /// Server port.
        var port: UInt16

        /// Server password.
        var password: String

        /// Server cipher.
        var cipher: ShadowsocksCipher
    }
}

extension PersistentAccessMethod {
    /// A kind of access method.
    var kind: AccessMethodKind {
        switch proxyConfiguration {
        case .direct:
            .direct
        case .bridges:
            .bridges
        case .shadowsocks:
            .shadowsocks
        case .socks5:
            .socks5
        }
    }
}

/// A kind of API access method.
enum AccessMethodKind: Equatable, Hashable, CaseIterable {
    /// Direct communication.
    case direct

    /// Communication over bridges.
    case bridges

    /// Communication over shadowsocks.
    case shadowsocks

    /// Communication over socks v5 proxy.
    case socks5
}

extension AccessMethodKind {
    /// Returns `true` if the method is permanent and cannot be deleted.
    var isPermanent: Bool {
        switch self {
        case .direct, .bridges:
            true
        case .shadowsocks, .socks5:
            false
        }
    }

    /// Returns all access method kinds that can be added by user.
    static var allUserDefinedKinds: [AccessMethodKind] {
        allCases.filter { !$0.isPermanent }
    }
}