From 21c1bbdf0db37d7c3c9d733aa184f0179b816a69 Mon Sep 17 00:00:00 2001 From: Davide Date: Thu, 7 Nov 2024 13:23:47 +0100 Subject: [PATCH] Fix missing profile attributes initialization (#828) Also, log them better during remote import. --- .../Business/NEProfileRepository.swift | 4 +- .../Business/ProfileManager.swift | 18 ++++----- .../Domain/ProfileAttributes.swift | 40 ++++++++++++++----- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Passepartout/Library/Sources/CommonLibrary/Business/NEProfileRepository.swift b/Passepartout/Library/Sources/CommonLibrary/Business/NEProfileRepository.swift index b2dc1e51..63b992fd 100644 --- a/Passepartout/Library/Sources/CommonLibrary/Business/NEProfileRepository.swift +++ b/Passepartout/Library/Sources/CommonLibrary/Business/NEProfileRepository.swift @@ -95,9 +95,7 @@ private extension NEProfileRepository { func onLoadedManagers(_ managers: [Profile.ID: NETunnelProviderManager]) { let profiles = managers.values.compactMap { do { - let profile = try repository.profile(from: $0) - pp_log(.App.profiles, .debug, "Attributes for profile \(profile.id): \(profile.attributes)") - return profile + return try repository.profile(from: $0) } catch { pp_log(.App.profiles, .error, "Unable to decode profile from NE manager '\($0.localizedDescription ?? "")': \(error)") return nil diff --git a/Passepartout/Library/Sources/CommonLibrary/Business/ProfileManager.swift b/Passepartout/Library/Sources/CommonLibrary/Business/ProfileManager.swift index ee4e695d..873c00f4 100644 --- a/Passepartout/Library/Sources/CommonLibrary/Business/ProfileManager.swift +++ b/Passepartout/Library/Sources/CommonLibrary/Business/ProfileManager.swift @@ -360,15 +360,15 @@ private extension ProfileManager { pp_log(.App.profiles, .info, "Start importing remote profiles...") - pp_log(.App.profiles, .debug, "Local fingerprints:") - let localFingerprints: [Profile.ID: UUID] = await allProfiles.values.reduce(into: [:]) { - $0[$1.id] = $1.attributes.fingerprint - pp_log(.App.profiles, .debug, "\t\($1.id) = \($1.attributes.fingerprint?.description ?? "nil")") + pp_log(.App.profiles, .debug, "Local attributes:") + let localAttributes: [Profile.ID: ProfileAttributes] = await allProfiles.values.reduce(into: [:]) { + $0[$1.id] = $1.attributes + pp_log(.App.profiles, .debug, "\t\($1.id) = \($1.attributes)") } - pp_log(.App.profiles, .debug, "Remote fingerprints:") - let remoteFingerprints: [Profile.ID: UUID] = result.reduce(into: [:]) { - $0[$1.id] = $1.attributes.fingerprint - pp_log(.App.profiles, .debug, "\t\($1.id) = \($1.attributes.fingerprint?.description ?? "nil")") + pp_log(.App.profiles, .debug, "Remote attributes:") + let remoteAttributes: [Profile.ID: ProfileAttributes] = result.reduce(into: [:]) { + $0[$1.id] = $1.attributes + pp_log(.App.profiles, .debug, "\t\($1.id) = \($1.attributes)") } let profilesToImport = result @@ -390,7 +390,7 @@ private extension ProfileManager { idsToRemove.append(remoteProfile.id) continue } - guard remoteFingerprints[remoteProfile.id] != localFingerprints[remoteProfile.id] else { + guard remoteAttributes[remoteProfile.id]?.fingerprint != localAttributes[remoteProfile.id]?.fingerprint else { pp_log(.App.profiles, .info, "Skip re-importing local profile \(remoteProfile.id)") continue } diff --git a/Passepartout/Library/Sources/CommonLibrary/Domain/ProfileAttributes.swift b/Passepartout/Library/Sources/CommonLibrary/Domain/ProfileAttributes.swift index c48c9ddb..737d2fa4 100644 --- a/Passepartout/Library/Sources/CommonLibrary/Domain/ProfileAttributes.swift +++ b/Passepartout/Library/Sources/CommonLibrary/Domain/ProfileAttributes.swift @@ -28,30 +28,48 @@ import Foundation import PassepartoutKit public struct ProfileAttributes: Hashable, Codable { - public var isAvailableForTV: Bool? - - public var expirationDate: Date? + public var fingerprint: UUID? public var lastUpdate: Date? - public var fingerprint: UUID? + public var isAvailableForTV: Bool? + + public var expirationDate: Date? public init() { } public init( - isAvailableForTV: Bool?, - expirationDate: Date?, + fingerprint: UUID?, lastUpdate: Date?, - fingerprint: UUID? + isAvailableForTV: Bool?, + expirationDate: Date? ) { - self.isAvailableForTV = isAvailableForTV - self.lastUpdate = lastUpdate self.fingerprint = fingerprint + self.lastUpdate = lastUpdate + self.isAvailableForTV = isAvailableForTV + self.expirationDate = expirationDate } +} - public func isEquivalent(to other: Self) -> Bool { - isAvailableForTV == other.isAvailableForTV +extension ProfileAttributes: CustomDebugStringConvertible { + public var debugDescription: String { + let descs = [ + fingerprint.map { + "fingerprint: \($0)" + }, + lastUpdate.map { + "lastUpdate: \($0)" + }, + isAvailableForTV.map { + "isAvailableForTV: \($0)" + }, + expirationDate.map { + "expirationDate: \($0)" + } + ].compactMap { $0 } + + return "{\(descs.joined(separator: ", "))}" } }