remote(server): fix crash when invalid port is specified

Fixes #6584
This commit is contained in:
osy 2024-08-23 00:51:32 -05:00
parent c197864054
commit 6bb5b20670
2 changed files with 6 additions and 3 deletions

View file

@ -223,9 +223,12 @@ struct ServerSettingsView: View {
.multilineTextAlignment(.trailing) .multilineTextAlignment(.trailing)
.help("Specify a port number to listen on. This is required if external clients are permitted.") .help("Specify a port number to listen on. This is required if external clients are permitted.")
.onChange(of: serverPort) { newValue in .onChange(of: serverPort) { newValue in
if serverPort == 0 { if newValue == 0 {
isServerExternal = false isServerExternal = false
} }
if newValue < 0 || newValue >= UInt16.max {
serverPort = defaultPort
}
} }
} }
Section(header: Text("Authentication")) { Section(header: Text("Authentication")) {

View file

@ -131,7 +131,7 @@ actor UTMRemoteServer {
registerNotifications() registerNotifications()
listener = Task { listener = Task {
await withErrorNotification { await withErrorNotification {
if isServerExternal && serverPort > 0 { if isServerExternal && serverPort > 0 && serverPort <= UInt16.max {
natPort = Port.TCP(internalPort: UInt16(serverPort)) natPort = Port.TCP(internalPort: UInt16(serverPort))
natPort!.mappingChangedHandler = { port in natPort!.mappingChangedHandler = { port in
Task { Task {
@ -146,7 +146,7 @@ actor UTMRemoteServer {
} }
} }
} }
let port = serverPort > 0 ? NWEndpoint.Port(integerLiteral: UInt16(serverPort)) : .any let port = serverPort > 0 && serverPort <= UInt16.max ? NWEndpoint.Port(integerLiteral: UInt16(serverPort)) : .any
for try await connection in Connection.advertise(on: port, forServiceType: service, txtRecord: metadata, connectionQueue: connectionQueue, identity: keyManager.identity) { for try await connection in Connection.advertise(on: port, forServiceType: service, txtRecord: metadata, connectionQueue: connectionQueue, identity: keyManager.identity) {
let connection = try? await Connection(connection: connection, connectionQueue: connectionQueue) { connection, error in let connection = try? await Connection(connection: connection, connectionQueue: connectionQueue) { connection, error in
Task { Task {