Skip to content

Commit

Permalink
UDS connect works, yay
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoreymendez committed May 10, 2024
1 parent 49d54fc commit 6ec61cc
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 22 deletions.
18 changes: 18 additions & 0 deletions Configuration/AppStore.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,21 @@ DBP_APP_GROUP[config=CI][sdk=*] = $(DBP_BASE_APP_GROUP).debug
DBP_APP_GROUP[config=Review][sdk=*] = $(DBP_BASE_APP_GROUP).review
DBP_APP_GROUP[config=Debug][sdk=*] = $(DBP_BASE_APP_GROUP).debug
DBP_APP_GROUP[config=Release][sdk=*] = $(DBP_BASE_APP_GROUP)

// IPC

// IMPORTANT: The reason this app group was created is because IPC through
// Unix Domain Sockets requires the socket file path to be no longer than
// 108 characters. Sandboxing requirements force us to place said socket
// within an app group container.
//
// Name coding:
// - ipc.a = ipc app store release
// - ipc.a.d = ipc app store debug
// - ipc.a.r = ipc app store review
//
IPC_APP_GROUP_BASE = $(DEVELOPMENT_TEAM).com.ddg.ipc.a
IPC_APP_GROUP[config=CI][sdk=*] = $(IPC_APP_GROUP_BASE).d
IPC_APP_GROUP[config=Review][sdk=*] = $(IPC_APP_GROUP_BASE).r
IPC_APP_GROUP[config=Debug][sdk=*] = $(IPC_APP_GROUP_BASE).d
IPC_APP_GROUP[config=Release][sdk=*] = $(IPC_APP_GROUP_BASE)
28 changes: 18 additions & 10 deletions Configuration/DeveloperID.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ NETP_APP_GROUP[config=Review][sdk=*] = $(NETP_BASE_APP_GROUP).review
NETP_APP_GROUP[config=Debug][sdk=*] = $(NETP_BASE_APP_GROUP).debug
NETP_APP_GROUP[config=Release][sdk=*] = $(NETP_BASE_APP_GROUP)

// It's important to try and keep this app group short since some IPC
// mechanisms require really short paths. For instance Unix Domain Sockets
// support paths of up to 108 bytes in length
// (di = developer ID)
BASE_IPC_APP_GROUP = $(DEVELOPMENT_TEAM).com.duckduckgo.di.ipc
IPC_APP_GROUP[config=CI][sdk=macos*] = $(BASE_IPC_APP_GROUP).debug
IPC_APP_GROUP[config=Review][sdk=macos*] = $(BASE_IPC_APP_GROUP).review
IPC_APP_GROUP[config=Debug][sdk=macos*] = $(BASE_IPC_APP_GROUP).debug
IPC_APP_GROUP[config=Release][sdk=macos*] = $(BASE_IPC_APP_GROUP)

SUBSCRIPTION_BASE_APP_GROUP = $(DEVELOPMENT_TEAM).$(MAIN_BUNDLE_IDENTIFIER_PREFIX).subscription
SUBSCRIPTION_APP_GROUP[config=CI][sdk=*] = $(SUBSCRIPTION_BASE_APP_GROUP).debug
SUBSCRIPTION_APP_GROUP[config=Review][sdk=*] = $(SUBSCRIPTION_BASE_APP_GROUP).review
Expand Down Expand Up @@ -110,3 +100,21 @@ DBP_APP_GROUP[config=CI][sdk=*] = $(DBP_BASE_APP_GROUP).debug
DBP_APP_GROUP[config=Review][sdk=*] = $(DBP_BASE_APP_GROUP).review
DBP_APP_GROUP[config=Debug][sdk=*] = $(DBP_BASE_APP_GROUP).debug
DBP_APP_GROUP[config=Release][sdk=*] = $(DBP_BASE_APP_GROUP)

// IPC

