Skip to content

Commit 575f029

Browse files
committed
Fix CI compile: WebProxyCatalog Codable + skip-rebuild helper
- WebProxyCatalogEntry: Decodable only (CodingKeys.secret broke Encodable) - WebProxyManager: implement shouldSkipCarrierRebuildDueToRecentActivity - AppDelegate: strong Box capture; use beginBackgroundKeepalive; wire willEnterForeground - MTWebSocketConnectionInterface: import Security for SNI override Co-authored-by: D3C0Y <decoder-dev@users.noreply.github.com>
1 parent 435b675 commit 575f029

4 files changed

Lines changed: 32 additions & 34 deletions

File tree

‎submodules/TelegramCore/Sources/Network/MTWebSocketConnectionInterface.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import Network
3+
import Security
34

45
import MtProtoKit
56
import SwiftSignalKit

‎submodules/TelegramUI/Sources/AppDelegate.swift‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,17 +2088,18 @@ private func extractAccountManagerState(records: AccountRecordsView<TelegramAcco
20882088
// enough — the relay's own keep-alive timer runs on the server side.
20892089
final class Box { var value: UIBackgroundTaskIdentifier = .invalid }
20902090
let webProxyTaskBox = Box()
2091-
webProxyTaskBox.value = application.beginBackgroundTask(withName: "web-proxy-keepalive") { [weak webProxyTaskBox] in
2092-
if let box = webProxyTaskBox, box.value != .invalid {
2093-
application.endBackgroundTask(box.value)
2094-
box.value = .invalid
2091+
let endWebProxyKeepaliveTask: () -> Void = {
2092+
if webProxyTaskBox.value != .invalid {
2093+
application.endBackgroundTask(webProxyTaskBox.value)
2094+
webProxyTaskBox.value = .invalid
20952095
}
20962096
}
2097-
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { [weak webProxyTaskBox] in
2098-
let _ = WebProxyManager.shared.sendKeepalivePing()
2099-
if let box = webProxyTaskBox, box.value != .invalid {
2100-
application.endBackgroundTask(box.value)
2101-
box.value = .invalid
2097+
webProxyTaskBox.value = application.beginBackgroundTask(withName: "web-proxy-keepalive", expirationHandler: endWebProxyKeepaliveTask)
2098+
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
2099+
// beginBackgroundKeepalive ends the task via expirationHandler ~1s after the ping;
2100+
// if there is no live WEB sidecar, end it here.
2101+
if !WebProxyManager.shared.beginBackgroundKeepalive(expirationHandler: endWebProxyKeepaliveTask) {
2102+
endWebProxyKeepaliveTask()
21022103
}
21032104
}
21042105
}
@@ -2144,6 +2145,8 @@ private func extractAccountManagerState(records: AccountRecordsView<TelegramAcco
21442145
}
21452146
}
21462147
}
2148+
2149+
WebProxyManager.shared.applicationWillEnterForeground()
21472150

21482151
self.runForegroundTasks()
21492152

‎submodules/WebProxyTransport/Sources/WebProxyCatalog.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22

33
/// A single entry in the WEB-proxy public catalog.
4-
public struct WebProxyCatalogEntry: Equatable, Codable {
4+
public struct WebProxyCatalogEntry: Equatable, Decodable {
55
public var title: String
66
public var host: String
77
/// Raw hex of the proxy secret (`dd…` or `ee…` prefix).

‎submodules/WebProxyTransport/Sources/WebProxyManager.swift‎

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,25 @@ public final class WebProxyManager {
209209
self.lock.unlock()
210210
guard let sidecar = sidecar else { return false }
211211
sidecar.sendKeepalivePing()
212+
self.lock.lock()
213+
self.lastActivityAt = CFAbsoluteTimeGetCurrent()
214+
self.lock.unlock()
212215
return true
213216
}
217+
218+
/// True when a live sidecar recently carried traffic (or we just keepalive-pinged it), so a
219+
/// foreground resume can leave the carrier alone instead of rebuilding.
220+
private func shouldSkipCarrierRebuildDueToRecentActivity(sidecar: WebProxySidecar?, hasEndpoint: Bool) -> Bool {
221+
guard hasEndpoint, let sidecar else {
222+
return false
223+
}
224+
let sidecarAge = sidecar.secondsSinceLastActivity()
225+
self.lock.lock()
226+
let managerStamp = self.lastActivityAt
227+
self.lock.unlock()
228+
let managerAge = managerStamp > 0 ? CFAbsoluteTimeGetCurrent() - managerStamp : Double.infinity
229+
return min(sidecarAge, managerAge) < 45.0
230+
}
214231

215232
/// Call from `applicationWillEnterForeground`. If the sidecar is still ready and recently
216233
/// active, skip the later carrier rebuild by clearing `enteredBackgroundAt` only.
@@ -239,17 +256,9 @@ public final class WebProxyManager {
239256
/// no live WEB sidecar. `expirationHandler` is invoked shortly after the ping is queued so the
240257
/// caller can end its `beginBackgroundTask`.
241258
public func beginBackgroundKeepalive(expirationHandler: @escaping () -> Void) -> Bool {
242-
self.lock.lock()
243-
let sidecar = self.sidecar
244-
let ready = sidecar != nil && self.endpoint != nil
245-
self.lock.unlock()
246-
guard ready, let sidecar else {
259+
guard self.sendKeepalivePing() else {
247260
return false
248261
}
249-
sidecar.sendKeepalivePing()
250-
self.lock.lock()
251-
self.lastActivityAt = CFAbsoluteTimeGetCurrent()
252-
self.lock.unlock()
253262
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 1.0, execute: expirationHandler)
254263
return true
255264
}
@@ -300,21 +309,6 @@ public final class WebProxyManager {
300309
return
301310
}
302311

303-
// If the sidecar is healthy and had recent activity (keepalive kept it alive), skip the
304-
// carrier rebuild. "Recent" = within the last 45 s — covers brief suspensions where the
305-
// background task managed to run.
306-
self.lock.lock()
307-
let liveSidecar = self.sidecar
308-
let hasLiveEndpoint = self.endpoint != nil
309-
self.lock.unlock()
310-
if hasLiveEndpoint, let liveSidecar = liveSidecar, liveSidecar.secondsSinceLastActivity() < 45 {
311-
self.startLock.lock()
312-
self.enteredBackgroundAt = 0
313-
self.startLock.unlock()
314-
WebProxyLog.log("skip carrier rebuild: recent activity (\(Int(liveSidecar.secondsSinceLastActivity()))s ago)")
315-
return
316-
}
317-
318312
if self.shouldSkipCarrierRebuildDueToRecentActivity(sidecar: sidecar, hasEndpoint: hasEndpoint) {
319313
self.startLock.lock()
320314
self.enteredBackgroundAt = 0

0 commit comments

Comments
 (0)