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
@@ -43,6 +43,7 @@
|
|||||||
B39575F5284F29E20080B4FF /* Roxas.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B39575F4284F29E20080B4FF /* Roxas.framework */; };
|
B39575F5284F29E20080B4FF /* Roxas.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B39575F4284F29E20080B4FF /* Roxas.framework */; };
|
||||||
B39F16132918D7C5002E9404 /* Consts.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39F16122918D7C5002E9404 /* Consts.swift */; };
|
B39F16132918D7C5002E9404 /* Consts.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39F16122918D7C5002E9404 /* Consts.swift */; };
|
||||||
B39F16152918D7DA002E9404 /* Consts+Proxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = B39F16142918D7DA002E9404 /* Consts+Proxy.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 */; };
|
B3C395F1284F2DE700DA9E2F /* KeychainAccess in Frameworks */ = {isa = PBXBuildFile; productRef = B3C395F0284F2DE700DA9E2F /* KeychainAccess */; };
|
||||||
B3C395F4284F35DD00DA9E2F /* Nuke in Frameworks */ = {isa = PBXBuildFile; productRef = B3C395F3284F35DD00DA9E2F /* Nuke */; };
|
B3C395F4284F35DD00DA9E2F /* Nuke in Frameworks */ = {isa = PBXBuildFile; productRef = B3C395F3284F35DD00DA9E2F /* Nuke */; };
|
||||||
B3C395F7284F362400DA9E2F /* AppCenterAnalytics in Frameworks */ = {isa = PBXBuildFile; productRef = B3C395F6284F362400DA9E2F /* AppCenterAnalytics */; };
|
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; };
|
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>"; };
|
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>"; };
|
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>"; };
|
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>"; };
|
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>"; };
|
B3C39608284F4C8400DA9E2F /* CodeSigning.xcconfig.sample */ = {isa = PBXFileReference; lastKnownFileType = text; path = CodeSigning.xcconfig.sample; sourceTree = "<group>"; };
|
||||||
@@ -1026,6 +1028,14 @@
|
|||||||
path = Consts;
|
path = Consts;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
|
B3C053B5295F921E0079DB81 /* Services */ = {
|
||||||
|
isa = PBXGroup;
|
||||||
|
children = (
|
||||||
|
B3C053B6295F921E0079DB81 /* NetworkService.swift */,
|
||||||
|
);
|
||||||
|
path = Services;
|
||||||
|
sourceTree = "<group>";
|
||||||
|
};
|
||||||
BF0DCA642433BDE200E3A595 /* Analytics */ = {
|
BF0DCA642433BDE200E3A595 /* Analytics */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
@@ -1560,6 +1570,7 @@
|
|||||||
BF3D648922E79A7700E9056B /* Types */,
|
BF3D648922E79A7700E9056B /* Types */,
|
||||||
BFD2479D2284FBBD00981D42 /* Extensions */,
|
BFD2479D2284FBBD00981D42 /* Extensions */,
|
||||||
BFD247962284D7C100981D42 /* Resources */,
|
BFD247962284D7C100981D42 /* Resources */,
|
||||||
|
B3C053B5295F921E0079DB81 /* Services */,
|
||||||
BF6C8FA8242935CA00125131 /* Dependencies */,
|
BF6C8FA8242935CA00125131 /* Dependencies */,
|
||||||
BFD247972284D7D800981D42 /* Supporting Files */,
|
BFD247972284D7D800981D42 /* Supporting Files */,
|
||||||
1920B04E2924AC8300744F60 /* Settings.bundle */,
|
1920B04E2924AC8300744F60 /* Settings.bundle */,
|
||||||
@@ -2473,6 +2484,7 @@
|
|||||||
BF88F97224F8727D00BB75DF /* AppManagerErrors.swift in Sources */,
|
BF88F97224F8727D00BB75DF /* AppManagerErrors.swift in Sources */,
|
||||||
B39F16152918D7DA002E9404 /* Consts+Proxy.swift in Sources */,
|
B39F16152918D7DA002E9404 /* Consts+Proxy.swift in Sources */,
|
||||||
BF6C8FAE2429597900125131 /* BannerCollectionViewCell.swift in Sources */,
|
BF6C8FAE2429597900125131 /* BannerCollectionViewCell.swift in Sources */,
|
||||||
|
B3C053B7295F921E0079DB81 /* NetworkService.swift in Sources */,
|
||||||
BF6F439223644C6E00A0B879 /* RefreshAltStoreViewController.swift in Sources */,
|
BF6F439223644C6E00A0B879 /* RefreshAltStoreViewController.swift in Sources */,
|
||||||
BFE60742231B07E6002B0E8E /* SettingsHeaderFooterView.swift in Sources */,
|
BFE60742231B07E6002B0E8E /* SettingsHeaderFooterView.swift in Sources */,
|
||||||
BFE338E822F10E56002E24B9 /* LaunchViewController.swift in Sources */,
|
BFE338E822F10E56002E24B9 /* LaunchViewController.swift in Sources */,
|
||||||
|
|||||||
@@ -754,7 +754,7 @@ private extension MyAppsViewController
|
|||||||
{
|
{
|
||||||
let downloadProgress = Progress.discreteProgress(totalUnitCount: 100)
|
let downloadProgress = Progress.discreteProgress(totalUnitCount: 100)
|
||||||
downloadOperation = RSTAsyncBlockOperation { (operation) in
|
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
|
do
|
||||||
{
|
{
|
||||||
let (fileURL, _) = try Result((fileURL, response), error).get()
|
let (fileURL, _) = try Result((fileURL, response), error).get()
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ final class DownloadAppOperation: ResultOperation<ALTApplication>
|
|||||||
private var sourceURL: URL?
|
private var sourceURL: URL?
|
||||||
private let destinationURL: URL
|
private let destinationURL: URL
|
||||||
|
|
||||||
private let session = URLSession(configuration: .default)
|
private let session: URLSession = AppServices.network.backgroundSession
|
||||||
private let temporaryDirectory = FileManager.default.uniqueTemporaryURL()
|
private let temporaryDirectory: URL = FileManager.default.uniqueTemporaryURL()
|
||||||
|
|
||||||
init(app: AppProtocol, destinationURL: URL, context: AppOperationContext)
|
init(app: AppProtocol, destinationURL: URL, context: AppOperationContext)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -35,29 +35,30 @@ final class FetchAnisetteDataOperation: ResultOperation<ALTAnisetteData>
|
|||||||
let url = AnisetteManager.currentURL
|
let url = AnisetteManager.currentURL
|
||||||
DLOG("Anisette URL: %@", url.absoluteString)
|
DLOG("Anisette URL: %@", url.absoluteString)
|
||||||
|
|
||||||
let task = URLSession.shared.dataTask(with: url) { data, response, error in
|
let task = AppServices.network.session.dataTask(with: url) { data, response, error in
|
||||||
guard let data = data, error == nil else { return }
|
|
||||||
|
|
||||||
do {
|
guard let data = data, error == nil else { return }
|
||||||
// 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) {
|
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)
|
DLOG("Anisette used: %@", formattedJSON)
|
||||||
self.finish(.success(anisette))
|
self.finish(.success(anisette))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch let error as NSError {
|
} catch let error as NSError {
|
||||||
print("Failed to load: \(error.localizedDescription)")
|
print("Failed to load: \(error.localizedDescription)")
|
||||||
self.finish(.failure(error))
|
self.finish(.failure(error))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
task.resume()
|
task.resume()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ final class FetchSourceOperation: ResultOperation<Source>
|
|||||||
let sourceURL: URL
|
let sourceURL: URL
|
||||||
let managedObjectContext: NSManagedObjectContext
|
let managedObjectContext: NSManagedObjectContext
|
||||||
|
|
||||||
private let session: URLSession
|
private let session: URLSession = AppServices.network.sessionNoCache
|
||||||
|
|
||||||
private lazy var dateFormatter: ISO8601DateFormatter = {
|
private lazy var dateFormatter: ISO8601DateFormatter = {
|
||||||
let dateFormatter = ISO8601DateFormatter()
|
let dateFormatter = ISO8601DateFormatter()
|
||||||
@@ -29,12 +29,6 @@ final class FetchSourceOperation: ResultOperation<Source>
|
|||||||
{
|
{
|
||||||
self.sourceURL = sourceURL
|
self.sourceURL = sourceURL
|
||||||
self.managedObjectContext = managedObjectContext
|
self.managedObjectContext = managedObjectContext
|
||||||
|
|
||||||
let configuration = URLSessionConfiguration.default
|
|
||||||
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
|
||||||
configuration.urlCache = nil
|
|
||||||
|
|
||||||
self.session = URLSession(configuration: configuration)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override func main()
|
override func main()
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ final class FetchTrustedSourcesOperation: ResultOperation<[FetchTrustedSourcesOp
|
|||||||
override func main()
|
override func main()
|
||||||
{
|
{
|
||||||
super.main()
|
super.main()
|
||||||
|
let session: URLSession = AppServices.network.session
|
||||||
let dataTask = URLSession.shared.dataTask(with: .trustedSources) { (data, response, error) in
|
let dataTask = session.dataTask(with: .trustedSources) { (data, response, error) in
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if let response = response as? HTTPURLResponse
|
if let response = response as? HTTPURLResponse
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ final class UpdatePatronsOperation: ResultOperation<Void>
|
|||||||
{
|
{
|
||||||
super.main()
|
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
|
do
|
||||||
{
|
{
|
||||||
if let response = response as? HTTPURLResponse
|
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