Compare commits

...

3 Commits

Author SHA1 Message Date
Joseph Mattello
5b977092f5 refs #103 use redirected url as source url
Signed-off-by: Joseph Mattello <mail@joemattiello.com>
2023-02-15 16:23:44 -08:00
Joseph Mattello
c6915d1f91 refs #103 redirects are working
Signed-off-by: Joseph Mattello <mail@joemattiello.com>
2023-02-15 16:23:44 -08:00
Joseph Mattello
d6ea725329 Add Network service and default sessions
Signed-off-by: Joseph Mattello <mail@joemattiello.com>
2023-02-15 16:23:44 -08:00
8 changed files with 261 additions and 97 deletions

View File

@@ -43,6 +43,7 @@
B39575F5284F29E20080B4FF /* Roxas.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B39575F4284F29E20080B4FF /* Roxas.framework */; };
B39F16132918D7C5002E9404 /* Consts.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39F16122918D7C5002E9404 /* Consts.swift */; };
B39F16152918D7DA002E9404 /* Consts+Proxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39F16142918D7DA002E9404 /* Consts+Proxy.swift */; };
B3C053B7295F921E0079DB81 /* NetworkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = B3C053B6295F921E0079DB81 /* NetworkService.swift */; };
B3C395F1284F2DE700DA9E2F /* KeychainAccess in Frameworks */ = {isa = PBXBuildFile; productRef = B3C395F0284F2DE700DA9E2F /* KeychainAccess */; };
B3C395F4284F35DD00DA9E2F /* Nuke in Frameworks */ = {isa = PBXBuildFile; productRef = B3C395F3284F35DD00DA9E2F /* Nuke */; };
B3C395F7284F362400DA9E2F /* AppCenterAnalytics in Frameworks */ = {isa = PBXBuildFile; productRef = B3C395F6284F362400DA9E2F /* AppCenterAnalytics */; };
@@ -533,6 +534,7 @@
B39575F4284F29E20080B4FF /* Roxas.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Roxas.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B39F16122918D7C5002E9404 /* Consts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Consts.swift; sourceTree = "<group>"; };
B39F16142918D7DA002E9404 /* Consts+Proxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Consts+Proxy.swift"; sourceTree = "<group>"; };
B3C053B6295F921E0079DB81 /* NetworkService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkService.swift; sourceTree = "<group>"; };
B3C39606284F4C8400DA9E2F /* CodeSigning.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = CodeSigning.xcconfig; sourceTree = "<group>"; };
B3C39607284F4C8400DA9E2F /* Build.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Build.xcconfig; sourceTree = "<group>"; };
B3C39608284F4C8400DA9E2F /* CodeSigning.xcconfig.sample */ = {isa = PBXFileReference; lastKnownFileType = text; path = CodeSigning.xcconfig.sample; sourceTree = "<group>"; };
@@ -1026,6 +1028,14 @@
path = Consts;
sourceTree = "<group>";
};
B3C053B5295F921E0079DB81 /* Services */ = {
isa = PBXGroup;
children = (
B3C053B6295F921E0079DB81 /* NetworkService.swift */,
);
path = Services;
sourceTree = "<group>";
};
BF0DCA642433BDE200E3A595 /* Analytics */ = {
isa = PBXGroup;
children = (
@@ -1560,6 +1570,7 @@
BF3D648922E79A7700E9056B /* Types */,
BFD2479D2284FBBD00981D42 /* Extensions */,
BFD247962284D7C100981D42 /* Resources */,
B3C053B5295F921E0079DB81 /* Services */,
BF6C8FA8242935CA00125131 /* Dependencies */,
BFD247972284D7D800981D42 /* Supporting Files */,
1920B04E2924AC8300744F60 /* Settings.bundle */,
@@ -2473,6 +2484,7 @@
BF88F97224F8727D00BB75DF /* AppManagerErrors.swift in Sources */,
B39F16152918D7DA002E9404 /* Consts+Proxy.swift in Sources */,
BF6C8FAE2429597900125131 /* BannerCollectionViewCell.swift in Sources */,
B3C053B7295F921E0079DB81 /* NetworkService.swift in Sources */,
BF6F439223644C6E00A0B879 /* RefreshAltStoreViewController.swift in Sources */,
BFE60742231B07E6002B0E8E /* SettingsHeaderFooterView.swift in Sources */,
BFE338E822F10E56002E24B9 /* LaunchViewController.swift in Sources */,

View File

@@ -754,7 +754,7 @@ private extension MyAppsViewController
{
let downloadProgress = Progress.discreteProgress(totalUnitCount: 100)
downloadOperation = RSTAsyncBlockOperation { (operation) in
let downloadTask = URLSession.shared.downloadTask(with: url) { (fileURL, response, error) in
let downloadTask = AppServices.network.session.downloadTask(with: url) { (fileURL, response, error) in
do
{
let (fileURL, _) = try Result((fileURL, response), error).get()

View File

@@ -39,8 +39,8 @@ final class DownloadAppOperation: ResultOperation<ALTApplication>
private var sourceURL: URL?
private let destinationURL: URL
private let session = URLSession(configuration: .default)
private let temporaryDirectory = FileManager.default.uniqueTemporaryURL()
private let session: URLSession = AppServices.network.backgroundSession
private let temporaryDirectory: URL = FileManager.default.uniqueTemporaryURL()
init(app: AppProtocol, destinationURL: URL, context: AppOperationContext)
{

View File

@@ -34,30 +34,31 @@ final class FetchAnisetteDataOperation: ResultOperation<ALTAnisetteData>
let url = AnisetteManager.currentURL
DLOG("Anisette URL: %@", url.absoluteString)
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else { return }
do {
// make sure this JSON is in the format we expect
// convert data to json
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: String] {
// try to read out a dictionary
//for some reason serial number isn't needed but it doesn't work unless it has a value
let formattedJSON: [String: String] = ["machineID": json["X-Apple-I-MD-M"]!, "oneTimePassword": json["X-Apple-I-MD"]!, "localUserID": json["X-Apple-I-MD-LU"]!, "routingInfo": json["X-Apple-I-MD-RINFO"]!, "deviceUniqueIdentifier": json["X-Mme-Device-Id"]!, "deviceDescription": json["X-MMe-Client-Info"]!, "date": json["X-Apple-I-Client-Time"]!, "locale": json["X-Apple-Locale"]!, "timeZone": json["X-Apple-I-TimeZone"]!, "deviceSerialNumber": "1"]
if let anisette = ALTAnisetteData(json: formattedJSON) {
let task = AppServices.network.session.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else { return }
do {
// make sure this JSON is in the format we expect
// convert data to json
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: String] {
// try to read out a dictionary
//for some reason serial number isn't needed but it doesn't work unless it has a value
let formattedJSON: [String: String] = ["machineID": json["X-Apple-I-MD-M"]!, "oneTimePassword": json["X-Apple-I-MD"]!, "localUserID": json["X-Apple-I-MD-LU"]!, "routingInfo": json["X-Apple-I-MD-RINFO"]!, "deviceUniqueIdentifier": json["X-Mme-Device-Id"]!, "deviceDescription": json["X-MMe-Client-Info"]!, "date": json["X-Apple-I-Client-Time"]!, "locale": json["X-Apple-Locale"]!, "timeZone": json["X-Apple-I-TimeZone"]!, "deviceSerialNumber": "1"]
if let anisette = ALTAnisetteData(json: formattedJSON) {
DLOG("Anisette used: %@", formattedJSON)
self.finish(.success(anisette))
}
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
self.finish(.failure(error))
}
}
task.resume()
self.finish(.success(anisette))
}
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
self.finish(.failure(error))
}
}
task.resume()
}
}

View File

@@ -12,13 +12,51 @@ import CoreData
import AltStoreCore
import Roxas
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let results = regex.matches(in: text,
range: NSRange(text.startIndex..., in: text))
return results.map {
String(text[Range($0.range, in: text)!])
}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
func containsRedirect(_ response: URLResponse?, data: Data?) -> String? {
if let httpResponse = response as? HTTPURLResponse {
print("Request status code: \(httpResponse.statusCode)")
print("Request headers: \(httpResponse.allHeaderFields.debugDescription)")
guard let data = data else {
print("Request error: missing data")
return nil
}
let rawHttp = String(decoding: data, as: UTF8.self)
let regex = "url=((https|http):\\/\\/[\\S]*)\">"
guard var redirectURL = matches(for: regex, in: rawHttp).first else {
return nil
}
redirectURL = redirectURL.replacingOccurrences(of: "url=", with: "")
redirectURL = redirectURL.replacingOccurrences(of: "\">", with: "")
print("redirectURL: \(redirectURL)")
return redirectURL
} else {
return nil
}
}
@objc(FetchSourceOperation)
final class FetchSourceOperation: ResultOperation<Source>
{
let sourceURL: URL
let managedObjectContext: NSManagedObjectContext
private let session: URLSession
private let session: URLSession = AppServices.network.sessionNoCache
private lazy var dateFormatter: ISO8601DateFormatter = {
let dateFormatter = ISO8601DateFormatter()
@@ -29,79 +67,98 @@ final class FetchSourceOperation: ResultOperation<Source>
{
self.sourceURL = sourceURL
self.managedObjectContext = managedObjectContext
let configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
configuration.urlCache = nil
self.session = URLSession(configuration: configuration)
}
override func main()
{
super.main()
let dataTask = self.session.dataTask(with: self.sourceURL) { (data, response, error) in
let childContext = DatabaseManager.shared.persistentContainer.newBackgroundContext(withParent: self.managedObjectContext)
childContext.mergePolicy = NSOverwriteMergePolicy
childContext.perform {
do
{
let (data, _) = try Result((data, response), error).get()
let decoder = AltStoreCore.JSONDecoder()
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.")
})
decoder.managedObjectContext = childContext
decoder.sourceURL = self.sourceURL
let source = try decoder.decode(Source.self, from: data)
let identifier = source.identifier
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))
}
}
}
catch
{
self.managedObjectContext.perform {
self.finish(.failure(error))
}
}
}
}
loadSource(self.sourceURL)
}
private func loadSource(_ url: URL) {
let dataTask = createDataTask(with: url)
self.progress.addChild(dataTask.progress, withPendingUnitCount: 1)
dataTask.resume()
}
private func createDataTask(with url: URL) -> URLSessionDataTask {
let dataTask = self.session.dataTask(with: url) { (data, response, error) in
// Test code for http redirect HTML, though seems I got jekyll/sidestore to work without this now - @JoeMatt
if let error = error {
print("Request error: \(error)")
// TODO: Handle error
self.finish(.failure(error))
return
}
if let redirect = containsRedirect(response, data: data), let redirectURL = URL(string: redirect) {
DispatchQueue.main.async {
self.loadSource(redirectURL)
}
} else {
self.processJSON(data: data, response: response, error: error)
}
}
return dataTask
}
private func processJSON(data: Data?, response: URLResponse?, error: Error?) {
let childContext = DatabaseManager.shared.persistentContainer.newBackgroundContext(withParent: self.managedObjectContext)
childContext.mergePolicy = NSOverwriteMergePolicy
childContext.perform {
do
{
let (data, _) = try Result((data, response), error).get()
let decoder = AltStoreCore.JSONDecoder()
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.")
})
decoder.managedObjectContext = childContext
// Note: This may need to be response.url instead, to handle redirects @JoeMatt
decoder.sourceURL = response?.url ?? self.sourceURL
let source = try decoder.decode(Source.self, from: data)
let identifier = source.identifier
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))
}
}
}
catch
{
self.managedObjectContext.perform {
self.finish(.failure(error))
}
}
}
}
}

