mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-09 06:43:25 +01:00
clean-checkpoint-1
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
//
|
||||
// AnisetteError.swift
|
||||
// AltServer
|
||||
//
|
||||
// Created by Riley Testut on 9/13/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension AnisetteError
|
||||
{
|
||||
enum Code: Int, ALTErrorCode
|
||||
{
|
||||
typealias Error = AnisetteError
|
||||
|
||||
case aosKitFailure
|
||||
case missingValue
|
||||
}
|
||||
|
||||
static func aosKitFailure(file: String = #fileID, line: UInt = #line) -> AnisetteError {
|
||||
AnisetteError(code: .aosKitFailure, sourceFile: file, sourceLine: line)
|
||||
}
|
||||
|
||||
static func missingValue(_ value: String?, file: String = #fileID, line: UInt = #line) -> AnisetteError {
|
||||
AnisetteError(code: .missingValue, value: value, sourceFile: file, sourceLine: line)
|
||||
}
|
||||
}
|
||||
|
||||
struct AnisetteError: ALTLocalizedError
|
||||
{
|
||||
var code: Code
|
||||
var errorTitle: String?
|
||||
var errorFailure: String?
|
||||
|
||||
@UserInfoValue
|
||||
var value: String?
|
||||
|
||||
var sourceFile: String?
|
||||
var sourceLine: UInt?
|
||||
|
||||
var errorFailureReason: String {
|
||||
switch self.code
|
||||
{
|
||||
case .aosKitFailure: return NSLocalizedString("AltServer could not retrieve anisette data from AOSKit.", comment: "")
|
||||
case .missingValue:
|
||||
let valueName = self.value.map { "anisette data value “\($0)”" } ?? NSLocalizedString("anisette data values.", comment: "")
|
||||
return String(format: NSLocalizedString("AltServer could not retrieve %@.", comment: ""), valueName)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
//
|
||||
// Logger+AltServer.swift
|
||||
// AltStore
|
||||
//
|
||||
// Created by Riley Testut on 9/6/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import OSLog
|
||||
|
||||
extension Logger
|
||||
{
|
||||
static let altserverSubsystem = Bundle.main.bundleIdentifier!
|
||||
|
||||
static let main = Logger(subsystem: altserverSubsystem, category: "AltServer")
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
//
|
||||
// Process+STPrivilegedTask.swift
|
||||
// AltServer
|
||||
//
|
||||
// Created by Riley Testut on 8/22/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Security
|
||||
import OSLog
|
||||
|
||||
import STPrivilegedTask
|
||||
|
||||
extension Process
|
||||
{
|
||||
class func runAsAdmin(_ program: String, arguments: [String], authorization: AuthorizationRef? = nil) throws -> AuthorizationRef?
|
||||
{
|
||||
var launchPath = "/usr/bin/" + program
|
||||
if !FileManager.default.fileExists(atPath: launchPath)
|
||||
{
|
||||
launchPath = "/bin/" + program
|
||||
}
|
||||
|
||||
if !FileManager.default.fileExists(atPath: launchPath)
|
||||
{
|
||||
launchPath = program
|
||||
}
|
||||
|
||||
Logger.main.info("Launching admin process: \(launchPath, privacy: .public)")
|
||||
|
||||
let task = STPrivilegedTask()
|
||||
task.launchPath = launchPath
|
||||
task.arguments = arguments
|
||||
task.freeAuthorizationWhenDone = false
|
||||
|
||||
let errorCode: OSStatus
|
||||
|
||||
if let authorization = authorization
|
||||
{
|
||||
errorCode = task.launch(withAuthorization: authorization)
|
||||
}
|
||||
else
|
||||
{
|
||||
errorCode = task.launch()
|
||||
}
|
||||
|
||||
let executableURL = URL(fileURLWithPath: launchPath)
|
||||
guard errorCode == 0 else { throw ProcessError.failed(executableURL: executableURL, exitCode: errorCode, output: nil) }
|
||||
|
||||
task.waitUntilExit()
|
||||
|
||||
Logger.main.info("Admin process \(launchPath, privacy: .public) terminated with exit code \(task.terminationStatus, privacy: .public).")
|
||||
|
||||
guard task.terminationStatus == 0 else {
|
||||
let executableURL = URL(fileURLWithPath: launchPath)
|
||||
|
||||
let outputData = task.outputFileHandle.readDataToEndOfFile()
|
||||
if let outputString = String(data: outputData, encoding: .utf8), !outputString.isEmpty
|
||||
{
|
||||
throw ProcessError.failed(executableURL: executableURL, exitCode: task.terminationStatus, output: outputString)
|
||||
}
|
||||
else
|
||||
{
|
||||
throw ProcessError.failed(executableURL: executableURL, exitCode: task.terminationStatus, output: nil)
|
||||
}
|
||||
}
|
||||
|
||||
return task.authorization
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
//
|
||||
// ProcessInfo+Device.swift
|
||||
// AltServer
|
||||
//
|
||||
// Created by Riley Testut on 9/13/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RegexBuilder
|
||||
|
||||
extension ProcessInfo
|
||||
{
|
||||
var deviceModel: String? {
|
||||
let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"))
|
||||
defer {
|
||||
IOObjectRelease(service)
|
||||
}
|
||||
|
||||
guard
|
||||
let modelData = IORegistryEntryCreateCFProperty(service, "model" as CFString, kCFAllocatorDefault, 0).takeRetainedValue() as? Data,
|
||||
let cDeviceModel = String(data: modelData, encoding: .utf8)?.cString(using: .utf8) // Remove trailing NULL character
|
||||
else { return nil }
|
||||
|
||||
let deviceModel = String(cString: cDeviceModel)
|
||||
return deviceModel
|
||||
}
|
||||
|
||||
var operatingSystemBuildVersion: String? {
|
||||
let osVersionString = ProcessInfo.processInfo.operatingSystemVersionString
|
||||
let buildVersion: String?
|
||||
|
||||
if #available(macOS 13, *), let match = osVersionString.firstMatch(of: Regex {
|
||||
"(Build "
|
||||
Capture {
|
||||
OneOrMore(.anyNonNewline)
|
||||
}
|
||||
")"
|
||||
})
|
||||
{
|
||||
buildVersion = String(match.1)
|
||||
}
|
||||
else if let build = osVersionString.split(separator: " ").last?.dropLast()
|
||||
{
|
||||
buildVersion = String(build)
|
||||
}
|
||||
else
|
||||
{
|
||||
buildVersion = nil
|
||||
}
|
||||
|
||||
return buildVersion
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
//
|
||||
// JITManager.swift
|
||||
// AltServer
|
||||
//
|
||||
// Created by Riley Testut on 8/30/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import RegexBuilder
|
||||
|
||||
import AltSign
|
||||
|
||||
private extension URL
|
||||
{
|
||||
static let python3 = URL(fileURLWithPath: "/usr/bin/python3")
|
||||
static let altjit = Bundle.main.executableURL!.deletingLastPathComponent().appendingPathComponent("altjit")
|
||||
}
|
||||
|
||||
class JITManager
|
||||
{
|
||||
static let shared = JITManager()
|
||||
|
||||
private let diskManager = DeveloperDiskManager()
|
||||
|
||||
private var authorization: AuthorizationRef?
|
||||
|
||||
private init()
|
||||
{
|
||||
}
|
||||
|
||||
func prepare(_ device: ALTDevice) async throws
|
||||
{
|
||||
let isMounted = try await ALTDeviceManager.shared.isDeveloperDiskImageMounted(for: device)
|
||||
guard !isMounted else { return }
|
||||
|
||||
if #available(macOS 13, *), device.osVersion.majorVersion >= 17
|
||||
{
|
||||
// iOS 17+
|
||||
try await self.installPersonalizedDeveloperDisk(onto: device)
|
||||
}
|
||||
else
|
||||
{
|
||||
try await self.installDeveloperDisk(onto: device)
|
||||
}
|
||||
}
|
||||
|
||||
func enableUnsignedCodeExecution(process: AppProcess, device: ALTDevice) async throws
|
||||
{
|
||||
try await self.prepare(device)
|
||||
|
||||
if #available(macOS 13, *), device.osVersion.majorVersion >= 17
|
||||
{
|
||||
// iOS 17+
|
||||
try await self.enableModernUnsignedCodeExecution(process: process, device: device)
|
||||
}
|
||||
else
|
||||
{
|
||||
try await self.enableLegacyUnsignedCodeExecution(process: process, device: device)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension JITManager
|
||||
{
|
||||
func installDeveloperDisk(onto device: ALTDevice) async throws
|
||||
{
|
||||
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
|
||||
self.diskManager.downloadDeveloperDisk(for: device) { (result) in
|
||||
switch result
|
||||
{
|
||||
case .failure(let error): continuation.resume(throwing: error)
|
||||
case .success((let diskFileURL, let signatureFileURL)):
|
||||
ALTDeviceManager.shared.installDeveloperDiskImage(at: diskFileURL, signatureURL: signatureFileURL, to: device) { (success, error) in
|
||||
switch Result(success, error)
|
||||
{
|
||||
case .failure(let error as ALTServerError) where error.code == .incompatibleDeveloperDisk:
|
||||
self.diskManager.setDeveloperDiskCompatible(false, with: device)
|
||||
continuation.resume(throwing: error)
|
||||
|
||||
case .failure(let error):
|
||||
// Don't mark developer disk as incompatible because it probably failed for a different reason.
|
||||
continuation.resume(throwing: error)
|
||||
|
||||
case .success:
|
||||
self.diskManager.setDeveloperDiskCompatible(true, with: device)
|
||||
continuation.resume()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func enableLegacyUnsignedCodeExecution(process: AppProcess, device: ALTDevice) async throws
|
||||
{
|
||||
let connection = try await ALTDeviceManager.shared.startDebugConnection(to: device)
|
||||
|
||||
switch process
|
||||
{
|
||||
case .name(let name): try await connection.enableUnsignedCodeExecutionForProcess(withName: name)
|
||||
case .pid(let pid): try await connection.enableUnsignedCodeExecutionForProcess(withID: pid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 13, *)
|
||||
private extension JITManager
|
||||
{
|
||||
func installPersonalizedDeveloperDisk(onto device: ALTDevice) async throws
|
||||
{
|
||||
do
|
||||
{
|
||||
_ = try await Process.launchAndWait(.altjit, arguments: ["mount", "--udid", device.identifier])
|
||||
}
|
||||
catch
|
||||
{
|
||||
try self.processAltJITError(error)
|
||||
}
|
||||
}
|
||||
|
||||
func enableModernUnsignedCodeExecution(process: AppProcess, device: ALTDevice) async throws
|
||||
{
|
||||
do
|
||||
{
|
||||
if self.authorization == nil
|
||||
{
|
||||
// runAsAdmin() only returns authorization if the process completes successfully,
|
||||
// so we request authorization for a command that can't fail, then re-use it for the failable command below.
|
||||
self.authorization = try Process.runAsAdmin("echo", arguments: ["altstore"], authorization: self.authorization)
|
||||
}
|
||||
|
||||
var arguments = ["enable"]
|
||||
switch process
|
||||
{
|
||||
case .name(let name): arguments.append(name)
|
||||
case .pid(let pid): arguments.append(String(pid))
|
||||
}
|
||||
arguments += ["--udid", device.identifier]
|
||||
|
||||
if let timeout = UserDefaults.standard.altJITTimeout
|
||||
{
|
||||
arguments += ["--timeout", String(timeout)]
|
||||
}
|
||||
|
||||
self.authorization = try Process.runAsAdmin(URL.altjit.path, arguments: arguments, authorization: self.authorization)
|
||||
}
|
||||
catch
|
||||
{
|
||||
try self.processAltJITError(error)
|
||||
}
|
||||
}
|
||||
|
||||
func processAltJITError(_ error: some Error) throws
|
||||
{
|
||||
do
|
||||
{
|
||||
throw error
|
||||
}
|
||||
catch let error as ProcessError where error.code == .failed
|
||||
{
|
||||
guard let output = error.output else { throw error }
|
||||
|
||||
let dependencyNotFoundRegex = Regex {
|
||||
"No module named"
|
||||
|
||||
OneOrMore(.whitespace)
|
||||
|
||||
Capture {
|
||||
OneOrMore(.anyNonNewline)
|
||||
}
|
||||
}
|
||||
|
||||
let deviceNotFoundRegex = Regex {
|
||||
"Device is not connected"
|
||||
}
|
||||
|
||||
if let match = output.firstMatch(of: dependencyNotFoundRegex)
|
||||
{
|
||||
let dependency = String(match.1)
|
||||
throw JITError.dependencyNotFound(dependency)
|
||||
}
|
||||
else if output.contains(deviceNotFoundRegex)
|
||||
{
|
||||
throw ALTServerError(.deviceNotFound, userInfo: [
|
||||
NSLocalizedRecoverySuggestionErrorKey: NSLocalizedString("Your device must be plugged into your computer to enable JIT on iOS 17 or later.", comment: "")
|
||||
])
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user