Files
SideStore/Shared/Server Protocol/ServerProtocol.swift
Riley Testut 3b38d725d7 [Shared] Refactors error handling based on ALTLocalizedError protocol (#1115)
* [Shared] Revises ALTLocalizedError protocol

* Refactors errors to conform to revised ALTLocalizedError protocol

* [Missing Commit] Remaining changes for ALTLocalizedError

* [AltServer] Refactors errors to conform to revised ALTLocalizedError protocol

* [Missing Commit] Declares ALTLocalizedTitleErrorKey + ALTLocalizedDescriptionKey

* Updates Objective-C errors to match revised ALTLocalizedError

* [Missing Commit] Unnecessary ALTLocalizedDescription logic

* [Shared] Refactors NSError.withLocalizedFailure to properly support ALTLocalizedError

* [Shared] Supports adding localized titles to errors via NSError.withLocalizedTitle()

* Revises ErrorResponse logic to support arbitrary errors and user info values

* [Missed Commit] Renames CodableServerError to CodableError

* Merges ConnectionError into OperationError

* [Missed Commit] Doesn’t check ALTWrappedError’s userInfo for localizedDescription

* [Missed] Fixes incorrect errorDomain for ALTErrorEnums

* [Missed] Removes nonexistent ALTWrappedError.h

* Includes source file and line number in OperationError.unknown failureReason

* Adds localizedTitle to AppManager operation errors

* Fixes adding localizedTitle + localizedFailure to ALTWrappedError

* Updates ToastView to use error’s localizedTitle as title

* [Shared] Adds NSError.formattedDetailedDescription(with:)

Returns formatted NSAttributedString containing all user info values intended for displaying to the user.

* [Shared] Updates Error.localizedErrorCode to say “code” instead of “error”

* Conforms ALTLocalizedError to CustomStringConvertible

* Adds “View More Details” option to Error Log context menu to view detailed error description

* [Shared] Fixes NSError.formattedDetailedDescription appearing black in dark mode

* [AltServer] Updates error alert to match revised error logic

Uses error’s localizedTitle as alert title.

* [AltServer] Adds “View More Details” button to error alert to view detailed error info

* [AltServer] Renames InstallError to OperationError and conforms to ALTErrorEnum

* [Shared] Removes CodableError support for Date user info values

Not currently used, and we don’t want to accidentally parse a non-Date as a Date in the meantime.

* [Shared] Includes dynamic UserInfoValueProvider values in NSError.formattedDetailedDescription()

* [Shared] Includes source file + line in NSError.formattedDetailedDescription()

Automatically captures source file + line when throwing ALTErrorEnums.

* [Shared] Captures source file + line for unknown errors

* Removes sourceFunction from OperationError

* Adds localizedTitle to AuthenticationViewController errors

* [Shared] Moves nested ALTWrappedError logic to ALTWrappedError initializer

* [AltServer] Removes now-redundant localized failure from JIT errors

All JIT errors now have a localizedTitle which effectively says the same thing.

* Makes OperationError.Code start at 1000

“Connection errors” subsection starts at 1200.

* [Shared] Updates Error domains to revised [Source].[ErrorType] format

* Updates ALTWrappedError.localizedDescription to prioritize using wrapped NSLocalizedDescription as failure reason

* Makes ALTAppleAPIError codes start at 3000

* [AltServer] Adds relevant localizedFailures to ALTDeviceManager.installApplication() errors

* Revises OperationError failureReasons and recovery suggestions

All failure reasons now read correctly when preceded by a failure reason and “because”.

* Revises ALTServerError error messages
All failure reasons now read correctly when preceded by a failure reason and “because”.

* Most failure reasons now read correctly when preceded by a failure reason and “because”.
* ALTServerErrorUnderlyingError forwards all user info provider calls to underlying error.

* Revises error messages for ALTAppleAPIErrorIncorrectCredentials

* [Missed] Removes NSError+AltStore.swift from AltStore target

* [Shared] Updates AltServerErrorDomain to revised [Source].[ErrorType] format

* [Shared] Removes “code” from Error.localizedErrorCode

* [Shared] Makes ALTServerError codes (appear to) start at 2000

We can’t change the actual error codes without breaking backwards compatibility, so instead we just add 2000 whenever we display ALTServerError codes to the user.

* Moves VerificationError.errorFailure to VerifyAppOperation

* Supports custom failure reason for OperationError.unknown

* [Shared] Changes AltServerErrorDomain to “AltServer.ServerError”

* [Shared] Converts ALTWrappedError to Objective-C class

NSError subclasses must be written in ObjC for Swift.Error <-> NSError bridging to work correctly.

# Conflicts:
#	AltStore.xcodeproj/project.pbxproj

* Fixes decoding CodableError nested user info values
2023-01-24 13:56:41 -06:00

469 lines
15 KiB
Swift

//
// ServerProtocol.swift
// AltServer
//
// Created by Riley Testut on 5/24/19.
// Copyright © 2019 Riley Testut. All rights reserved.
//
import Foundation
import AltSign
public let ALTServerServiceType = "_altserver._tcp"
protocol ServerMessageProtocol: Codable
{
var version: Int { get }
var identifier: String { get }
}
public enum ServerRequest: Decodable
{
case anisetteData(AnisetteDataRequest)
case prepareApp(PrepareAppRequest)
case beginInstallation(BeginInstallationRequest)
case installProvisioningProfiles(InstallProvisioningProfilesRequest)
case removeProvisioningProfiles(RemoveProvisioningProfilesRequest)
case removeApp(RemoveAppRequest)
case enableUnsignedCodeExecution(EnableUnsignedCodeExecutionRequest)
case unknown(identifier: String, version: Int)
var identifier: String {
switch self
{
case .anisetteData(let request): return request.identifier
case .prepareApp(let request): return request.identifier
case .beginInstallation(let request): return request.identifier
case .installProvisioningProfiles(let request): return request.identifier
case .removeProvisioningProfiles(let request): return request.identifier
case .removeApp(let request): return request.identifier
case .enableUnsignedCodeExecution(let request): return request.identifier
case .unknown(let identifier, _): return identifier
}
}
var version: Int {
switch self
{
case .anisetteData(let request): return request.version
case .prepareApp(let request): return request.version
case .beginInstallation(let request): return request.version
case .installProvisioningProfiles(let request): return request.version
case .removeProvisioningProfiles(let request): return request.version
case .removeApp(let request): return request.version
case .enableUnsignedCodeExecution(let request): return request.version
case .unknown(_, let version): return version
}
}
private enum CodingKeys: String, CodingKey
{
case identifier
case version
}
public init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)
let version = try container.decode(Int.self, forKey: .version)
let identifier = try container.decode(String.self, forKey: .identifier)
switch identifier
{
case "AnisetteDataRequest":
let request = try AnisetteDataRequest(from: decoder)
self = .anisetteData(request)
case "PrepareAppRequest":
let request = try PrepareAppRequest(from: decoder)
self = .prepareApp(request)
case "BeginInstallationRequest":
let request = try BeginInstallationRequest(from: decoder)
self = .beginInstallation(request)
case "InstallProvisioningProfilesRequest":
let request = try InstallProvisioningProfilesRequest(from: decoder)
self = .installProvisioningProfiles(request)
case "RemoveProvisioningProfilesRequest":
let request = try RemoveProvisioningProfilesRequest(from: decoder)
self = .removeProvisioningProfiles(request)
case "RemoveAppRequest":
let request = try RemoveAppRequest(from: decoder)
self = .removeApp(request)
case "EnableUnsignedCodeExecutionRequest":
let request = try EnableUnsignedCodeExecutionRequest(from: decoder)
self = .enableUnsignedCodeExecution(request)
default:
self = .unknown(identifier: identifier, version: version)
}
}
}
public enum ServerResponse: Decodable
{
case anisetteData(AnisetteDataResponse)
case installationProgress(InstallationProgressResponse)
case installProvisioningProfiles(InstallProvisioningProfilesResponse)
case removeProvisioningProfiles(RemoveProvisioningProfilesResponse)
case removeApp(RemoveAppResponse)
case enableUnsignedCodeExecution(EnableUnsignedCodeExecutionResponse)
case error(ErrorResponse)
case unknown(identifier: String, version: Int)
var identifier: String {
switch self
{
case .anisetteData(let response): return response.identifier
case .installationProgress(let response): return response.identifier
case .installProvisioningProfiles(let response): return response.identifier
case .removeProvisioningProfiles(let response): return response.identifier
case .removeApp(let response): return response.identifier
case .enableUnsignedCodeExecution(let response): return response.identifier
case .error(let response): return response.identifier
case .unknown(let identifier, _): return identifier
}
}
var version: Int {
switch self
{
case .anisetteData(let response): return response.version
case .installationProgress(let response): return response.version
case .installProvisioningProfiles(let response): return response.version
case .removeProvisioningProfiles(let response): return response.version
case .removeApp(let response): return response.version
case .enableUnsignedCodeExecution(let response): return response.version
case .error(let response): return response.version
case .unknown(_, let version): return version
}
}
private enum CodingKeys: String, CodingKey
{
case identifier
case version
}
public init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)
let version = try container.decode(Int.self, forKey: .version)
let identifier = try container.decode(String.self, forKey: .identifier)
switch identifier
{
case "AnisetteDataResponse":
let response = try AnisetteDataResponse(from: decoder)
self = .anisetteData(response)
case "InstallationProgressResponse":
let response = try InstallationProgressResponse(from: decoder)
self = .installationProgress(response)
case "InstallProvisioningProfilesResponse":
let response = try InstallProvisioningProfilesResponse(from: decoder)
self = .installProvisioningProfiles(response)
case "RemoveProvisioningProfilesResponse":
let response = try RemoveProvisioningProfilesResponse(from: decoder)
self = .removeProvisioningProfiles(response)
case "RemoveAppResponse":
let response = try RemoveAppResponse(from: decoder)
self = .removeApp(response)
case "EnableUnsignedCodeExecutionResponse":
let response = try EnableUnsignedCodeExecutionResponse(from: decoder)
self = .enableUnsignedCodeExecution(response)
case "ErrorResponse":
let response = try ErrorResponse(from: decoder)
self = .error(response)
default:
self = .unknown(identifier: identifier, version: version)
}
}
}
// _Don't_ provide generic SuccessResponse, as that would prevent us
// from easily changing response format for a request in the future.
public struct ErrorResponse: ServerMessageProtocol
{
public var version = 3
public var identifier = "ErrorResponse"
public var error: ALTServerError {
// Must be ALTServerError
return self.serverError.map { ALTServerError($0.error) } ?? ALTServerError(self.errorCode)
}
private var serverError: CodableError?
// Legacy (v1)
private var errorCode: ALTServerError.Code
public init(error: ALTServerError)
{
self.serverError = CodableError(error: error)
self.errorCode = error.code
}
}
public struct AnisetteDataRequest: ServerMessageProtocol
{
public var version = 1
public var identifier = "AnisetteDataRequest"
public init()
{
}
}
public struct AnisetteDataResponse: ServerMessageProtocol
{
public var version = 1
public var identifier = "AnisetteDataResponse"
public var anisetteData: ALTAnisetteData
private enum CodingKeys: String, CodingKey
{
case identifier
case version
case anisetteData
}
public init(anisetteData: ALTAnisetteData)
{
self.anisetteData = anisetteData
}
public init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)
self.version = try container.decode(Int.self, forKey: .version)
self.identifier = try container.decode(String.self, forKey: .identifier)
let json = try container.decode([String: String].self, forKey: .anisetteData)
if let anisetteData = ALTAnisetteData(json: json)
{
self.anisetteData = anisetteData
}
else
{
throw DecodingError.dataCorruptedError(forKey: CodingKeys.anisetteData, in: container, debugDescription: "Couuld not parse anisette data from JSON")
}
}
public func encode(to encoder: Encoder) throws
{
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.version, forKey: .version)
try container.encode(self.identifier, forKey: .identifier)
let json = self.anisetteData.json()
try container.encode(json, forKey: .anisetteData)
}
}
public struct PrepareAppRequest: ServerMessageProtocol
{
public var version = 1
public var identifier = "PrepareAppRequest"
public var udid: String
public var contentSize: Int
public var fileURL: URL?
public init(udid: String, contentSize: Int, fileURL: URL? = nil)
{
self.udid = udid
self.contentSize = contentSize
self.fileURL = fileURL
}
}
public struct BeginInstallationRequest: ServerMessageProtocol
{
public var version = 3
public var identifier = "BeginInstallationRequest"
// If activeProfiles is non-nil, then AltServer should remove all profiles except active ones.
public var activeProfiles: Set<String>?
public var bundleIdentifier: String?
public init(activeProfiles: Set<String>?, bundleIdentifier: String?)
{
self.activeProfiles = activeProfiles
self.bundleIdentifier = bundleIdentifier
}
}
public struct InstallationProgressResponse: ServerMessageProtocol
{
public var version = 1
public var identifier = "InstallationProgressResponse"
public var progress: Double
public init(progress: Double)
{
self.progress = progress
}
}
public struct InstallProvisioningProfilesRequest: ServerMessageProtocol
{
public var version = 1
public var identifier = "InstallProvisioningProfilesRequest"
public var udid: String
public var provisioningProfiles: Set<ALTProvisioningProfile>
// If activeProfiles is non-nil, then AltServer should remove all profiles except active ones.
public var activeProfiles: Set<String>?
private enum CodingKeys: String, CodingKey
{
case identifier
case version
case udid
case provisioningProfiles
case activeProfiles
}
public init(udid: String, provisioningProfiles: Set<ALTProvisioningProfile>, activeProfiles: Set<String>?)
{
self.udid = udid
self.provisioningProfiles = provisioningProfiles
self.activeProfiles = activeProfiles
}
public init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)
self.version = try container.decode(Int.self, forKey: .version)
self.identifier = try container.decode(String.self, forKey: .identifier)
self.udid = try container.decode(String.self, forKey: .udid)
let rawProvisioningProfiles = try container.decode([Data].self, forKey: .provisioningProfiles)
let provisioningProfiles = try rawProvisioningProfiles.map { (data) -> ALTProvisioningProfile in
guard let profile = ALTProvisioningProfile(data: data) else {
throw DecodingError.dataCorruptedError(forKey: CodingKeys.provisioningProfiles, in: container, debugDescription: "Could not parse provisioning profile from data.")
}
return profile
}
self.provisioningProfiles = Set(provisioningProfiles)
self.activeProfiles = try container.decodeIfPresent(Set<String>.self, forKey: .activeProfiles)
}
public func encode(to encoder: Encoder) throws
{
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.version, forKey: .version)
try container.encode(self.identifier, forKey: .identifier)
try container.encode(self.udid, forKey: .udid)
try container.encode(self.provisioningProfiles.map { $0.data }, forKey: .provisioningProfiles)
try container.encodeIfPresent(self.activeProfiles, forKey: .activeProfiles)
}
}
public struct InstallProvisioningProfilesResponse: ServerMessageProtocol
{
public var version = 1
public var identifier = "InstallProvisioningProfilesResponse"
public init()
{
}
}
public struct RemoveProvisioningProfilesRequest: ServerMessageProtocol
{
public var version = 1
public var identifier = "RemoveProvisioningProfilesRequest"
public var udid: String
public var bundleIdentifiers: Set<String>
public init(udid: String, bundleIdentifiers: Set<String>)
{
self.udid = udid
self.bundleIdentifiers = bundleIdentifiers
}
}
public struct RemoveProvisioningProfilesResponse: ServerMessageProtocol
{
public var version = 1
public var identifier = "RemoveProvisioningProfilesResponse"
public init()
{
}
}
public struct RemoveAppRequest: ServerMessageProtocol
{
public var version = 1
public var identifier = "RemoveAppRequest"
public var udid: String
public var bundleIdentifier: String
public init(udid: String, bundleIdentifier: String)
{
self.udid = udid
self.bundleIdentifier = bundleIdentifier
}
}
public struct RemoveAppResponse: ServerMessageProtocol
{
public var version = 1
public var identifier = "RemoveAppResponse"
public init()
{
}
}
public struct EnableUnsignedCodeExecutionRequest: ServerMessageProtocol
{
public var version = 1
public var identifier = "EnableUnsignedCodeExecutionRequest"
public var udid: String
public var processID: Int?
public var processName: String?
public init(udid: String, processID: Int? = nil, processName: String? = nil)
{
self.udid = udid
self.processID = processID
self.processName = processName
}
}
public struct EnableUnsignedCodeExecutionResponse: ServerMessageProtocol
{
public var version = 1
public var identifier = "EnableUnsignedCodeExecutionResponse"
public init()
{
}
}