mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-09 06:43:25 +01:00
Add Network service and default sessions
Signed-off-by: Joseph Mattello <mail@joemattiello.com>
This commit is contained in:
committed by
Spidy123222
parent
79d677cf3c
commit
d6ea725329
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ 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,12 +29,6 @@ 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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
81
AltStore/Services/NetworkService.swift
Normal file
81
AltStore/Services/NetworkService.swift
Normal file
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// 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, URLSessionTaskDelegate {
|
||||
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
|
||||
|
||||
// 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 {
|
||||
public let session: URLSession = {
|
||||
let configuration: URLSessionConfiguration = URLSessionConfiguration.default
|
||||
configuration.httpShouldSetCookies = true
|
||||
configuration.httpShouldUsePipelining = true
|
||||
let session = URLSession.init(configuration: configuration, delegate: AltNetworkDelegate(), delegateQueue: nil)
|
||||
return session
|
||||
}()
|
||||
|
||||
public let sessionNoCache: URLSession = {
|
||||
let configuration: URLSessionConfiguration = URLSessionConfiguration.default
|
||||
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
||||
configuration.urlCache = nil
|
||||
let session = URLSession.init(configuration: configuration, delegate: AltNetworkDelegate(), delegateQueue: nil)
|
||||
return session
|
||||
}()
|
||||
|
||||
static let backgroundSessionIdentifier = "SideStoreBackgroundSession"
|
||||
|
||||
public let backgroundSession: URLSession = {
|
||||
let configuration: URLSessionConfiguration = URLSessionConfiguration.background(withIdentifier: AltNetworkService.backgroundSessionIdentifier)
|
||||
let session = URLSession.init(configuration: configuration, delegate: AltNetworkDelegate(), delegateQueue: nil)
|
||||
return session
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user