diff --git a/ShhShell/Host/Host.swift b/ShhShell/Host/Host.swift index 4b4b89f..d73bc38 100644 --- a/ShhShell/Host/Host.swift +++ b/ShhShell/Host/Host.swift @@ -13,6 +13,9 @@ protocol HostPr: Codable, Identifiable, Equatable { var port: Int { get set } var username: String { get set } var password: String { get set } + var publicKey: Data? { get set } + var privateKey: Data? { get set } + var passphrase: String { get set } var key: Data? { get set } } @@ -22,28 +25,37 @@ struct Host: HostPr { var port: Int var username: String var password: String + var publicKey: Data? + var privateKey: Data? + var passphrase: String = "" var key: Data? init( address: String, port: Int = 22, - username: String, - password: String, + username: String = "", + password: String = "", + publicKey: Data? = nil, + privateKey: Data? = nil, + passphrase: String = "", hostkey: Data? = nil ) { self.address = address self.port = port self.username = username self.password = password + self.publicKey = publicKey + self.privateKey = privateKey + self.passphrase = passphrase self.key = hostkey } } extension Host { static var blank: Host { - Host(address: "", port: 22, username: "", password: "") + Host(address: "") } static var debug: Host { - Host(address: "localhost", port: 22, username: "default", password: "") + Host(address: "localhost", username: "default", password: "") } } diff --git a/ShhShell/SSH/SSHHandler.swift b/ShhShell/SSH/SSHHandler.swift index 2809538..c785bff 100644 --- a/ShhShell/SSH/SSHHandler.swift +++ b/ShhShell/SSH/SSHHandler.swift @@ -19,19 +19,16 @@ class SSHHandler: ObservableObject { @Published var authorized: Bool = false @Published var testSuceeded: Bool = false - @Published var host: HostPr + @Published var host: Host @Published var terminal: String = "" private let userDefaults = NSUbiquitousKeyValueStore.default private let logger = Logger(subsystem: "xy", category: "sshHandler") init( - host: HostPr + host: Host ) { self.host = host -#if DEBUG - self.host = Host.debug -#endif } func getHostkey() -> Data? { diff --git a/ShhShell/Views/ConnectionView.swift b/ShhShell/Views/ConnectionView.swift index 72cded5..0e40959 100644 --- a/ShhShell/Views/ConnectionView.swift +++ b/ShhShell/Views/ConnectionView.swift @@ -79,11 +79,6 @@ struct ConnectionView: View { .modifier(foregroundColorStyle(handler.authorized ? .green : .red)) } - // if let testSucceded = testSucceded { - // Image(systemName: testSucceded ? "checkmark.circle" : "xmark.circle") - // .modifier(foregroundColorStyle(testSucceded ? .green : .red)) - // } - if handler.host.key != nil { Text("Hostkey: \(handler.host.key!.base64EncodedString())") } @@ -125,7 +120,7 @@ struct ConnectionView: View { Label("Connect", systemImage: "powerplug.portrait") } .disabled( - pubkey == nil && privkey == nil || + pubkey == nil && privkey == nil && handler.host.username.isEmpty && handler.host.password.isEmpty ) } @@ -147,10 +142,16 @@ struct ConnectionView: View { Label("Test Connection", systemImage: "checkmark") } } - .disabled(!(handler.connected && handler.authorized)) +// .disabled(!(handler.connected && handler.authorized)) } .transition(.opacity) } + .onDisappear { + if let index = hostsManager.savedHosts.firstIndex(where: { $0.id == handler.host.id }) { + hostsManager.savedHosts[index] = handler.host + hostsManager.saveSavedHosts() + } + } } } diff --git a/ShhShell/Views/HostsView.swift b/ShhShell/Views/HostsView.swift index 89e9b7f..f9d69ff 100644 --- a/ShhShell/Views/HostsView.swift +++ b/ShhShell/Views/HostsView.swift @@ -14,28 +14,64 @@ struct HostsView: View { var body: some View { NavigationStack { List { - Text("hi") - ForEach(hostsManager.savedHosts) { host in - NavigationLink() { - ConnectionView( - handler: SSHHandler(host: host), - keyManager: keyManager, - hostsManager: hostsManager - ) + if hostsManager.savedHosts.isEmpty { + Text("Add your first Host!") + Button() { + withAnimation { hostsManager.savedHosts.append(Host.blank) } } label: { - Text(host.address) + Text("Create") +// .font() } - .onChange(of: host) { _ in - hostsManager.saveSavedHosts() + .buttonStyle(.borderedProminent) + } + ForEach(hostsManager.savedHosts) { host in + HStack { + if host.address.isEmpty { + Text(host.id.uuidString) + } else { + Text(host.address) + } + NavigationLink() { + ConnectionView( + handler: SSHHandler(host: host), + keyManager: keyManager, + hostsManager: hostsManager + ) + } label: { + Image(systemName: "info.circle") + } + .onChange(of: host) { _ in + hostsManager.saveSavedHosts() + } + } + .swipeActions(edge: .trailing) { + Button(role: .destructive) { + if let index = hostsManager.savedHosts.firstIndex(where: { $0.id == host.id }) { + let _ = withAnimation { hostsManager.savedHosts.remove(at: index) } + hostsManager.saveSavedHosts() + } + } label: { + Label("Delete", systemImage: "trash") + } } } } + .transition(.scale) .toolbar { ToolbarItem(placement: .confirmationAction) { - Button { - hostsManager.savedHosts.append(Host.blank) - } label: { - Label("Add", systemImage: "plus") + if !hostsManager.savedHosts.isEmpty { + NavigationLink { + ConnectionView( + handler: SSHHandler(host: hostsManager.savedHosts.last!), + keyManager: keyManager, + hostsManager: hostsManager + ) + .onAppear() { + withAnimation { hostsManager.savedHosts.append(Host.blank) } + } + } label: { + Label("Add", systemImage: "plus") + } } } }