React on providers eligibility (#848)

Check .providers eligibility in tunnel to prevent from starting if
profile has an active provider module. Do not alter original profile.
This commit is contained in:
Davide 2024-11-11 12:50:26 +01:00 committed by GitHub
parent 30ccd58d4a
commit 0686650ccf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 8 deletions

View File

@ -52,3 +52,9 @@ extension AppFeature: Identifiable {
rawValue
}
}
extension AppFeature: CustomStringConvertible {
public var description: String {
rawValue
}
}

View File

@ -54,3 +54,9 @@ extension AppProduct {
}
}
}
extension AppProduct: CustomStringConvertible {
public var description: String {
rawValue
}
}

View File

@ -144,10 +144,12 @@ private extension AppContext {
pp_log(.app, .notice, "Application did update eligible features")
pendingTask = Task {
let isEligible = features.contains(.sharing)
// toggle sync based on .sharing eligibility
let isEligibleForSharing = features.contains(.sharing)
do {
pp_log(.App.profiles, .info, "Refresh remote profiles observers (eligible=\(isEligible), CloudKit=\(isCloudKitEnabled))...")
try await profileManager.observeRemote(isEligible && isCloudKitEnabled)
pp_log(.App.profiles, .info, "Refresh remote profiles observers (eligible=\(isEligibleForSharing), CloudKit=\(isCloudKitEnabled))...")
try await profileManager.observeRemote(isEligibleForSharing && isCloudKitEnabled)
} catch {
pp_log(.App.profiles, .error, "Unable to re-observe remote profiles: \(error)")
}

View File

@ -36,7 +36,6 @@ final class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
parameters: Constants.shared.log,
logsPrivateData: UserDefaults.appGroup.bool(forKey: AppPreference.logsPrivateData.key)
)
try await checkEligibility(environment: .shared)
do {
fwd = try await NEPTPForwarder(
provider: self,
@ -44,6 +43,7 @@ final class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
registry: .shared,
environment: .shared
)
try await checkEligibility(of: fwd!.profile, environment: .shared)
try await fwd?.startTunnel(options: options)
} catch {
pp_log(.app, .fault, "Unable to start tunnel: \(error)")
@ -76,13 +76,15 @@ final class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
}
}
// MARK: - Eligibility
@MainActor
private extension PacketTunnelProvider {
var iapManager: IAPManager {
.shared
}
var isEligible: Bool {
var isEligibleForPlatform: Bool {
#if os(tvOS)
iapManager.isEligible(for: .appleTV)
#else
@ -90,12 +92,22 @@ private extension PacketTunnelProvider {
#endif
}
func checkEligibility(environment: TunnelEnvironment) async throws {
func isEligibleForProviders(_ profile: Profile) -> Bool {
profile.firstProviderModuleWithMetadata == nil || iapManager.isEligible(for: .providers)
}
func checkEligibility(of profile: Profile, environment: TunnelEnvironment) async throws {
await iapManager.reloadReceipt()
guard isEligible else {
guard isEligibleForPlatform else {
let error = PassepartoutError(.App.ineligibleProfile)
environment.setEnvironmentValue(error.code, forKey: TunnelEnvironmentKeys.lastErrorCode)
pp_log(.app, .fault, "Profile is ineligible, purchase required")
pp_log(.app, .fault, "Profile is ineligible for this platform")
throw error
}
guard isEligibleForProviders(profile) else {
let error = PassepartoutError(.App.ineligibleProfile)
environment.setEnvironmentValue(error.code, forKey: TunnelEnvironmentKeys.lastErrorCode)
pp_log(.app, .fault, "Profile is ineligible for providers")
throw error
}
}