fix(network): 禁止公网查询持久化缓存及会话数据
This commit is contained in:
@@ -5,20 +5,37 @@ import Darwin
|
||||
actor PublicIPAddressService {
|
||||
static let shared = PublicIPAddressService()
|
||||
private let session: URLSession
|
||||
private let ownsSession: Bool
|
||||
private let cacheLifetime: TimeInterval
|
||||
private let retryInterval: TimeInterval
|
||||
private let now: @Sendable () -> TimeInterval
|
||||
private var cached: (route: String, expiresAt: TimeInterval, address: String?)?
|
||||
|
||||
init(session: URLSession = .shared, cacheLifetime: TimeInterval = 600, retryInterval: TimeInterval = 30,
|
||||
init(session: URLSession? = nil, cacheLifetime: TimeInterval = 600, retryInterval: TimeInterval = 30,
|
||||
now: @escaping @Sendable () -> TimeInterval = { ProcessInfo.processInfo.systemUptime }) {
|
||||
precondition(cacheLifetime.isFinite && cacheLifetime > 0 && retryInterval.isFinite && retryInterval > 0)
|
||||
self.session = session
|
||||
self.ownsSession = session == nil
|
||||
self.session = session ?? URLSession(configuration: Self.sessionConfiguration())
|
||||
self.cacheLifetime = cacheLifetime
|
||||
self.retryInterval = retryInterval
|
||||
self.now = now
|
||||
}
|
||||
|
||||
/// No HTTP cache, cookies or credentials are persisted; only `cached` above retains results.
|
||||
static func sessionConfiguration() -> URLSessionConfiguration {
|
||||
let configuration = URLSessionConfiguration.ephemeral
|
||||
configuration.urlCache = nil
|
||||
configuration.httpCookieStorage = nil
|
||||
configuration.urlCredentialStorage = nil
|
||||
configuration.httpShouldSetCookies = false
|
||||
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
||||
return configuration
|
||||
}
|
||||
|
||||
deinit {
|
||||
if ownsSession { session.invalidateAndCancel() }
|
||||
}
|
||||
|
||||
/// The caller owns the task lifetime; route changes and hidden views cancel both HTTP and sleep.
|
||||
func refreshContinuously(route: String, onUpdate: @Sendable (String?) async -> Void) async {
|
||||
while !Task.isCancelled {
|
||||
|
||||
Reference in New Issue
Block a user