diff --git a/DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionIPCTunnelController.swift b/DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionIPCTunnelController.swift index 95b6398669..acc2b43407 100644 --- a/DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionIPCTunnelController.swift +++ b/DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionIPCTunnelController.swift @@ -106,7 +106,7 @@ extension NetworkProtectionIPCTunnelController: TunnelController { do { try await enableLoginItems() - try? await udsClient.send(.start) + try await udsClient.send(.start) /* ipcClient.start { [pixelKit] error in if let error { diff --git a/DuckDuckGoVPN/TunnelControllerIPCService.swift b/DuckDuckGoVPN/TunnelControllerIPCService.swift index 011ce69b0d..a3f25e4917 100644 --- a/DuckDuckGoVPN/TunnelControllerIPCService.swift +++ b/DuckDuckGoVPN/TunnelControllerIPCService.swift @@ -73,9 +73,7 @@ final class TunnelControllerIPCService { switch request { case .start: start { _ in - Task { - await self.tunnelController.start() - } + // no-op } case .stop: stop { _ in @@ -84,7 +82,7 @@ final class TunnelControllerIPCService { } } } catch { - fatalError() + fatalError(error.localizedDescription) } } diff --git a/LocalPackages/UDSHelper/Sources/UDSHelper/UDSClient.swift b/LocalPackages/UDSHelper/Sources/UDSHelper/UDSClient.swift index 07c984c4ee..07ee13e396 100644 --- a/LocalPackages/UDSHelper/Sources/UDSHelper/UDSClient.swift +++ b/LocalPackages/UDSHelper/Sources/UDSHelper/UDSClient.swift @@ -65,66 +65,53 @@ public actor UDSClient { /// Establishes a new connection /// private func connect() async throws -> NWConnection { - /*let shortSocketURL: URL - - do { - shortSocketURL = try urlShortener.shorten(socketFileURL, symlinkName: "appgroup") - } catch { - os_log("UDSClient - Error creating short path for socket: %{public}@", - log: log, - type: .error, - String(describing: error)) - throw error - }*/ - - //os_log("UDSClient - Connecting to shortened path: %{public}@", log: log, type: .info, shortSocketURL.path) - let endpoint = NWEndpoint.unix(path: socketFileURL.path) let parameters = NWParameters.tcp let connection = NWConnection(to: endpoint, using: parameters) - internalConnection = connection - return try await withTaskCancellationHandler { - try await withCheckedThrowingContinuation { continuation in - connection.stateUpdateHandler = { [weak self] state in - guard let self else { return } - - Task { - switch state { - case .cancelled: - os_log("UDSClient - Connection cancelled", log: self.log, type: .info) - - await self.releaseConnection() - continuation.resume(throwing: ConnectionError.cancelled) - case .failed(let error): - os_log("UDSClient - Connection failed with error: %{public}@", log: self.log, type: .error, String(describing: error)) - - await self.releaseConnection() - continuation.resume(throwing: ConnectionError.failure(error)) - case .ready: - os_log("UDSClient - Connection ready", log: self.log, type: .info) - - await self.retainConnection(connection) - continuation.resume(returning: connection) - case .waiting(let error): - os_log("UDSClient - Waiting to connect... %{public}@", log: self.log, type: .info, String(describing: error)) - default: - os_log("UDSClient - Unexpected state", log: self.log, type: .info) - - break - } - } - } + connection.stateUpdateHandler = { state in + Task { + try await self.statusUpdateHandler(state) + } + } - connection.start(queue: queue) + internalConnection = connection + connection.start(queue: queue) + + while connection.state != .ready { + switch connection.state { + case .cancelled: + throw ConnectionError.cancelled + case .failed(let error): + throw ConnectionError.failure(error) + default: + try await Task.sleep(nanoseconds: 200 * MSEC_PER_SEC) } - } onCancel: { - connection.cancel() } + + return connection } - private func retainConnection(_ connection: NWConnection) { - internalConnection = connection + private func statusUpdateHandler(_ state: NWConnection.State) async throws { + switch state { + case .cancelled: + os_log("UDSClient - Connection cancelled", log: self.log, type: .info) + + self.releaseConnection() + throw ConnectionError.cancelled + case .failed(let error): + os_log("UDSClient - Connection failed with error: %{public}@", log: self.log, type: .error, String(describing: error)) + + self.releaseConnection() + throw ConnectionError.failure(error) + case .ready: + os_log("UDSClient - Connection ready", log: self.log, type: .info) + case .waiting(let error): + os_log("UDSClient - Waiting to connect... %{public}@", log: self.log, type: .info, String(describing: error)) + default: + os_log("UDSClient - Unexpected state", log: self.log, type: .info) + break + } } private func releaseConnection() { diff --git a/LocalPackages/UDSHelper/Sources/UDSHelper/UDSServer.swift b/LocalPackages/UDSHelper/Sources/UDSHelper/UDSServer.swift index de46bdea13..d73a76a1e0 100644 --- a/LocalPackages/UDSHelper/Sources/UDSHelper/UDSServer.swift +++ b/LocalPackages/UDSHelper/Sources/UDSHelper/UDSServer.swift @@ -91,6 +91,12 @@ public final class UDSServer { self.log = log self.receiver = UDSReceiver(log: log) + do { + try fileManager.removeItem(at: socketFileURL) + } catch { + print(error) + } + os_log("UDSServer - Initialized with path: %{public}@", log: log, type: .info, socketFileURL.path) } @@ -99,13 +105,10 @@ public final class UDSServer { do { let params = NWParameters() - let shortSocketURL = try fileManager.shortenSocketURL(socketFileURL: socketFileURL, symlinkName: "appgroup") - - //os_log("UDSServer - Listening on shortened path: %{public}@", log: log, type: .info, shortSocketURL.path) - params.defaultProtocolStack.transportProtocol = NWProtocolTCP.Options() params.requiredLocalEndpoint = NWEndpoint.unix(path: socketFileURL.path) params.allowLocalEndpointReuse = true + //params.acceptLocalOnly = true listener = try NWListener(using: params) self.listener = listener