Skip to content

Retry in-app verification 3 times before failing #1358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Packages/App/Sources/CommonLibrary/Domain/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public struct Constants: Decodable, Sendable {
public let delay: TimeInterval

public let interval: TimeInterval

public let attempts: Int

public let retryInterval: TimeInterval
}

public let production: Parameters
Expand Down
8 changes: 6 additions & 2 deletions Packages/App/Sources/CommonLibrary/Resources/Constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@
"verification": {
"production": {
"delay": 120.0,
"interval": 3600.0
"interval": 3600.0,
"attempts": 3,
"retryInterval": 20.0
},
"beta": {
"delay": 600.0,
"interval": 600.0
"interval": 600.0,
"attempts": 3,
"retryInterval": 10.0
}
}
},
Expand Down
32 changes: 27 additions & 5 deletions Passepartout/Tunnel/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ final class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
await verifyEligibility(
of: fwd.originalProfile,
environment: environment,
interval: params.interval
params: params
)
}
} catch {
Expand Down Expand Up @@ -134,13 +134,32 @@ final class PacketTunnelProvider: NEPacketTunnelProvider, @unchecked Sendable {
// MARK: - Eligibility

private extension PacketTunnelProvider {
func verifyEligibility(of profile: Profile, environment: TunnelEnvironment, interval: TimeInterval) async {

@MainActor
func verifyEligibility(
of profile: Profile,
environment: TunnelEnvironment,
params: Constants.Tunnel.Verification.Parameters
) async {
var attempts = params.attempts

while true {
do {
pp_log(.app, .info, "Verify profile, requires: \(profile.features)")
await context.iapManager.reloadReceipt()
try await context.iapManager.verify(profile)
try context.iapManager.verify(profile)
} catch {

// mitigate the StoreKit inability to report errors, sometimes it
// would just return empty products, e.g. on network failure. in those
// cases, retry a few times before failing
if attempts > 0 {
attempts -= 1
pp_log(.app, .error, "Verification failed for profile \(profile.id), next attempt in \(params.retryInterval) seconds... (remaining: \(attempts), products: \(context.iapManager.purchasedProducts))")
try? await Task.sleep(interval: params.retryInterval)
continue
}

let error = PartoutError(.App.ineligibleProfile)
environment.setEnvironmentValue(error.code, forKey: TunnelEnvironmentKeys.lastErrorCode)
pp_log(.app, .fault, "Verification failed for profile \(profile.id), shutting down: \(error)")
Expand All @@ -151,8 +170,11 @@ private extension PacketTunnelProvider {
return
}

pp_log(.app, .info, "Will verify profile again in \(interval) seconds...")
try? await Task.sleep(interval: interval)
pp_log(.app, .info, "Will verify profile again in \(params.interval) seconds...")
try? await Task.sleep(interval: params.interval)

// reset attempts for next verification
attempts = params.attempts
}
}
}
Expand Down