|
8 | 8 |
|
9 | 9 | import Foundation
|
10 | 10 |
|
11 |
| -/// Whether UDP-over-TCP obfuscation is enabled |
| 11 | +/// Whether obfuscation is enabled and which method is used |
12 | 12 | ///
|
13 | 13 | /// `.automatic` means an algorithm will decide whether to use it or not.
|
14 | 14 | public enum WireGuardObfuscationState: Codable {
|
15 |
| - case automatic |
| 15 | + @available(*, deprecated, renamed: "udpTcp") |
16 | 16 | case on
|
| 17 | + |
| 18 | + case automatic |
| 19 | + case udpOverTcp |
| 20 | + case shadowsocks |
17 | 21 | case off
|
| 22 | + |
| 23 | + public init(from decoder: Decoder) throws { |
| 24 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 25 | + |
| 26 | + var allKeys = ArraySlice(container.allKeys) |
| 27 | + guard let key = allKeys.popFirst(), allKeys.isEmpty else { |
| 28 | + throw DecodingError.typeMismatch( |
| 29 | + WireGuardObfuscationState.self, |
| 30 | + DecodingError.Context( |
| 31 | + codingPath: container.codingPath, |
| 32 | + debugDescription: "Invalid number of keys found, expected one.", |
| 33 | + underlyingError: nil |
| 34 | + ) |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + switch key { |
| 39 | + case .automatic: |
| 40 | + self = .automatic |
| 41 | + case .on, .udpOverTcp: |
| 42 | + self = .udpOverTcp |
| 43 | + case .shadowsocks: |
| 44 | + self = .shadowsocks |
| 45 | + case .off: |
| 46 | + self = .off |
| 47 | + } |
| 48 | + } |
18 | 49 | }
|
19 | 50 |
|
20 |
| -/// The port to select when using UDP-over-TCP obfuscation |
21 |
| -/// |
22 |
| -/// `.automatic` means an algorith will decide between using `port80` or `port5001` |
23 |
| -public enum WireGuardObfuscationPort: UInt16, Codable, CaseIterable { |
24 |
| - case automatic = 0 |
25 |
| - case port80 = 80 |
26 |
| - case port5001 = 5001 |
| 51 | +public enum WireGuardObfuscationUdpOverTcpPort: Codable, Equatable, CustomStringConvertible { |
| 52 | + case automatic |
| 53 | + case port80 |
| 54 | + case port5001 |
27 | 55 |
|
28 |
| - /// The `UInt16` representation of the port. |
29 |
| - /// - Returns: `0` if `.automatic`, `80` or `5001` otherwise. |
30 |
| - public var portValue: UInt16 { |
31 |
| - self == .automatic ? 0 : rawValue |
| 56 | + public var portValue: UInt16? { |
| 57 | + switch self { |
| 58 | + case .automatic: |
| 59 | + nil |
| 60 | + case .port80: |
| 61 | + 80 |
| 62 | + case .port5001: |
| 63 | + 5001 |
| 64 | + } |
32 | 65 | }
|
33 | 66 |
|
34 |
| - public init?(rawValue: UInt16) { |
35 |
| - switch rawValue { |
36 |
| - case 80: |
37 |
| - self = .port80 |
38 |
| - case 5001: |
39 |
| - self = .port5001 |
40 |
| - default: self = .automatic |
| 67 | + public var description: String { |
| 68 | + switch self { |
| 69 | + case .automatic: |
| 70 | + NSLocalizedString( |
| 71 | + "WIREGUARD_OBFUSCATION_UDP_TCP_PORT_AUTOMATIC", |
| 72 | + tableName: "VPNSettings", |
| 73 | + value: "Automatic", |
| 74 | + comment: "" |
| 75 | + ) |
| 76 | + case .port80: |
| 77 | + "80" |
| 78 | + case .port5001: |
| 79 | + "5001" |
41 | 80 | }
|
42 | 81 | }
|
| 82 | +} |
43 | 83 |
|
44 |
| - public init(from decoder: Decoder) throws { |
45 |
| - let container = try decoder.singleValueContainer() |
46 |
| - let decodedValue = try? container.decode(UInt16.self) |
| 84 | +public enum WireGuardObfuscationShadowsockPort: Codable, Equatable, CustomStringConvertible { |
| 85 | + case automatic |
| 86 | + case custom(UInt16) |
47 | 87 |
|
48 |
| - let port = WireGuardObfuscationPort.allCases.first(where: { $0.rawValue == decodedValue }) |
49 |
| - self = port ?? .automatic |
| 88 | + public var portValue: UInt16? { |
| 89 | + switch self { |
| 90 | + case .automatic: |
| 91 | + nil |
| 92 | + case let .custom(port): |
| 93 | + port |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public var description: String { |
| 98 | + switch self { |
| 99 | + case .automatic: |
| 100 | + NSLocalizedString( |
| 101 | + "WIREGUARD_OBFUSCATION_SHADOWSOCKS_PORT_AUTOMATIC", |
| 102 | + tableName: "VPNSettings", |
| 103 | + value: "Automatic", |
| 104 | + comment: "" |
| 105 | + ) |
| 106 | + case let .custom(port): |
| 107 | + String(port) |
| 108 | + } |
50 | 109 | }
|
51 | 110 | }
|
52 | 111 |
|
| 112 | +// Can't deprecate the whole type since it'll yield a lint warning when decoding |
| 113 | +// port in `WireGuardObfuscationSettings`. |
| 114 | +private enum WireGuardObfuscationPort: UInt16, Codable { |
| 115 | + @available(*, deprecated, message: "Use `udpOverTcpPort` instead") |
| 116 | + case automatic = 0 |
| 117 | + @available(*, deprecated, message: "Use `udpOverTcpPort` instead") |
| 118 | + case port80 = 80 |
| 119 | + @available(*, deprecated, message: "Use `udpOverTcpPort` instead") |
| 120 | + case port5001 = 5001 |
| 121 | +} |
| 122 | + |
53 | 123 | public struct WireGuardObfuscationSettings: Codable, Equatable {
|
| 124 | + @available(*, deprecated, message: "Use `udpOverTcpPort` instead") |
| 125 | + private var port: WireGuardObfuscationPort = .automatic |
| 126 | + |
54 | 127 | public let state: WireGuardObfuscationState
|
55 |
| - public let port: WireGuardObfuscationPort |
| 128 | + public let udpOverTcpPort: WireGuardObfuscationUdpOverTcpPort |
| 129 | + public let shadowsocksPort: WireGuardObfuscationShadowsockPort |
56 | 130 |
|
57 |
| - public init(state: WireGuardObfuscationState = .automatic, port: WireGuardObfuscationPort = .automatic) { |
| 131 | + public init( |
| 132 | + state: WireGuardObfuscationState = .automatic, |
| 133 | + udpOverTcpPort: WireGuardObfuscationUdpOverTcpPort = .automatic, |
| 134 | + shadowsocksPort: WireGuardObfuscationShadowsockPort = .automatic |
| 135 | + ) { |
58 | 136 | self.state = state
|
59 |
| - self.port = port |
| 137 | + self.udpOverTcpPort = udpOverTcpPort |
| 138 | + self.shadowsocksPort = shadowsocksPort |
| 139 | + } |
| 140 | + |
| 141 | + public init(from decoder: Decoder) throws { |
| 142 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 143 | + |
| 144 | + state = try container.decode(WireGuardObfuscationState.self, forKey: .state) |
| 145 | + shadowsocksPort = try container.decodeIfPresent( |
| 146 | + WireGuardObfuscationShadowsockPort.self, |
| 147 | + forKey: .shadowsocksPort |
| 148 | + ) ?? .automatic |
| 149 | + |
| 150 | + if let port = try? container.decodeIfPresent(WireGuardObfuscationUdpOverTcpPort.self, forKey: .udpOverTcpPort) { |
| 151 | + udpOverTcpPort = port |
| 152 | + } else if let port = try? container.decodeIfPresent(WireGuardObfuscationPort.self, forKey: .port) { |
| 153 | + switch port { |
| 154 | + case .automatic: |
| 155 | + udpOverTcpPort = .automatic |
| 156 | + case .port80: |
| 157 | + udpOverTcpPort = .port80 |
| 158 | + case .port5001: |
| 159 | + udpOverTcpPort = .port5001 |
| 160 | + } |
| 161 | + } else { |
| 162 | + udpOverTcpPort = .automatic |
| 163 | + } |
60 | 164 | }
|
61 | 165 | }
|
0 commit comments