From 86417bf442631fc4c146e64bd478b84d0b0e7c17 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Fri, 1 Aug 2025 16:00:55 +0100 Subject: [PATCH] redid hostkey checking system fix hostkey checking just not happening :skulk: - moved logic into handler --- ShhShell/SSH/SSHHandler.swift | 13 +++++++-- ShhShell/Views/Fonts/FontManagerView.swift | 1 + ShhShell/Views/Hosts/ConnectionView.swift | 34 +++++++++++++--------- ShhShell/Views/Keys/KeyManagerView.swift | 1 + 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/ShhShell/SSH/SSHHandler.swift b/ShhShell/SSH/SSHHandler.swift index a214899..8ac2616 100644 --- a/ShhShell/SSH/SSHHandler.swift +++ b/ShhShell/SSH/SSHHandler.swift @@ -36,6 +36,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { @Published var bell: Bool = false @Published var host: Host + @Published var hostkeyChanged: Bool = false private let userDefaults = NSUbiquitousKeyValueStore.default private let logger = Logger(subsystem: "xy", category: "sshHandler") @@ -64,12 +65,15 @@ class SSHHandler: @unchecked Sendable, ObservableObject { return } + checkHostkey(recieved: getHostkey()) + guard hostkeyChanged == false else { return } + do { try authWithNone() } catch { print("auth with none is not authed") } guard state != .authorized else { return } - //TODO: check hostkey + for method in getAuthMethods() { guard state != .authorized else { break } @@ -164,8 +168,11 @@ class SSHHandler: @unchecked Sendable, ObservableObject { self.session = nil } - func checkHostkey() { - + func checkHostkey(recieved: String?) { + guard host.key == recieved else { + self.hostkeyChanged = true + return + } } func ring() { diff --git a/ShhShell/Views/Fonts/FontManagerView.swift b/ShhShell/Views/Fonts/FontManagerView.swift index 2e925af..c8d785a 100644 --- a/ShhShell/Views/Fonts/FontManagerView.swift +++ b/ShhShell/Views/Fonts/FontManagerView.swift @@ -60,6 +60,7 @@ struct FontManagerView: View { Section("Test String") { TextEditor(text: $testLine) + .fixedSize() } } } diff --git a/ShhShell/Views/Hosts/ConnectionView.swift b/ShhShell/Views/Hosts/ConnectionView.swift index 6251798..894f1a4 100644 --- a/ShhShell/Views/Hosts/ConnectionView.swift +++ b/ShhShell/Views/Hosts/ConnectionView.swift @@ -19,8 +19,6 @@ struct ConnectionView: View { @State var showTerminal: Bool = false - @State var hostKeyChangedAlert: Bool = false - var body: some View { ZStack { hostsManager.selectedTheme.background.suiColor.opacity(0.7) @@ -110,28 +108,36 @@ struct ConnectionView: View { } .scrollContentBackground(.hidden) .transition(.opacity) - .onChange(of: handler.host.key) { _ in - guard let previousKnownHost = hostsManager.getHostMatching(handler.host) else { return } - guard handler.host.key == previousKnownHost.key else { - hostKeyChangedAlert = true - return - } - } .onDisappear { hostsManager.updateHost(handler.host) } - .alert("Hostkey changed", isPresented: $hostKeyChangedAlert) { - Button("Accept New Hostkey", role: .destructive) { + .alert("Hostkey changed", isPresented: $handler.hostkeyChanged) { + Button(role: .destructive) { + handler.host.key = handler.getHostkey() hostsManager.updateHost(handler.host) handler.go() + } label: { + Text("Accept Hostkey") } - Button("Disconnect", role: .cancel) { + Button(role: .cancel) { handler.disconnect() - handler.host.key = hostsManager.getHostMatching(handler.host)?.key + } label: { + Text("Disconnect") + .tint(.blue) + .foregroundStyle(.blue) } + .tint(.blue) + .foregroundStyle(.blue) } message: { - Text("Expected \(handler.host.key ?? "nil")\nbut recieved \(handler.getHostkey() ?? "nil") from the server") + if let expectedKey = handler.host.key { + Text("Expected \(expectedKey)\nbut recieved\n \(handler.getHostkey() ?? "nil") from server") + } else { + Text(""" +The authenticity of \(handler.host.description) can't be established. +Hostkey fingerprint is \(handler.getHostkey() ?? "nil") +""") + } } .toolbar { ToolbarItem() { diff --git a/ShhShell/Views/Keys/KeyManagerView.swift b/ShhShell/Views/Keys/KeyManagerView.swift index 9884321..d9cc296 100644 --- a/ShhShell/Views/Keys/KeyManagerView.swift +++ b/ShhShell/Views/Keys/KeyManagerView.swift @@ -56,6 +56,7 @@ struct KeyManagerView: View { let comment = UIDevice().model + " at " + Date().formatted(date: .numeric, time: .omitted) keyManager.generateKey(type: .ed25519, comment: comment) } + .listRowSeparator(.hidden) CenteredLabel(title: "Import a key", systemName: "square.and.arrow.down") .onTapGesture {