Compare commits

..

1 Commits

114 changed files with 599 additions and 3945 deletions
+2 -2
View File
@@ -8,10 +8,10 @@
## AI tool usage
How much of the code in this PR was AI-assisted? (check one)
How much of this PR was AI-assisted? (check one)
- [ ] **0** - No AI was used to write code
- [ ] **1** - I was assisted by AI in writing code. I reviewed the finished result.
- [ ] **1** - I was assisted by AI. I reviewed the finished result.
- [ ] **2** - I set the AI going and left it to it; nobody has read the result - no review, or AI review only
<!-- If an AI agent is filling this in: declare the level honestly, and open as a draft if it is #2. Do not lower the declared level to get the PR reviewed. -->
-1
View File
@@ -13,4 +13,3 @@ DerivedData/
/openapi
/.tmp
/xtool
+7 -9
View File
@@ -42,19 +42,17 @@ opt_in_rules:
analyzer_rules:
- unused_import
custom_rules:
prefer_dependency_print:
name: "Dependency Output"
included: '/Sources/'
excluded: '/(Tests|Examples)/'
regex: '(?<![\w.])(?:Swift\s*\.\s*)?print\s*\('
match_kinds:
- identifier
message: "Use d.out.print instead of print or Swift.print."
severity: error
spaces_not_tabs:
name: "Spaces not Tabs"
regex: ^\t
message: "Use four spaces, not tabs"
point_zero:
name: "Point Zero"
regex: '(?<!iOS\s)(?<!macOS\s)(?<!\.)\b[\d_]+\.0\b'
match_kinds:
- number
- attribute.builtin
message: "Don't add a .0 to the end of floating point literals"
force_try: warning
force_cast: warning
function_body_length: 100
+1 -4
View File
@@ -13,7 +13,4 @@ The `xtool` CLI supports macOS, Linux, WSL. The lower-level XKit (provisioning a
## Conventions
- Use modern Swift 6. Strict concurrency is enabled.
- Use swift-dependencies (`@Dependency(\.foo)` / `d.foo`) for Dependency Injection. Passing dependencies around as arguments is an anti-pattern.
- Commits should be in the format `subsystem: imperative description` e.g. `auth: support reading password from stdin`.
- Don't use `Task.detached { ... }`. If you need to escape isolation, you want `Task { @concurrent in ... }` instead.
- Uphold API stability guarantees for consumers who use xtool via the command line interface (e.g. don't remove subcommands or config fields). However, the Swift interface (including `public` types) when consuming xtool itself as a Swift Package is _NOT_ API stable; it's okay to break Swift API.
- Use swift-dependencies for Dependency Injection. Passing dependencies around as arguments is an anti-pattern.
+1 -1
View File
@@ -1,7 +1,7 @@
# Note: We use 22.04 since AppImage recommends building on the
# oldest configuration that you support
ARG SWIFT_VERSION=6.4.2
ARG SWIFT_VERSION=6.4.1
FROM swift:${SWIFT_VERSION}-jammy AS build-base
+1 -1
View File
@@ -11,7 +11,7 @@ Some environment variables that help with debugging. Use these as e.g. `XTL_FOO=
### Disk
- `XTL_DEBUG_TMP`: Set to `1` to log details when xtool creates/destroys temporary directories.
- `XTL_TMPDIR`: Control the location where xtool stores temporary directories. We also respect `XDG_CACHE_HOME` on Linux.
- `XTL_TMPDIR`: Control the location where xtool stores temporary directories. We also respect `TMPDIR` and (on Linux) `XDG_CACHE_HOME`.
### Networking
+1 -10
View File
@@ -1,5 +1,5 @@
{
"originHash" : "87792a9d5953dfe62ed2edf7c4f142774813bd3ea31882cbf6659b62fb2827ab",
"originHash" : "4a80ae2e9e4c4e1e4e29c32e93dc624202a42a942e44d358035684ec5e0746f7",
"pins" : [
{
"identity" : "combine-schedulers",
@@ -10,15 +10,6 @@
"version" : "1.2.2"
}
},
{
"identity" : "opencombine",
"kind" : "remoteSourceControl",
"location" : "https://github.com/OpenCombine/OpenCombine.git",
"state" : {
"revision" : "8576f0d579b27020beccbccc3ea6844f3ddfc2c2",
"version" : "0.14.0"
}
},
{
"identity" : "swift-case-paths",
"kind" : "remoteSourceControl",
-3
View File
@@ -17,9 +17,6 @@ let package = Package(
dependencies: [
.package(path: "../ExampleSupport"),
.package(url: "https://github.com/pointfreeco/swift-composable-architecture.git", from: "1.0.0"),
// we don't use this but need the explicit dependency to keep the lockfile stable.
// TODO: remove once https://github.com/pointfreeco/combine-schedulers/issues/120 is fixed
.package(url: "https://github.com/OpenCombine/OpenCombine.git", from: "0.14.0"),
],
targets: [
.target(
+10 -19
View File
@@ -1,5 +1,5 @@
{
"originHash" : "2ccd077fc130f0f1b1279d87aa61d1fc243964d7ad49489a12021577c54df9de",
"originHash" : "df62267a8ffd060087e903a9d112d24562a959d43a0819a6511367da533c5594",
"pins" : [
{
"identity" : "aexml",
@@ -172,15 +172,6 @@
"version" : "5.0.3"
}
},
{
"identity" : "swift-custom-dump",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-custom-dump",
"state" : {
"revision" : "4fb9e7cbb4b8f5a7354005db39447f570d6b3e19",
"version" : "1.7.3"
}
},
{
"identity" : "swift-dependencies",
"kind" : "remoteSourceControl",
@@ -343,15 +334,6 @@
"version" : "1.4.0"
}
},
{
"identity" : "swift-rcodesign",
"kind" : "remoteSourceControl",
"location" : "https://github.com/xtool-org/swift-rcodesign",
"state" : {
"revision" : "08650ab9d3bf95a0c585581b8bd63eb7b8315264",
"version" : "0.2.0"
}
},
{
"identity" : "swift-service-context",
"kind" : "remoteSourceControl",
@@ -459,6 +441,15 @@
"revision" : "a27b21e0c81c5bf42049b897a62aaf387e80f279",
"version" : "6.2.2"
}
},
{
"identity" : "zsign",
"kind" : "remoteSourceControl",
"location" : "https://github.com/xtool-org/zsign",
"state" : {
"revision" : "f6158be9e62a5ce2a53d72b1a6d880880da956c8",
"version" : "1.9.0"
}
}
],
"version" : 3
+7 -55
View File
@@ -9,14 +9,12 @@ let xtoolVersion: String? = {
let gitCommit = info.currentCommit
let gitTag = info.currentTag
let gitRef = gitTag ?? "commit \(gitCommit)"
return gitRef
return "\(gitRef)\(info.hasUncommittedChanges ? " (dirty)" : "")"
} else {
return nil
}
}()
let hasUncommittedChanges = Context.gitInformation?.hasUncommittedChanges ?? false
let cSettings: [CSetting] = [
.define("_GNU_SOURCE", .when(platforms: [.linux])),
]
@@ -52,7 +50,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/xtool-org/xtool-core", .upToNextMinor(from: "1.5.1")),
.package(url: "https://github.com/xtool-org/SwiftyMobileDevice", .upToNextMinor(from: "1.7.0")),
.package(url: "https://github.com/xtool-org/swift-rcodesign", .upToNextMinor(from: "0.2.0")),
.package(url: "https://github.com/xtool-org/zsign", .upToNextMinor(from: "1.9.0")),
.package(url: "https://github.com/apple/swift-system", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-http-types", from: "1.3.1"),
@@ -62,6 +60,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-nio", from: "2.77.0"),
.package(url: "https://github.com/apple/swift-collections", from: "1.7.1"),
.package(url: "https://github.com/apple/xcode-project-format.git", .upToNextMinor(from: "0.1.0")),
@@ -74,7 +73,6 @@ let package = Package(
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.3.0"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.6.2"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.3"),
// we don't use this but need the explicit dependency to keep the lockfile stable.
// TODO: remove once https://github.com/pointfreeco/combine-schedulers/issues/120 is fixed
.package(url: "https://github.com/OpenCombine/OpenCombine.git", from: "0.14.0"),
@@ -92,9 +90,7 @@ let package = Package(
dependencies: [
.product(name: "OpenSSL", package: "xtool-core")
],
cSettings: cSettings + [
.define("XTOOL_HAS_UNCOMMITTED_CHANGES", to: hasUncommittedChanges ? "1" : "0"),
] + (
cSettings: cSettings + (
xtoolVersion
.map { [.define("XTOOL_VERSION", to: "\"\($0)\"")] }
?? []
@@ -112,9 +108,6 @@ let package = Package(
.target(
name: "XUtils",
dependencies: [
"CXKit",
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "SystemPackage", package: "swift-system"),
.product(
name: "Subprocess",
@@ -124,25 +117,6 @@ let package = Package(
],
swiftSettings: swiftSettings,
),
.systemLibrary(
name: "CLZMA",
pkgConfig: "liblzma",
providers: [
.apt(["liblzma-dev"]),
]
),
// Apple Archive (.aar) and Apple Encrypted Archive (.aea) support
.target(
name: "XAppleArchive",
dependencies: [
"XUtils",
.target(name: "CLZMA", condition: .when(platforms: [.linux])),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "X509", package: "swift-certificates"),
],
swiftSettings: swiftSettings,
),
.target(
name: "XKit",
dependencies: [
@@ -152,7 +126,8 @@ let package = Package(
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "SwiftyMobileDevice", package: "SwiftyMobileDevice"),
.product(name: "RCodesign", package: "swift-rcodesign"),
.product(name: "Zupersign", package: "zsign"),
.product(name: "SignerSupport", package: "xtool-core"),
.product(name: "ProtoCodable", package: "xtool-core"),
.product(name: "Superutils", package: "xtool-core"),
.product(name: "Crypto", package: "swift-crypto"),
@@ -186,31 +161,9 @@ let package = Package(
dependencies: [
"XKit",
"XToolSupport",
"XCLIUtils",
"PackLib",
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "X509", package: "swift-certificates"),
.product(name: "CustomDump", package: "swift-custom-dump"),
],
swiftSettings: swiftSettings,
),
.testTarget(
name: "XAppleArchiveTests",
dependencies: [
"XAppleArchive",
"XUtils",
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "Crypto", package: "swift-crypto"),
],
exclude: ["Fixtures"],
swiftSettings: swiftSettings,
),
.target(
name: "XCLIUtils",
dependencies: [
"XUtils",
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
],
swiftSettings: swiftSettings,
),
@@ -219,11 +172,10 @@ let package = Package(
dependencies: [
"SwiftyMobileDevice",
"XKit",
"XCLIUtils",
"PackLib",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SystemPackage", package: "swift-system"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "NIOPosix", package: "swift-nio"),
.product(name: "libunxip", package: "unxip"),
],
cSettings: cSettings,
+1 -3
View File
@@ -57,9 +57,7 @@ DEVICE SUBCOMMANDS:
### Library
You can consume xtool itself as a Swift Package, to enable your own app to interact with Apple Developer Services, iOS devices, and more. You can use this by adding `XKit` or `XToolSupport` as a SwiftPM dependency.
Note: while we aim to uphold semver semantics on the CLI interface (breaking change = major version bump), the Swift interface does not offer the same guarantees.
xtool includes a library that you can use to interact with Apple Developer Services, iOS devices, and more from your own app. You can use this by adding `XKit` as a SwiftPM dependency.
```swift
// package dependency:
-5
View File
@@ -1,5 +0,0 @@
module CLZMA [system] {
header "shim.h"
link "lzma"
export *
}
-3
View File
@@ -1,3 +0,0 @@
#pragma once
#include <lzma.h>
-3
View File
@@ -1,4 +1 @@
#include <stdbool.h>
const char * _Nonnull xtl_version(void);
bool xtl_has_uncommitted_changes(void);
-13
View File
@@ -1,5 +1,4 @@
#include "version.h"
#include <stddef.h>
const char *xtl_version(void) {
#ifdef XTOOL_VERSION
@@ -8,15 +7,3 @@ const char *xtl_version(void) {
return "unversioned";
#endif
}
bool xtl_has_uncommitted_changes(void) {
#ifndef XTOOL_VERSION
return true;
#else
#ifdef XTOOL_HAS_UNCOMMITTED_CHANGES
return XTOOL_HAS_UNCOMMITTED_CHANGES != 0;
#else
return false;
#endif
#endif
}
-7
View File
@@ -17,13 +17,6 @@ public struct Packer: Sendable {
try? FileManager.default.removeItem(at: packageDir)
try FileManager.default.createDirectory(at: packageDir, withIntermediateDirectories: true)
if URL(fileURLWithPath: "Packages").dirExists {
try FileManager.default.createSymbolicLink(
atPath: packageDir.appendingPathComponent("Packages").path,
withDestinationPath: "../../Packages"
)
}
let packageSwift = packageDir.appendingPathComponent("Package.swift")
let contents = """
// swift-tools-version: 6.0
+5 -5
View File
@@ -220,16 +220,16 @@ public struct XcodePacker {
importedProducts: [],
)
let project = XcodeProject(project: schema)
let projectURL = projectDir.appending(path: "\(plan.app.product).xcodeproj")
let project = XcodeProject(project: schema, url: projectURL)
try project.write()
try project.write(to: projectURL)
let workspaceURL = xtoolDir.appending(path: "\(plan.app.product).xcworkspace")
let workspace = XcodeWorkspace(children: [
.file(XcodeWorkspace.Location(base: .container, path: "..")),
.file(XcodeWorkspace.Location(base: .group, path: ".xtool-tmp/\(projectURL.lastPathComponent)"))
], url: workspaceURL)
try workspace.write()
])
let workspaceURL = xtoolDir.appending(path: "\(plan.app.product).xcworkspace")
try workspace.write(to: workspaceURL)
return workspaceURL
}
-512
View File
@@ -1,512 +0,0 @@
import Foundation
import Crypto
import X509
import XUtils
/// A reader for Apple Encrypted Archives (`.aea`).
///
/// Only the signed, unencrypted profile (`hkdf_sha256_hmac__none__ecdsa_p256`)
/// is currently supported. This is the profile used to distribute Xcode.
///
/// Opening an archive validates its prologue (the ECDSA signature and the root
/// header MAC), and segment data is authenticated as it is decoded. Note that
/// the signing certificate itself is not validated, so this only guarantees
/// integrity, not authenticity.
final class AEAArchive: Sendable {
struct Options: Sendable {
/// How to determine the key that the archive was signed with.
var signingKey: SigningKey = .embeddedCertificate
/// The maximum number of segments to decode concurrently. Each one
/// needs up to twice the archive's segment size in memory.
var maxConcurrentSegments = min(ProcessInfo.processInfo.activeProcessorCount, 8)
}
enum SigningKey: Sendable {
/// Use the leaf of the archive's embedded certificate chain.
case embeddedCertificate
/// Use the provided key.
case publicKey(P256.Signing.PublicKey)
}
struct RootHeader: Hashable, Sendable {
/// The size of the decoded (plaintext) stream.
var rawSize: UInt64
/// The size of the encoded archive.
var containerSize: UInt64
var segmentSize: UInt32
var segmentsPerCluster: UInt32
var compression: AEACompressionAlgorithm
var checksum: AEAChecksumAlgorithm
}
static let certificateChainKey = "com.apple.ias.signature.certificate-chain"
let rootHeader: RootHeader
private let authData: Data
private let signingPublicKey: P256.Signing.PublicKey
private let fd: FileDescriptor
private let mainKey: SymmetricKey
private let firstClusterOffset: Int64
private let firstClusterMAC: Data
private let options: Options
private static let prologueSize = 304
private static let signatureSize = 128
private static let macSize = 32
convenience init(path: FilePath, options: Options = .init()) throws {
let fd = try FileDescriptor.open(path, .readOnly)
do {
try self.init(fileDescriptor: fd, options: options)
} catch {
try? fd.close()
throw error
}
}
/// On success, the archive takes ownership of `fileDescriptor`.
private init(fileDescriptor fd: FileDescriptor, options: Options = .init()) throws {
self.fd = fd
self.options = options
let fileSize = try fd.seek(offset: 0, from: .end)
let header = try fd.readExactly(count: 12, at: 0)
guard header.prefix(4) == Data("AEA1".utf8) else {
throw AEAError("Not an Apple Encrypted Archive (bad magic)")
}
// low 24 bits: profile, high 8 bits: scrypt strength. We only support
// profile 0 (signed, unencrypted), where the scrypt strength is unused.
let rawProfile = header.loadLE(UInt32.self, at: 4)
guard rawProfile == 0 else {
throw AEAError("Unsupported AEA profile: \(rawProfile & 0xFF_FFFF) (scrypt strength \(rawProfile >> 24))")
}
let authDataSize = Int(header.loadLE(UInt32.self, at: 8))
let authEnd = 12 + authDataSize
guard Int64(authEnd + Self.prologueSize) <= fileSize else {
throw AEAError("Archive is truncated")
}
let prologue = try fd.readExactly(count: authEnd + Self.prologueSize, at: 0)
authData = prologue.bytes(at: 12, count: authDataSize)
var cursor = authEnd
func take(_ count: Int) -> Data {
defer { cursor += count }
return prologue.bytes(at: cursor, count: count)
}
let signature = take(Self.signatureSize)
let keyMaterial = take(32)
let salt = take(32)
let rootHeaderMAC = take(Self.macSize)
let rootHeaderBytes = take(48)
firstClusterMAC = take(Self.macSize)
firstClusterOffset = Int64(cursor)
signingPublicKey = try Self.signingKey(authData: authData, source: options.signingKey)
// The signature covers the SHA-256 digest of the prologue, with the
// signature itself zeroed.
var signedPrologue = prologue
signedPrologue.replaceSubrange(
(prologue.startIndex + authEnd)..<(prologue.startIndex + authEnd + Self.signatureSize),
with: Data(count: Self.signatureSize)
)
guard let derSignature = Self.trimDERSignature(signature),
let ecdsaSignature = try? P256.Signing.ECDSASignature(derRepresentation: derSignature),
signingPublicKey.isValidSignature(ecdsaSignature, for: SHA256.hash(data: signedPrologue))
else { throw AEAError("Invalid archive signature") }
var info = Data("AEA_AMK".utf8)
info.appendLE(rawProfile)
info.append(signingPublicKey.x963Representation)
mainKey = HKDF<SHA256>.deriveKey(
inputKeyMaterial: SymmetricKey(data: keyMaterial),
salt: salt,
info: info,
outputByteCount: 32
)
let rootHeaderKey = Self.macKey(from: mainKey, info: Data("AEA_RHEK".utf8))
guard Self.mac(key: rootHeaderKey, salt: [firstClusterMAC, authData], data: rootHeaderBytes)
== rootHeaderMAC else {
throw AEAError("Root header MAC mismatch (corrupt archive or wrong signing key)")
}
rootHeader = RootHeader(
rawSize: rootHeaderBytes.loadLE(at: 0),
containerSize: rootHeaderBytes.loadLE(at: 8),
segmentSize: rootHeaderBytes.loadLE(at: 16),
segmentsPerCluster: rootHeaderBytes.loadLE(at: 20),
compression: AEACompressionAlgorithm(rawValue: rootHeaderBytes[rootHeaderBytes.startIndex + 24]),
checksum: AEAChecksumAlgorithm(rawValue: rootHeaderBytes[rootHeaderBytes.startIndex + 25])
)
guard rootHeader.containerSize == UInt64(fileSize) else {
throw AEAError("Archive size mismatch: expected \(rootHeader.containerSize), got \(fileSize)")
}
guard rootHeader.segmentSize > 0, rootHeader.segmentSize <= 256 << 20,
rootHeader.segmentsPerCluster > 0, rootHeader.segmentsPerCluster <= 256 << 10
else { throw AEAError("Invalid segment layout") }
guard rootHeader.checksum.size != nil else {
throw AEAError("Unsupported checksum algorithm: \(rootHeader.checksum.rawValue)")
}
guard rootHeader.compression.isSupported else {
throw AEAError("Unsupported compression algorithm: \(rootHeader.compression)")
}
}
deinit {
try? fd.close()
}
// MARK: - Decoding
/// Decodes the archive, calling `body` with each consecutive chunk of the
/// plaintext stream.
///
/// Segments are decoded concurrently but `body` is invoked serially,
/// in order. Supports cooperative cancellation.
func decode(
progress: ((AEAProgress) -> Void)? = nil,
into body: (Data) async throws -> Void
) async throws {
let maxConcurrent = max(1, options.maxConcurrentSegments)
let total = rootHeader.rawSize
var completed: UInt64 = 0
progress?(AEAProgress(completedBytes: 0, totalBytes: total))
var segments = SegmentIterator(archive: self)
try await withThrowingTaskGroup(of: (Int, Data).self) { group in
var running = 0
var finished: [Int: Data] = [:]
var nextToEmit = 0
func receive() async throws {
guard let (index, data) = try await group.next() else { return }
running -= 1
finished[index] = data
while let data = finished.removeValue(forKey: nextToEmit) {
try Task.checkCancellation()
try await body(data)
nextToEmit += 1
completed += UInt64(data.count)
progress?(AEAProgress(completedBytes: completed, totalBytes: total))
}
}
while let segment = try segments.next() {
try Task.checkCancellation()
// bound memory usage: segments that are finished but blocked
// on an earlier one still count towards the limit.
while running + finished.count >= maxConcurrent {
try await receive()
}
group.addTask {
(segment.index, try self.decodeSegment(segment))
}
running += 1
}
while running > 0 {
try await receive()
}
}
guard completed == total else {
throw AEAError("Decoded \(completed) bytes, expected \(total)")
}
}
/// Writes the decoded plaintext stream to `path`.
func decode(
to path: FilePath,
progress: ((AEAProgress) -> Void)? = nil
) async throws {
let out = try FileDescriptor.open(
path, .writeOnly,
options: [.create, .truncate],
permissions: [.ownerReadWrite, .groupRead, .otherRead]
)
defer { try? out.close() }
try await decode(progress: progress) { data in
try data.withUnsafeBytes { _ = try out.writeAll($0) }
}
}
/// Extracts the Apple Archive contained in this AEA into `directory`.
func extract(
to directory: FilePath,
filter: ((String) -> Bool)? = nil,
progress: ((AEAProgress) -> Void)? = nil
) async throws {
let extractor = try AppleArchiveExtractor(destination: directory, filter: filter)
var decoder = AppleArchiveDecoder()
try await decode(progress: progress) { data in
try decoder.decode(data) { try extractor.handle($0) }
}
try decoder.finish()
try extractor.finish()
}
// MARK: - Segments
fileprivate struct Segment: Sendable {
var index: Int
var offset: Int64
var compressedSize: Int
var rawSize: Int
var checksum: Data
var mac: Data
var macKey: SymmetricKey
}
private struct SegmentIterator {
let archive: AEAArchive
var cluster: UInt32 = 0
var offset: Int64
var expectedClusterMAC: Data
var pending: [Segment] = []
var emitted = 0
var remaining: UInt64
init(archive: AEAArchive) {
self.archive = archive
offset = archive.firstClusterOffset
expectedClusterMAC = archive.firstClusterMAC
remaining = archive.rootHeader.rawSize
}
mutating func next() throws -> Segment? {
while pending.isEmpty {
guard remaining > 0 else {
try archive.verifyPadding(at: offset, expectedMAC: expectedClusterMAC)
return nil
}
try readCluster()
}
return pending.removeFirst()
}
private mutating func readCluster() throws {
let root = archive.rootHeader
let count = Int(root.segmentsPerCluster)
let entrySize = 8 + root.checksum.size!
let entriesSize = count * entrySize
let headerSize = entriesSize + AEAArchive.macSize * (count + 1)
let header = try archive.fd.readExactly(count: headerSize, at: offset)
offset += Int64(headerSize)
let entries = header.bytes(at: 0, count: entriesSize)
let macs = header.bytes(at: entriesSize, count: headerSize - entriesSize)
var clusterInfo = Data("AEA_CK".utf8)
clusterInfo.appendLE(cluster)
let clusterKey = HKDF<SHA256>.deriveKey(
inputKeyMaterial: archive.mainKey,
salt: Data(),
info: clusterInfo,
outputByteCount: 32
)
let headerKey = AEAArchive.macKey(from: clusterKey, info: Data("AEA_CHEK".utf8))
guard AEAArchive.mac(key: headerKey, salt: [macs], data: entries) == expectedClusterMAC else {
throw AEAError("Cluster \(cluster) header MAC mismatch")
}
expectedClusterMAC = macs.bytes(at: 0, count: AEAArchive.macSize)
let segmentPRK = HKDF<SHA256>.extract(inputKeyMaterial: clusterKey, salt: Data())
for i in 0..<count {
let entry = entries.bytes(at: i * entrySize, count: entrySize)
let rawSize = Int(entry.loadLE(UInt32.self, at: 0))
let compressedSize = Int(entry.loadLE(UInt32.self, at: 4))
let expectedSize = Int(min(UInt64(root.segmentSize), remaining))
guard rawSize == expectedSize else {
throw AEAError("Segment \(i) of cluster \(cluster) has size \(rawSize), expected \(expectedSize)")
}
if expectedSize == 0 {
guard compressedSize == 0 else {
throw AEAError("Unused segment \(i) of cluster \(cluster) is not empty")
}
continue
}
guard compressedSize > 0, compressedSize <= rawSize,
offset + Int64(compressedSize) <= Int64(root.containerSize)
else {
throw AEAError("Segment \(i) of cluster \(cluster) has invalid size \(compressedSize)")
}
var segmentInfo = Data("AEA_SK".utf8)
segmentInfo.appendLE(UInt32(i))
let segmentKey = HKDF<SHA256>.expand(
pseudoRandomKey: segmentPRK,
info: segmentInfo,
outputByteCount: 32
)
pending.append(Segment(
index: emitted,
offset: offset,
compressedSize: compressedSize,
rawSize: rawSize,
checksum: entry.bytes(at: 8, count: entrySize - 8),
mac: macs.bytes(at: AEAArchive.macSize * (i + 1), count: AEAArchive.macSize),
macKey: segmentKey
))
emitted += 1
offset += Int64(compressedSize)
remaining -= UInt64(rawSize)
}
cluster += 1
}
}
private func decodeSegment(_ segment: Segment) throws -> Data {
let stored = try fd.readExactly(count: segment.compressedSize, at: segment.offset)
guard Self.mac(key: segment.macKey, salt: [], data: stored) == segment.mac else {
throw AEAError("Segment \(segment.index) MAC mismatch")
}
let compression: AEACompressionAlgorithm = segment.compressedSize == segment.rawSize
? .none
: rootHeader.compression
let raw = try compression.decompress(stored, size: segment.rawSize)
switch rootHeader.checksum {
case .sha256:
guard Data(SHA256.hash(data: raw)) == segment.checksum else {
throw AEAError("Segment \(segment.index) checksum mismatch")
}
case .murmur64:
guard murmur64(raw) == segment.checksum.loadLE(UInt64.self, at: 0) else {
throw AEAError("Segment \(segment.index) checksum mismatch")
}
default:
break
}
return raw
}
private func verifyPadding(at offset: Int64, expectedMAC: Data) throws {
let size = Int64(rootHeader.containerSize) - offset
guard size > 0 else { return }
let key = Self.macKey(from: mainKey, info: Data("AEA_PAK".utf8))
var hmac = HMAC<SHA256>(key: key)
var position = offset
while position < offset + size {
try Task.checkCancellation()
let chunk = try fd.readExactly(count: Int(min(1 << 20, offset + size - position)), at: position)
hmac.update(data: chunk)
position += Int64(chunk.count)
}
guard Data(hmac.finalize()) == expectedMAC else {
throw AEAError("Padding MAC mismatch")
}
}
// MARK: - Crypto helpers
/// Derives the MAC key of an HKDF data key.
private static func macKey(from key: SymmetricKey, info: Data) -> SymmetricKey {
HKDF<SHA256>.deriveKey(inputKeyMaterial: key, salt: Data(), info: info, outputByteCount: 32)
}
/// HMAC-SHA256 over `salt || data || LE64(salt.count)`.
private static func mac(key: SymmetricKey, salt: [Data], data: Data) -> Data {
var hmac = HMAC<SHA256>(key: key)
var saltSize: UInt64 = 0
for part in salt {
hmac.update(data: part)
saltSize += UInt64(part.count)
}
hmac.update(data: data)
var suffix = Data()
suffix.appendLE(saltSize)
hmac.update(data: suffix)
return Data(hmac.finalize())
}
/// Strips the zero padding from a DER-encoded ECDSA signature.
private static func trimDERSignature(_ signature: Data) -> Data? {
guard signature.count >= 2, signature[signature.startIndex] == 0x30 else { return nil }
let length = Int(signature[signature.startIndex + 1])
// P-256 signatures are always short enough for the short-form length.
guard length < 0x80, 2 + length <= signature.count else { return nil }
return signature.prefix(2 + length)
}
// MARK: - Signing key
private static func signingKey(authData: Data, source: SigningKey) throws -> P256.Signing.PublicKey {
switch source {
case .publicKey(let key):
return key
case .embeddedCertificate:
return try embeddedSigningKey(authData: authData)
}
}
/// The public key of the leaf certificate in the auth data's certificate chain.
static func embeddedSigningKey(authData: Data) throws -> P256.Signing.PublicKey {
guard let entries = parseAuthData(authData),
let pem = entries.first(where: { $0.key == certificateChainKey })?.value
else { throw AEAError("Archive does not contain a signing certificate chain") }
// the leaf comes first
guard let leaf = String(decoding: pem, as: UTF8.self).components(separatedBy: endMarker).first,
let certificate = try? Certificate(pemEncoded: leaf + endMarker),
let key = P256.Signing.PublicKey(certificate.publicKey)
else { throw AEAError("Archive does not contain a valid P-256 signing certificate") }
return key
}
private static let endMarker = "-----END CERTIFICATE-----"
private static func parseAuthData(_ data: Data) -> [(key: String, value: Data)]? {
var entries: [(key: String, value: Data)] = []
var offset = 0
while offset < data.count {
guard data.count - offset >= 4 else { return nil }
let size = Int(data.loadLE(UInt32.self, at: offset))
guard size > 4, size <= data.count - offset else { return nil }
let payload = data.bytes(at: offset + 4, count: size - 4)
guard let nul = payload.firstIndex(of: 0) else { return nil }
entries.append((
key: String(decoding: payload[..<nul], as: UTF8.self),
value: Data(payload[(nul + 1)...])
))
offset += size
}
return entries
}
}
struct AEAProgress: Sendable, Hashable {
/// The number of plaintext bytes decoded so far.
var completedBytes: UInt64
/// The total number of plaintext bytes in the archive.
var totalBytes: UInt64
var fractionCompleted: Double {
totalBytes == 0 ? 1 : Double(completedBytes) / Double(totalBytes)
}
}
extension FileDescriptor {
fileprivate func readExactly(count: Int, at offset: Int64) throws -> Data {
var data = Data(count: count)
let read = try data.withUnsafeMutableBytes { buf in
var total = 0
while total < count {
let n = try self.read(
fromAbsoluteOffset: offset + Int64(total),
into: UnsafeMutableRawBufferPointer(rebasing: buf[total...])
)
if n == 0 { break }
total += n
}
return total
}
guard read == count else { throw AEAError("Archive is truncated") }
return data
}
}
-167
View File
@@ -1,167 +0,0 @@
import Foundation
#if canImport(Compression)
import Compression
#elseif canImport(CLZMA)
import CLZMA
#endif
/// The algorithm used to compress the segments of an encrypted archive.
struct AEACompressionAlgorithm: RawRepresentable, Hashable, Sendable, CustomStringConvertible {
var rawValue: UInt8
static let none = Self(rawValue: UInt8(ascii: "-"))
static let lz4 = Self(rawValue: UInt8(ascii: "4"))
static let lzbitmap = Self(rawValue: UInt8(ascii: "b"))
static let lzfse = Self(rawValue: UInt8(ascii: "e"))
static let lzvn = Self(rawValue: UInt8(ascii: "f"))
static let lzma = Self(rawValue: UInt8(ascii: "x"))
static let zlib = Self(rawValue: UInt8(ascii: "z"))
var description: String {
switch self {
case .none: "none"
case .lz4: "lz4"
case .lzbitmap: "lzbitmap"
case .lzfse: "lzfse"
case .lzvn: "lzvn"
case .lzma: "lzma"
case .zlib: "zlib"
default: "unknown (0x\(String(rawValue, radix: 16)))"
}
}
/// Whether segments compressed with this algorithm can be decoded on the current platform.
var isSupported: Bool {
switch self {
case .none, .lzma:
true
#if canImport(Compression)
case .lz4, .lzbitmap, .lzfse, .zlib:
true
#endif
default:
false
}
}
/// Decompresses `input` into exactly `size` bytes.
func decompress(_ input: Data, size: Int) throws -> Data {
if self == .none {
guard input.count == size else {
throw AEAError("Stored segment has size \(input.count), expected \(size)")
}
return input
}
#if canImport(Compression)
let algorithm: compression_algorithm
switch self {
case .lzma: algorithm = COMPRESSION_LZMA
// segments are raw LZ4 blocks without the "bv41" framing
case .lz4: algorithm = COMPRESSION_LZ4_RAW
case .lzbitmap: algorithm = COMPRESSION_LZBITMAP
case .lzfse: algorithm = COMPRESSION_LZFSE
case .zlib: algorithm = COMPRESSION_ZLIB
default: throw AEAError("Unsupported compression algorithm: \(self)")
}
let input = self == .zlib ? try Self.stripZlibWrapper(input) : input
// The buffer API silently truncates overlong output, so leave
// room for one extra byte to be able to detect it.
var output = Data(count: size + 1)
let written = output.withUnsafeMutableBytes { dst in
input.withUnsafeBytes { src in
compression_decode_buffer(
dst.bindMemory(to: UInt8.self).baseAddress!, dst.count,
src.bindMemory(to: UInt8.self).baseAddress!, src.count,
nil, algorithm
)
}
}
guard written == size else {
throw AEAError("Segment decompressed to \(written) bytes, expected \(size)")
}
output.removeLast()
return output
#elseif canImport(CLZMA)
guard self == .lzma else {
throw AEAError("Unsupported compression algorithm on this platform: \(self)")
}
var output = Data(count: size)
var memlimit = UInt64.max
var inPos = 0
var outPos = 0
let result = output.withUnsafeMutableBytes { dst in
input.withUnsafeBytes { src in
lzma_stream_buffer_decode(
&memlimit, 0, nil,
src.bindMemory(to: UInt8.self).baseAddress!, &inPos, src.count,
dst.bindMemory(to: UInt8.self).baseAddress!, &outPos, dst.count
)
}
}
guard result == LZMA_OK, inPos == input.count, outPos == size else {
throw AEAError("Failed to decompress LZMA segment (status \(result.rawValue))")
}
return output
#else
throw AEAError("Unsupported compression algorithm on this platform: \(self)")
#endif
}
#if canImport(Compression)
/// COMPRESSION_ZLIB operates on raw DEFLATE streams, but AEA stores
/// zlib-wrapped (RFC 1950) data.
private static func stripZlibWrapper(_ input: Data) throws -> Data {
guard input.count >= 6,
input[input.startIndex] & 0x0F == 8,
(UInt16(input[input.startIndex]) << 8 | UInt16(input[input.startIndex + 1])) % 31 == 0,
input[input.startIndex + 1] & 0x20 == 0
else { throw AEAError("Invalid zlib segment header") }
return input.dropFirst(2).dropLast(4)
}
#endif
}
/// The checksum stored alongside each segment of an encrypted archive.
struct AEAChecksumAlgorithm: RawRepresentable, Hashable, Sendable {
var rawValue: UInt8
static let none = Self(rawValue: 0)
static let murmur64 = Self(rawValue: 1)
static let sha256 = Self(rawValue: 2)
var size: Int? {
switch self {
case .none: 0
case .murmur64: 8
case .sha256: 32
default: nil
}
}
}
/// MurmurHash64A with AEA's seed.
func murmur64(_ data: Data) -> UInt64 {
let m: UInt64 = 0xc6a4_a793_5bd1_e995
var h: UInt64 = 0xE223_6FDC_26A5_F6D2 ^ (UInt64(data.count) &* m)
let blocks = data.count / 8
for i in 0..<blocks {
var k = data.loadLE(UInt64.self, at: i * 8)
k = k &* m
k ^= k >> 47
k = k &* m
h ^= k
h = h &* m
}
let tail = data.count % 8
if tail != 0 {
for i in 0..<tail {
h ^= UInt64(data[data.startIndex + blocks * 8 + i]) << (8 * i)
}
h = h &* m
}
h ^= h >> 47
h = h &* m
h ^= h >> 47
return h
}
-8
View File
@@ -1,8 +0,0 @@
/// An error encountered while reading an Apple Encrypted Archive.
public struct AEAError: Error, CustomStringConvertible, Sendable {
public var description: String
init(_ description: String) {
self.description = description
}
}
-69
View File
@@ -1,69 +0,0 @@
import Dependencies
import Foundation
import XUtils
/// Extracts Apple Encrypted Archives (`.aea`), such as the ones Xcode is distributed as.
///
/// The archive's contents are integrity-checked as they are extracted. Note that
/// its signing certificate is not validated.
public struct AEAExtractor: DependencyKey, Sendable {
public struct Options: Sendable {
/// Called with the path of each entry, relative to the root of the archive.
/// Return `false` to skip the entry.
public var filter: (@Sendable (_ path: String) -> Bool)?
public init(filter: (@Sendable (_ path: String) -> Bool)? = nil) {
self.filter = filter
}
}
public var extractArchive: @Sendable (
_ archive: FilePath,
_ destination: FilePath,
_ options: Options,
_ progress: @escaping @Sendable (_ fractionCompleted: Double) -> Void
) async throws -> Void
public init(
extractArchive: @escaping @Sendable (
FilePath, FilePath, Options, @escaping @Sendable (Double) -> Void
) async throws -> Void
) {
self.extractArchive = extractArchive
}
/// Extracts the archive at `archive` into the directory `destination`, creating it if needed.
///
/// Supports cooperative cancellation. `progress` is called with the fraction
/// of the archive that has been extracted so far.
public func extract(
_ archive: FilePath,
to destination: FilePath,
options: Options = .init(),
progress: @escaping @Sendable (_ fractionCompleted: Double) -> Void = { _ in }
) async throws {
try await extractArchive(archive, destination, options, progress)
}
public static var liveValue: Self {
Self { archive, destination, options, progress in
let archive = try AEAArchive(path: archive)
try await archive.extract(
to: destination,
filter: options.filter,
progress: { progress($0.fractionCompleted) }
)
}
}
public static var testValue: Self {
Self(extractArchive: unimplemented("AEAExtractor.extractArchive"))
}
}
extension DependencyValues {
public var aeaExtractor: AEAExtractor {
get { self[AEAExtractor.self] }
set { self[AEAExtractor.self] = newValue }
}
}
@@ -1,223 +0,0 @@
import Foundation
/// The header of an entry in an Apple Archive (`.aar`) stream.
struct AppleArchiveHeader: Sendable {
/// A field key such as `PAT` or `DAT`.
struct Key: RawRepresentable, Hashable, Sendable, CustomStringConvertible {
var rawValue: String
var description: String { rawValue }
static let type = Self(rawValue: "TYP")
static let path = Self(rawValue: "PAT")
static let linkPath = Self(rawValue: "LNK")
static let mode = Self(rawValue: "MOD")
static let modificationTime = Self(rawValue: "MTM")
static let data = Self(rawValue: "DAT")
static let hardLinkCluster = Self(rawValue: "HLC")
}
enum Value: Sendable, Hashable {
case flag
case uint(UInt64)
case string(Data)
case hash(Data)
case timespec(seconds: Int64, nanoseconds: UInt32)
/// A blob whose contents follow the header.
case blob(size: UInt64)
}
struct EntryType: RawRepresentable, Hashable, Sendable {
var rawValue: UInt8
static let regularFile = Self(rawValue: UInt8(ascii: "F"))
static let directory = Self(rawValue: UInt8(ascii: "D"))
static let symbolicLink = Self(rawValue: UInt8(ascii: "L"))
static let metadata = Self(rawValue: UInt8(ascii: "M"))
}
/// The header's fields, in order.
var fields: [(key: Key, value: Value)]
subscript(key: Key) -> Value? {
fields.first { $0.key == key }?.value
}
var entryType: EntryType? {
guard case .uint(let value)? = self[.type], let byte = UInt8(exactly: value) else { return nil }
return EntryType(rawValue: byte)
}
/// The entry's path, relative to the root of the archive.
var path: String? { string(.path) }
var linkPath: String? { string(.linkPath) }
var mode: UInt16? { uint(.mode).flatMap { UInt16(exactly: $0 & 0o7777) } }
var hardLinkCluster: UInt64? { uint(.hardLinkCluster) }
var modificationDate: Date? {
guard case .timespec(let seconds, let nanoseconds)? = self[.modificationTime] else { return nil }
return Date(timeIntervalSince1970: Double(seconds) + Double(nanoseconds) / 1e9)
}
func uint(_ key: Key) -> UInt64? {
guard case .uint(let value)? = self[key] else { return nil }
return value
}
func string(_ key: Key) -> String? {
guard case .string(let value)? = self[key] else { return nil }
return String(decoding: value, as: UTF8.self)
}
var blobs: [(key: Key, size: UInt64)] {
fields.compactMap {
guard case .blob(let size) = $0.value else { return nil }
return ($0.key, size)
}
}
}
/// An incremental (push) decoder for Apple Archive (`.aar`) streams.
///
/// Feed it consecutive chunks of the stream with ``decode(_:handler:)``.
struct AppleArchiveDecoder {
enum Event {
/// The start of a new entry.
case header(AppleArchiveHeader)
/// A chunk of the blob `key` of the current entry. Blobs are delivered
/// in the order they appear in the header.
case blob(key: AppleArchiveHeader.Key, data: Data)
/// The end of the current entry.
case endOfEntry
}
private enum State {
case header
case blobs([(key: AppleArchiveHeader.Key, size: UInt64)], index: Int, remaining: UInt64)
}
private static let prefixSize = 6
private var state = State.header
private var buffer = Data()
init() {}
mutating func decode(_ chunk: Data, handler: (Event) throws -> Void) throws {
var input = chunk[...]
while !input.isEmpty {
switch state {
case .header:
if buffer.count < Self.prefixSize {
let needed = Self.prefixSize - buffer.count
buffer.append(input.prefix(needed))
input = input.dropFirst(needed)
guard buffer.count == Self.prefixSize else { continue }
guard buffer.prefix(4) == Data("AA01".utf8) || buffer.prefix(4) == Data("YAA1".utf8) else {
throw AEAError("Invalid Apple Archive header magic")
}
}
let size = Int(buffer.loadLE(UInt16.self, at: 4))
guard size >= Self.prefixSize else {
throw AEAError("Invalid Apple Archive header size: \(size)")
}
let needed = size - buffer.count
buffer.append(input.prefix(needed))
input = input.dropFirst(needed)
guard buffer.count == size else { continue }
let header = try Self.parseHeader(buffer.dropFirst(Self.prefixSize))
buffer.removeAll(keepingCapacity: true)
try handler(.header(header))
state = .blobs(header.blobs, index: 0, remaining: header.blobs.first?.size ?? 0)
try advance(handler: handler)
case .blobs(let blobs, let index, let remaining):
let count = Int(min(remaining, UInt64(input.count)))
try handler(.blob(key: blobs[index].key, data: input.prefix(count)))
input = input.dropFirst(count)
state = .blobs(blobs, index: index, remaining: remaining - UInt64(count))
try advance(handler: handler)
}
}
}
/// Validates that the stream did not end in the middle of an entry.
func finish() throws {
guard case .header = state, buffer.isEmpty else {
throw AEAError("Apple Archive stream is truncated")
}
}
/// Skips past completed blobs, ending the entry if there are none left.
private mutating func advance(handler: (Event) throws -> Void) throws {
guard case .blobs(let blobs, var index, var remaining) = state else { return }
while remaining == 0 {
index += 1
guard index < blobs.count else {
state = .header
try handler(.endOfEntry)
return
}
remaining = blobs[index].size
}
state = .blobs(blobs, index: index, remaining: remaining)
}
private static let uintSizes: [UInt8: Int] = [
UInt8(ascii: "1"): 1, UInt8(ascii: "2"): 2, UInt8(ascii: "4"): 4, UInt8(ascii: "8"): 8,
]
private static let blobSizeSizes: [UInt8: Int] = [
UInt8(ascii: "A"): 2, UInt8(ascii: "B"): 4, UInt8(ascii: "C"): 8,
]
private static let hashSizes: [UInt8: Int] = [
UInt8(ascii: "F"): 4, UInt8(ascii: "G"): 20, UInt8(ascii: "H"): 32, UInt8(ascii: "I"): 48, UInt8(ascii: "J"): 64,
]
private static func parseHeader(_ data: Data) throws -> AppleArchiveHeader {
let data = Data(data)
var fields: [(key: AppleArchiveHeader.Key, value: AppleArchiveHeader.Value)] = []
var offset = 0
func take(_ count: Int) throws -> Data {
guard data.count - offset >= count else {
throw AEAError("Truncated Apple Archive header field")
}
defer { offset += count }
return data.bytes(at: offset, count: count)
}
func uint(size: Int) throws -> UInt64 {
try take(size).reversed().reduce(0) { $0 << 8 | UInt64($1) }
}
while offset < data.count {
let tag = try take(4)
let key = AppleArchiveHeader.Key(rawValue: String(decoding: tag.prefix(3), as: UTF8.self))
let type = tag[3]
let value: AppleArchiveHeader.Value
if type == UInt8(ascii: "*") {
value = .flag
} else if let size = uintSizes[type] {
value = .uint(try uint(size: size))
} else if let size = blobSizeSizes[type] {
value = .blob(size: try uint(size: size))
} else if let size = hashSizes[type] {
value = .hash(try take(size))
} else if type == UInt8(ascii: "P") {
value = .string(try take(Int(try uint(size: 2))))
} else if type == UInt8(ascii: "S") {
value = .timespec(seconds: Int64(bitPattern: try uint(size: 8)), nanoseconds: 0)
} else if type == UInt8(ascii: "T") {
let seconds = Int64(bitPattern: try uint(size: 8))
value = .timespec(seconds: seconds, nanoseconds: UInt32(try uint(size: 4)))
} else {
throw AEAError("Unknown Apple Archive field type: \(String(decoding: tag, as: UTF8.self))")
}
fields.append((key, value))
}
return AppleArchiveHeader(fields: fields)
}
}
@@ -1,266 +0,0 @@
import Foundation
import XUtils
/// Extracts Apple Archive entries into a directory.
///
/// Supports regular files, directories, symbolic links and hard links,
/// along with their permissions and modification times. Ownership, flags,
/// extended attributes and ACLs are not restored.
///
/// Entries may not escape the destination, either via `..` components
/// or via symbolic links created by earlier entries.
final class AppleArchiveExtractor {
private struct PendingDirectory {
var path: String
var mode: UInt16
var modificationDate: Date?
}
private struct OpenFile {
var path: String
var fd: FileDescriptor
var header: AppleArchiveHeader
}
private enum Current {
case none
case skipped
case file(OpenFile)
}
private let fileManager = FileManager.default
private let destination: String
private let filter: ((String) -> Bool)?
private var current = Current.none
private var knownDirectories: Set<String> = [""]
private var symlinks: Set<String> = []
private var hardLinkClusters: [UInt64: String] = [:]
private var directories: [PendingDirectory] = []
/// - Parameter filter: Called with each entry's path. Return `false` to skip the entry.
init(destination: FilePath, filter: ((String) -> Bool)? = nil) throws {
self.destination = destination.string
self.filter = filter
try fileManager.createDirectory(atPath: self.destination, withIntermediateDirectories: true)
}
deinit {
if case .file(let file) = current {
try? file.fd.close()
}
}
/// Extracts the (unencrypted) Apple Archive at `archive` into `destination`.
static func extract(archive: FilePath, to destination: FilePath) throws {
let extractor = try AppleArchiveExtractor(destination: destination)
let input = try FileDescriptor.open(archive, .readOnly)
defer { try? input.close() }
var decoder = AppleArchiveDecoder()
var buffer = Data(count: 1 << 20)
while true {
let count = try buffer.withUnsafeMutableBytes { try input.read(into: $0) }
if count == 0 { break }
try decoder.decode(buffer.prefix(count)) { try extractor.handle($0) }
}
try decoder.finish()
try extractor.finish()
}
func handle(_ event: AppleArchiveDecoder.Event) throws {
switch event {
case .header(let header):
current = try begin(header)
case .blob(let key, let data):
guard key == .data, case .file(let file) = current else { break }
_ = try file.fd.writeAll(data)
case .endOfEntry:
let entry = current
current = .none
if case .file(let file) = entry {
try end(file)
}
}
}
/// Applies deferred directory metadata. Call this once all entries
/// have been handled.
func finish() throws {
// children before parents, so that read-only directories
// and modification times are applied last.
for directory in directories.reversed() {
try setAttributes(
path: directory.path,
mode: directory.mode,
modificationDate: directory.modificationDate
)
}
directories.removeAll()
}
private func absolute(_ path: String) -> String {
path.isEmpty ? destination : "\(destination)/\(path)"
}
private func begin(_ header: AppleArchiveHeader) throws -> Current {
guard let type = header.entryType, let rawPath = header.path else {
throw AEAError("Apple Archive entry is missing a type or path")
}
if type == .metadata { return .skipped }
let path = try Self.sanitize(rawPath)
if let filter, !filter(path) { return .skipped }
if !path.isEmpty {
try ensureParent(of: path)
}
switch type {
case .directory:
try createDirectory(path: path, header: header)
return .skipped
case .symbolicLink:
guard let target = header.linkPath, !path.isEmpty else {
throw AEAError("Invalid symbolic link entry '\(rawPath)'")
}
try createSymlink(path: path, target: target, header: header)
return .skipped
case .regularFile:
guard !path.isEmpty else {
throw AEAError("Invalid file entry '\(rawPath)'")
}
return try createFile(path: path, header: header)
default:
throw AEAError(
"Unsupported Apple Archive entry type '\(UnicodeScalar(type.rawValue))' for '\(rawPath)'"
)
}
}
private func createDirectory(path: String, header: AppleArchiveHeader) throws {
if !path.isEmpty {
// the real mode is applied by finish(), once we're done writing to it
do {
try fileManager.createDirectory(atPath: absolute(path), withIntermediateDirectories: false)
} catch where Self.isFileExists(error) {
let type = try fileManager.attributesOfItem(atPath: absolute(path))[.type] as? FileAttributeType
if type != .typeDirectory {
try fileManager.removeItem(atPath: absolute(path))
symlinks.remove(path)
try fileManager.createDirectory(atPath: absolute(path), withIntermediateDirectories: false)
}
}
knownDirectories.insert(path)
}
directories.append(PendingDirectory(
path: path,
mode: header.mode ?? 0o755,
modificationDate: header.modificationDate
))
}
private func createSymlink(path: String, target: String, header: AppleArchiveHeader) throws {
try replacingExisting(path) {
try fileManager.createSymbolicLink(atPath: absolute(path), withDestinationPath: target)
}
symlinks.insert(path)
#if canImport(Darwin)
// only Darwin supports modes on symlinks, and FileManager.setAttributes
// follows them, so this needs lchmod.
if let mode = header.mode, lchmod(absolute(path), mode_t(Self.permissions(mode))) != 0 {
throw Errno(rawValue: errno)
}
#endif
}
private func createFile(path: String, header: AppleArchiveHeader) throws -> Current {
// every member of a hard link cluster may carry its own copy of the data;
// link to the first one we extracted and discard the rest.
if let cluster = header.hardLinkCluster {
if let existing = hardLinkClusters[cluster] {
try replacingExisting(path) {
try fileManager.linkItem(atPath: absolute(existing), toPath: absolute(path))
}
return .skipped
}
hardLinkClusters[cluster] = path
}
let fd = try replacingExisting(path) {
try FileDescriptor.open(
absolute(path), .writeOnly,
options: [.create, .exclusiveCreate],
permissions: .ownerReadWrite
)
}
return .file(OpenFile(path: path, fd: fd, header: header))
}
private func end(_ file: OpenFile) throws {
try file.fd.close()
try setAttributes(
path: file.path,
mode: file.header.mode ?? 0o644,
modificationDate: file.header.modificationDate
)
}
/// Sets the attributes of the item at `path`, following symlinks.
private func setAttributes(path: String, mode: UInt16, modificationDate: Date?) throws {
var attributes: [FileAttributeKey: Any] = [.posixPermissions: Int(Self.permissions(mode))]
attributes[.modificationDate] = modificationDate
try fileManager.setAttributes(attributes, ofItemAtPath: absolute(path))
}
/// Drops setuid/setgid bits.
private static func permissions(_ mode: UInt16) -> UInt16 {
mode & 0o1777
}
/// Creates the parent directory of `path` if needed, rejecting paths that
/// traverse symlinks created by earlier entries.
private func ensureParent(of path: String) throws {
var prefix = ""
for component in path.split(separator: "/").dropLast() {
prefix = prefix.isEmpty ? String(component) : "\(prefix)/\(component)"
if symlinks.contains(prefix) {
throw AEAError("Apple Archive entry '\(path)' traverses a symbolic link")
}
}
guard !knownDirectories.contains(prefix) else { return }
try fileManager.createDirectory(atPath: absolute(prefix), withIntermediateDirectories: true)
while !prefix.isEmpty, knownDirectories.insert(prefix).inserted {
prefix = String(prefix[..<(prefix.lastIndex(of: "/") ?? prefix.startIndex)])
}
}
/// Runs `create`. If that fails because a non-directory already exists at
/// `path` (relative to the destination), removes it and tries again.
@discardableResult
private func replacingExisting<T>(_ path: String, _ create: () throws -> T) throws -> T {
do {
return try create()
} catch where Self.isFileExists(error) {
let absolutePath = absolute(path)
if try fileManager.attributesOfItem(atPath: absolutePath)[.type] as? FileAttributeType == .typeDirectory {
throw AEAError("Cannot replace directory '\(absolutePath)'")
}
try fileManager.removeItem(atPath: absolutePath)
symlinks.remove(path)
return try create()
}
}
private static func isFileExists(_ error: any Error) -> Bool {
(error as? CocoaError)?.code == .fileWriteFileExists || (error as? Errno) == .fileExists
}
/// Normalizes an entry path, rejecting ones that could escape the destination.
static func sanitize(_ path: String) throws -> String {
guard !path.hasPrefix("/") else {
throw AEAError("Apple Archive entry has an absolute path: '\(path)'")
}
let components = path.split(separator: "/").filter { $0 != "." }
guard !components.contains("..") else {
throw AEAError("Apple Archive entry escapes the destination: '\(path)'")
}
return components.joined(separator: "/")
}
}
@@ -1,23 +0,0 @@
import Foundation
extension Data {
/// Reads a little-endian integer at the zero-based `offset`.
func loadLE<T: FixedWidthInteger>(_: T.Type = T.self, at offset: Int) -> T {
var value: T = 0
Swift.withUnsafeMutableBytes(of: &value) { dest in
let start = startIndex + offset
dest.copyBytes(from: self[start..<(start + MemoryLayout<T>.size)])
}
return T(littleEndian: value)
}
/// A copy of the `count` bytes starting at the zero-based `offset`.
func bytes(at offset: Int, count: Int) -> Data {
let start = startIndex + offset
return Data(self[start..<(start + count)])
}
mutating func appendLE<T: FixedWidthInteger>(_ value: T) {
Swift.withUnsafeBytes(of: value.littleEndian) { append(contentsOf: $0) }
}
}
-65
View File
@@ -1,65 +0,0 @@
import Dependencies
import Foundation
import XUtils
package struct CommandInput: DependencyKey, Sendable {
// nil = EOF
private let readImpl: @Sendable (DataChannel.Limit) async throws -> Data?
package init(
read: @escaping @Sendable (DataChannel.Limit) async throws -> Data?,
) {
self.readImpl = read
}
package init(channel: DataChannel) {
self.init(read: { try await channel.read(upTo: $0) })
}
package func read(upTo limit: DataChannel.Limit = .unlimited) async throws -> Data? {
try Task.checkCancellation()
return try await readImpl(limit)
}
/// Cancellation discards bytes already consumed for this line.
package func readLine() async throws -> String? {
var line = Data()
while let chunk = try await read(upTo: .newline) {
line.append(chunk)
if line.last == UInt8(ascii: "\n") {
line.removeLast()
return String(decoding: line, as: UTF8.self).trimmingTrailingCarriageReturn()
}
}
guard !line.isEmpty else { return nil }
return String(decoding: line, as: UTF8.self).trimmingTrailingCarriageReturn()
}
package static var liveValue: CommandInput {
@Dependency(\.shutdownRegistry) var shutdownRegistry
do {
let input = try DispatchInput(fileDesciptor: .standardInput)
shutdownRegistry.register { await input.close() }
return Self(read: { try await input.read(upTo: $0) })
} catch {
return Self(read: { _ in throw error })
}
}
package static var testValue: CommandInput {
Self(read: unimplemented("CommandInput.read", placeholder: nil))
}
}
private extension String {
func trimmingTrailingCarriageReturn() -> String {
hasSuffix("\r") ? String(dropLast()) : self
}
}
extension DependencyValues {
package var input: CommandInput {
get { self[CommandInput.self] }
set { self[CommandInput.self] = newValue }
}
}
-19
View File
@@ -1,19 +0,0 @@
public struct CommandVersion: Sendable, Hashable {
public var version: String
public var hasUncommittedChanges: Bool
public init(version: String, hasUncommittedChanges: Bool) {
self.version = version
self.hasUncommittedChanges = hasUncommittedChanges
}
public static var unversioned: Self {
.init(version: "unversioned", hasUncommittedChanges: true)
}
}
extension CommandVersion: CustomStringConvertible {
public var description: String {
version + (hasUncommittedChanges ? "-dirty" : "")
}
}
-44
View File
@@ -1,44 +0,0 @@
import ConcurrencyExtras
import Dependencies
/// Anything registered here will be cleaned up when the process exits
public final class ShutdownRegistry: DependencyKey, Sendable {
public static var liveValue: ShutdownRegistry { ShutdownRegistry() }
public static var testValue: ShutdownRegistry { ShutdownRegistry() }
private struct State {
var operations: [@Sendable () async -> Void] = []
var shutdownTask: Task<Void, Never>?
}
private let state = LockIsolated(State())
public init() {}
/// Register cleanup before shutdown begins.
public func register(_ operation: @escaping @Sendable () async -> Void) {
state.withValue { $0.operations.append(operation) }
}
/// Runs cleanup in reverse registration order, once, even for concurrent callers.
public func shutdown() async {
let task = state.withValue { state in
if let task = state.shutdownTask { return task }
let pending = state.operations.reversed()
state.operations.removeAll()
let task = Task {
for operation in pending { await operation() }
}
state.shutdownTask = task
return task
}
await task.value
}
}
extension DependencyValues {
public var shutdownRegistry: ShutdownRegistry {
get { self[ShutdownRegistry.self] }
set { self[ShutdownRegistry.self] = newValue }
}
}
@@ -8,7 +8,6 @@
import Foundation
/// Replaces all of the app's groups with the given ones
public final class DeveloperServicesAssignAppGroupRequest: DeveloperServicesPlatformRequest {
public struct Response: Decodable, Sendable {}
@@ -17,14 +16,14 @@ public final class DeveloperServicesAssignAppGroupRequest: DeveloperServicesPlat
public let platform: DeveloperServicesPlatform
public let teamID: DeveloperServicesTeam.ID
public let appIDID: String
public let groupIDs: [DeveloperServicesAppGroup.ID]
public let groupID: DeveloperServicesAppGroup.ID
var subAction: String { return "assignApplicationGroupToAppId" }
var subParameters: [String: Any] {
return [
"teamId": teamID.rawValue,
"appIdId": appIDID,
"applicationGroups": groupIDs.map(\.rawValue)
"applicationGroups": groupID.rawValue
]
}
@@ -32,12 +31,12 @@ public final class DeveloperServicesAssignAppGroupRequest: DeveloperServicesPlat
platform: DeveloperServicesPlatform,
teamID: DeveloperServicesTeam.ID,
appIDID: String,
groupIDs: [DeveloperServicesAppGroup.ID]
groupID: DeveloperServicesAppGroup.ID
) {
self.platform = platform
self.teamID = teamID
self.appIDID = appIDID
self.groupIDs = groupIDs
self.groupID = groupID
}
}
@@ -8,7 +8,6 @@
import Foundation
import DeveloperAPI
import XUtils
public struct DeveloperServicesAssignAppGroupsOperation: DeveloperServicesOperation {
@@ -34,23 +33,36 @@ public struct DeveloperServicesAssignAppGroupsOperation: DeveloperServicesOperat
self.client = DeveloperServicesClient(authData: authData)
}
private func getOrCreateAppGroup(
private func upsertAppGroup(
_ groupID: DeveloperServicesAppGroup.GroupID,
existingGroups: [String: DeveloperServicesAppGroup]
) async throws -> DeveloperServicesAppGroup {
) async throws -> DeveloperServicesAppGroup.GroupID {
let sanitized = ProvisioningIdentifiers.sanitize(groupID: groupID)
let group: DeveloperServicesAppGroup
if let existingGroup = existingGroups[sanitized] {
return existingGroup
group = existingGroup
} else {
let groupID = ProvisioningIdentifiers.groupID(fromSanitized: sanitized, context: context)
let name = ProvisioningIdentifiers.groupName(fromSanitized: sanitized)
let request = DeveloperServicesAddAppGroupRequest(
platform: .iOS,
teamID: xcodeAuthData.teamID,
name: name,
groupID: groupID
)
group = try await client.send(request)
}
let groupID = ProvisioningIdentifiers.groupID(fromSanitized: sanitized, context: context)
let name = ProvisioningIdentifiers.groupName(fromSanitized: sanitized)
let request = DeveloperServicesAddAppGroupRequest(
platform: .iOS,
teamID: xcodeAuthData.teamID,
name: name,
groupID: groupID
_ = try await client.send(
DeveloperServicesAssignAppGroupRequest(
platform: .iOS,
teamID: xcodeAuthData.teamID,
appIDID: appID.id,
groupID: group.id
)
)
return try await client.send(request)
return group.groupID
}
public func perform() async throws -> [DeveloperServicesAppGroup.GroupID] {
@@ -59,20 +71,14 @@ public struct DeveloperServicesAssignAppGroupsOperation: DeveloperServicesOperat
))
let sanitized = existing.map { (ProvisioningIdentifiers.sanitize(groupID: $0.groupID), $0) }
let dict = Dictionary(sanitized, uniquingKeysWith: { $1 })
let groups = try await groupIDs.concurrentMap { groupID in
try await getOrCreateAppGroup(groupID, existingGroups: dict)
return try await withThrowingTaskGroup(of: DeveloperServicesAppGroup.GroupID.self) { group in
for groupID in groupIDs {
group.addTask {
try await upsertAppGroup(groupID, existingGroups: dict)
}
}
return try await group.reduce(into: []) { $0.append($1) }
}
_ = try await client.send(
DeveloperServicesAssignAppGroupRequest(
platform: .iOS,
teamID: xcodeAuthData.teamID,
appIDID: appID.id,
groupIDs: groups.map(\.id)
)
)
return groups.map(\.groupID)
}
}
@@ -8,29 +8,22 @@
import Foundation
import DeveloperAPI
import Dependencies
public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {
@Dependency(\.signer) private var signer
public enum Error: LocalizedError, CustomStringConvertible {
public enum Error: LocalizedError {
case invalidApp(URL)
case tooManyAppGroups(bundleID: String, groups: [DeveloperServicesAppGroup.GroupID])
public var errorDescription: String? {
description
}
public var description: String {
switch self {
case .invalidApp(let url):
"Invalid app: \(url.path)"
case .tooManyAppGroups(let bundleID, let groups):
"""
App '\(bundleID)' requests multiple app groups: \(groups.map(\.rawValue).joined(separator: ", ")), \
which is not permitted when using a free team.
"""
return url.path.withCString {
String.localizedStringWithFormat(
NSLocalizedString(
"add_app_operation.error.invalid_app", value: "Invalid app: %s", comment: ""
), $0
)
}
}
}
}
@@ -179,7 +172,7 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {
var entitlements: Entitlements
// if the executable doesn't already have entitlements, that's
// okay. We don't have to throw an error.
if let entitlementsData = try? await signer.analyze(executable: executableURL),
if let entitlementsData = try? await context.signer.analyze(executable: executableURL),
let decodedEntitlements = try? PropertyListDecoder().decode(Entitlements.self, from: entitlementsData) {
entitlements = decodedEntitlements
@@ -187,10 +180,6 @@ public struct DeveloperServicesAddAppOperation: DeveloperServicesOperation {
// re-assign rather than using updateEntitlements since the latter will
// retain unrecognized entitlements
let filtered = try entitlements.entitlements().filter { $0.anyCapability?.isFree != false }
if let groups = filtered.lazy.compactMap({ $0 as? AppGroupEntitlement }).first?.rawValue,
groups.count > 1 {
throw Error.tooManyAppGroups(bundleID: bundleID, groups: groups)
}
entitlements = try Entitlements(entitlements: filtered)
}
} else {
@@ -73,23 +73,9 @@ extension DataProtectionEntitlement: EntitlementWithCapability {
extension AppGroupEntitlement: EntitlementWithCapability {
var capability: DeveloperServicesCapability {
DeveloperServicesCapability(.appGroups, isFree: true)
}
}
extension AppleSignInEntitlement: EntitlementWithCapability {
var capability: DeveloperServicesCapability {
// A primary App ID: the app is its own Sign in with Apple group.
DeveloperServicesCapability(
.appleIdAuth,
isFree: false,
settings: [
.init(
key: .init(.appleIdAuthAppConsent),
options: [.init(key: .init(.primaryAppConsent))]
)
]
)
// FIXME: Enable app groups on free accounts
// but we need to always assign only one group
DeveloperServicesCapability(.appGroups, isFree: false)
}
}
@@ -25,8 +25,8 @@ public struct DeveloperAPIPages<Page>: AsyncSequence {
public var getNext: @Sendable (Page) -> String?
public init(
_ getNext: @escaping @Sendable (Page) -> String?,
request: @escaping @Sendable () async throws -> Page,
next getNext: @escaping @Sendable (Page) -> String?
) {
self.request = request
self.getNext = getNext
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import OpenAPIRuntime
import HTTPTypes
@@ -24,7 +23,7 @@ struct LoggingMiddleware: ClientMiddleware {
) != nil
else { return (response, body) }
d.out.print("\n\(operationID) response status -> \(response.status)")
print("\n\(operationID) response status -> \(response.status)")
if let unwrapped = body {
let data = try await Data(collecting: unwrapped, upTo: .max)
@@ -32,7 +31,7 @@ struct LoggingMiddleware: ClientMiddleware {
body = .init(data)
let text = String(decoding: data, as: UTF8.self)
d.out.print("\(operationID) response body -> \(text)")
print("\(operationID) response body -> \(text)")
}
return (response, body)
@@ -49,45 +49,32 @@ public struct DeveloperServicesFetchProfileOperation: DeveloperServicesOperation
throw Errors.tooManyMatchingBundleIDs
}
// match locally rather than with filter[serialNumber]: server-side matching bugs out
// when the serial has leading zeroes (#305)
let certs = try await DeveloperAPIPages(\.links.next) {
let profiles = bundleID.relationships?.profiles?.data ?? []
switch profiles.count {
case 0:
// we're good
break
case 1:
_ = try await context.developerAPIClient.profilesDeleteInstance(path: .init(id: profiles[0].id)).noContent
default:
// if the user has >1 profile, it's probably okay to add another one (acceptable for non-free accounts?)
break
}
// Match locally rather than with filter[serialNumber]: the API lists serials with a
// leading zero nibble ("0E41…") but its filter only matches them without it ("E41…").
let certs = try await DeveloperAPIPages {
try await context.developerAPIClient.certificatesGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
.filter { $0.attributes?.serialNumber.map(signingInfo.certificate.hasSerialNumber) == true }
let allDevices = try await DeveloperAPIPages(\.links.next) {
try await context.developerAPIClient.devicesGetCollection().ok.body.json
}
.map(\.data)
.reduce(into: [], +=)
.filter {
$0.attributes?.status?.value1 != .disabled
&& $0.attributes?.deviceClass?.value1?.supportsIOSApps == true
}
try Task.checkCancellation()
let profileName = "XTL profile \(bundleID.id)"
let profiles = bundleID.relationships?.profiles?.data ?? []
if !profiles.isEmpty {
// we can't create multiple profiles with the same name, so
// if there's already one delete it first
let matchingProfiles = try await context.developerAPIClient.profilesGetCollection(query: .init(
filter_lbrack_name_rbrack_: [profileName],
filter_lbrack_id_rbrack_: profiles.map(\.id),
fields_lbrack_profiles_rbrack_: [.name],
))
let allDevices = try await context.developerAPIClient.devicesGetCollection()
.ok.body.json.data
.filter { $0.attributes?.name == profileName }
_ = try await matchingProfiles.concurrentMap {
try await context.developerAPIClient.profilesDeleteInstance(path: .init(id: $0.id)).noContent
}
}
.filter { $0.attributes?.deviceClass?.value1?.supportsIOSApps == true }
let response = try await context.developerAPIClient.profilesCreateInstance(
body: .json(
@@ -95,7 +82,7 @@ public struct DeveloperServicesFetchProfileOperation: DeveloperServicesOperation
data: .init(
_type: .profiles,
attributes: .init(
name: profileName,
name: "XTL profile \(bundleID.id)",
profileType: .init(.iosAppDevelopment)
),
relationships: .init(
@@ -4,7 +4,7 @@ import ConcurrencyExtras
import Dependencies
public protocol RawADIProvider: Sendable {
func clientInfo() async throws -> ClientInfoWithUserAgent
func clientInfo() async throws -> String
func startProvisioning(
spim: Data,
@@ -18,11 +18,6 @@ public protocol RawADIProvider: Sendable {
) async throws -> (machineID: Data, otp: Data)
}
public struct ClientInfoWithUserAgent: Sendable {
public let clientInfo: String
public let akdUserAgent: String
}
private struct UnimplementedRawADIProvider: RawADIProvider {
func startProvisioning(
spim: Data,
@@ -41,8 +36,8 @@ private struct UnimplementedRawADIProvider: RawADIProvider {
return try await closure(userID, &routingInfo, provisioningInfo)
}
func clientInfo() async throws -> ClientInfoWithUserAgent {
let closure: () async throws -> ClientInfoWithUserAgent = unimplemented()
func clientInfo() async throws -> String {
let closure: () async throws -> String = unimplemented()
return try await closure()
}
}
@@ -85,7 +80,7 @@ public struct ADIDataProvider: AnisetteDataProvider {
private let localUserUID: UUID
private let localUserID: String
private let _clientInfo = LockIsolated<ClientInfoWithUserAgent?>(nil)
private let _clientInfo = LockIsolated<String?>(nil)
public init() {
@Dependency(\.keyValueStorage) var storage
@@ -150,7 +145,7 @@ public struct ADIDataProvider: AnisetteDataProvider {
return encoder
}()
private func clientInfo() async throws -> ClientInfoWithUserAgent {
private func clientInfo() async throws -> String {
if let clientInfo = _clientInfo.value { return clientInfo }
let clientInfo = try await rawProvider.clientInfo()
_clientInfo.setValue(clientInfo)
@@ -178,7 +173,7 @@ public struct ADIDataProvider: AnisetteDataProvider {
request.headerFields[.init(key)!] = value
}
let (_, response) = try await self.httpClient.makeRequest(request, body: body, ephemeral: true)
let (_, response) = try await self.httpClient.makeRequest(request, body: body)
return try GrandSlamOperationDecoder<Response>.decode(data: response)
}
@@ -280,11 +275,9 @@ public struct ADIDataProvider: AnisetteDataProvider {
}
public func deviceInfo() async throws -> DeviceInfo {
let clientInfo = try await clientInfo()
return DeviceInfo(
DeviceInfo(
deviceID: localUserUID.uuidString,
clientInfo: clientInfo.clientInfo,
akdUserAgent: clientInfo.akdUserAgent,
clientInfo: try await clientInfo(),
)
}
@@ -10,14 +10,14 @@ import Foundation
public struct AnisetteData {
private static let clientTimeFormat = Date.ISO8601FormatStyle(
dateSeparator: .dash,
dateTimeSeparator: .standard,
timeSeparator: .colon,
timeZoneSeparator: .omitted,
includingFractionalSeconds: false,
timeZone: .gmt
)
private static let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
return dateFormatter
}()
/// `X-Apple-I-Locale`
static let iLocaleKey = "X-Apple-I-Locale"
@@ -62,7 +62,7 @@ public struct AnisetteData {
return [
Self.localeKey: localeIdentifier,
Self.timeZoneKey: timeZone.abbreviation() ?? "UTC",
Self.clientTimeKey: clientTime.formatted(Self.clientTimeFormat),
Self.clientTimeKey: Self.dateFormatter.string(from: clientTime),
Self.routingInfoKey: "\(routingInfo)",
Self.machineIDKey: machineID,
Self.localUserIDKey: localUserID,
@@ -3,6 +3,7 @@ import Foundation
public struct DeviceInfo: Codable, Sendable {
public static let xcodeVersionKey = "X-Xcode-Version"
public static let xcodeVersion = "26.5 (17F42)"
public static let akdUserAgent = "akd/1.0 CFNetwork/808.1.4"
public static let deviceIDKey = "X-Mme-Device-Id"
public let deviceID: String
@@ -10,8 +11,6 @@ public struct DeviceInfo: Codable, Sendable {
public static let clientInfoKey = "X-MMe-Client-Info"
public let clientInfo: String
public let akdUserAgent: String
public var dictionary: [String: String] {
[
Self.clientInfoKey: clientInfo,
@@ -27,19 +27,15 @@ struct OmnisetteADIProvider: RawADIProvider {
return encoder
}()
func clientInfo() async throws -> ClientInfoWithUserAgent {
func clientInfo() async throws -> String {
struct ClientInfo: Decodable {
let clientInfo: String
let userAgent: String
}
let body = try await client.makeRequest(
HTTPRequest(url: url.appendingPathComponent("v3/client_info"))
).body
let clientInfo = try Self.decoder.decode(ClientInfo.self, from: body)
return ClientInfoWithUserAgent(
clientInfo: clientInfo.clientInfo,
akdUserAgent: clientInfo.userAgent,
)
return clientInfo.clientInfo
}
func startProvisioning(spim: Data, userID: UUID) async throws -> (RawADIProvisioningSession, Data) {
+7 -7
View File
@@ -24,25 +24,25 @@ struct GrandSlamClient: Sendable {
let anisetteData = try await anisetteDataProvider.fetchAnisetteData()
let url = try await lookupManager.fetchURL(forEndpoint: R.endpoint)
let content = request.content(anisetteData: anisetteData)
var httpRequest = HTTPRequest(method: content.httpMethod, url: url)
let method = request.method(anisetteData: anisetteData)
var httpRequest = HTTPRequest(method: method.httpMethod, url: url)
httpRequest.headerFields[.contentType] = "text/x-xml-plist"
httpRequest.headerFields[.init(DeviceInfo.clientInfoKey)!] = anisetteData.deviceInfo.clientInfo
let body: Data? = switch content {
let body: Data? = switch method {
case .get:
nil
case .data(let body, _, .xml):
case .post(let body):
try PropertyListSerialization.data(
fromPropertyList: body, format: .xml, options: 0
)
case .data(let body, _, .json):
try JSONSerialization.data(withJSONObject: body)
}
request.configure(request: &httpRequest, anisetteData: anisetteData)
// GrandSlam doesn't seem to like reused (keep-alive) connections so create a
// new one for each request. cf https://github.com/rileytestut/AltSign/pull/52
let resp = try await httpClient.makeRequest(httpRequest, body: body, ephemeral: true)
let resp = try await httpClient.withEphemeralClient {
try await $0.makeRequest(httpRequest, body: body)
}
return try R.Decoder.decode(data: resp.body)
}
@@ -31,7 +31,7 @@ actor GrandSlamLookupManager {
var request = HTTPRequest(url: Self.lookupURL)
request.headerFields = [
.userAgent: deviceInfo.akdUserAgent,
.userAgent: DeviceInfo.akdUserAgent,
.init(AnisetteData.iLocaleKey)!: Locale.current.identifier,
.init(AnisetteData.timeZoneKey)!: TimeZone.current.identifier,
.init("X-Apple-I-TimeZone-Offset")!: "\(TimeZone.current.secondsFromGMT())"
@@ -42,7 +42,7 @@ actor GrandSlamLookupManager {
request.headerFields[.init(key)!] = value
}
let (_, body) = try await httpClient.makeRequest(request, ephemeral: true)
let (_, body) = try await httpClient.makeRequest(request)
return try self.decoder.decode(Response.self, from: body).urls
}
@@ -6,7 +6,6 @@
// Copyright © 2019 Kabir Oberai. All rights reserved.
//
import XUtils
import Foundation
public protocol TwoFactorAuthDelegate: AnyObject, Sendable {
@@ -27,11 +26,11 @@ struct GrandSlamTwoFactorAuthenticateOperation {
private func performSMSAuth() async throws {
// TODO: parse phone number list from o=complete req?
let phoneNumberID = 1
let phoneNumberID = "1"
do {
try await client.send(GrandSlamSMSAuthRequest(loginData: loginData, phoneNumberID: phoneNumberID))
} catch let error as HTTPResponseError where error.status.code == 403 {
d.out.print("""
print("""
warning: Could not send verification code via SMS. Falling back to trusted device authentication...
""")
try await performTrustedDeviceAuth()
@@ -40,7 +39,7 @@ struct GrandSlamTwoFactorAuthenticateOperation {
try await validateCode(phoneNumberID: phoneNumberID)
}
private func validateCode(phoneNumberID: Int? = nil) async throws {
private func validateCode(phoneNumberID: String? = nil) async throws {
guard let code = await self.delegate.fetchCode() else { throw Error.userCancelled }
let request: any GrandSlamRequest = if let phoneNumberID {
GrandSlamValidateSMSRequest(
@@ -63,7 +62,7 @@ struct GrandSlamTwoFactorAuthenticateOperation {
do {
try await client.send(request)
} catch let error as HTTPResponseError where error.status.code == 403 {
d.out.print("""
print("""
warning: Could not send verification code. Generate one manually.
See https://github.com/xtool-org/xtool/discussions/368
""")
@@ -35,22 +35,16 @@ struct GrandSlamSMSAuthRequest: GrandSlamTwoFactorRequest {
static func decode(data: Data) throws {}
}
static let endpoint: GrandSlamEndpoint = .url(URL(string: "https://gsa.apple.com/auth/verify/phone")!)
static let endpoint: GrandSlamEndpoint = .url(URL(string: "https://gsa.apple.com/auth/verify/phone/put?mode=sms")!)
let loginData: GrandSlamLoginData
let phoneNumberID: Int
let phoneNumberID: String
var extraHeaders: [String: String] {
[
"Content-Type": "application/json",
"Accept": "application/json",
]
}
func content(anisetteData: AnisetteData) -> GrandSlamRequestContent {
.put([
"phoneNumber": ["id": phoneNumberID],
"mode": "sms",
], type: .json)
func method(anisetteData: AnisetteData) -> GrandSlamMethod {
.post([
"serverInfo": [
"phoneNumber.id": phoneNumberID
],
])
}
}
@@ -29,25 +29,20 @@ struct GrandSlamValidateSMSRequest: GrandSlamTwoFactorRequest {
}
static let endpoint: GrandSlamEndpoint = .url(URL(
string: "https://gsa.apple.com/auth/verify/phone/securitycode"
string: "https://gsa.apple.com/auth/verify/phone/securitycode?referrer=/auth/verify/phone/put"
)!)
let loginData: GrandSlamLoginData
let phoneNumberID: Int
let phoneNumberID: String
let verificationCode: String
var extraHeaders: [String: String] {
[
"Content-Type": "application/json",
"Accept": "application/json",
]
}
func content(anisetteData: AnisetteData) -> GrandSlamRequestContent {
func method(anisetteData: AnisetteData) -> GrandSlamMethod {
.post([
"securityCode": ["code": verificationCode],
"phoneNumber": ["id": phoneNumberID],
"mode": "sms",
], type: .json)
"securityCode.code": verificationCode,
"serverInfo": [
"mode": "sms",
"phoneNumber.id": phoneNumberID
]
])
}
}
@@ -71,10 +71,10 @@ extension GrandSlamOperationRequest {
func configure(request: inout HTTPRequest, anisetteData: AnisetteData) {
request.headerFields[.accept] = "*/*"
request.headerFields[.userAgent] = anisetteData.deviceInfo.akdUserAgent
request.headerFields[.userAgent] = DeviceInfo.akdUserAgent
}
func content(anisetteData: AnisetteData) -> GrandSlamRequestContent {
func method(anisetteData: AnisetteData) -> GrandSlamMethod {
var clientData: [String: Any] = [
"bootstrap": true,
"icscrec": true,
@@ -7,29 +7,15 @@
//
import Foundation
import HTTPTypes
enum GrandSlamRequestContent {
enum ContentType {
case xml
case json
}
enum GrandSlamMethod {
case get
case data([String: Any], method: HTTPRequest.Method, type: ContentType = .xml)
static func post(_ body: [String: Any], type: ContentType = .xml) -> Self {
.data(body, method: .post, type: type)
}
static func put(_ body: [String: Any], type: ContentType = .xml) -> Self {
.data(body, method: .put, type: type)
}
case post([String: Any])
var httpMethod: HTTPRequest.Method {
switch self {
case .get: return .get
case .data(_, let method, _): return method
case .post: return .post
}
}
}
@@ -45,5 +31,5 @@ protocol GrandSlamRequest: Sendable {
static var endpoint: GrandSlamEndpoint { get }
func configure(request: inout HTTPRequest, anisetteData: AnisetteData)
func content(anisetteData: AnisetteData) -> GrandSlamRequestContent
func method(anisetteData: AnisetteData) -> GrandSlamMethod
}
@@ -20,7 +20,7 @@ extension GrandSlamTwoFactorRequest {
func configure(request: inout HTTPRequest, anisetteData: AnisetteData) {
request.headerFields[.accept] = "application/x-buddyml"
request.headerFields[.contentType] = "application/x-plist"
request.headerFields[.userAgent] = anisetteData.deviceInfo.akdUserAgent
request.headerFields[.userAgent] = DeviceInfo.akdUserAgent
request.headerFields[.init("X-Apple-App-Info")!] = "com.apple.gs.xcode.auth"
request.headerFields[.init(DeviceInfo.xcodeVersionKey)!] = DeviceInfo.xcodeVersion
request.headerFields[.init("X-Apple-Identity-Token")!] = loginData.identityToken
@@ -28,7 +28,7 @@ extension GrandSlamTwoFactorRequest {
extraHeaders.forEach { request.headerFields[.init($0)!] = $1 }
}
func content(anisetteData: AnisetteData) -> GrandSlamRequestContent {
func method(anisetteData: AnisetteData) -> GrandSlamMethod {
.get
}
@@ -46,21 +46,8 @@ extension HTTPClientProtocol {
_ request: HTTPRequest,
body: Data? = nil,
requireHTTPSuccess: Bool = true,
ephemeral: Bool = false,
onProgress: @isolated(any) (Double?) -> Void = { _ in }
) async throws -> (response: HTTPResponse, body: Data) {
if ephemeral {
return try await self.withEphemeralClient { client in
try await client.makeRequest(
request,
body: body,
requireHTTPSuccess: requireHTTPSuccess,
ephemeral: false,
onProgress: onProgress
)
}
}
await onProgress(0)
let (response, responseBody) = try await send(request, body: body.map { HTTPBody($0) })
guard !requireHTTPSuccess || ![.clientError, .serverError].contains(response.status.kind) else {
@@ -7,7 +7,6 @@
//
import Foundation
import XUtils
import SwiftyMobileDevice
public final class ProfileManager {
@@ -54,16 +53,16 @@ public final class ProfileManager {
for (bundleID, profiles) in toInstall {
guard profiles.count == 1 else {
d.out.print("Unexpected # of profiles for \(bundleID): \(profiles.count). Skipping.")
print("Unexpected # of profiles for \(bundleID): \(profiles.count). Skipping.")
continue
}
let profile = profiles[0]
let installedProfiles = installed[bundleID] ?? []
for installedProfile in installedProfiles {
d.out.print("Removing \(installedProfile.1.uuid) for \(bundleID)")
print("Removing \(installedProfile.1.uuid) for \(bundleID)")
try client.removeProfile(withUUID: installedProfile.1.uuid)
}
d.out.print("Installing profile with UUID \(profile.1.uuid)")
print("Installing profile with UUID \(profile.1.uuid)")
try client.install(profile: profile.0.data())
}
}
@@ -294,7 +294,7 @@ public actor IntegratedInstaller {
app: URL,
targetDevice: SigningContext.TargetDevice? = nil,
) async throws -> String {
let context = SigningContext(
let context = try SigningContext(
auth: auth,
targetDevice: targetDevice,
)
@@ -29,7 +29,6 @@ extension EntitlementContainer {
WiFiAwareEntitlement.self,
HomeKitEntitlement.self,
HealthKitEntitlement.self,
AppleSignInEntitlement.self,
]
}
@@ -100,21 +99,6 @@ public struct AssociatedDomainsEntitlement: Entitlement, RawRepresentable {
}
}
public struct AppleSignInEntitlement: Entitlement, RawRepresentable {
public static let identifier = "com.apple.developer.applesignin"
public var rawValue: [String]
public init(rawValue: [String]) { self.rawValue = rawValue }
public init(from decoder: Decoder) throws {
rawValue = try decoder.singleValueContainer().decode([String].self)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(rawValue)
}
}
public enum APSEnvironmentEntitlement: String, Entitlement {
public static let identifier = "aps-environment"
+5 -8
View File
@@ -1,17 +1,17 @@
import Foundation
import Dependencies
/// Provisions and codesigns.
public struct AutoSigner {
@Dependency(\.signer) private var signer
public enum Error: LocalizedError {
case noSigners
case errorReading(String)
case errorWriting(String)
public var errorDescription: String? {
switch self {
case .noSigners:
return NSLocalizedString("signer.error.no_signers", value: "No signers found", comment: "")
case .errorReading(let file):
return "\(file)".withCString {
String.localizedStringWithFormat(
@@ -82,16 +82,13 @@ public struct AutoSigner {
try info.mobileprovision.data().write(to: profURL)
}
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let entitlements = try provisioningDict.mapValues { try encoder.encode($0.entitlements) }
let entitlements = provisioningDict.mapValues(\.entitlements)
status(NSLocalizedString("signer.signing", value: "Signing", comment: ""))
try await signer.sign(
try await context.signer.sign(
app: app,
identity: .real(signingInfo.certificate, signingInfo.privateKey),
entitlementMapping: entitlements,
deep: true,
progress: progress
)
progress(1)
-57
View File
@@ -1,57 +0,0 @@
import Foundation
import RCodesign
public actor RCodesignSigner: Signer {
public enum Error: LocalizedError {
case badFilePath
public var errorDescription: String? {
switch self {
case .badFilePath:
return NSLocalizedString(
"signer_impl.error.bad_file_path", value: "A bad file path was provided", comment: ""
)
}
}
}
public init() {}
public func sign(
app: URL,
identity: SignerIdentity,
entitlementMapping: [URL: Data],
deep: Bool,
progress: @escaping @Sendable (Double?) -> Void
) throws {
progress(nil)
guard app.isFileURL else { throw Error.badFilePath }
let root = app.standardizedFileURL.pathComponents
var entitlements: [String: Data] = [:]
for (bundle, value) in entitlementMapping {
let components = bundle.standardizedFileURL.pathComponents
guard bundle.isFileURL, components.starts(with: root) else { throw Error.badFilePath }
// RCodesign scopes entitlements by bundle-relative path: "" for the
// app itself and e.g. "PlugIns/Widget.appex" for an extension. Compare
// path components so a sibling such as App.app-copy cannot match App.app.
let path = components.dropFirst(root.count).joined(separator: "/")
guard entitlements[path] == nil else { throw Error.badFilePath }
entitlements[path] = value
}
let signingIdentity: RCodesign.Identity
switch identity {
case .real(let certificate, let privateKey):
signingIdentity = .certificate(certificateDER: try certificate.data(), privateKeyPEM: privateKey.data)
case .adhoc:
signingIdentity = .adHoc
}
try RCodesign.sign(at: app, identity: signingIdentity, options: .init(entitlements: entitlements, shallow: !deep))
}
public func analyze(executable: URL) throws -> Data? {
try RCodesign.entitlements(at: executable)
}
}
-50
View File
@@ -1,50 +0,0 @@
import Foundation
import Dependencies
public enum SignerIdentity: Sendable {
case real(Certificate, PrivateKey)
case adhoc
}
public protocol Signer: Sendable {
// entitlementMapping is [bundle url] -> [plist data]
func sign(
app: URL,
identity: SignerIdentity,
entitlementMapping: [URL: Data],
deep: Bool,
progress: @escaping @Sendable (Double?) -> Void
) async throws
func analyze(executable: URL) async throws -> Data?
}
private enum SignerDependencyKey: DependencyKey {
static var liveValue: any Signer { RCodesignSigner() }
static var testValue: any Signer { UnimplementedSigner() }
}
extension DependencyValues {
public var signer: any Signer {
get { self[SignerDependencyKey.self] }
set { self[SignerDependencyKey.self] = newValue }
}
}
private struct UnimplementedSigner: Signer {
func sign(
app: URL,
identity: SignerIdentity,
entitlementMapping: [URL: Data],
deep: Bool,
progress: @escaping @Sendable (Double?) -> Void
) async throws {
let operation: () throws -> Void = unimplemented()
try operation()
}
func analyze(executable: URL) async throws -> Data? {
let operation: () throws -> Data? = unimplemented()
return try operation()
}
}
+178
View File
@@ -0,0 +1,178 @@
import Foundation
import CXKit
import SignerSupport
import ConcurrencyExtras
/// a wrapper around `signer_t`
public actor Signer {
public enum Error: LocalizedError {
case notFound
case badFilePath
case signer(String?)
public var errorDescription: String? {
switch self {
case .notFound:
return NSLocalizedString(
"signer_impl.error.not_found", value: "No signer implementation found", comment: ""
)
case .badFilePath:
return NSLocalizedString(
"signer_impl.error.bad_file_path", value: "A bad file path was provided", comment: ""
)
case .signer(let error?):
return error
case .signer(nil):
return NSLocalizedString(
"signer_impl.error.unknown", value: "An unknown signing error occurred", comment: ""
)
}
}
}
public enum Identity {
case real(Certificate, PrivateKey)
case adhoc
}
private let signer: UnsafePointer<signer_t>
public var name: String {
String(cString: signer.pointee.name)
}
private init(signer: UncheckedSendable<UnsafePointer<signer_t>>) {
self.signer = signer.value
}
public static func all() -> some Collection<Signer> {
var list = get_signers()
var arr: [Signer] = []
while let curr = list {
arr.append(Signer(signer: UncheckedSendable(curr)))
list = curr.pointee.next
}
return arr
}
public static func first() throws -> Signer {
try all().first.orThrow(Error.notFound)
}
private func _sign(
app: URL,
identity: Identity,
entitlementMapping: [URL: Entitlements],
progress: @escaping (Double?) -> Void
) throws {
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let entsArray: [entitlements_data_t] = try entitlementMapping.map { url, ents in
try url.withUnsafeFileSystemRepresentation { bundlePath in
guard let bundlePath = bundlePath else { throw Error.badFilePath }
return try encoder.encode(ents).withUnsafeBytes { bytes in
let copy = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: bytes.count)
bytes.copyBytes(to: copy)
// an XML plist should always be non-empty so force unwrapping baseAddress is safe
return entitlements_data_t(
bundle_path: strdup(bundlePath),
data: copy.baseAddress!,
len: copy.count
)
}
}
}
defer {
entsArray.forEach {
free(UnsafeMutablePointer(mutating: $0.bundle_path))
$0.data.deallocate()
}
}
let sendableProgress = UncheckedSendable(progress)
let sendableEntsArray = UncheckedSendable(entsArray)
let certificateData: Data
let privateKeyData: Data
let lengthOverride: Int?
switch identity {
case .real(let certificate, let privateKey):
certificateData = try certificate.data()
privateKeyData = privateKey.data
lengthOverride = nil
case .adhoc:
// we can't use empty data here because withUnsafeBytes
// might return a bufferpointer with a nil base address,
// but signer.h expects non-null values. The real fix is
// updating xtool-core/SignerSupport/signer.h to annotate
// the pointers as nullable, but alas that requires effort.
certificateData = Data([0])
privateKeyData = Data([0])
lengthOverride = 0
}
try certificateData.withUnsafeBytes { cert in
try privateKeyData.withUnsafeBytes { priv in
var exception: UnsafeMutablePointer<Int8>?
defer { exception.map { free($0) } }
let progress = sendableProgress.value
let entsArray = sendableEntsArray.value
guard let certBase = cert.baseAddress,
let privBase = priv.baseAddress
else { throw Error.signer(nil) }
let box = Unmanaged.passRetained(progress as AnyObject)
defer { box.release() }
guard signer.pointee.sign(
app.path,
certBase, lengthOverride ?? cert.count,
privBase, lengthOverride ?? priv.count,
entsArray, entsArray.count,
{
(Unmanaged<AnyObject>
.fromOpaque($0)
.takeUnretainedValue()
// swiftlint:disable:next force_cast
as! (Double?) -> Void
)($1 == -1 ? nil : $1)
},
box.toOpaque(),
&exception
) == 0 else {
throw Error.signer(exception.map { String(cString: $0) })
}
}
}
}
public func sign(
app: URL,
identity: Identity,
entitlementMapping: [URL: Entitlements],
progress: @escaping (Double?) -> Void
) async throws {
// TODO: #131: update Zupersign to handle multiple entitlements files
// this is a temp hack to just pass along the root app entitlements
let entitlementMapping = entitlementMapping.filter { $0.key.path == app.path }
try self._sign(
app: app,
identity: identity,
entitlementMapping: entitlementMapping,
progress: progress
)
}
public func analyze(executable: URL) throws -> Data {
try executable.withUnsafeFileSystemRepresentation { (path: UnsafePointer<Int8>?) -> Data in
guard let path = path else { throw Error.badFilePath }
var exception: UnsafeMutablePointer<Int8>?
defer { exception.map { free($0) } }
var count = 0
guard let bytes = signer.pointee.analyze(path, &count, &exception)
else { throw Error.signer(exception.map { String(cString: $0) })}
return Data(bytesNoCopy: bytes, count: count, deallocator: .free)
}
}
}
+5 -2
View File
@@ -18,6 +18,7 @@ public struct SigningContext: Sendable {
public var auth: DeveloperAPIAuthData
public var targetDevice: TargetDevice?
public var signer: Signer
public var developerAPIClient: DeveloperAPIClient {
DeveloperAPIClient(auth: auth)
@@ -25,10 +26,12 @@ public struct SigningContext: Sendable {
public init(
auth: DeveloperAPIAuthData,
targetDevice: TargetDevice? = nil
) {
targetDevice: TargetDevice? = nil,
signer: Signer? = nil
) throws {
self.auth = auth
self.targetDevice = targetDevice
self.signer = try signer ?? .first()
}
}
@@ -9,7 +9,6 @@
import Foundation
import ConcurrencyExtras
import Dependencies
import XUtils
public enum KeyValueStorageError: Error {
case stringConversionFailure
+9 -11
View File
@@ -1,5 +1,3 @@
import XUtils
import XCLIUtils
import Foundation
import XKit
import ArgumentParser
@@ -29,7 +27,7 @@ struct AuthOperation {
func run() async throws {
if let token = try? AuthToken.saved(), !logoutFromExisting {
if !quiet {
d.out.print("Logged in.\n\(token)")
print("Logged in.\n\(token)")
}
return
}
@@ -54,7 +52,7 @@ struct AuthOperation {
}
try token.save()
d.out.print("Logged in.\n\(token)")
print("Logged in.\n\(token)")
}
private func logInWithKey() async throws -> AuthToken {
@@ -78,7 +76,7 @@ struct AuthOperation {
}
private func logInWithPassword() async throws -> AuthToken {
d.out.print("""
print("""
Heads up:
This works by emulating a virtual Mac to interact with Apple services.
We use a third-party service to create a randomized device ID token
@@ -98,7 +96,7 @@ struct AuthOperation {
throw Console.Error("Password cannot be empty.")
}
d.out.print("Logging in...")
print("Logging in...")
let authDelegate = XToolAuthDelegate()
let manager = DeveloperServicesLoginManager()
@@ -177,15 +175,15 @@ struct AuthLogoutCommand: AsyncParsableCommand {
func run() async throws {
if (try? AuthToken.saved()) != nil {
try AuthToken.clear()
d.out.print("Logged out")
print("Logged out")
} else {
d.out.print("Already logged out")
print("Already logged out")
}
if reset2FA {
@Dependency(\.anisetteDataProvider) var anisetteProvider
await anisetteProvider.resetProvisioning()
d.out.print("Forgot device")
print("Forgot device")
}
}
}
@@ -198,9 +196,9 @@ struct AuthStatusCommand: AsyncParsableCommand {
func run() async throws {
if let token = try? AuthToken.saved() {
d.out.print("Logged in.\n\(token)")
print("Logged in.\n\(token)")
} else {
d.out.print("Logged out")
print("Logged out")
}
}
}
-2
View File
@@ -1,5 +1,3 @@
import XCLIUtils
import XUtils
import Foundation
import XKit
import Dependencies
+1 -3
View File
@@ -1,5 +1,3 @@
import XUtils
import XCLIUtils
import Foundation
import XKit
import ArgumentParser
@@ -19,7 +17,7 @@ struct ConnectionOptions: ParsableArguments {
extension ConnectionOptions.WithoutSearchMode {
func client(searchMode search: ClientDevice.SearchMode) async throws -> ClientDevice {
d.out.print("Waiting for device to be connected...")
print("Waiting for device to be connected...")
let stream = try await ClientDevice.search(mode: search)
for await devices in stream {
@@ -1,15 +1,48 @@
import Foundation
import XKit
import XUtils
import Dependencies
import NIOPosix
import NIOCore
package enum Console {
package static func prompt(_ message: String) async throws -> String {
d.out.print(message, terminator: "", flush: true)
guard let line = try await d.input.readLine() else { throw Console.Error("Input ended before a response was received.") }
return line
enum Console {
private static func withStdio<T>(
_ body: (
_ stdin: NIOAsyncChannelInboundStream<ByteBuffer>,
_ stdout: NIOAsyncChannelOutboundWriter<ByteBuffer>
) async throws -> T
) async throws -> T {
try await NIOPipeBootstrap(group: .singletonMultiThreadedEventLoopGroup)
.takingOwnershipOfDescriptors(
input: FileDescriptor.standardInput.duplicate().rawValue,
output: FileDescriptor.standardOutput.duplicate().rawValue
)
.flatMapThrowing { try NIOAsyncChannel(wrappingChannelSynchronously: $0) }
.get()
.executeThenClose { try await body($0, $1) }
}
package static func promptRequired(_ message: String, existing: String?) async throws -> String {
static func prompt(_ message: String) async throws -> String {
try await withStdio { stdin, stdout in
try await stdout.write(ByteBuffer(bytes: message.utf8))
fflush(stdoutSafe)
var data = Data()
for try await chunk in stdin {
let view = chunk.readableBytesView
if let endIndex = view.firstIndex(of: UInt8(ascii: "\n")) {
data += view[..<endIndex]
break
} else {
data += view
}
}
let str = String(decoding: data, as: UTF8.self)
return str.hasSuffix("\r") ? String(str.dropLast()) : str
}
}
static func promptRequired(_ message: String, existing: String?) async throws -> String {
let value: String
if let existing {
value = existing
@@ -22,12 +55,12 @@ package enum Console {
return value
}
package static func getPassword(_ message: String) async throws -> String {
static func getPassword(_ message: String) async throws -> String {
if !message.isEmpty {
d.out.print(message, terminator: "")
print(message, terminator: "")
}
let password = try await withoutEcho { try await prompt("") }
d.out.print()
print()
return password
}
@@ -58,7 +91,7 @@ package enum Console {
#endif
}
package static func chooseNumber(in range: Range<Int>) async throws -> Int {
static func chooseNumber(in range: Range<Int>) async throws -> Int {
let message = "Choice (\(range.lowerBound)-\(range.upperBound - 1)): "
while true {
if let choice = try await Int(prompt(message)), range.contains(choice) {
@@ -67,7 +100,7 @@ package enum Console {
}
}
package static func choose<T>(
static func choose<T>(
from elements: [T],
onNoElement: () throws -> T,
multiPrompt: @autoclosure () -> String,
@@ -79,9 +112,9 @@ package enum Console {
case 1:
return elements[0]
default:
d.out.print(multiPrompt())
print(multiPrompt())
try elements.enumerated().forEach { index, element in
try d.out.print("\(index): \(formatter(element))")
try print("\(index): \(formatter(element))")
}
let choice = try await chooseNumber(in: elements.indices)
return elements[choice]
@@ -91,7 +124,7 @@ package enum Console {
private static let yesSet: Set<String> = ["yes", "y"]
private static let noSet: Set<String> = ["no", "n"]
package static func confirm(_ message: String) async throws -> Bool {
static func confirm(_ message: String) async throws -> Bool {
while true {
let resp = try await prompt("\(message) (yes/no): ").lowercased()
if yesSet.contains(resp) {
@@ -102,9 +135,9 @@ package enum Console {
}
}
package struct Error: Swift.Error, CustomStringConvertible {
package let description: String
package init(_ description: String) {
struct Error: Swift.Error, CustomStringConvertible {
let description: String
init(_ description: String) {
self.description = description
}
}
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import ArgumentParser
import XKit
@@ -25,64 +24,66 @@ struct DSCertificatesListCommand: AsyncParsableCommand {
func run() async throws {
let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
let certificates = try await DeveloperAPIPages(\.links.next) {
let certificates = try await DeveloperAPIPages {
try await client.certificatesGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
for certificate in certificates {
d.out.print("- id: \(certificate.id)")
print("- id: \(certificate.id)")
guard let attributes = certificate.attributes else {
continue
}
if let name = attributes.name {
d.out.print(" name: \(name)")
print(" name: \(name)")
}
if let certificateType = attributes.certificateType {
d.out.print(" type: \(certificateType.rawValue)")
print(" type: \(certificateType.rawValue)")
}
if let displayName = attributes.displayName {
d.out.print(" display name: \(displayName)")
print(" display name: \(displayName)")
}
if let serialNumber = attributes.serialNumber {
d.out.print(" serial number: \(serialNumber)")
print(" serial number: \(serialNumber)")
}
if let platform = attributes.platform {
d.out.print(" platform: \(platform.rawValue)")
print(" platform: \(platform.rawValue)")
}
if let expirationDate = attributes.expirationDate {
d.out.print(" expiry: \(expirationDate.formatted(.dateTime))")
print(" expiry: \(expirationDate.formatted(.dateTime))")
}
if let contentString = attributes.certificateContent {
guard let content = Data(base64Encoded: contentString) else {
d.out.print(" content: error: bad base64")
print(" content: error: bad base64")
continue
}
do {
let certificate = try Certificate(data: content)
d.out.print(" content:")
print(" content:")
switch Result(catching: { try certificate.developerIdentity() }) {
case .success(let id):
d.out.print(" common name: \(id)")
print(" common name: \(id)")
case .failure(let error):
d.out.print(" common name: error: \(error)")
print(" common name: error: \(error)")
}
switch Result(catching: { try certificate.teamID() }) {
case .success(let teamID):
d.out.print(" team id: \(teamID)")
print(" team id: \(teamID)")
case .failure(let error):
d.out.print(" team id: error: \(error)")
print(" team id: error: \(error)")
}
} catch {
d.out.print(" content: error: \(error)")
print(" content: error: \(error)")
}
}
}
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import XKit
import ArgumentParser
@@ -14,7 +13,7 @@ struct DSAnisetteCommand: AsyncParsableCommand {
@Dependency(\.anisetteDataProvider) var provider
let res = try await provider.fetchAnisetteData()
for (key, value) in res.dictionary.sorted(by: { $0.key < $1.key }) {
d.out.print("\(key): \(value)")
print("\(key): \(value)")
}
}
}
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import ArgumentParser
import XKit
@@ -24,44 +23,46 @@ struct DSDevicesListCommand: AsyncParsableCommand {
func run() async throws {
let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
let devices = try await DeveloperAPIPages(\.links.next) {
let devices = try await DeveloperAPIPages {
try await client.devicesGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
for device in devices {
d.out.print("- id: \(device.id)")
print("- id: \(device.id)")
guard let attributes = device.attributes else {
continue
}
if let name = attributes.name {
d.out.print(" name: \(name)")
print(" name: \(name)")
}
if let platform = attributes.platform {
d.out.print(" platform: \(platform.rawValue)")
print(" platform: \(platform.rawValue)")
}
if let udid = attributes.udid {
d.out.print(" udid: \(udid)")
print(" udid: \(udid)")
}
if let deviceClass = attributes.deviceClass {
d.out.print(" device class: \(deviceClass.rawValue)")
print(" device class: \(deviceClass.rawValue)")
}
if let status = attributes.status {
d.out.print(" status: \(status.rawValue)")
print(" status: \(status.rawValue)")
}
if let model = attributes.model {
d.out.print(" model: \(model)")
print(" model: \(model)")
}
if let addedDate = attributes.addedDate {
d.out.print(" added date: \(addedDate.formatted(.dateTime))")
print(" added date: \(addedDate.formatted(.dateTime))")
}
}
}
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import ArgumentParser
import XKit
@@ -24,28 +23,30 @@ struct DSIdentifiersListCommand: AsyncParsableCommand {
func run() async throws {
let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
let bundleIDs = try await DeveloperAPIPages(\.links.next) {
let bundleIDs = try await DeveloperAPIPages {
try await client.bundleIdsGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
for bundleID in bundleIDs {
d.out.print("- id: \(bundleID.id)")
print("- id: \(bundleID.id)")
guard let attributes = bundleID.attributes else {
continue
}
if let name = attributes.name {
d.out.print(" name: \(name)")
print(" name: \(name)")
}
if let identifier = attributes.identifier {
d.out.print(" identifier: \(identifier)")
print(" identifier: \(identifier)")
}
if let platform = attributes.platform {
d.out.print(" platform: \(platform.rawValue)")
print(" platform: \(platform.rawValue)")
}
if let seedId = attributes.seedId {
d.out.print(" seedId: \(seedId)")
print(" seedId: \(seedId)")
}
}
}
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import ArgumentParser
import XKit
@@ -27,71 +26,73 @@ struct DSProfilesListCommand: AsyncParsableCommand {
func run() async throws {
let client = DeveloperAPIClient(auth: try AuthToken.saved().authData())
let profiles = try await DeveloperAPIPages(\.links.next) {
let profiles = try await DeveloperAPIPages {
try await client.profilesGetCollection().ok.body.json
} next: {
$0.links.next
}
.map(\.data)
.reduce(into: [], +=)
for profile in profiles {
d.out.print("- id: \(profile.id)")
print("- id: \(profile.id)")
guard let attributes = profile.attributes else {
continue
}
if let name = attributes.name {
d.out.print(" name: \(name)")
print(" name: \(name)")
}
if let platform = attributes.platform {
d.out.print(" platform: \(platform.rawValue)")
print(" platform: \(platform.rawValue)")
}
if let profileType = attributes.profileType {
d.out.print(" profile type: \(profileType.rawValue)")
print(" profile type: \(profileType.rawValue)")
}
if let profileState = attributes.profileState {
d.out.print(" profile state: \(profileState.rawValue)")
print(" profile state: \(profileState.rawValue)")
}
if let uuid = attributes.uuid {
d.out.print(" uuid: \(uuid)")
print(" uuid: \(uuid)")
}
if let createdDate = attributes.createdDate {
d.out.print(" created date: \(createdDate.formatted(.dateTime))")
print(" created date: \(createdDate.formatted(.dateTime))")
}
if let expirationDate = attributes.expirationDate {
d.out.print(" expiration date: \(expirationDate.formatted(.dateTime))")
print(" expiration date: \(expirationDate.formatted(.dateTime))")
}
if let contentString = attributes.profileContent {
guard let contentData = Data(base64Encoded: contentString) else {
d.out.print(" content: error: bad base64")
print(" content: error: bad base64")
continue
}
do {
let profile = try Mobileprovision(data: contentData)
let digest = try profile.digest()
d.out.print(" team identifiers:")
print(" team identifiers:")
for teamID in digest.teamIdentifiers {
d.out.print(" - \(teamID.rawValue)")
print(" - \(teamID.rawValue)")
}
d.out.print(" certificates:")
print(" certificates:")
for certificate in digest.certificates {
d.out.print(" - \(certificate.serialNumber())")
print(" - \(certificate.serialNumber())")
}
d.out.print(" devices:")
print(" devices:")
for device in digest.devices {
d.out.print(" - \(device)")
print(" - \(device)")
}
let entitlements = try? String(decoding: Self.encoder.encode(digest.entitlements), as: UTF8.self)
d.out.print(" entitlements: \(entitlements ?? "<error>")")
print(" entitlements: \(entitlements ?? "<error>")")
} catch {
d.out.print(" content: error: \(error)")
print(" content: error: \(error)")
}
}
}
@@ -1,5 +1,3 @@
import XCLIUtils
import XUtils
import Foundation
import ArgumentParser
import XKit
@@ -29,7 +27,7 @@ struct DSTeamsListCommand: AsyncParsableCommand {
}
let client = DeveloperServicesClient(authData: authData)
let teams: [DeveloperServicesTeam] = try await client.send(DeveloperServicesListTeamsRequest())
d.out.print(
print(
teams.map {
"\($0.name) [\($0.status)]: \($0.id.rawValue)" +
$0.memberships.map { "\n- \($0.name) (\($0.platform))" }.joined()
-2
View File
@@ -1,5 +1,3 @@
import XCLIUtils
import XUtils
import ArgumentParser
import Foundation
import XKit
+19 -19
View File
@@ -1,4 +1,3 @@
import XCLIUtils
import Foundation
import ArgumentParser
import PackLib
@@ -39,7 +38,7 @@ struct PackOperation {
try await EnsureSDKOperation(quiet: true).run()
d.out.print("Planning...")
print("Planning...")
let schema: PackSchema
let configPath = URL(fileURLWithPath: "xtool.yml")
@@ -47,7 +46,7 @@ struct PackOperation {
schema = try await PackSchema(url: configPath)
} else {
schema = .default
d.out.print("""
print("""
warning: Could not locate configuration file '\(configPath.path)'. Using default \
configuration with 'com.example' organization ID.
""")
@@ -81,21 +80,22 @@ struct PackOperation {
.allProducts
.compactMap { p in p.entitlementsPath.map { (p, $0) } }
if !productsWithEntitlements.isEmpty {
let mapping = try await withThrowingTaskGroup(of: (URL, Data).self) { group in
let mapping = try await withThrowingTaskGroup(of: (URL, Entitlements).self) { group in
for (product, path) in productsWithEntitlements {
group.addTask {
let entitlements = try await Data(reading: URL(fileURLWithPath: path))
let data = try await Data(reading: URL(fileURLWithPath: path))
let decoder = PropertyListDecoder()
let entitlements = try decoder.decode(Entitlements.self, from: data)
return (product.directory(inApp: bundle), entitlements)
}
}
return try await group.reduce(into: [:]) { $0[$1.0] = $1.1 }
}
d.out.print("Applying entitlements...")
try await d.signer.sign(
print("Applying entitlements...")
try await Signer.first().sign(
app: bundle,
identity: .adhoc,
entitlementMapping: mapping,
deep: true,
progress: { _ in }
)
}
@@ -112,7 +112,7 @@ struct DevXcodeCommand: AsyncParsableCommand {
func run() async throws {
let result = try await PackOperation(xcode: true).run()
d.out.print("Wrote to \(result.path)")
print("Wrote to \(result.path)")
}
}
@@ -167,12 +167,12 @@ struct DevBuildCommand: AsyncParsableCommand {
delegate: installDelegate
)
do {
defer { d.out.print() }
defer { print() }
try await installer.signInPlace(app: url)
} catch let error as CancellationError {
throw error
} catch {
d.out.print("Error: \(error)")
print("Error: \(error)")
throw ExitCode.failure
}
}
@@ -187,19 +187,19 @@ struct DevBuildCommand: AsyncParsableCommand {
let ipaURL = try await compressor.compress(directory: payloadDir) { progress in
if let progress {
let percent = Int(progress * 100)
d.out.print("\rPackaging... \(percent)%", terminator: "")
print("\rPackaging... \(percent)%", terminator: "")
} else {
d.out.print("\rPackaging...", terminator: "")
print("\rPackaging...", terminator: "")
}
}
d.out.print()
print()
try? FileManager.default.removeItem(at: finalURL)
try FileManager.default.moveItem(at: ipaURL, to: finalURL)
} else {
finalURL = url
}
d.out.print("Wrote to \(finalURL.path)")
print("Wrote to \(finalURL.path)")
}
}
@@ -251,7 +251,7 @@ struct DevRunCommand: AsyncParsableCommand {
let token = try AuthToken.saved()
let client = try await connectionOptions.client()
d.out.print("Installing to device: \(client.deviceName) (udid: \(client.udid))")
print("Installing to device: \(client.deviceName) (udid: \(client.udid))")
let installDelegate = XToolInstallerDelegate()
let installer = IntegratedInstaller(
@@ -259,7 +259,7 @@ struct DevRunCommand: AsyncParsableCommand {
delegate: installDelegate
)
defer { d.out.print() }
defer { print() }
do {
try await installer.install(
@@ -271,7 +271,7 @@ struct DevRunCommand: AsyncParsableCommand {
} catch let error as CancellationError {
throw error
} catch {
d.out.print("\nError: \(error)")
print("\nError: \(error)")
throw ExitCode.failure
}
}
@@ -309,7 +309,7 @@ struct SimInstallOperation {
output: .discarded
)
.checkSuccess()
d.out.print("Installed to simulator")
print("Installed to simulator")
}
}
#endif
+2 -3
View File
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import XKit
import ArgumentParser
@@ -32,10 +31,10 @@ struct DevicesCommand: AsyncParsableCommand {
guard wait else { break }
if shouldPrint {
shouldPrint = false
d.out.print("Waiting for devices to be connected...")
print("Waiting for devices to be connected...")
}
}
d.out.print(foundDevices.map { "\($0.deviceName) [\($0.connectionType)]: \($0.udid)" }.joined(separator: "\n"))
print(foundDevices.map { "\($0.deviceName) [\($0.connectionType)]: \($0.udid)" }.joined(separator: "\n"))
}
}
+3 -4
View File
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import XKit
import SwiftyMobileDevice
@@ -21,7 +20,7 @@ struct InstallCommand: AsyncParsableCommand {
let client = try await connectionOptions.client()
d.out.print("Installing to device: \(client.deviceName) (udid: \(client.udid))")
print("Installing to device: \(client.deviceName) (udid: \(client.udid))")
let installDelegate = XToolInstallerDelegate()
let installer = IntegratedInstaller(
@@ -36,11 +35,11 @@ struct InstallCommand: AsyncParsableCommand {
lookupMode: .only(client.connectionType),
configureDevice: false,
)
d.out.print("\nSuccessfully installed!")
print("\nSuccessfully installed!")
} catch let error as CancellationError {
throw error
} catch {
d.out.print("\nError: \(error)")
print("\nError: \(error)")
throw ExitCode.failure
}
}
+2 -3
View File
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import PackLib
@@ -35,7 +34,7 @@ enum LSPConfig {
do {
try await ensure(using: schema, quiet: quiet, currentDirectory: directory)
} catch {
d.out.print("warning: failed to validate LSP configuration: \(error)")
print("warning: failed to validate LSP configuration: \(error)")
}
}
@@ -81,7 +80,7 @@ enum LSPConfig {
}
if !quiet {
d.out.print("""
print("""
[LSP] Created \(primary.0) for enhanced IDE support. You may
need to restart your editor for the change to take effect.
You can disable this with `skipLSP: true` in xtool.yml.
+1 -3
View File
@@ -1,5 +1,3 @@
import XCLIUtils
import XUtils
import Foundation
import XKit
import SwiftyMobileDevice
@@ -35,7 +33,7 @@ struct LaunchCommand: AsyncParsableCommand {
throw Console.Error("Could not find an installed app with bundle ID '\(bundleID)'")
}
d.out.print("Launching \(executable.lastPathComponent)...")
print("Launching \(executable.lastPathComponent)...")
let debugserver = try DebugserverClient(device: client.device, label: "xtool")
guard try debugserver.launch(executable: executable, arguments: args) == "OK" else {
+5 -7
View File
@@ -1,5 +1,3 @@
import XUtils
import XCLIUtils
import ArgumentParser
import Foundation
import PackLib
@@ -63,7 +61,7 @@ struct NewCommand: AsyncParsableCommand {
throw Console.Error("Cannot create \(name): a file already exists at that path.")
}
d.out.print("Creating package: \(name)")
print("Creating package: \(name)")
let moduleName = name.replacingOccurrences(of: "-", with: "_")
@@ -164,7 +162,7 @@ struct NewCommand: AsyncParsableCommand {
at: url.deletingLastPathComponent(),
withIntermediateDirectories: true
)
d.out.print("Creating \(path)")
print("Creating \(path)")
try "\(contents)\n".write(to: url, atomically: true, encoding: .utf8)
}
@@ -174,8 +172,8 @@ struct NewCommand: AsyncParsableCommand {
currentDirectory: baseURL,
)
d.out.print("\nFinished generating project \(name). Next steps:")
d.out.print("- Enter the directory with `cd \(name)`")
d.out.print("- Build and run with `xtool dev`")
print("\nFinished generating project \(name). Next steps:")
print("- Enter the directory with `cd \(name)`")
print("- Build and run with `xtool dev`")
}
}
+20 -39
View File
@@ -1,4 +1,3 @@
import XCLIUtils
import Foundation
import Dependencies
import libunxip
@@ -136,13 +135,13 @@ struct SDKBuilder {
let iPhoneSimSDK = try sdk(platform: "iPhoneSimulator", prefix: "iPhoneSimulator")
let macOSSDK = try sdk(platform: "MacOSX", prefix: "MacOSX")
d.out.print("""
print("""
- \(iPhoneOSSDK)
- \(iPhoneSimSDK)
- \(macOSSDK)
""")
d.out.print("[Writing metadata]")
print("[Writing metadata]")
try """
{
"schemaVersion": "1.0",
@@ -245,33 +244,11 @@ struct SDKBuilder {
)
if let envOverride, let path = ProcessInfo.processInfo.environment[envOverride] {
let source = URL(fileURLWithPath: path)
if source.dirExists {
d.out.print("[Copying \(displayName)] \(source.path)")
try await Subprocess.run(
.name("cp"),
arguments: ["-RL", "\(source.path)/.", "."],
workingDirectory: FilePath(directory),
output: .discarded,
error: .discarded,
)
.checkSuccess()
// later steps write into the copy
try await Subprocess.run(
.name("chmod"),
arguments: ["-R", "u+w", "."],
workingDirectory: FilePath(directory),
output: .discarded,
error: .discarded,
)
.checkSuccess()
return
}
d.out.print("[Extracting \(displayName)] \(source.path)")
let archive = URL(fileURLWithPath: path)
print("[Extracting \(displayName)] \(archive.path)")
try await Subprocess.run(
.name("tar"),
arguments: ["xzf", source.path],
arguments: ["xzf", archive.path],
workingDirectory: FilePath(directory),
output: .discarded,
error: .discarded,
@@ -290,7 +267,7 @@ struct SDKBuilder {
case .unknown: nil
}
defer { d.out.print() }
defer { print() }
try await Subprocess.run(
.name("tar"),
arguments: ["xzf", "-"],
@@ -309,7 +286,8 @@ struct SDKBuilder {
guard let length else { continue }
let progress = Int(Double(totalWritten) / Double(length) * 100)
d.out.print("\r[Downloading \(displayName)] \(progress)%", terminator: "", flush: true)
print("\r[Downloading \(displayName)] \(progress)%", terminator: "")
fflush(stdoutSafe)
}
}
try await execution.standardInputWriter.finish()
@@ -525,7 +503,7 @@ struct SDKBuilder {
let sourceContentsDir = source.appendingPathComponent("Contents")
let expectedContentsDir = expectedAppDir.appendingPathComponent("Contents")
try FileManager.default.createDirectory(at: expectedContentsDir, withIntermediateDirectories: true)
d.out.print("[Copying Xcode.app] This might take a minute...")
print("[Copying Xcode.app] This might take a minute...")
for child in Self.expectedXcodeChildren {
let expectedFile = expectedContentsDir.appendingPathComponent(child)
let sourceFile = sourceContentsDir.appendingPathComponent(child)
@@ -559,11 +537,13 @@ struct SDKBuilder {
count += 1
if let wanted {
let progress = Int(Double(count) / Double(wanted) * 100)
d.out.print("\r[Installing SDKs] \(progress)%", terminator: "", flush: true)
print("\r[Installing SDKs] \(progress)%", terminator: "")
fflush(stdoutSafe)
}
if count % 100 == 0 {
if wanted == nil {
d.out.print("\r[Installing SDKs] Installed \(count) files", terminator: "", flush: true)
print("\r[Installing SDKs] Installed \(count) files", terminator: "")
fflush(stdoutSafe)
}
await Task.yield()
}
@@ -587,16 +567,16 @@ struct SDKBuilder {
if wanted != nil {
// otherwise the last log might say 99%
d.out.print("\r[Installing SDKs] 100%", terminator: "")
print("\r[Installing SDKs] 100%", terminator: "")
}
d.out.print()
print()
if let cleanupStageDir {
d.out.print("[Cleaning up]")
print("[Cleaning up]")
try FileManager.default.removeItem(at: cleanupStageDir)
}
d.out.print("[Finalizing SDKs]")
print("[Finalizing SDKs]")
for platform in ["iPhoneOS", "MacOSX", "iPhoneSimulator"] {
let lib = "../../../../../Library"
@@ -747,7 +727,8 @@ struct SDKBuilder {
for try await chunk in observer {
read += chunk.count
let progress = Int(Double(read) / Double(length) * 100)
d.out.print("\r[Extracting XIP] \(progress)%", terminator: "", flush: true)
print("\r[Extracting XIP] \(progress)%", terminator: "")
fflush(stdoutSafe)
if read == length { break }
}
}()
@@ -783,7 +764,7 @@ struct SDKBuilder {
}
_ = try await readTask
d.out.print()
print()
return wanted
}
+17 -18
View File
@@ -1,4 +1,3 @@
import XCLIUtils
import Foundation
import XKit
import ArgumentParser
@@ -52,7 +51,7 @@ struct DevSDKBuildCommand: AsyncParsableCommand {
let output = URL(fileURLWithPath: outputDir, isDirectory: true).appending(path: "darwin.xtoolsdk")
let builder = SDKBuilder(input: input, output: output, arch: builderArch, mode: .buildSlim)
try await builder.buildSDK()
d.out.print("Built SDK at \(output.path). You can install it with `xtool sdk install`.")
print("Built SDK at \(output.path). You can install it with `xtool sdk install`.")
}
}
@@ -123,7 +122,7 @@ struct DevSDKRemoveCommand: AsyncParsableCommand {
throw Console.Error("Cannot remove SDK: no Darwin SDK installed")
}
try sdk.remove()
d.out.print("Uninstalled SDK")
print("Uninstalled SDK")
}
}
@@ -135,12 +134,12 @@ struct DevSDKStatusCommand: AsyncParsableCommand {
func run() async throws {
if let sdk = try DarwinSDK.current() {
d.out.print("Darwin SDK is installed")
d.out.print(" Path: \(sdk.bundle.path)")
d.out.print(" Flavor: \(sdk.flavor)")
d.out.print(" Version: \(sdk.version)")
print("Darwin SDK is installed")
print(" Path: \(sdk.bundle.path)")
print(" Flavor: \(sdk.flavor)")
print(" Version: \(sdk.version)")
} else {
d.out.print("Not installed")
print("Not installed")
}
}
}
@@ -157,14 +156,14 @@ struct EnsureSDKOperation {
func run() async throws {
#if os(macOS)
if !quiet {
d.out.print("Skipping Darwin SDK setup since we're on macOS.")
print("Skipping Darwin SDK setup since we're on macOS.")
}
#else
let sdk = try DarwinSDK.current()
switch sdk.map({ ($0.isUpToDate(), $0.flavor) }) {
case (true, _)?: // swiftlint:disable:this optional_enum_case_matching
if !quiet {
d.out.print("Darwin SDK is up to date.")
print("Darwin SDK is up to date.")
}
case (false, .slim)?: // swiftlint:disable:this optional_enum_case_matching
throw Console.Error("""
@@ -175,10 +174,10 @@ struct EnsureSDKOperation {
xtool sdk install [--slim]
""")
case (false, .normal)?: // swiftlint:disable:this optional_enum_case_matching
d.out.print("Darwin SDK is out of date. Rebuilding...")
print("Darwin SDK is out of date. Rebuilding...")
try await UpdateSDKOperation().run()
case (false, .legacy)?: // swiftlint:disable:this optional_enum_case_matching
d.out.print("""
print("""
Darwin SDK is incompatible: built with an older version of xtool.
Requesting re-install. This is a one-time rebuild; after this,
@@ -187,7 +186,7 @@ struct EnsureSDKOperation {
""")
try await generateSDK()
case nil:
d.out.print("Now generating the Darwin SDK.\n")
print("Now generating the Darwin SDK.\n")
try await generateSDK()
}
@@ -218,13 +217,13 @@ struct InstallSDKOperation {
func run() async throws {
#if os(macOS)
d.out.print("Skipping SDK install; the iOS SDK ships with Xcode on macOS")
print("Skipping SDK install; the iOS SDK ships with Xcode on macOS")
#else
let temporaryBundle = try DarwinSDK.prepareTemporaryBundle()
if path.hasSuffix(".xtoolsdk") {
d.out.print("Installing prebuilt SDK...")
print("Installing prebuilt SDK...")
try await FileManager.default.copyItem(at: URL(filePath: path), to: temporaryBundle.url, preserveOwner: false)
} else {
// validate input before removing existing SDK
@@ -237,7 +236,7 @@ struct InstallSDKOperation {
}
if let sdk = try DarwinSDK.current() {
d.out.print("Removing existing SDK...")
print("Removing existing SDK...")
try sdk.remove()
}
@@ -249,7 +248,7 @@ struct InstallSDKOperation {
struct UpdateSDKOperation {
func run() async throws {
#if os(macOS)
d.out.print("Skipping SDK install; the iOS SDK ships with Xcode on macOS")
print("Skipping SDK install; the iOS SDK ships with Xcode on macOS")
#else
guard let existing = try DarwinSDK.current() else {
throw Console.Error("Could not locate existing SDK; cannot perform update.")
@@ -281,7 +280,7 @@ struct UpdateSDKOperation {
try existing.remove()
try await temporaryBundle.install()
d.out.print("Updated SDK")
print("Updated SDK")
#endif
}
}
+2 -3
View File
@@ -1,4 +1,3 @@
import XUtils
import Foundation
import XKit
import SwiftyMobileDevice
@@ -25,9 +24,9 @@ struct UninstallCommand: AsyncParsableCommand {
progress: { _ in }
)
} catch {
d.out.print("Failed: \(error)")
print("Failed: \(error)")
return
}
d.out.print("Success!")
print("Success!")
}
}
+1 -5
View File
@@ -1,9 +1,5 @@
import CXKit
import XCLIUtils
extension XTool {
public static let version = CommandVersion(
version: String(cString: xtl_version()),
hasUncommittedChanges: xtl_has_uncommitted_changes(),
)
public static let version = String(cString: xtl_version())
}
+3 -7
View File
@@ -1,9 +1,7 @@
import XCLIUtils
import Foundation
import XKit
import ArgumentParser
import XUtils
import Dependencies
public enum XTool {
public static func run(arguments: [String]? = nil) async throws {
@@ -58,8 +56,6 @@ extension ParsableCommand where Self: SendableMetatype {
for await _ in canStart {}
guard !Task.isCancelled else { return }
do {
@Dependency(\.shutdownRegistry) var shutdownRegistry
defer { await shutdownRegistry.shutdown() }
var command = try self.parseAsRoot(arguments)
if var asyncCommand = command as? AsyncParsableCommand {
try await asyncCommand.run()
@@ -67,12 +63,12 @@ extension ParsableCommand where Self: SendableMetatype {
try command.run()
}
} catch where error is CancellationError || Task.isCancelled {
d.out.print("Cancelled.", stream: .stderr)
print("Cancelled.")
self.exit()
} catch {
if ProcessInfo.processInfo.environment["XTL_DEBUG_ERRORS"] != nil {
d.out.print("ERROR DETAILS:")
d.out.print(String(reflecting: error))
print("ERROR DETAILS:")
print(String(reflecting: error))
}
self.exit(withError: error)
}
@@ -1,9 +1,6 @@
import XCLIUtils
import Foundation
import XKit
import ConcurrencyExtras
import Dependencies
import XUtils
final class XToolAuthDelegate: TwoFactorAuthDelegate {
func fetchCode() async -> String? {
@@ -12,8 +9,7 @@ final class XToolAuthDelegate: TwoFactorAuthDelegate {
}
actor XToolInstallerDelegate: IntegratedInstallerDelegate {
private nonisolated let _output = Dependency(\.out)
private nonisolated var output: CommandOutput { _output.wrappedValue }
init() {}
private let updateTask = LockIsolated<Task<Void, Never>?>(nil)
@@ -27,7 +23,8 @@ actor XToolInstallerDelegate: IntegratedInstallerDelegate {
case nil:
text = "Continuing..."
}
output.print("\n\(text)", terminator: "", flush: true)
print("\n\(text)", terminator: "")
fflush(stdoutSafe)
}
var prevStage: String?
@@ -55,15 +52,19 @@ actor XToolInstallerDelegate: IntegratedInstallerDelegate {
if stage != prevStage {
if let progString = progString {
output.print("\n[\(stage)] \(progString)", terminator: "", flush: true)
print("\n[\(stage)] \(progString)", terminator: "")
fflush(stdoutSafe)
} else {
output.print("\n[\(stage)] ...", terminator: "", flush: true)
print("\n[\(stage)] ...", terminator: "")
fflush(stdoutSafe)
}
} else if progString != prevProgress {
if let progString = progString {
output.print("\r[\(stage)] \(progString)", terminator: "", flush: true)
print("\r[\(stage)] \(progString)", terminator: "")
fflush(stdoutSafe)
} else {
output.print("\r[\(stage)]", terminator: "", flush: true)
print("\r[\(stage)]", terminator: "")
fflush(stdoutSafe)
}
}
}
@@ -78,8 +79,8 @@ actor XToolInstallerDelegate: IntegratedInstallerDelegate {
}
func confirmRevocation(of certificates: [DeveloperServicesCertificate]) async -> Bool {
output.print("\nThe following certificates must be revoked:")
output.print(
print("\nThe following certificates must be revoked:")
print(
certificates.map {
"- \($0.attributes!.name!) (expires \($0.attributes!.expirationDate!.formatted(date: .abbreviated, time: .shortened)))"
}.joined(separator: "\n")
-79
View File
@@ -1,79 +0,0 @@
// swiftlint:disable prefer_dependency_print
import Dependencies
import Foundation
import CXKit
public struct CommandOutput: DependencyKey, Sendable {
public enum Stream: String, Codable, Sendable {
case stdout
case stderr
}
public var write: @Sendable (_ data: Data, _ stream: Stream, _ flush: Bool) -> Void
public init(write: @escaping @Sendable (Data, Stream, Bool) -> Void) {
self.write = write
}
public func print(
_ message: String = "",
terminator: String = "\n",
stream: Stream = .stdout,
flush: Bool = false
) {
write(Data((message + terminator).utf8), stream, flush)
}
@_disfavoredOverload
public func print(
_ items: Any...,
separator: String = " ",
terminator: String = "\n",
stream: Stream = .stdout,
flush: Bool = false
) {
print(
items.map { String(describing: $0) }.joined(separator: separator),
terminator: terminator,
stream: stream,
flush: flush
)
}
public static var liveValue: Self {
let outputLock = NSLock()
return Self { data, stream, flush in
let descriptor: FileDescriptor = stream == .stdout ? .standardOutput : .standardError
// Keep concurrent messages together, including across partial writes.
outputLock.withLock {
try? data.withUnsafeBytes { try descriptor.writeAllWaiting($0) }
if flush { fflush(get_stdout()) }
}
}
}
public static var testValue: Self { Self(write: unimplemented("CommandOutput.write")) }
}
extension CommandOutput {
/// Create a `FileDescriptor` for writing to the given `CommandOutput` stream.
public func makeDescriptor(for stream: Stream) throws -> FileDescriptor {
let (readFD, writeFD) = try FileDescriptor.pipe()
let input = try DispatchInput(fileDesciptor: readFD)
// no need to keep a handle to the task, it'll terminate when writeFD is closed
Task {
while let next = try? await input.read() {
self.write(next, stream, false)
}
}
return writeFD
}
}
extension DependencyValues {
public var out: CommandOutput {
get { self[CommandOutput.self] }
set { self[CommandOutput.self] = newValue }
}
}
-132
View File
@@ -1,132 +0,0 @@
import Foundation
import ConcurrencyExtras
/// A bi-directional channel for streaming data from producer -> consumer.
///
/// Supports backpressure via `onRequest`.
public struct DataChannel: Sendable {
enum Phase {
case active
case closed(Error?)
}
public enum Errors: Error {
case concurrentRead
}
public struct Limit: Sendable {
public var limit: @Sendable (Data) throws -> Int
public init(limit: @escaping @Sendable (Data) throws -> Int) {
self.limit = limit
}
public static var unlimited: Limit {
Limit { _ in .max }
}
public static func bytes(_ count: Int) -> Limit {
Limit { _ in count }
}
public static var newline: Limit {
Limit { buffer in
if let newlineIndex = buffer.firstIndex(of: UInt8(ascii: "\n")) {
return buffer.distance(from: buffer.startIndex, to: newlineIndex) + 1
} else {
return .max
}
}
}
}
private struct State {
var waiter: (@Sendable () -> Void)?
var phase: Phase = .active
var buffer = Data()
var request: (@Sendable () -> Void)?
}
private let state = LockIsolated(State())
public init() {}
public func onRequest(_ request: @escaping @Sendable () -> Void) {
self.state.withValue { $0.request = request }
}
public func write(_ data: Data) {
let waiter: (@Sendable () -> Void)? = self.state.withValue { state in
guard case .active = state.phase else { return nil }
state.buffer.append(contentsOf: data)
return state.waiter.take()
}
waiter?()
}
public func finish(error: Error?) {
let waiter: (@Sendable () -> Void)? = self.state.withValue { state in
guard case .active = state.phase else { return nil }
state.phase = .closed(error)
return state.waiter.take()
}
waiter?()
}
private enum Outcome<T: Sendable>: Sendable {
case result(Result<T?, Error>)
case callRequest((@Sendable () -> Void)?)
}
nonisolated(nonsending) public func read<T: Sendable>(drain: @Sendable (inout Data) throws -> T) async throws -> T? {
while true {
try Task.checkCancellation()
let result: Result<T?, Error>? = await withTaskCancellationHandler {
await withCheckedContinuation { continuation in
let outcome: Outcome<T> = state.withValue { state in
guard !Task.isCancelled else {
return .result(.failure(CancellationError()))
}
if !state.buffer.isEmpty {
return .result(Result { try drain(&state.buffer) })
}
switch state.phase {
case .active:
guard state.waiter == nil else {
return .result(.failure(Errors.concurrentRead))
}
state.waiter = { continuation.resume(returning: nil) }
return .callRequest(state.request)
case .closed(nil):
return .result(.success(nil))
case .closed(let error?):
return .result(.failure(error))
}
}
switch outcome {
case .result(let result):
continuation.resume(returning: result)
case .callRequest(let request):
request?()
}
}
} onCancel: {
self.state.withValue { $0.waiter.take()?() }
}
if let result {
return try result.get()
}
}
}
nonisolated(nonsending) public func read(upTo limit: Limit = .unlimited) async throws -> Data? {
try await self.read { buffer in
let count = min(try limit.limit(buffer), buffer.count)
guard count > 0 else { return Data() }
let result = buffer.prefix(count)
buffer.removeFirst(count)
return result
}
}
}
-10
View File
@@ -1,10 +0,0 @@
import Dependencies
/// The current `DependencyValues`.
///
/// Prefer adding `@Dependency` fields to your type when possible, as
/// they remember the value assigned during initialization.
public var d: DependencyValues {
@Dependency(\.self) var values
return values
}
-141
View File
@@ -1,141 +0,0 @@
import Foundation
import ConcurrencyExtras
public final class DispatchInput: Sendable {
private struct State {
var hasClosed = false
var isReading = false
}
public static let defaultChunkSize: Int = 64 * 1024
let queue = DispatchQueue(label: "xtool.dispatch-reader")
let channel: DispatchIO
let chunkSize: Int
private let closeSignal: Task<Void, Never>
private let dataChannel = DataChannel()
private let state = LockIsolated(State())
public init(fileDesciptor: FileDescriptor, chunkSize: Int = DispatchInput.defaultChunkSize) throws {
let closeSignal = Task { _ = try? await Task.never() }
self.closeSignal = closeSignal
self.chunkSize = chunkSize
enum InitState {
case pending
case done(onClose: @Sendable (Int32) -> Void)
case failed(Int32)
}
let initState = LockIsolated<InitState>(.pending)
self.channel = DispatchIO(
type: .stream,
fileDescriptor: fileDesciptor.rawValue,
queue: queue,
) { err in
initState.withValue { state in
switch state {
case .pending:
state = .failed(err)
case .done(let onClose):
onClose(err)
case .failed: // wat
break
}
}
}
channel.setLimit(lowWater: 1)
try initState.withValue { state in
switch state {
case .pending:
state = .done { [weak self] err in
self?.didClose(code: err)
closeSignal.cancel()
}
case .done: // wat
break
case .failed(let err):
throw Errno(rawValue: err)
}
}
dataChannel.onRequest { [weak self] in
self?.onRequest()
}
}
nonisolated(nonsending) public func read(upTo limit: DataChannel.Limit = .unlimited) async throws -> Data? {
try await dataChannel.read(upTo: limit)
}
private func onRequest() {
let didMarkStarted = state.withValue { state in
guard !state.isReading else { return false }
state.isReading = true
return true
}
guard didMarkStarted else { return }
channel.read(
offset: 0,
length: chunkSize,
queue: queue,
) { [weak self] done, data, error in
guard let self else { return }
let data = data.map { Data($0) }
self.onRead(
done: done,
data: data,
error: error,
)
}
}
private func onRead(
done: Bool,
data: Data?,
error: Int32,
) {
if done {
state.withValue { $0.isReading = false }
}
let data = data.map { Data($0) }
if let data, !data.isEmpty {
dataChannel.write(data)
}
if error != 0 {
requestClose(error: Errno(rawValue: error))
} else if done, data?.isEmpty == true {
requestClose(error: nil)
}
}
nonisolated(nonsending) public func close() async {
requestClose(error: nil)
await closeSignal.value
}
private func requestClose(error: Error?) {
guard markClosedIfNeeded() else { return }
channel.close(flags: .stop)
dataChannel.finish(error: error)
}
private func didClose(code: Int32) {
guard markClosedIfNeeded() else { return }
dataChannel.finish(error: code == 0 ? nil : Errno(rawValue: code))
}
private func markClosedIfNeeded() -> Bool {
state.withValue { state in
let hadClosed = state.hasClosed
state.hasClosed = true
return !hadClosed
}
}
deinit {
guard !state.withValue(\.hasClosed) else { return }
channel.close(flags: .stop)
dataChannel.finish(error: nil)
}
}
+1 -1
View File
@@ -43,7 +43,7 @@ extension FileManager {
do {
try result.checkSuccess()
} catch {
d.out.print("Error copying \(srcURL.path) to \(dstURL.path): \(result.standardError)")
print("Error copying \(srcURL.path) to \(dstURL.path): \(result.standardError)")
throw error
}
return
-66
View File
@@ -1,66 +0,0 @@
extension Sequence where Element: Sendable {
/// Like `map`, but runs transformations concurrently.
///
/// - Parameter maxConcurrency: The maximum number of transforms to run concurrently.
/// If `nil`, we choose a reasonable default value. Use `.max` to run all at once.
///
/// - Parameter transform: The transformation to apply to each element.
///
/// - Returns: Transformed elements in the same order as the original sequence.
public nonisolated(nonsending) func concurrentMap<T: Sendable, E: Error>(
maxConcurrency: Int? = nil,
_ transform: @escaping @Sendable @isolated(any) (Element) async throws(E) -> T,
) async throws(E) -> [T] {
try await _concurrentMap(
maxConcurrency: maxConcurrency ?? 10,
transform,
willWaitForNext: nil,
)
}
nonisolated(nonsending) func _concurrentMap<T: Sendable, E: Error>(
maxConcurrency: Int,
_ transform: @escaping @Sendable @isolated(any) (Element) async throws(E) -> T,
willWaitForNext: (() -> Void)?, // hook for testing
) async throws(E) -> [T] {
precondition(maxConcurrency > 0, "maxConcurrency must be > 0")
return try await withTaskGroup(of: (Int, Result<T, E>).self) { group -> Result<[T], E> in
var results: [T?] = []
results.reserveCapacity(self.underestimatedCount)
var iterator = makeIterator()
var exhausted = false
var inFlight = 0
while true {
while !exhausted && inFlight < maxConcurrency {
guard let element = iterator.next() else {
exhausted = true
break
}
let index = results.count
results.append(nil)
group.addTask {
let result = await Result { () async throws(E) in
try await transform(element)
}
return (index, result)
}
inFlight += 1
}
willWaitForNext?()
guard let (index, result) = await group.next() else { break }
inFlight -= 1
switch result {
case .success(let value):
results[index] = value
case .failure(let error):
group.cancelAll()
return .failure(error)
}
}
// we can't use `as! [T]` because it breaks nested optionals (Foo??)
return .success(results.map { $0! })
}
.get()
}
}
-17
View File
@@ -54,21 +54,4 @@ extension Environment {
)
}
}
extension OutputProtocol where Self == FileDescriptorOutput {
/// Writes subprocess output to `d.out`'s stdout
public static func xtoolOutput() throws -> Self {
try makeXToolOutput(for: .stdout)
}
/// Writes subprocess output to `d.out`'s stderr
public static func xtoolError() throws -> Self {
try makeXToolOutput(for: .stderr)
}
private static func makeXToolOutput(for stream: CommandOutput.Stream) throws -> Self {
let fd = try d.out.makeDescriptor(for: stream)
return .fileDescriptor(fd, closeAfterSpawningProcess: true)
}
}
#endif
+2 -29
View File
@@ -28,34 +28,7 @@ extension FilePath {
#endif
extension FileDescriptor {
/// Writes every byte, waiting for capacity if the descriptor is nonblocking.
package func writeAllWaiting(_ bytes: UnsafeRawBufferPointer) throws {
var offset = 0
while offset < bytes.count {
do {
// Swift System retries EINTR; retain the offset when a write is partial.
let count = try write(UnsafeRawBufferPointer(rebasing: bytes[offset...]))
guard count > 0 else { throw Errno.ioError }
offset += count
} catch let error as Errno where error == .resourceTemporarilyUnavailable || error == .wouldBlock {
var descriptor = pollfd(fd: rawValue, events: Int16(POLLOUT), revents: 0)
while true {
try Task.checkCancellation()
// periodically wake up to check for cancellation
let result = poll(&descriptor, 1, 100)
if result > 0 { break }
if result == -1 {
let error = Errno(rawValue: errno)
guard error != .interrupted else { continue }
throw error
}
}
// Retry write to report terminal errors (including POLLERR/POLLNVAL).
}
}
}
public enum LockMode {
enum LockMode {
case shared
case exclusive
@@ -67,7 +40,7 @@ extension FileDescriptor {
}
}
public func tryLock(mode: LockMode) throws -> Bool {
func tryLock(mode: LockMode) throws -> Bool {
if flock(rawValue, mode.raw | LOCK_NB) == 0 {
return true
}
+18 -1
View File
@@ -71,7 +71,24 @@ private struct TemporaryDirectoryRoot {
}
private init() {
let url = XToolCache.directory
let url: URL
let env = ProcessInfo.processInfo.environment
if let tmpdir = env["XTL_TMPDIR"] ?? env["TMPDIR"] {
url = URL(fileURLWithPath: tmpdir).appendingPathComponent("sh.xtool")
} else {
#if os(Linux)
// On Linux, /tmp is commonly a tmpfs mount while ~/.swiftpm lives on ext4.
// Foundation's FileManager.copyItem uses sendfile(2) internally, which returns
// EINVAL when copying between different filesystem types (e.g. tmpfs → ext4).
// This causes `swift sdk install` to fail on distros like Linux Mint (#181).
// Using a cache dir on the home filesystem avoids the cross-fs copy entirely.
let xdgCache = env["XDG_CACHE_HOME"].map { URL(fileURLWithPath: $0) }
?? FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".cache")
url = xdgCache.appendingPathComponent("xtool")
#else
url = FileManager.default.temporaryDirectory.appendingPathComponent("sh.xtool")
#endif
}
Self.pruneOrphans(in: url)
-22
View File
@@ -1,22 +0,0 @@
import Foundation
package enum XToolCache {
package static var directory: URL {
let env = ProcessInfo.processInfo.environment
if let tmpdir = env["XTL_TMPDIR"], !tmpdir.isEmpty {
return URL(fileURLWithPath: tmpdir).appendingPathComponent("sh.xtool")
}
#if os(Linux)
let xdgCache: URL
if let xdgCachePath = env["XDG_CACHE_HOME"],
(xdgCachePath as NSString).isAbsolutePath {
xdgCache = URL(filePath: xdgCachePath)
} else {
xdgCache = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(".cache")
}
return xdgCache.appending(component: "xtool")
#else
return URL.cachesDirectory.appending(component: "sh.xtool")
#endif
}
}
+1 -2
View File
@@ -2,10 +2,9 @@ import Foundation
private import XcodeProj
extension XCSchema.Project {
public func pbxprojRepresentation(name: String, projectDirPath: String = "") throws -> Data {
public func pbxprojRepresentation(name: String) throws -> Data {
let xcprojData = try self.jsonRepresentation()
let pbxproj = try PBXProj(xcprojData: xcprojData, projectName: name)
try pbxproj.rootProject()?.projectDirPath = projectDirPath
// dataRepresentation should never return nil
return try pbxproj.dataRepresentation() ?? Data()
}
+8 -43
View File
@@ -1,5 +1,4 @@
import Foundation
private import XcodeProj
@_exported import XcodeProjectFormat
// This library is a lightweight layer on top of the XcodeProjectFormat
@@ -17,31 +16,13 @@ public struct XcodeProject: Equatable, Sendable {
public var project: XCSchema.Project
public var format: Format
/// The .xcodeproj bundle's location, including for projects constructed in memory.
public var url: URL
/// For pbxproj projects, an explicit source root.
public var projectDirPath = ""
public var sourceRoot: URL {
if projectDirPath.isEmpty {
url.deletingLastPathComponent()
} else {
URL(
filePath: projectDirPath,
directoryHint: .isDirectory,
relativeTo: url.deletingLastPathComponent(),
).standardizedFileURL
}
}
public init(
project: XCSchema.Project,
url: URL,
format: Format = .pbxproj,
) {
self.project = project
self.format = format
self.url = url.standardizedFileURL.resolvingSymlinksInPath()
}
/// Reads the project from the given `.xcodeproj` path.
@@ -49,8 +30,6 @@ public struct XcodeProject: Equatable, Sendable {
guard url.pathExtension == "xcodeproj" else {
throw Errors.notAnXcodeproj(url.path)
}
let url = url.standardizedFileURL.resolvingSymlinksInPath()
self.url = url
let xcprojURL = url.appending(path: "project.xcproj")
lazy var pbxprojURL = url.appending(path: "project.pbxproj")
if FileManager.default.fileExists(atPath: xcprojURL.path) {
@@ -59,9 +38,7 @@ public struct XcodeProject: Equatable, Sendable {
self.format = .xcproj
} else if FileManager.default.fileExists(atPath: pbxprojURL.path) {
let data = try Data(contentsOf: pbxprojURL)
let pbxproj = try PBXProj(data: data)
self.project = try XCSchema.Project(jsonRepresentation: pbxproj.xcprojData())
self.projectDirPath = try pbxproj.rootProject()?.projectDirPath ?? ""
self.project = try XCSchema.Project(pbxprojRepresentation: data)
self.format = .pbxproj
} else {
throw Errors.missingProjectFile(url.path)
@@ -75,39 +52,27 @@ public struct XcodeProject: Equatable, Sendable {
return copy
}
/// Returns a copy at a new bundle location.
public func withURL(_ url: URL) -> Self {
var copy = self
copy.url = url.standardizedFileURL.resolvingSymlinksInPath()
return copy
}
/// Writes the project to its current `.xcodeproj` URL.
public func write() throws {
/// Writes the project to the given `.xcodeproj` path.
public func write(to url: URL) throws {
try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
switch format {
case .pbxproj:
try project.pbxprojRepresentation(
name: url.deletingPathExtension().lastPathComponent,
projectDirPath: projectDirPath,
).write(to: url.appendingPathComponent("project.pbxproj"))
try project
.pbxprojRepresentation(name: url.deletingPathExtension().lastPathComponent)
.write(to: url.appendingPathComponent("project.pbxproj"))
case .xcproj:
guard projectDirPath.isEmpty else {
throw Errors.xcprojWithExplicitSourceRoot
}
try project
.jsonRepresentation()
.write(to: url.appendingPathComponent("project.xcproj"))
}
let workspaceURL = url.appendingPathComponent("project.xcworkspace")
try XcodeWorkspace(children: [.file(.current)], url: workspaceURL).write()
try XcodeWorkspace(children: [.file(.current)])
.write(to: url.appendingPathComponent("project.xcworkspace"))
}
public enum Errors: Error {
case notAnXcodeproj(String)
case missingProjectFile(String)
case xcprojWithExplicitSourceRoot
}
}
@@ -4,15 +4,14 @@ private import XcodeProj
extension XcodeWorkspace {
public init(url: URL) throws {
let workspace = try XCWorkspace(pathString: url.path)
self.init(workspace.data, url: url)
self.init(workspace.data)
}
public func dataRepresentation() throws -> Data {
try xcWorkspaceData.dataRepresentation() ?? Data()
}
/// Writes the workspace to its current URL.
public func write() throws {
public func write(to url: URL) throws {
try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
try self.dataRepresentation()
.write(to: url.appendingPathComponent("contents.xcworkspacedata"))
@@ -22,8 +21,8 @@ extension XcodeWorkspace {
// MARK: - Conversion
extension XcodeWorkspace {
fileprivate init(_ xcWorkspaceData: XCWorkspaceData, url: URL) {
self.init(children: xcWorkspaceData.children.map { .init(from: $0) }, url: url)
fileprivate init(_ xcWorkspaceData: XCWorkspaceData) {
self.init(children: xcWorkspaceData.children.map { .init(from: $0) })
}
fileprivate var xcWorkspaceData: XCWorkspaceData {
+1 -10
View File
@@ -63,18 +63,9 @@ public struct XcodeWorkspace: Hashable, Sendable {
}
public var children: [Element]
public var url: URL
public init(children: [Element], url: URL) {
public init(children: [Element]) {
self.children = children
self.url = url.standardizedFileURL.resolvingSymlinksInPath()
}
/// Returns a copy at a new workspace location.
public func withURL(_ url: URL) -> Self {
var copy = self
copy.url = url.standardizedFileURL.resolvingSymlinksInPath()
return copy
}
}
@@ -1,266 +0,0 @@
import Foundation
import Testing
import ConcurrencyExtras
import Crypto
@testable import XAppleArchive
import XUtils
// Fixtures were created with `aa archive -a raw` and
// `aea encrypt -profile 0 -sign-priv <key> -b 16k`.
private let fixtures = URL(filePath: #filePath)
.deletingLastPathComponent()
.appending(path: "Fixtures")
// swiftlint:disable:next force_try
private let fixtureKey = try! P256.Signing.PublicKey(x963Representation: Data(hex:
"04c001feac563995c90c0ba02909e5c0cf80d8da04fb19493b7bbbeceeee7cd37e"
+ "01818da73a57e8f5dadeadca2da926a9e4ca83c075f81ef0143a9522b7082369"
))
private let fixtureOptions = AEAArchive.Options(signingKey: .publicKey(fixtureKey))
private func fixture(_ name: String) -> FilePath {
FilePath(fixtures.appending(path: name).path)
}
private func withTemporaryDirectory<T>(_ body: (URL) async throws -> T) async throws -> T {
let dir = FileManager.default.temporaryDirectory
.appending(path: "xtool-aa-tests-\(UUID().uuidString)")
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
defer {
// the fixture contains a read-only directory
_ = try? FileManager.default.setAttributes(
[.posixPermissions: 0o755],
ofItemAtPath: dir.appending(path: "dir/sub").path
)
try? FileManager.default.removeItem(at: dir)
}
return try await body(dir)
}
private func checkSmallTree(at dir: URL, hardLinks: Bool = true) throws {
let fm = FileManager.default
#expect(try String(contentsOf: dir.appending(path: "hello.txt"), encoding: .utf8) == "hello world\n")
#expect(try String(contentsOf: dir.appending(path: "dir/run.sh"), encoding: .utf8) == "#!/bin/sh\necho hi\n")
#expect(try fm.destinationOfSymbolicLink(atPath: dir.appending(path: "dir/link").path) == "../hello.txt")
func attributes(_ path: String) throws -> [FileAttributeKey: Any] {
try fm.attributesOfItem(atPath: dir.appending(path: path).path)
}
#expect(try attributes("dir/run.sh")[.posixPermissions] as? Int == 0o755)
#expect(try attributes("hello.txt")[.posixPermissions] as? Int == 0o644)
#expect(try attributes("dir/sub")[.posixPermissions] as? Int == 0o555)
#expect(try attributes("dir/sub")[.type] as? FileAttributeType == .typeDirectory)
let mtime = try #require(attributes("hello.txt")[.modificationDate] as? Date)
#expect(Int(mtime.timeIntervalSince1970) == 1_793_949_863)
guard hardLinks else { return }
let original = try #require(attributes("hello.txt")[.systemFileNumber] as? Int)
let link = try #require(attributes("dir/hardlink.txt")[.systemFileNumber] as? Int)
#expect(original == link)
}
@Suite struct AppleArchiveTests {
#if canImport(Compression)
static let variants = ["lzma", "lzfse", "zlib", "lz4", "lzbitmap", "none", "padded-murmur"]
#else
static let variants = ["lzma", "none", "padded-murmur"]
#endif
@Test(arguments: variants)
func extractsFixture(variant: String) async throws {
let archive = try AEAArchive(path: fixture("small-\(variant).aea"), options: fixtureOptions)
#expect(archive.rootHeader.segmentSize == 16 << 10)
try await withTemporaryDirectory { dir in
try await archive.extract(to: FilePath(dir.path))
try checkSmallTree(at: dir)
}
}
@Test func decodesToRawArchive() async throws {
let archive = try AEAArchive(path: fixture("small-lzma.aea"), options: fixtureOptions)
try await withTemporaryDirectory { dir in
let out = dir.appending(path: "out.aar")
try await archive.decode(to: FilePath(out.path))
#expect(try Data(contentsOf: out) == Data(contentsOf: fixtures.appending(path: "small.aar")))
}
}
@Test func extractsRawArchive() async throws {
try await withTemporaryDirectory { dir in
try AppleArchiveExtractor.extract(archive: fixture("small.aar"), to: FilePath(dir.path))
try checkSmallTree(at: dir)
}
}
@Test func extractsMultipleClusters() async throws {
let archive = try AEAArchive(
path: fixture("multicluster.aea"),
options: .init(signingKey: .publicKey(fixtureKey), maxConcurrentSegments: 3)
)
let segments = archive.rootHeader.rawSize / UInt64(archive.rootHeader.segmentSize)
#expect(segments > UInt64(archive.rootHeader.segmentsPerCluster))
var reports: [AEAProgress] = []
try await withTemporaryDirectory { dir in
try await archive.extract(to: FilePath(dir.path), progress: { reports.append($0) })
let big = try Data(contentsOf: dir.appending(path: "big.bin"))
#expect(big == Data((0..<270).flatMap { Array(String(format: "%08d", $0).utf8).repeated(2048) }))
// the multi-cluster fixture was copied without preserving hard links
try checkSmallTree(at: dir, hardLinks: false)
}
#expect(reports.first?.completedBytes == 0)
#expect(reports.last?.fractionCompleted == 1)
#expect(zip(reports, reports.dropFirst()).allSatisfy { $0.completedBytes < $1.completedBytes })
}
@Test func filtersEntries() async throws {
let archive = try AEAArchive(path: fixture("small-lzma.aea"), options: fixtureOptions)
try await withTemporaryDirectory { dir in
try await archive.extract(to: FilePath(dir.path), filter: { $0 == "dir/run.sh" })
let fm = FileManager.default
#expect(fm.fileExists(atPath: dir.appending(path: "dir/run.sh").path))
#expect(!fm.fileExists(atPath: dir.appending(path: "hello.txt").path))
#expect(!fm.fileExists(atPath: dir.appending(path: "dir/sub").path))
}
}
@Test func supportsCancellation() async throws {
let archive = try AEAArchive(path: fixture("multicluster.aea"), options: fixtureOptions)
let task = Task {
var chunks = 0
try await archive.decode { _ in
chunks += 1
if chunks == 5 {
withUnsafeCurrentTask { $0?.cancel() }
}
}
return chunks
}
await #expect(throws: CancellationError.self) { try await task.value }
}
@Test func rejectsWrongSigningKey() throws {
let other = P256.Signing.PrivateKey().publicKey
#expect(throws: AEAError.self) {
_ = try AEAArchive(path: fixture("small-lzma.aea"), options: .init(signingKey: .publicKey(other)))
}
}
@Test func readsEmbeddedSigningKey() throws {
// the auth data of Xcode_27.3_beta.aea
let pem = try Data(contentsOf: fixtures.appending(path: "xcode-27.3-beta-chain.pem"))
var authData = Data()
let entry = Data(AEAArchive.certificateChainKey.utf8) + [0] + pem
authData.appendLE(UInt32(4 + entry.count))
authData.append(entry)
let key = try AEAArchive.embeddedSigningKey(authData: authData)
#expect(key.x963Representation == Data(hex:
"0462656520562A5A7EC6E277DDD29A6735E54FA102A0C68FDB679C04EC5B216B"
+ "F59AF739BD06B23732ACB347B59A22AA6FE142337E00161AD8D13CFB80DC72F084"
))
}
@Test func requiresCertificateChainByDefault() async throws {
await #expect(throws: AEAError.self) {
try await withTemporaryDirectory { dir in
try await AEAExtractor.liveValue.extract(fixture("small-lzma.aea"), to: FilePath(dir.path))
}
}
}
// offsets of the signature, root header, cluster header, and the last segment byte
@Test(arguments: [22, 239, 317, -1])
func rejectsCorruption(offset: Int) async throws {
var data = try Data(contentsOf: fixtures.appending(path: "small-lzma.aea"))
let index = offset < 0 ? data.count + offset : offset
data[index] ^= 0x01
try await withTemporaryDirectory { dir in
let path = dir.appending(path: "corrupt.aea")
try data.write(to: path)
await #expect(throws: AEAError.self) {
let archive = try AEAArchive(path: FilePath(path.path), options: fixtureOptions)
try await archive.decode { _ in }
}
}
}
@Test func rejectsPathTraversal() throws {
#expect(throws: AEAError.self) { try AppleArchiveExtractor.sanitize("../evil") }
#expect(throws: AEAError.self) { try AppleArchiveExtractor.sanitize("a/../../evil") }
#expect(throws: AEAError.self) { try AppleArchiveExtractor.sanitize("/etc/passwd") }
#expect(try AppleArchiveExtractor.sanitize("./a//b/./c") == "a/b/c")
}
@Test func rejectsWritesThroughSymlinks() async throws {
try await withTemporaryDirectory { dir in
let extractor = try AppleArchiveExtractor(destination: FilePath(dir.appending(path: "out").path))
func entry(_ type: Character, _ path: String, link: String? = nil) -> AppleArchiveHeader {
var fields: [(key: AppleArchiveHeader.Key, value: AppleArchiveHeader.Value)] = [
(.type, .uint(UInt64(type.asciiValue!))),
(.path, .string(Data(path.utf8))),
]
if let link { fields.append((.linkPath, .string(Data(link.utf8)))) }
return AppleArchiveHeader(fields: fields)
}
try extractor.handle(.header(entry("L", "escape", link: dir.path)))
try extractor.handle(.endOfEntry)
#expect(throws: AEAError.self) {
try extractor.handle(.header(entry("F", "escape/pwned")))
}
#expect(!FileManager.default.fileExists(atPath: dir.appending(path: "pwned").path))
}
}
}
/// Extracts a real Xcode `.aea`. Set `XTL_TEST_XCODE_AEA` to its path to run.
@Test(.enabled(if: ProcessInfo.processInfo.environment["XTL_TEST_XCODE_AEA"] != nil))
func extractsXcodeAEA() async throws {
let environment = ProcessInfo.processInfo.environment
let path = try #require(environment["XTL_TEST_XCODE_AEA"])
let output = environment["XTL_TEST_XCODE_AEA_OUTPUT"].map { URL(filePath: $0) }
?? FileManager.default.temporaryDirectory.appending(path: "xtool-xcode-aea-\(UUID().uuidString)")
defer {
if environment["XTL_TEST_XCODE_AEA_OUTPUT"] == nil {
try? FileManager.default.removeItem(at: output)
}
}
let start = Date()
let lastPercent = LockIsolated(-1)
try await AEAExtractor.liveValue.extract(FilePath(path), to: FilePath(output.path)) { fraction in
let percent = Int(fraction * 100)
lastPercent.withValue {
guard percent != $0 else { return }
$0 = percent
print("[Extracting AEA] \(percent)%")
}
}
print("Extracted in \(Date().timeIntervalSince(start))s")
#expect(lastPercent.value == 100)
let apps = try FileManager.default.contentsOfDirectory(atPath: output.path).filter { $0.hasSuffix(".app") }
let app = try #require(apps.first)
let plist = try Data(contentsOf: output.appending(path: "\(app)/Contents/Info.plist"))
let info = try #require(try PropertyListSerialization.propertyList(from: plist, format: nil) as? [String: Any])
#expect(info["CFBundleIdentifier"] as? String == "com.apple.dt.Xcode")
}
extension Data {
init(hex: String) {
var bytes: [UInt8] = []
var index = hex.startIndex
while index < hex.endIndex {
let next = hex.index(index, offsetBy: 2)
bytes.append(UInt8(hex[index..<next], radix: 16)!)
index = next
}
self.init(bytes)
}
}
extension Array {
fileprivate func repeated(_ count: Int) -> [Element] {
Array([[Element]](repeating: self, count: count).joined())
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More