2019-06-17 14:49:23 -07:00
|
|
|
//
|
2019-07-30 17:00:04 -07:00
|
|
|
// FetchSourceOperation.swift
|
2019-06-17 14:49:23 -07:00
|
|
|
// AltStore
|
|
|
|
|
//
|
2019-07-30 17:00:04 -07:00
|
|
|
// Created by Riley Testut on 7/30/19.
|
2019-06-17 14:49:23 -07:00
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2020-03-24 13:27:44 -07:00
|
|
|
import CoreData
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
import AltStoreCore
|
2019-06-17 14:49:23 -07:00
|
|
|
import Roxas
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
@objc(FetchSourceOperation)
|
2023-01-04 09:52:12 -05:00
|
|
|
final class FetchSourceOperation: ResultOperation<Source>
|
2019-06-17 14:49:23 -07:00
|
|
|
{
|
2019-07-30 17:00:04 -07:00
|
|
|
let sourceURL: URL
|
2020-03-24 13:27:44 -07:00
|
|
|
let managedObjectContext: NSManagedObjectContext
|
2019-07-30 17:00:04 -07:00
|
|
|
|
2023-05-11 18:17:26 -05:00
|
|
|
// Non-nil when updating an existing source.
|
|
|
|
|
@Managed
|
|
|
|
|
private var source: Source?
|
|
|
|
|
|
2019-09-19 11:27:38 -07:00
|
|
|
private let session: URLSession
|
2019-06-17 14:49:23 -07:00
|
|
|
|
2019-09-07 15:37:08 -07:00
|
|
|
private lazy var dateFormatter: ISO8601DateFormatter = {
|
|
|
|
|
let dateFormatter = ISO8601DateFormatter()
|
2019-06-17 14:49:23 -07:00
|
|
|
return dateFormatter
|
|
|
|
|
}()
|
|
|
|
|
|
2023-05-11 18:17:26 -05:00
|
|
|
// New source
|
|
|
|
|
convenience init(sourceURL: URL, managedObjectContext: NSManagedObjectContext = DatabaseManager.shared.persistentContainer.newBackgroundContext())
|
|
|
|
|
{
|
|
|
|
|
self.init(sourceURL: sourceURL, source: nil, managedObjectContext: managedObjectContext)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Existing source
|
|
|
|
|
convenience init(source: Source, managedObjectContext: NSManagedObjectContext = DatabaseManager.shared.persistentContainer.newBackgroundContext())
|
|
|
|
|
{
|
|
|
|
|
self.init(sourceURL: source.sourceURL, source: source, managedObjectContext: managedObjectContext)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private init(sourceURL: URL, source: Source?, managedObjectContext: NSManagedObjectContext)
|
2019-07-30 17:00:04 -07:00
|
|
|
{
|
|
|
|
|
self.sourceURL = sourceURL
|
2020-03-24 13:27:44 -07:00
|
|
|
self.managedObjectContext = managedObjectContext
|
2023-05-11 18:17:26 -05:00
|
|
|
self.source = source
|
2019-09-19 11:27:38 -07:00
|
|
|
|
|
|
|
|
let configuration = URLSessionConfiguration.default
|
|
|
|
|
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
|
|
|
|
configuration.urlCache = nil
|
|
|
|
|
|
|
|
|
|
self.session = URLSession(configuration: configuration)
|
2019-07-30 17:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-17 14:49:23 -07:00
|
|
|
override func main()
|
|
|
|
|
{
|
|
|
|
|
super.main()
|
|
|
|
|
|
2023-05-16 15:39:38 -05:00
|
|
|
if let source = self.source
|
|
|
|
|
{
|
|
|
|
|
// Check if source is blocked before fetching it.
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
try self.managedObjectContext.performAndWait {
|
|
|
|
|
// Source must be from self.managedObjectContext
|
|
|
|
|
let source = self.managedObjectContext.object(with: source.objectID) as! Source
|
|
|
|
|
try self.verifySourceNotBlocked(source, response: nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
self.managedObjectContext.perform {
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
let dataTask = self.session.dataTask(with: self.sourceURL) { (data, response, error) in
|
2020-08-27 16:23:50 -07:00
|
|
|
|
|
|
|
|
let childContext = DatabaseManager.shared.persistentContainer.newBackgroundContext(withParent: self.managedObjectContext)
|
|
|
|
|
childContext.mergePolicy = NSOverwriteMergePolicy
|
|
|
|
|
childContext.perform {
|
2019-06-17 14:49:23 -07:00
|
|
|
do
|
|
|
|
|
{
|
2023-05-15 16:25:25 -05:00
|
|
|
let (data, response) = try Result((data, response), error).get()
|
2019-06-17 14:49:23 -07:00
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
let decoder = AltStoreCore.JSONDecoder()
|
2019-09-07 15:37:08 -07:00
|
|
|
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
|
|
|
|
|
let container = try decoder.singleValueContainer()
|
|
|
|
|
let text = try container.decode(String.self)
|
|
|
|
|
|
|
|
|
|
// Full ISO8601 Format.
|
|
|
|
|
self.dateFormatter.formatOptions = [.withFullDate, .withFullTime, .withTimeZone]
|
|
|
|
|
if let date = self.dateFormatter.date(from: text)
|
|
|
|
|
{
|
|
|
|
|
return date
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Just date portion of ISO8601.
|
|
|
|
|
self.dateFormatter.formatOptions = [.withFullDate]
|
|
|
|
|
if let date = self.dateFormatter.date(from: text)
|
|
|
|
|
{
|
|
|
|
|
return date
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Date is in invalid format.")
|
|
|
|
|
})
|
|
|
|
|
|
2020-08-27 16:23:50 -07:00
|
|
|
decoder.managedObjectContext = childContext
|
2020-03-24 13:27:44 -07:00
|
|
|
decoder.sourceURL = self.sourceURL
|
2019-06-17 14:49:23 -07:00
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
let source = try decoder.decode(Source.self, from: data)
|
2020-08-27 16:23:50 -07:00
|
|
|
let identifier = source.identifier
|
2019-11-04 13:38:54 -08:00
|
|
|
|
2023-05-15 16:25:25 -05:00
|
|
|
try self.verify(source, response: response)
|
2022-11-22 13:02:19 -06:00
|
|
|
|
2020-08-27 16:23:50 -07:00
|
|
|
try childContext.save()
|
|
|
|
|
|
|
|
|
|
self.managedObjectContext.perform {
|
|
|
|
|
if let source = Source.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(Source.identifier), identifier), in: self.managedObjectContext)
|
|
|
|
|
{
|
|
|
|
|
self.finish(.success(source))
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self.finish(.failure(OperationError.noSources))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-17 14:49:23 -07:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2020-08-27 16:23:50 -07:00
|
|
|
self.managedObjectContext.perform {
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
2019-06-17 14:49:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.progress.addChild(dataTask.progress, withPendingUnitCount: 1)
|
|
|
|
|
|
|
|
|
|
dataTask.resume()
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-22 13:02:19 -06:00
|
|
|
|
|
|
|
|
private extension FetchSourceOperation
|
|
|
|
|
{
|
2023-05-15 16:25:25 -05:00
|
|
|
func verify(_ source: Source, response: URLResponse) throws
|
2022-11-22 13:02:19 -06:00
|
|
|
{
|
2023-05-16 15:39:38 -05:00
|
|
|
try self.verifySourceNotBlocked(source, response: response)
|
2022-11-22 13:02:19 -06:00
|
|
|
|
|
|
|
|
var bundleIDs = Set<String>()
|
|
|
|
|
for app in source.apps
|
|
|
|
|
{
|
|
|
|
|
guard !bundleIDs.contains(app.bundleIdentifier) else { throw SourceError.duplicateBundleID(app.bundleIdentifier, source: source) }
|
|
|
|
|
bundleIDs.insert(app.bundleIdentifier)
|
2022-11-23 19:14:20 -06:00
|
|
|
|
|
|
|
|
var versions = Set<String>()
|
|
|
|
|
for version in app.versions
|
|
|
|
|
{
|
2023-05-18 14:51:26 -05:00
|
|
|
guard !versions.contains(version.versionID) else { throw SourceError.duplicateVersion(version.localizedVersion, for: app, source: source) }
|
|
|
|
|
versions.insert(version.versionID)
|
2022-11-23 19:14:20 -06:00
|
|
|
}
|
2023-05-15 15:29:51 -05:00
|
|
|
|
2023-05-26 14:58:52 -05:00
|
|
|
for permission in app.permissions where permission.type == .privacy
|
2023-05-15 15:29:51 -05:00
|
|
|
{
|
2023-05-26 14:58:52 -05:00
|
|
|
// Privacy permissions MUST have a usage description.
|
|
|
|
|
guard permission.usageDescription != nil else { throw SourceError.missingPermissionUsageDescription(for: permission.permission, app: app, source: source) }
|
2023-05-15 15:29:51 -05:00
|
|
|
}
|
2022-11-22 13:02:19 -06:00
|
|
|
}
|
2023-05-11 18:17:26 -05:00
|
|
|
|
|
|
|
|
if let previousSourceID = self.$source.identifier
|
|
|
|
|
{
|
|
|
|
|
guard source.identifier == previousSourceID else { throw SourceError.changedID(source.identifier, previousID: previousSourceID, source: source) }
|
|
|
|
|
}
|
2022-11-22 13:02:19 -06:00
|
|
|
}
|
2023-05-16 15:39:38 -05:00
|
|
|
|
|
|
|
|
func verifySourceNotBlocked(_ source: Source, response: URLResponse?) throws
|
|
|
|
|
{
|
|
|
|
|
guard let blockedSources = UserDefaults.shared.blockedSources else { return }
|
|
|
|
|
|
|
|
|
|
for blockedSource in blockedSources
|
|
|
|
|
{
|
|
|
|
|
guard
|
|
|
|
|
source.identifier != blockedSource.identifier,
|
|
|
|
|
source.sourceURL.absoluteString.lowercased() != blockedSource.sourceURL?.absoluteString.lowercased()
|
|
|
|
|
else { throw SourceError.blocked(source, bundleIDs: blockedSource.bundleIDs, existingSource: self.source) }
|
|
|
|
|
|
|
|
|
|
if let responseURL = response?.url
|
|
|
|
|
{
|
|
|
|
|
// responseURL may differ from source.sourceURL (e.g. due to redirects), so double-check it's also not blocked.
|
|
|
|
|
guard responseURL.absoluteString.lowercased() != blockedSource.sourceURL?.absoluteString.lowercased() else {
|
|
|
|
|
throw SourceError.blocked(source, bundleIDs: blockedSource.bundleIDs, existingSource: self.source)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-22 13:02:19 -06:00
|
|
|
}
|