View File

@@ -37,8 +37,8 @@ final class FetchTrustedSourcesOperation: ResultOperation<[FetchTrustedSourcesOp
override func main()
{
super.main()
let dataTask = URLSession.shared.dataTask(with: .trustedSources) { (data, response, error) in
let session: URLSession = AppServices.network.session
let dataTask = session.dataTask(with: .trustedSources) { (data, response, error) in
do
{
if let response = response as? HTTPURLResponse

View File

@@ -43,7 +43,7 @@ final class UpdatePatronsOperation: ResultOperation<Void>
{
super.main()
let dataTask = URLSession.shared.dataTask(with: .patreonInfo) { (data, response, error) in
let dataTask = AppServices.network.session.dataTask(with: .patreonInfo) { (data, response, error) in
do
{
if let response = response as? HTTPURLResponse

View File

@@ -0,0 +1,94 @@
//
// NetworkService.swift
// SideStore
//
// Created by Joseph Mattiello on 11/6/22.
// Copyright © 2022 Riley Testut. All rights reserved.
//
import Foundation
public protocol Services {
var network: any NetworkService { get }
}
public protocol NetworkService {
var session: URLSession { get }
var sessionNoCache: URLSession { get }
var backgroundSession: URLSession { get }
}
public struct DefaultServices: Services {
public var network: NetworkService = AltNetworkService()
}
let AppServices = DefaultServices()
final public class AltNetworkDelegate: NSObject, URLSessionDelegate, URLSessionTaskDelegate {
public override init() {
super.init()
}
public struct Options: OptionSet {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}
public static let redirect = Options(rawValue: 1 << 0)
public static let all: Options = [.redirect]
}
var options: Options = .all
// public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void) {
// if options.contains(.redirect) {
// completionHandler(request)
// } else {
// completionHandler(nil)
// }
// }
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) async -> URLRequest? {
if options.contains(.redirect) {
return request
} else {
return nil
}
}
}
public final class AltNetworkService: NetworkService {
let delegate = AltNetworkDelegate()
lazy var delegateQueue: OperationQueue = {
let queue = OperationQueue.init()
queue.name = "com.sidestore.NetworkService.serialOperationQueue"
queue.maxConcurrentOperationCount = 1
return queue
}()
public lazy var session: URLSession = {
let configuration: URLSessionConfiguration = URLSessionConfiguration.default
configuration.httpShouldSetCookies = true
configuration.httpShouldUsePipelining = true
let session = URLSession.init(configuration: configuration, delegate: delegate, delegateQueue: delegateQueue)
return session
}()
public lazy var sessionNoCache: URLSession = {
let configuration: URLSessionConfiguration = URLSessionConfiguration.default
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
configuration.urlCache = nil
let session = URLSession.init(configuration: configuration, delegate: delegate, delegateQueue: delegateQueue)
return session
}()
static let backgroundSessionIdentifier = "SideStoreBackgroundSession"
public lazy var backgroundSession: URLSession = {
let configuration: URLSessionConfiguration = URLSessionConfiguration.background(withIdentifier: AltNetworkService.backgroundSessionIdentifier)
let session = URLSession.init(configuration: configuration, delegate: delegate, delegateQueue: nil)
return session
}()
}