Use pure Swift to easily and securely communicate with XPC services and Mach services. A client-server model enables you
to use your own Codable
conforming types to send requests
to routes you define and receive responses.
SecureXPC uses Swift concurrency on macOS 10.15 and later allowing clients to make non-blocking asynchronous requests to servers. A closure-based API is also available providing compatibility back to OS X 10.10.
This package can be used to communicate with any type of XPC service or Mach service, with customized support for:
SMJobBless
SMLoginItemSetEnabled
SMAppService.daemon(plistName:)
SMAppService.agent(plistName:)
It's built with security in mind, minimizing the opportunities for exploits. Security checks are performed against the actual calling process instead of relying on PIDs which are known to be insecure.
The envisioned pattern when using this package is to define routes in a shared file, create a server in one program (such as a helper tool) and register these routes, then from another program (such as an app) create a client and send requests to these routes.
In a file shared by the client and server define one or more routes:
let route = XPCRoute.named("bedazzle")
.withMessageType(String.self)
.withReplyType(Bool.self)
In one program retrieve a server, register those routes, and then start the server:
...
let server = <# server retrieval here #>
server.registerRoute(route, handler: bedazzle)
server.startAndBlock()
}
private func bedazzle(message: String) throws -> Bool {
<# implementation here #>
}
On macOS 10.15 and later async
functions and closures can also be registered as the handler for a route.
There are multiple types of servers which can be retrieved:
XPCServer.forThisXPCService()
XPCServer.forMachService()
SMAppService
, SMJobBless
helper tools, and SMLoginItemSetEnabled
login itemsXPCServer.forMachService(withCriteria:)
XPCServer.makeAnonymous()
XPCServer.makeAnonymous(withClientRequirements:)
In another program retrieve a client, then send a request to a registered route:
let client = <# client retrieval here #>
let reply = try await client.sendMessage("Get Schwifty", to: route)
Closure-based variants are available for macOS 10.14 and earlier:
let client = <# client retrieval here #>
client.sendMessage("Get Schwifty", to: route, withResponse: { result in
switch result {
case .success(let reply):
<# use the reply #>
case .failure(let error):
<# handle the error #>
}
})
There are three types of clients which can be retrieved:
XPCClient.forXPCService(named:)
XPCServer.forThisXPCService()
XPCClient.forMachService(named:withServerRequirement:)
XPCServer.forMachService()
or
XPCServer.forMachService(withCriteria:)
XPCClient.forEndpoint(_:withServerRequirement:)
XPCServer.makeAnonymous()
or
XPCServer.makeAnonymous(withClientRequirements:)
endpoint
propertySee the FAQ for answers to questions you may have or didn't even realize you wanted answered including topics such as using live file handles, sharing memory between processes, and working within sandbox restrictions.
link |
Stars: 53 |
Last commit: 3 weeks ago |
This is a big release! Continued thanks to @amomchilov for his contributions and code reviews.
SMAppService
's daemons & agents.
XPCServer.forMachService()
and an auto-configured server will be returned.SharedTrivial
demonstrates this functionality.SharedSemaphore
, SharedMemory
, and SharedRawMemory
should enable you to build your own custom multi-process data structures.XPCClient
now provides the unforgeable identity of the server it is connected to via its serverIdentity
async property (or the equivalent callback function).XPCClient
now automatically verifies the identity of the server in common cases and this can be customized with XPCClient.ServerRequirement
.XPCServer
instances now always have an endpoint
property (previously only those also conforming to XPCNonBlockingServer
had this property).IOSurface
instances over XPC connections using the IOSurfaceForXPC
property wrapper.Data
instances and arrays of trivial types can optionally be more efficiently sent across XPC connections by using the DataOptimizedForXPC
and ArrayOptimizedForXPC
property wrappers.XPCServer
retrieval has been simplified. There are now just three main entry points:
XPCServer.forThisXPCService()
XPCServer.forMachService()
XPCServer.makeAnonymous()
XPCServer
retrieval is now more customizable.
XPCServer.MachServiceCriteria
then call XPCServer.forMachService(withCriteria:)
XPCServer.makeAnonymous(withClientRequirement:)
XPCServer
's client requirements no longer require using SecRequirement
directly (although that's still supported).
XPCServer.ClientRequirement
and declarative in nature: XPCServer.makeAnonymous(withClientRequirement: try .sameTeamIdentifier || try .teamIdentifier("Q55ZG849VX"))
XPCClient
's factory methods have been tweaked to now optionally take a XPCClient.ServerRequirement
instance.XPCFileDescriptorContainer
has been replaced by the FileHandleForXPC
and FileDescriptorForXPC
property wrappers.XPCRequestContext
has been renamed to XPCServer.ClientIdentity
.XPCError
cases have been removed and a few have been added.Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics