From a1c9049b4bfa6c082e1876cf122e8a61cd7c66e0 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Thu, 1 Aug 2019 10:45:54 -0700 Subject: [PATCH] [AltServer] Advertises unique server ID + writes to AltStore Info.plist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AltServer adds its own server ID to AltStore’s Info.plist when resigning. This allows AltStore to prefer the server it was installed with. --- AltKit/Bundle+AltStore.swift | 1 + AltServer/AppDelegate.swift | 2 ++ AltServer/Connections/ConnectionManager.swift | 17 ++++++++++- .../Connections/UserDefaults+AltServer.swift | 29 +++++++++++++++++++ .../ALTDeviceManager+Installation.swift | 1 + AltStore.xcodeproj/project.pbxproj | 4 +++ 6 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 AltServer/Connections/UserDefaults+AltServer.swift diff --git a/AltKit/Bundle+AltStore.swift b/AltKit/Bundle+AltStore.swift index b1d3c656..d5802cd4 100644 --- a/AltKit/Bundle+AltStore.swift +++ b/AltKit/Bundle+AltStore.swift @@ -13,6 +13,7 @@ public extension Bundle struct Info { public static let deviceID = "ALTDeviceID" + public static let serverID = "ALTServerID" public static let appGroups = "ALTAppGroups" public static let urlTypes = "CFBundleURLTypes" diff --git a/AltServer/AppDelegate.swift b/AltServer/AppDelegate.swift index aeab490b..98fb03ca 100644 --- a/AltServer/AppDelegate.swift +++ b/AltServer/AppDelegate.swift @@ -28,6 +28,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { + UserDefaults.standard.registerDefaults() + UNUserNotificationCenter.current().delegate = self ConnectionManager.shared.start() diff --git a/AltServer/Connections/ConnectionManager.swift b/AltServer/Connections/ConnectionManager.swift index d3e74ad1..ce5ac20a 100644 --- a/AltServer/Connections/ConnectionManager.swift +++ b/AltServer/Connections/ConnectionManager.swift @@ -83,7 +83,22 @@ private extension ConnectionManager func makeListener() -> NWListener { let listener = try! NWListener(using: .tcp) - listener.service = NWListener.Service(type: ALTServerServiceType) + + let service: NWListener.Service + + if let serverID = UserDefaults.standard.serverID?.data(using: .utf8) + { + let txtDictionary = ["serverID": serverID] + let txtData = NetService.data(fromTXTRecord: txtDictionary) + + service = NWListener.Service(name: nil, type: ALTServerServiceType, domain: nil, txtRecord: txtData) + } + else + { + service = NWListener.Service(type: ALTServerServiceType) + } + + listener.service = service listener.serviceRegistrationUpdateHandler = { (serviceChange) in switch serviceChange diff --git a/AltServer/Connections/UserDefaults+AltServer.swift b/AltServer/Connections/UserDefaults+AltServer.swift new file mode 100644 index 00000000..cb4634b3 --- /dev/null +++ b/AltServer/Connections/UserDefaults+AltServer.swift @@ -0,0 +1,29 @@ +// +// UserDefaults+AltServer.swift +// AltServer +// +// Created by Riley Testut on 7/31/19. +// Copyright © 2019 Riley Testut. All rights reserved. +// + +import Foundation + +extension UserDefaults +{ + var serverID: String? { + get { + return self.string(forKey: "serverID") + } + set { + self.set(newValue, forKey: "serverID") + } + } + + func registerDefaults() + { + if self.serverID == nil + { + self.serverID = UUID().uuidString + } + } +} diff --git a/AltServer/Devices/ALTDeviceManager+Installation.swift b/AltServer/Devices/ALTDeviceManager+Installation.swift index 320567e8..a14b1ada 100644 --- a/AltServer/Devices/ALTDeviceManager+Installation.swift +++ b/AltServer/Devices/ALTDeviceManager+Installation.swift @@ -352,6 +352,7 @@ extension ALTDeviceManager guard var infoDictionary = NSDictionary(contentsOf: infoPlistURL) as? [String: Any] else { throw ALTError(.missingInfoPlist) } infoDictionary[kCFBundleIdentifierKey as String] = profile.bundleIdentifier infoDictionary[Bundle.Info.deviceID] = device.identifier + infoDictionary[Bundle.Info.serverID] = UserDefaults.standard.serverID try (infoDictionary as NSDictionary).write(to: infoPlistURL) let resigner = ALTSigner(team: team, certificate: certificate) diff --git a/AltStore.xcodeproj/project.pbxproj b/AltStore.xcodeproj/project.pbxproj index 1418a595..a4d904c2 100644 --- a/AltStore.xcodeproj/project.pbxproj +++ b/AltStore.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ BF0201BE22C2EFBC000B93E4 /* openssl.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = BF4713A422976CFC00784A2F /* openssl.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; BF02419422F2156E00129732 /* RefreshAttempt.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF02419322F2156E00129732 /* RefreshAttempt.swift */; }; BF02419622F2199300129732 /* RefreshAttemptsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF02419522F2199300129732 /* RefreshAttemptsViewController.swift */; }; + BF0241AA22F29CCD00129732 /* UserDefaults+AltServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF0241A922F29CCD00129732 /* UserDefaults+AltServer.swift */; }; BF08858322DE795100DE9F1E /* MyAppsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF08858222DE795100DE9F1E /* MyAppsViewController.swift */; }; BF08858522DE7EC800DE9F1E /* UpdateCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF08858422DE7EC800DE9F1E /* UpdateCollectionViewCell.swift */; }; BF0C4EBD22A1BD8B009A2DD7 /* AppManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF0C4EBC22A1BD8B009A2DD7 /* AppManager.swift */; }; @@ -256,6 +257,7 @@ A136EE677716B80768E9F0A2 /* Pods-AltStore.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AltStore.release.xcconfig"; path = "Target Support Files/Pods-AltStore/Pods-AltStore.release.xcconfig"; sourceTree = ""; }; BF02419322F2156E00129732 /* RefreshAttempt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefreshAttempt.swift; sourceTree = ""; }; BF02419522F2199300129732 /* RefreshAttemptsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefreshAttemptsViewController.swift; sourceTree = ""; }; + BF0241A922F29CCD00129732 /* UserDefaults+AltServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UserDefaults+AltServer.swift"; sourceTree = ""; }; BF08858222DE795100DE9F1E /* MyAppsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyAppsViewController.swift; sourceTree = ""; }; BF08858422DE7EC800DE9F1E /* UpdateCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateCollectionViewCell.swift; sourceTree = ""; }; BF0C4EBC22A1BD8B009A2DD7 /* AppManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppManager.swift; sourceTree = ""; }; @@ -880,6 +882,7 @@ isa = PBXGroup; children = ( BF1E3129229F474900370A3C /* ConnectionManager.swift */, + BF0241A922F29CCD00129732 /* UserDefaults+AltServer.swift */, ); path = Connections; sourceTree = ""; @@ -1213,6 +1216,7 @@ BF1E312B229F474900370A3C /* ConnectionManager.swift in Sources */, BF458690229872EA00BD7491 /* AppDelegate.swift in Sources */, BF4586C52298CDB800BD7491 /* ALTDeviceManager.mm in Sources */, + BF0241AA22F29CCD00129732 /* UserDefaults+AltServer.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };