fix(mac): harden gateway lock and ip decoding
parent
1a0e57d926
commit
d33a3f619a
|
|
@ -50,6 +50,21 @@ final class GatewayProcessManager: ObservableObject {
|
||||||
private var stopping = false
|
private var stopping = false
|
||||||
private var recentCrashes: [Date] = []
|
private var recentCrashes: [Date] = []
|
||||||
|
|
||||||
|
private final class GatewayLockHandle {
|
||||||
|
private let fd: FileDescriptor
|
||||||
|
private let path: String
|
||||||
|
|
||||||
|
init(fd: FileDescriptor, path: String) {
|
||||||
|
self.fd = fd
|
||||||
|
self.path = path
|
||||||
|
}
|
||||||
|
|
||||||
|
func cancel() {
|
||||||
|
try? self.fd.close()
|
||||||
|
try? FileManager.default.removeItem(atPath: self.path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private let logger = Logger(subsystem: "com.steipete.clawdis", category: "gateway")
|
private let logger = Logger(subsystem: "com.steipete.clawdis", category: "gateway")
|
||||||
private let logLimit = 20000 // characters to keep in-memory
|
private let logLimit = 20000 // characters to keep in-memory
|
||||||
private let maxCrashes = 3
|
private let maxCrashes = 3
|
||||||
|
|
@ -182,20 +197,17 @@ final class GatewayProcessManager: ObservableObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Minimal clone of the Node gateway lock: bind a UDS and return the listener.
|
/// Minimal clone of the Node gateway lock: take an exclusive file lock.
|
||||||
private func acquireGatewayLock(path: String) throws -> NWListener {
|
private func acquireGatewayLock(path: String) throws -> GatewayLockHandle {
|
||||||
// Remove stale socket if needed
|
// Remove stale lock if needed (mirrors CLI behavior).
|
||||||
try? FileManager.default.removeItem(atPath: path)
|
try? FileManager.default.removeItem(atPath: path)
|
||||||
let params = NWParameters.tcp
|
let fd = try FileDescriptor.open(
|
||||||
params.allowLocalEndpointReuse = false
|
FilePath(path),
|
||||||
let endpoint = NWEndpoint.unix(path: path)
|
.readWrite,
|
||||||
let listener = try NWListener(using: params, on: endpoint)
|
options: [.create, .exclusiveCreate],
|
||||||
listener.newConnectionHandler = { connection in
|
permissions: [.ownerReadWrite]
|
||||||
// Any new connection indicates another starter; reject.
|
)
|
||||||
connection.cancel()
|
return GatewayLockHandle(fd: fd, path: path)
|
||||||
}
|
|
||||||
listener.start(queue: .global())
|
|
||||||
return listener
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func didStart(_ execution: Execution) {
|
private func didStart(_ execution: Execution) {
|
||||||
|
|
|
||||||
|
|
@ -226,7 +226,9 @@ extension WebChatWindowController {
|
||||||
0,
|
0,
|
||||||
NI_NUMERICHOST) == 0
|
NI_NUMERICHOST) == 0
|
||||||
{
|
{
|
||||||
let ip = String(cString: hostname)
|
let end = hostname.firstIndex(of: 0) ?? hostname.count
|
||||||
|
let bytes = hostname[..<end].map { UInt8(bitPattern: $0) }
|
||||||
|
let ip = String(decoding: bytes, as: UTF8.self)
|
||||||
if !ip.hasPrefix("169.254") { return ip }
|
if !ip.hasPrefix("169.254") { return ip }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue