xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / GrandSlam / Anisette / OmnisetteADIProvider.swift
OmnisetteADIProvider.swift187 lines · 5.8 KiBRaw
| 1 | import Foundation |
| 2 | import Dependencies |
| 3 | |
| 4 | struct OmnisetteADIProvider: RawADIProvider { |
| 5 | @Dependency(\.httpClient) private var client |
| 6 | |
| 7 | // should implement v3 of https://github.com/SideStore/omnisette-server |
| 8 | // list: https://servers.sidestore.io/servers.json |
| 9 | private let url: URL |
| 10 | init( |
| 11 | url: URL = URL(string: "https://mac-ani.jkcoxson.com")! |
| 12 | ) { |
| 13 | self.url = url |
| 14 | } |
| 15 | |
| 16 | static let decoder: JSONDecoder = { |
| 17 | let decoder = JSONDecoder() |
| 18 | decoder.keyDecodingStrategy = .convertFromSnakeCase |
| 19 | decoder.dataDecodingStrategy = .base64 |
| 20 | return decoder |
| 21 | }() |
| 22 | |
| 23 | static let encoder: JSONEncoder = { |
| 24 | let encoder = JSONEncoder() |
| 25 | encoder.keyEncodingStrategy = .convertToSnakeCase |
| 26 | encoder.dataEncodingStrategy = .base64 |
| 27 | return encoder |
| 28 | }() |
| 29 | |
| 30 | func clientInfo() async throws -> ClientInfoWithUserAgent { |
| 31 | struct ClientInfo: Decodable { |
| 32 | let clientInfo: String |
| 33 | let userAgent: String |
| 34 | } |
| 35 | let body = try await client.makeRequest( |
| 36 | HTTPRequest(url: url.appendingPathComponent("v3/client_info")) |
| 37 | ).body |
| 38 | let clientInfo = try Self.decoder.decode(ClientInfo.self, from: body) |
| 39 | return ClientInfoWithUserAgent( |
| 40 | clientInfo: clientInfo.clientInfo, |
| 41 | akdUserAgent: clientInfo.userAgent, |
| 42 | ) |
| 43 | } |
| 44 | |
| 45 | func startProvisioning(spim: Data, userID: UUID) async throws -> (RawADIProvisioningSession, Data) { |
| 46 | var url = url.appendingPathComponent("v3/provisioning_session") |
| 47 | var components = URLComponents(url: url, resolvingAgainstBaseURL: false)! |
| 48 | components.scheme = components.scheme == "http" ? "ws" : "wss" |
| 49 | url = components.url! |
| 50 | |
| 51 | let task = try await client.makeWebSocket(url: url) |
| 52 | let connection = OmnisetteProvisioningSession(task: task) |
| 53 | let cpim = try await connection.startProvisioning(spim: spim, userID: userID) |
| 54 | return (connection, cpim) |
| 55 | } |
| 56 | |
| 57 | func requestOTP( |
| 58 | userID: UUID, |
| 59 | routingInfo: inout UInt64, |
| 60 | provisioningInfo: Data |
| 61 | ) async throws -> (machineID: Data, otp: Data) { |
| 62 | struct Request: Encodable { |
| 63 | let identifier: Data |
| 64 | let adiPb: Data |
| 65 | } |
| 66 | |
| 67 | struct Response: Decodable { |
| 68 | var machineID: Data |
| 69 | var otp: Data |
| 70 | var rinfo: String |
| 71 | |
| 72 | private enum CodingKeys: String, CodingKey { |
| 73 | case machineID = "X-Apple-I-MD-M" |
| 74 | case otp = "X-Apple-I-MD" |
| 75 | case rinfo = "X-Apple-I-MD-RINFO" |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | var request = HTTPRequest(url: url.appendingPathComponent("v3/get_headers")) |
| 80 | request.method = .post |
| 81 | request.headerFields[.contentType] = "application/json" |
| 82 | let body = try Self.encoder.encode(Request( |
| 83 | identifier: userID.rawBytes, |
| 84 | adiPb: provisioningInfo |
| 85 | )) |
| 86 | let response = try await client.makeRequest(request, body: body).body |
| 87 | let decoded = try Self.decoder.decode(Response.self, from: response) |
| 88 | if let rinfo = UInt64(decoded.rinfo) { |
| 89 | routingInfo = rinfo |
| 90 | } |
| 91 | return (decoded.machineID, decoded.otp) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private final class OmnisetteProvisioningSession: RawADIProvisioningSession { |
| 96 | let task: WebSocketSession |
| 97 | |
| 98 | init(task: WebSocketSession) { |
| 99 | self.task = task |
| 100 | } |
| 101 | |
| 102 | deinit { |
| 103 | close() |
| 104 | } |
| 105 | |
| 106 | private func close() { |
| 107 | task.close() |
| 108 | } |
| 109 | |
| 110 | func startProvisioning( |
| 111 | spim: Data, |
| 112 | userID: UUID |
| 113 | ) async throws -> Data { |
| 114 | do { |
| 115 | struct Response: Encodable { |
| 116 | let identifier: Data |
| 117 | } |
| 118 | try await receive("GiveIdentifier") |
| 119 | try await send(Response(identifier: userID.rawBytes)) |
| 120 | } |
| 121 | |
| 122 | do { |
| 123 | struct Response: Encodable { |
| 124 | let spim: Data |
| 125 | } |
| 126 | try await receive("GiveStartProvisioningData") |
| 127 | try await send(Response(spim: spim)) |
| 128 | } |
| 129 | |
| 130 | do { |
| 131 | struct Request: Decodable { |
| 132 | let cpim: Data |
| 133 | } |
| 134 | return try await receive("GiveEndProvisioningData", as: Request.self).cpim |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func endProvisioning(routingInfo: UInt64, ptm: Data, tk: Data) async throws -> Data { |
| 139 | defer { close() } |
| 140 | do { |
| 141 | struct Response: Encodable { |
| 142 | let ptm: Data |
| 143 | let tk: Data |
| 144 | } |
| 145 | try await send(Response(ptm: ptm, tk: tk)) |
| 146 | } |
| 147 | |
| 148 | do { |
| 149 | struct Request: Decodable { |
| 150 | let adiPb: Data |
| 151 | } |
| 152 | return try await receive("ProvisioningSuccess", as: Request.self).adiPb |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private struct Header: Decodable { |
| 157 | let result: String |
| 158 | } |
| 159 | |
| 160 | @discardableResult |
| 161 | private func receive<T: Decodable>(_ message: String, as type: T.Type = EmptyResponse.self) async throws -> T { |
| 162 | let data = switch try await task.receive() { |
| 163 | case .data(let data): data |
| 164 | case .text(let text): Data(text.utf8) |
| 165 | @unknown default: Data() |
| 166 | } |
| 167 | let header = try OmnisetteADIProvider.decoder.decode(Header.self, from: data) |
| 168 | guard header.result == message else { |
| 169 | throw DecodingError.dataCorrupted(.init( |
| 170 | codingPath: [], |
| 171 | debugDescription: "Expected message '\(message)', got \(header.result)" |
| 172 | )) |
| 173 | } |
| 174 | return try OmnisetteADIProvider.decoder.decode(type, from: data) |
| 175 | } |
| 176 | |
| 177 | private func send<T: Encodable>(_ message: T) async throws { |
| 178 | let encoded = try OmnisetteADIProvider.encoder.encode(message) |
| 179 | try await task.send(.text(String(decoding: encoded, as: UTF8.self))) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | extension UUID { |
| 184 | fileprivate var rawBytes: Data { |
| 185 | withUnsafeBytes(of: uuid) { Data($0) } |
| 186 | } |
| 187 | } |