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:
parent
30ccd58d4a
commit
0686650ccf
|
@ -52,3 +52,9 @@ extension AppFeature: Identifiable {
|
||||||
rawValue
|
rawValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension AppFeature: CustomStringConvertible {
|
||||||
|
public var description: String {
|
||||||
|
rawValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -54,3 +54,9 @@ extension AppProduct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension AppProduct: CustomStringConvertible {
|
||||||
|
public var description: String {
|
||||||
|
rawValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -144,10 +144,12 @@ private extension AppContext {
|
||||||
|
|
||||||
pp_log(.app, .notice, "Application did update eligible features")
|
pp_log(.app, .notice, "Application did update eligible features")
|
||||||
pendingTask = Task {
|
pendingTask = Task {
|
||||||
let isEligible = features.contains(.sharing)
|
|
||||||
|
// toggle sync based on .sharing eligibility
|
||||||
|
let isEligibleForSharing = features.contains(.sharing)
|
||||||
do {
|
do {
|
||||||
pp_log(.App.profiles, .info, "Refresh remote profiles observers (eligible=\(isEligible), CloudKit=\(isCloudKitEnabled))...")
|
pp_log(.App.profiles, .info, "Refresh remote profiles observers (eligible=\(isEligibleForSharing), CloudKit=\(isCloudKitEnabled))...")
|
||||||
try await profileManager.observeRemote(isEligible && isCloudKitEnabled)
|
try await profileManager.observeRemote(isEligibleForSharing && isCloudKitEnabled)
|
||||||
} catch {
|
} catch {
|
||||||
pp_log(.App.profiles, .error, "Unable to re-observe remote profiles: \(error)")
|
pp_log(.App.profiles, .error, "Unable to re-observe remote profiles: \(error)")
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,6 @@ final class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
|
||||||
parameters: Constants.shared.log,
|
parameters: Constants.shared.log,
|
||||||
logsPrivateData: UserDefaults.appGroup.bool(forKey: AppPreference.logsPrivateData.key)
|
logsPrivateData: UserDefaults.appGroup.bool(forKey: AppPreference.logsPrivateData.key)
|
||||||
)
|
)
|
||||||
try await checkEligibility(environment: .shared)
|
|
||||||
do {
|
do {
|
||||||
fwd = try await NEPTPForwarder(
|
fwd = try await NEPTPForwarder(
|
||||||
provider: self,
|
provider: self,
|
||||||
|
@ -44,6 +43,7 @@ final class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
|
||||||
registry: .shared,
|
registry: .shared,
|
||||||
environment: .shared
|
environment: .shared
|
||||||
)
|
)
|
||||||
|
try await checkEligibility(of: fwd!.profile, environment: .shared)
|
||||||
try await fwd?.startTunnel(options: options)
|
try await fwd?.startTunnel(options: options)
|
||||||
} catch {
|
} catch {
|
||||||
pp_log(.app, .fault, "Unable to start tunnel: \(error)")
|
pp_log(.app, .fault, "Unable to start tunnel: \(error)")
|
||||||
|
@ -76,13 +76,15 @@ final class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Eligibility
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
private extension PacketTunnelProvider {
|
private extension PacketTunnelProvider {
|
||||||
var iapManager: IAPManager {
|
var iapManager: IAPManager {
|
||||||
.shared
|
.shared
|
||||||
}
|
}
|
||||||
|
|
||||||
var isEligible: Bool {
|
var isEligibleForPlatform: Bool {
|
||||||
#if os(tvOS)
|
#if os(tvOS)
|
||||||
iapManager.isEligible(for: .appleTV)
|
iapManager.isEligible(for: .appleTV)
|
||||||
#else
|
#else
|
||||||
|
@ -90,12 +92,22 @@ private extension PacketTunnelProvider {
|
||||||
#endif
|
#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()
|
await iapManager.reloadReceipt()
|
||||||
guard isEligible else {
|
guard isEligibleForPlatform else {
|
||||||
let error = PassepartoutError(.App.ineligibleProfile)
|
let error = PassepartoutError(.App.ineligibleProfile)
|
||||||
environment.setEnvironmentValue(error.code, forKey: TunnelEnvironmentKeys.lastErrorCode)
|
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
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue