2022-04-14 15:27:57 -07:00
|
|
|
//
|
|
|
|
|
// FetchTrustedSourcesOperation.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 4/13/22.
|
|
|
|
|
// Copyright © 2022 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2023-03-01 00:48:36 -05:00
|
|
|
private extension URL {
|
2022-04-14 15:27:57 -07:00
|
|
|
#if STAGING
|
2023-03-01 00:48:36 -05:00
|
|
|
static let trustedSources = URL(string: "https://raw.githubusercontent.com/SideStore/SideStore/develop/trustedapps.json")!
|
2022-04-14 15:27:57 -07:00
|
|
|
#else
|
2023-03-01 00:48:36 -05:00
|
|
|
static let trustedSources = URL(string: "https://raw.githubusercontent.com/SideStore/SideStore/develop/trustedapps.json")!
|
2022-04-14 15:27:57 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 19:09:33 -05:00
|
|
|
public extension FetchTrustedSourcesOperation {
|
2023-03-02 00:40:11 -05:00
|
|
|
struct TrustedSource: Decodable {
|
2023-03-01 19:09:33 -05:00
|
|
|
public var identifier: String
|
|
|
|
|
public var sourceURL: URL?
|
2022-04-14 15:27:57 -07:00
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
private struct Response: Decodable {
|
2022-04-14 15:27:57 -07:00
|
|
|
var version: Int
|
|
|
|
|
var sources: [FetchTrustedSourcesOperation.TrustedSource]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 19:09:33 -05:00
|
|
|
public final class FetchTrustedSourcesOperation: ResultOperation<[FetchTrustedSourcesOperation.TrustedSource]> {
|
|
|
|
|
public override func main() {
|
2022-04-14 15:27:57 -07:00
|
|
|
super.main()
|
2023-03-01 00:48:36 -05:00
|
|
|
|
|
|
|
|
let dataTask = URLSession.shared.dataTask(with: .trustedSources) { data, response, error in
|
|
|
|
|
do {
|
|
|
|
|
if let response = response as? HTTPURLResponse {
|
2022-04-14 18:29:34 -07:00
|
|
|
guard response.statusCode != 404 else {
|
|
|
|
|
self.finish(.failure(URLError(.fileDoesNotExist, userInfo: [NSURLErrorKey: URL.trustedSources])))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
2022-04-14 15:27:57 -07:00
|
|
|
guard let data = data else { throw error! }
|
2023-03-01 00:48:36 -05:00
|
|
|
|
2022-04-14 15:27:57 -07:00
|
|
|
let response = try Foundation.JSONDecoder().decode(Response.self, from: data)
|
|
|
|
|
self.finish(.success(response.sources))
|
2023-03-01 00:48:36 -05:00
|
|
|
} catch {
|
2022-04-14 15:27:57 -07:00
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-01 00:48:36 -05:00
|
|
|
|
2022-04-14 15:27:57 -07:00
|
|
|
dataTask.resume()
|
|
|
|
|
}
|
|
|
|
|
}
|