diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2022-02-04 14:09:46 +0100 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2022-02-08 11:00:06 +0100 |
| commit | 03af3e14f41555693342d623dc5137691c623c38 (patch) | |
| tree | 4d3799a613f68702ce2d568b1ca9a15122577e5e | |
| parent | 43c7e66870e96d252f9c8cf8256efdcd4f178f90 (diff) | |
| download | mullvadvpn-03af3e14f41555693342d623dc5137691c623c38.tar.xz mullvadvpn-03af3e14f41555693342d623dc5137691c623c38.zip | |
Add DNSBlockingOptions
| -rw-r--r-- | ios/MullvadVPN/TunnelSettings.swift | 71 |
1 files changed, 60 insertions, 11 deletions
diff --git a/ios/MullvadVPN/TunnelSettings.swift b/ios/MullvadVPN/TunnelSettings.swift index c9a96fad80..aa0554d3c2 100644 --- a/ios/MullvadVPN/TunnelSettings.swift +++ b/ios/MullvadVPN/TunnelSettings.swift @@ -7,6 +7,7 @@ // import Foundation +import struct Network.IPv4Address import class WireGuardKit.PublicKey import struct WireGuardKit.IPAddressRange @@ -63,16 +64,49 @@ struct TunnelSettings: Codable, Equatable { var interface = InterfaceSettings() } +/// A struct describing Mullvad DNS blocking options. +struct DNSBlockingOptions: OptionSet, Codable { + typealias RawValue = UInt32 + + let rawValue: RawValue + + static let blockAdvertising = DNSBlockingOptions(rawValue: 1 << 0) + static let blockTracking = DNSBlockingOptions(rawValue: 1 << 1) + static let blockMalware = DNSBlockingOptions(rawValue: 1 << 2) + + var serverAddress: IPv4Address? { + if isEmpty { + return nil + } else { + return IPv4Address("100.64.0.\(rawValue)") + } + } + + init(rawValue: RawValue) { + self.rawValue = rawValue + } + + init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + let rawValue = try container.decode(RawValue.self) + + self.init(rawValue: rawValue) + } + + func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + + try container.encode(rawValue) + } +} + /// A struct that holds DNS settings. struct DNSSettings: Codable, Equatable { /// Maximum number of allowed DNS domains. static let maxAllowedCustomDNSDomains = 3 - /// Block advertising. - var blockAdvertising: Bool = false - - /// Block tracking. - var blockTracking: Bool = false + /// DNS blocking options. + var blockingOptions: DNSBlockingOptions = [] /// Enable custom DNS. var enableCustomDNS: Bool = false @@ -82,11 +116,17 @@ struct DNSSettings: Codable, Equatable { /// Effective state of the custom DNS setting. var effectiveEnableCustomDNS: Bool { - return !blockAdvertising && !blockTracking && enableCustomDNS && !customDNSDomains.isEmpty + return blockingOptions.isEmpty && enableCustomDNS && !customDNSDomains.isEmpty } private enum CodingKeys: String, CodingKey { - case blockAdvertising, blockTracking, enableCustomDNS, customDNSDomains + // Removed in 2022.1 in favor of `blockingOptions` + case blockAdvertising, blockTracking + + // Added in 2022.1 + case blockingOptions + + case enableCustomDNS, customDNSDomains } init() {} @@ -94,8 +134,18 @@ struct DNSSettings: Codable, Equatable { init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) - blockAdvertising = try container.decode(Bool.self, forKey: .blockAdvertising) - blockTracking = try container.decode(Bool.self, forKey: .blockTracking) + // Added in 2022.1 + if let storedBlockingOptions = try container.decodeIfPresent(DNSBlockingOptions.self, forKey: .blockingOptions) { + blockingOptions = storedBlockingOptions + } + + if let storedBlockAdvertising = try container.decodeIfPresent(Bool.self, forKey: .blockAdvertising), storedBlockAdvertising { + blockingOptions.insert(.blockAdvertising) + } + + if let storedBlockTracking = try container.decodeIfPresent(Bool.self, forKey: .blockTracking), storedBlockTracking { + blockingOptions.insert(.blockTracking) + } if let storedEnableCustomDNS = try container.decodeIfPresent(Bool.self, forKey: .enableCustomDNS) { enableCustomDNS = storedEnableCustomDNS @@ -109,8 +159,7 @@ struct DNSSettings: Codable, Equatable { func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(blockAdvertising, forKey: .blockAdvertising) - try container.encode(blockTracking, forKey: .blockTracking) + try container.encode(blockingOptions, forKey: .blockingOptions) try container.encode(enableCustomDNS, forKey: .enableCustomDNS) try container.encode(customDNSDomains, forKey: .customDNSDomains) } |
