2023-05-15 16:25:25 -05:00
|
|
|
//
|
|
|
|
|
// UpdateKnownSourcesOperation.swift
|
|
|
|
|
// AltStore
|
|
|
|
|
//
|
|
|
|
|
// Created by Riley Testut on 4/13/22.
|
|
|
|
|
// Copyright © 2022 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2023-10-16 18:18:06 -05:00
|
|
|
import AltStoreCore
|
|
|
|
|
|
2023-05-15 16:25:25 -05:00
|
|
|
private extension URL
|
|
|
|
|
{
|
|
|
|
|
#if STAGING
|
|
|
|
|
static let sources = URL(string: "https://f000.backblazeb2.com/file/altstore-staging/altstore/sources.json")!
|
|
|
|
|
#else
|
|
|
|
|
static let sources = URL(string: "https://cdn.altstore.io/file/altstore/altstore/sources.json")!
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension UpdateKnownSourcesOperation
|
|
|
|
|
{
|
|
|
|
|
private struct Response: Decodable
|
|
|
|
|
{
|
|
|
|
|
var version: Int
|
|
|
|
|
|
2023-05-16 15:39:38 -05:00
|
|
|
var trusted: [KnownSource]?
|
|
|
|
|
var blocked: [KnownSource]?
|
2023-05-15 16:25:25 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 15:39:38 -05:00
|
|
|
class UpdateKnownSourcesOperation: ResultOperation<([KnownSource], [KnownSource])>
|
2023-05-15 16:25:25 -05:00
|
|
|
{
|
2023-05-29 12:10:44 -05:00
|
|
|
private let session: URLSession
|
|
|
|
|
|
|
|
|
|
override init()
|
|
|
|
|
{
|
|
|
|
|
let configuration = URLSessionConfiguration.default
|
|
|
|
|
|
|
|
|
|
if UserDefaults.standard.responseCachingDisabled
|
|
|
|
|
{
|
|
|
|
|
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
|
|
|
|
configuration.urlCache = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.session = URLSession(configuration: configuration)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 16:25:25 -05:00
|
|
|
override func main()
|
|
|
|
|
{
|
|
|
|
|
super.main()
|
|
|
|
|
|
2023-05-29 12:10:44 -05:00
|
|
|
let dataTask = self.session.dataTask(with: .sources) { (data, response, error) in
|
2023-05-15 16:25:25 -05:00
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if let response = response as? HTTPURLResponse
|
|
|
|
|
{
|
|
|
|
|
guard response.statusCode != 404 else {
|
|
|
|
|
self.finish(.failure(URLError(.fileDoesNotExist, userInfo: [NSURLErrorKey: URL.sources])))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let data = data else { throw error! }
|
|
|
|
|
|
|
|
|
|
let response = try Foundation.JSONDecoder().decode(Response.self, from: data)
|
2023-05-16 15:39:38 -05:00
|
|
|
let sources = (trusted: response.trusted ?? [], blocked: response.blocked ?? [])
|
2023-05-15 16:25:25 -05:00
|
|
|
|
2023-05-16 15:39:38 -05:00
|
|
|
// Cache sources
|
2023-10-16 18:18:06 -05:00
|
|
|
UserDefaults.shared.recommendedSources = sources.trusted
|
2023-05-16 15:39:38 -05:00
|
|
|
UserDefaults.shared.blockedSources = sources.blocked
|
2023-05-15 16:25:25 -05:00
|
|
|
|
|
|
|
|
self.finish(.success(sources))
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataTask.resume()
|
|
|
|
|
}
|
|
|
|
|
}
|