Skip to content

Commit

Permalink
remote(client): support connecting to specified host and port
Browse files Browse the repository at this point in the history
  • Loading branch information
osy committed Feb 12, 2024
1 parent ad616a1 commit 8ecdb85
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
23 changes: 19 additions & 4 deletions Platform/iOS/UTMRemoteConnectView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,31 @@ private struct ServerConnectView: View {
NavigationView {
Form {
Section {
DefaultTextField("", text: $server.name, prompt: "Name")
if #available(iOS 15, *) {
TextField("", text: $server.name, prompt: Text("Name (optional)"))
} else {
DefaultTextField("", text: $server.name, prompt: "Name (optional)")
}
} header: {
Text("Name")
}
Section {
if server.endpoint != nil {
Text(server.hostname)
} else {
DefaultTextField("", text: $server.hostname, prompt: "Hostname or IP address")
NumberTextField("", number: $server.port, prompt: "Port")
if #available(iOS 15, *) {
TextField("", text: $server.hostname, prompt: Text("Hostname or IP address"))
.keyboardType(.asciiCapable)
.autocorrectionDisabled()
.textInputAutocapitalization(.never)
TextField("", value: $server.port, format: .number.grouping(.never), prompt: Text("Port"))
.keyboardType(.decimalPad)
} else {
DefaultTextField("", text: $server.hostname, prompt: "Hostname or IP address")
.keyboardType(.asciiCapable)
.autocorrectionDisabled()
NumberTextField("", number: $server.port, prompt: "Port")
}
}
} header: {
Text("Host")
Expand Down Expand Up @@ -202,7 +217,7 @@ private struct ServerConnectView: View {
} else {
Text("Connect")
}
}
}.disabled(server.hostname.isEmpty || !server.isAvailable)
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions Remote/UTMRemoteClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import Network
import SwiftConnect

let service = "_utm_server._tcp"
let defaultServerPort = 21589

actor UTMRemoteClient {
let state: State
Expand Down Expand Up @@ -86,9 +85,7 @@ actor UTMRemoteClient {
func connect(_ server: State.SavedServer) async throws {
var server = server
var isSuccessful = false
guard let endpoint = server.endpoint else {
throw ConnectionError.cannotFindEndpoint
}
let endpoint = server.endpoint ?? NWEndpoint.hostPort(host: .init(server.hostname), port: .init(integerLiteral: UInt16(server.port ?? 0)))
try await keyManager.load()
let connection = try await Connection(endpoint: endpoint, identity: keyManager.identity)
defer {
Expand All @@ -110,13 +107,17 @@ actor UTMRemoteClient {
try Task.checkCancellation()
let peer = Peer(connection: connection, localInterface: local)
let remote = Remote(peer: peer, host: host)
try await remote.handshake()
let device = try await remote.handshake()
self.server = remote
await state.setConnected(true)
if !server.shouldSavePassword {
server.password = nil
}
if server.name.isEmpty {
server.name = server.hostname
}
server.lastSeen = Date()
server.model = device.model
await state.save(server: server)
isSuccessful = true
}
Expand Down Expand Up @@ -158,12 +159,11 @@ extension UTMRemoteClient {
}

var isAvailable: Bool {
endpoint != nil || port != nil
endpoint != nil || (port != nil && port != 0)
}

init() {
self.hostname = ""
self.port = defaultServerPort
self.name = ""
self.lastSeen = Date()
self.fingerprint = ""
Expand Down Expand Up @@ -309,12 +309,13 @@ extension UTMRemoteClient {
peer.close()
}

func handshake() async throws {
func handshake() async throws -> MacDevice {
let reply = try await _handshake(parameters: .init(version: UTMRemoteMessageServer.version))
guard reply.version == UTMRemoteMessageServer.version else {
throw ClientError.versionMismatch
}
capabilities = reply.capabilities
return MacDevice(model: reply.model)
}

func listVirtualMachines() async throws -> [M.ListVirtualMachines.Information] {
Expand Down Expand Up @@ -445,7 +446,6 @@ extension UTMRemoteClient {

extension UTMRemoteClient {
enum ConnectionError: LocalizedError {
case cannotFindEndpoint
case cannotDetermineHost
case cannotFindFingerprint
case passwordRequired
Expand All @@ -455,8 +455,6 @@ extension UTMRemoteClient {

var errorDescription: String? {
switch self {
case .cannotFindEndpoint:
return NSLocalizedString("The server has disappeared.", comment: "UTMRemoteClient")
case .cannotDetermineHost:
return NSLocalizedString("Failed to determine host name.", comment: "UTMRemoteClient")
case .cannotFindFingerprint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"location" : "https://github.com/utmapp/SwiftConnect",
"state" : {
"branch" : "main",
"revision" : "c6e84abcc1563a1ec6521d6649b5b918494539bc"
"revision" : "1ba2f10260ad46ddcdb3f8c0d44be385ff89f11f"
}
},
{
Expand Down

0 comments on commit 8ecdb85

Please sign in to comment.