Skip to content

Commit

Permalink
UDS works to start and stop the VPN
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoreymendez committed May 13, 2024
1 parent 6ec61cc commit 55785b0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions DuckDuckGoVPN/TunnelControllerIPCService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ final class TunnelControllerIPCService {
switch request {
case .start:
start { _ in
Task {
await self.tunnelController.start()
}
// no-op
}
case .stop:
stop { _ in
Expand All @@ -84,7 +82,7 @@ final class TunnelControllerIPCService {
}
}
} catch {
fatalError()
fatalError(error.localizedDescription)
}
}

Expand Down
89 changes: 38 additions & 51 deletions LocalPackages/UDSHelper/Sources/UDSHelper/UDSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,66 +65,53 @@ public actor UDSClient<Incoming: Codable, Outgoing: Codable> {
/// 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

Check failure on line 113 in LocalPackages/UDSHelper/Sources/UDSHelper/UDSClient.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Avoid using unneeded break statements (unneeded_break_in_switch)
}
}

private func releaseConnection() {
Expand Down
11 changes: 7 additions & 4 deletions LocalPackages/UDSHelper/Sources/UDSHelper/UDSServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public final class UDSServer<Message: Codable> {
self.log = log
self.receiver = UDSReceiver<Message>(log: log)

do {
try fileManager.removeItem(at: socketFileURL)
} catch {
print(error)
}

os_log("UDSServer - Initialized with path: %{public}@", log: log, type: .info, socketFileURL.path)
}

Expand All @@ -99,13 +105,10 @@ public final class UDSServer<Message: Codable> {

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

Check failure on line 111 in LocalPackages/UDSHelper/Sources/UDSHelper/UDSServer.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Prefer at least one space after slashes for comments (comment_spacing)

listener = try NWListener(using: params)
self.listener = listener
Expand Down

0 comments on commit 55785b0

Please sign in to comment.