Skip to content

Commit 8310bf6

Browse files
committed
app-ios: remove port forwards when client disconnects
1 parent 83c5c35 commit 8310bf6

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

app-ios/app-ios/ViewModels/ClientViewModel.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ class ClientViewModel: ObservableObject {
9292
private func handleAgentLeft(_ client: Client) {
9393
clients.removeAll { $0.mid == client.mid }
9494
sortClients()
95+
96+
// Post a notification that a client has left
97+
// This will be observed by PortForwardViewModel to clean up port forwards
98+
NotificationCenter.default.post(
99+
name: .clientDisconnected,
100+
object: nil,
101+
userInfo: ["clientId": client.mid]
102+
)
95103
}
96104

97105
func setFilterPattern(_ pattern: String) {
@@ -151,6 +159,7 @@ class ClientViewModel: ObservableObject {
151159

152160
extension Notification.Name {
153161
static let logoutRequested = Notification.Name("logoutRequested")
162+
static let clientDisconnected = Notification.Name("clientDisconnected")
154163
}
155164

156165
// Extension to convert Publisher to async/await

app-ios/app-ios/ViewModels/PortForwardViewModel.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ class PortForwardViewModel: ObservableObject {
5252

5353
init(webSocketService: WebSocketService = WebSocketService()) {
5454
self.webSocketService = webSocketService
55+
56+
// Set up notification observer for client disconnection
57+
NotificationCenter.default.addObserver(
58+
self,
59+
selector: #selector(handleClientDisconnected(_:)),
60+
name: .clientDisconnected,
61+
object: nil
62+
)
63+
}
64+
65+
deinit {
66+
NotificationCenter.default.removeObserver(self)
67+
}
68+
69+
@objc private func handleClientDisconnected(_ notification: Notification) {
70+
// Extract the client ID from the notification
71+
guard let clientId = notification.userInfo?["clientId"] as? String else {
72+
return
73+
}
74+
75+
// Close all port forwards for this client
76+
DispatchQueue.main.async { [weak self] in
77+
self?.closeAllPortForwardsForClient(clientId: clientId)
78+
}
5579
}
5680

5781
// Helper method for logging errors
@@ -142,6 +166,17 @@ class PortForwardViewModel: ObservableObject {
142166
objectWillChange.send()
143167
}
144168

169+
// Close all port forwards for a specific client
170+
func closeAllPortForwardsForClient(clientId: String) {
171+
// Get all port forward IDs for this client
172+
let portForwardIds = portForwards.values.filter { $0.clientId == clientId }.map { $0.id }
173+
174+
// Close each port forward
175+
for id in portForwardIds {
176+
closePortForward(id: id)
177+
}
178+
}
179+
145180
private func startLocalServer(for portForward: PortForward) {
146181
// Create a TCP listener on localhost with the specified port
147182
let port = NWEndpoint.Port(integerLiteral: UInt16(portForward.localPort))

0 commit comments

Comments
 (0)