// IMPORTANT: The reason this app group was created is because IPC through
// Unix Domain Sockets requires the socket file path to be no longer than
// 108 characters. Sandboxing requirements force us to place said socket
// within an app group container.
//
// Name coding:
// - ipc.d = ipc developer id release
// - ipc.d.d = ipc developer id debug
// - ipc.d.r = ipc developer id review
//
IPC_APP_GROUP_BASE = $(DEVELOPMENT_TEAM).com.ddg.ipc
IPC_APP_GROUP[config=CI][sdk=*] = $(IPC_APP_GROUP_BASE).d
IPC_APP_GROUP[config=Review][sdk=*] = $(IPC_APP_GROUP_BASE).r
IPC_APP_GROUP[config=Debug][sdk=*] = $(IPC_APP_GROUP_BASE).d
IPC_APP_GROUP[config=Release][sdk=*] = $(IPC_APP_GROUP_BASE)
3 changes: 3 additions & 0 deletions DuckDuckGo/Common/Extensions/BundleExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,16 @@ extension Bundle {

enum BundleGroup {
case netP
case ipc
case dbp
case subs

var appGroupKey: String {
switch self {
case .dbp:
return "DBP_APP_GROUP"
case .ipc:
return "IPC_APP_GROUP"
case .netP:
return "NETP_APP_GROUP"
case .subs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ final class NetworkProtectionIPCTunnelController {
self.loginItemsManager = loginItemsManager
self.ipcClient = ipcClient

let socketFileURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: Bundle.main.appGroup(bundle: .netP))!.appendingPathComponent("vpn.sock")
let socketFileURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: Bundle.main.appGroup(bundle: .ipc))!.appendingPathComponent("vpn.ipc")

self.udsClient = UDSClient(socketFileURL: socketFileURL, log: .networkProtectionIPCLog)
self.pixelKit = pixelKit
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGoVPN/TunnelControllerIPCService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final class TunnelControllerIPCService {
self.statusReporter = statusReporter
self.defaults = defaults

let socketFileURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: Bundle.main.appGroup(bundle: .netP))!.appendingPathComponent("vpn.sock")
let socketFileURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: Bundle.main.appGroup(bundle: .ipc))!.appendingPathComponent("vpn.ipc")

udsServer = UDSServer<VPNIPCServerCommand>(socketFileURL: socketFileURL, log: .networkProtectionIPCLog)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ extension FileManager {

// Just make extra sure there's no pre-existing file at the shortened file path
//try? removeItem(at: shortenedFileURL)
try createSymbolicLink(at: shortenedFileURL, withDestinationURL: fileURL)
do {
try createSymbolicLink(at: shortenedFileURL, withDestinationURL: fileURL)
} catch {
print(error.localizedDescription)
}

return shortenedFileURL
}
Expand All @@ -74,7 +78,7 @@ extension FileManager {
let directoryURL = socketFileURL.deletingLastPathComponent()
let shortenedDirectoryURL = try shortenURL(for: directoryURL, symlinkName: symlinkName)
let shortSocketURL = shortenedDirectoryURL.appendingPathComponent(socketFileURL.lastPathComponent)
/*

do {
try removeItem(at: shortSocketURL)
} catch let error as CocoaError {
Expand All @@ -85,7 +89,7 @@ extension FileManager {
default:
throw error
}
}*/
}

return shortSocketURL
}
Expand Down
10 changes: 5 additions & 5 deletions LocalPackages/UDSHelper/Sources/UDSHelper/UDSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public actor UDSClient<Incoming: Codable, Outgoing: Codable> {
/// Establishes a new connection
///
private func connect() async throws -> NWConnection {
let shortSocketURL: URL
/*let shortSocketURL: URL

do {
shortSocketURL = try urlShortener.shorten(socketFileURL, symlinkName: "appgroup")
Expand All @@ -75,12 +75,12 @@ public actor UDSClient<Incoming: Codable, Outgoing: Codable> {
type: .error,
String(describing: error))
throw error
}
}*/

os_log("UDSClient - Connecting to shortened path: %{public}@", log: log, type: .info, shortSocketURL.path)
//os_log("UDSClient - Connecting to shortened path: %{public}@", log: log, type: .info, shortSocketURL.path)

let endpoint = NWEndpoint.unix(path: shortSocketURL.path)
let parameters = NWParameters()
let endpoint = NWEndpoint.unix(path: socketFileURL.path)
let parameters = NWParameters.tcp
let connection = NWConnection(to: endpoint, using: parameters)
internalConnection = connection

Expand Down
4 changes: 2 additions & 2 deletions LocalPackages/UDSHelper/Sources/UDSHelper/UDSServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public final class UDSServer<Message: Codable> {
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)
//os_log("UDSServer - Listening on shortened path: %{public}@", log: log, type: .info, shortSocketURL.path)

params.defaultProtocolStack.transportProtocol = NWProtocolTCP.Options()
params.requiredLocalEndpoint = NWEndpoint.unix(path: shortSocketURL.path)
params.requiredLocalEndpoint = NWEndpoint.unix(path: socketFileURL.path)
params.allowLocalEndpointReuse = true

listener = try NWListener(using: params)
Expand Down

0 comments on commit 6ec61cc

Please sign in to comment.