Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving users access methods configuration into ios 420 #5609

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
//

import Foundation
import MullvadTypes
import Network

public struct ShadowsocksConfiguration: Codable {
public let bridgeAddress: IPv4Address
public let bridgePort: UInt16
public let address: AnyIPAddress
public let port: UInt16
public let password: String
public let cipher: String

public init(bridgeAddress: IPv4Address, bridgePort: UInt16, password: String, cipher: String) {
self.bridgeAddress = bridgeAddress
self.bridgePort = bridgePort
public init(address: AnyIPAddress, port: UInt16, password: String, cipher: String) {
self.address = address
self.port = port
self.password = password
self.cipher = cipher
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public final class ShadowsocksTransport: RESTTransport {
shadowsocksProxy = ShadowsocksProxy(
forwardAddress: apiAddress.ip,
forwardPort: apiAddress.port,
bridgeAddress: configuration.bridgeAddress,
bridgePort: configuration.bridgePort,
bridgeAddress: configuration.address,
bridgePort: configuration.port,
password: configuration.password,
cipher: configuration.cipher
)
Expand Down
10 changes: 0 additions & 10 deletions ios/MullvadREST/Transport/Socks5/AnyIPEndpoint+Socks5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,4 @@ extension AnyIPEndpoint {
.ipv6(endpoint)
}
}

/// Convert `AnyIPEndpoint` to `NWEndpoint`.
var nwEndpoint: NWEndpoint {
switch self {
case let .ipv4(endpoint):
.hostPort(host: .ipv4(endpoint.ip), port: NWEndpoint.Port(integerLiteral: endpoint.port))
case let .ipv6(endpoint):
.hostPort(host: .ipv6(endpoint.ip), port: NWEndpoint.Port(integerLiteral: endpoint.port))
}
}
}
19 changes: 15 additions & 4 deletions ios/MullvadREST/Transport/Socks5/Socks5Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@

import Foundation
import MullvadTypes
import Network

/// Socks5 configuration.
/// - See: ``URLSessionSocks5Transport``
public struct Socks5Configuration {
/// The socks proxy endpoint.
public var proxyEndpoint: AnyIPEndpoint
public let address: AnyIPAddress
public let port: UInt16

public init(proxyEndpoint: AnyIPEndpoint) {
self.proxyEndpoint = proxyEndpoint
public init(address: AnyIPAddress, port: UInt16) {
self.address = address
self.port = port
}

var nwEndpoint: NWEndpoint {
switch self.address {
case let .ipv4(endpoint):
.hostPort(host: .ipv4(endpoint), port: NWEndpoint.Port(integerLiteral: port))
case let .ipv6(endpoint):
.hostPort(host: .ipv6(endpoint), port: NWEndpoint.Port(integerLiteral: port))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class URLSessionSocks5Transport: RESTTransport {
let apiAddress = addressCache.getCurrentEndpoint()

socksProxy = Socks5ForwardingProxy(
socksProxyEndpoint: configuration.proxyEndpoint.nwEndpoint,
socksProxyEndpoint: configuration.nwEndpoint,
remoteServerEndpoint: apiAddress.socksEndpoint
)

Expand Down
9 changes: 3 additions & 6 deletions ios/MullvadREST/Transport/TransportProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ public final class TransportProvider: RESTTransportProvider {
private func socks5() -> RESTTransport? {
return URLSessionSocks5Transport(
urlSession: urlSessionTransport.urlSession,
configuration: Socks5Configuration(proxyEndpoint: AnyIPEndpoint.ipv4(IPv4Endpoint(
ip: .loopback,
port: 8889
))),
configuration: Socks5Configuration(address: .ipv4(.loopback), port: 8889),
addressCache: addressCache
)
}
Expand Down Expand Up @@ -113,8 +110,8 @@ public final class TransportProvider: RESTTransportProvider {
guard let bridgeAddress = closestRelay?.ipv4AddrIn, let bridgeConfiguration else { throw POSIXError(.ENOENT) }

let newConfiguration = ShadowsocksConfiguration(
bridgeAddress: bridgeAddress,
bridgePort: bridgeConfiguration.port,
address: .ipv4(bridgeAddress),
port: bridgeConfiguration.port,
password: bridgeConfiguration.password,
cipher: bridgeConfiguration.cipher
)
Expand Down
57 changes: 57 additions & 0 deletions ios/MullvadSettings/AccessMethodKind.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// AccessMethodKind.swift
// MullvadVPN
//
// Created by pronebird on 02/11/2023.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//

import Foundation

/// A kind of API access method.
public 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
}

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

extension PersistentAccessMethod {
/// A kind of access method.
public var kind: AccessMethodKind {
switch proxyConfiguration {
case .direct:
.direct
case .bridges:
.bridges
case .shadowsocks:
.shadowsocks
case .socks5:
.socks5
}
}
}
118 changes: 118 additions & 0 deletions ios/MullvadSettings/AccessMethodRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// AccessMethodRepository.swift
// MullvadVPN
//
// Created by Jon Petersson on 12/12/2023.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//

import Combine
import Foundation

public class AccessMethodRepository: AccessMethodRepositoryProtocol {
let passthroughSubject: CurrentValueSubject<[PersistentAccessMethod], Never> = CurrentValueSubject([
PersistentAccessMethod(
id: UUID(uuidString: "C9DB7457-2A55-42C3-A926-C07F82131994")!,
name: "",
isEnabled: true,
proxyConfiguration: .direct
),
PersistentAccessMethod(
id: UUID(uuidString: "8586E75A-CA7B-4432-B70D-EE65F3F95084")!,
name: "",
isEnabled: true,
proxyConfiguration: .bridges
),
])

public var publisher: AnyPublisher<[PersistentAccessMethod], Never> {
passthroughSubject.eraseToAnyPublisher()
}

public var accessMethods: [PersistentAccessMethod] {
passthroughSubject.value
}

public static let shared = AccessMethodRepository()

private init() {
add(passthroughSubject.value)
}

public func add(_ method: PersistentAccessMethod) {
add([method])
}

public func add(_ methods: [PersistentAccessMethod]) {
var storedMethods = fetchAll()

methods.forEach { method in
guard !storedMethods.contains(where: { $0.id == method.id }) else { return }
storedMethods.append(method)
}

do {
try writeApiAccessMethods(storedMethods)
} catch {
print("Could not add access method(s): \(methods) \nError: \(error)")
}
}

public func update(_ method: PersistentAccessMethod) {
var methods = fetchAll()

guard let index = methods.firstIndex(where: { $0.id == method.id }) else { return }
methods[index] = method

do {
try writeApiAccessMethods(methods)
} catch {
print("Could not update access method: \(method) \nError: \(error)")
}
}

public func delete(id: UUID) {
var methods = fetchAll()
guard let index = methods.firstIndex(where: { $0.id == id }) else { return }

// Prevent removing methods that have static UUIDs and are always present.
let method = methods[index]
if !method.kind.isPermanent {
methods.remove(at: index)
}

do {
try writeApiAccessMethods(methods)
} catch {
print("Could not delete access method with id: \(id) \nError: \(error)")
}
}

public func fetch(by id: UUID) -> PersistentAccessMethod? {
fetchAll().first { $0.id == id }
}

public func fetchAll() -> [PersistentAccessMethod] {
(try? readApiAccessMethods()) ?? []
}

private func readApiAccessMethods() throws -> [PersistentAccessMethod] {
let parser = makeParser()
let data = try SettingsManager.store.read(key: .apiAccessMethods)

return try parser.parseUnversionedPayload(as: [PersistentAccessMethod].self, from: data)
}

private func writeApiAccessMethods(_ accessMethods: [PersistentAccessMethod]) throws {
let parser = makeParser()
let data = try parser.produceUnversionedPayload(accessMethods)

try SettingsManager.store.write(data, for: .apiAccessMethods)

passthroughSubject.send(accessMethods)
}

private func makeParser() -> SettingsParser {
SettingsParser(decoder: JSONDecoder(), encoder: JSONEncoder())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import Combine
import Foundation

protocol AccessMethodRepositoryProtocol {
public protocol AccessMethodRepositoryDataSource {
/// Publisher that propagates a snapshot of persistent store upon modifications.
var publisher: PassthroughSubject<[PersistentAccessMethod], Never> { get }
var publisher: AnyPublisher<[PersistentAccessMethod], Never> { get }
}

public protocol AccessMethodRepositoryProtocol: AccessMethodRepositoryDataSource {
/// Add new access method.
/// - Parameter method: persistent access method model.
func add(_ method: PersistentAccessMethod)
Expand Down
4 changes: 0 additions & 4 deletions ios/MullvadSettings/MigrationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Foundation
import MullvadLogging
import MullvadREST
import MullvadTypes

public enum SettingsMigrationResult {
Expand Down Expand Up @@ -37,7 +36,6 @@ public struct MigrationManager {
/// - migrationCompleted: Completion handler called with a migration result.
public func migrateSettings(
store: SettingsStore,
proxyFactory: REST.ProxyFactory,
migrationCompleted: @escaping (SettingsMigrationResult) -> Void
) {
let resetStoreHandler = { (result: SettingsMigrationResult) in
Expand All @@ -51,7 +49,6 @@ public struct MigrationManager {
do {
try upgradeSettingsToLatestVersion(
store: store,
proxyFactory: proxyFactory,
migrationCompleted: migrationCompleted
)
} catch .itemNotFound as KeychainError {
Expand All @@ -63,7 +60,6 @@ public struct MigrationManager {

private func upgradeSettingsToLatestVersion(
store: SettingsStore,
proxyFactory: REST.ProxyFactory,
migrationCompleted: @escaping (SettingsMigrationResult) -> Void
) throws {
let parser = SettingsParser(decoder: JSONDecoder(), encoder: JSONEncoder())
Expand Down
Loading
Loading