From 43b407b0382f76aae541eda4479510214e708e91 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Wed, 25 Jun 2025 13:47:07 +0100 Subject: [PATCH] removed lots of testsucceeded = false by just setting it as false and then making it true if it suceeds added connecition failed and authfailed sshstates fix crash when unwrapping the hostkey data to a string --- ShhShell/SSH/SSHHandler.swift | 35 +++++++++-------------------- ShhShell/SSH/SSHState.swift | 3 +++ ShhShell/Views/ConnectionView.swift | 5 +++-- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/ShhShell/SSH/SSHHandler.swift b/ShhShell/SSH/SSHHandler.swift index 15be48f..8c8b5d0 100644 --- a/ShhShell/SSH/SSHHandler.swift +++ b/ShhShell/SSH/SSHHandler.swift @@ -40,10 +40,8 @@ class SSHHandler: @unchecked Sendable, ObservableObject { var hostkeyB64: UnsafeMutablePointer? = nil let status = ssh_pki_export_pubkey_base64(hostkey, &hostkeyB64) - guard status == SSH_OK else { return nil } - guard let data = hostkeyB64 else { return nil } - - return Data(base64Encoded: String(cString: data)) + guard status == SSH_OK, let cString = hostkeyB64 else { return nil } + return String(cString: cString).data(using: .utf8) } func go() { @@ -60,6 +58,9 @@ class SSHHandler: @unchecked Sendable, ObservableObject { } guard connected else { return } + try? authWithNone() + getAuthMethods() + self.host.key = getHostkey() if !host.password.isEmpty { do { try authWithPw() } catch { @@ -86,11 +87,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject { func connect() throws(SSHError) { withAnimation { state = .connecting } - defer { - try? authWithNone() - getAuthMethods() - self.host.key = getHostkey() - } var verbosity: Int = 0 // var verbosity: Int = SSH_LOG_FUNCTIONS @@ -154,25 +150,20 @@ class SSHHandler: @unchecked Sendable, ObservableObject { } func testExec() { + var success = false defer { - let result = self.testSuceeded disconnect() - withAnimation { testSuceeded = result } + withAnimation { testSuceeded = success } } if !checkAuth(state) { go() } + guard checkAuth(state) else { return } - if ssh_is_connected(session) == 0 { - withAnimation { testSuceeded = false } - return - } + if ssh_is_connected(session) == 0 { return } - guard checkAuth(state) else { - withAnimation { testSuceeded = false } - return - } + guard checkAuth(state) else { return } var status: CInt var buffer: [CChar] = Array(repeating: 0, count: 256) @@ -180,7 +171,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject { let testChannel = ssh_channel_new(session) guard testChannel != nil else { - withAnimation { testSuceeded = false } return } @@ -189,7 +179,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject { ssh_channel_free(testChannel) logger.critical("session opening error") logSshGetError() - withAnimation { testSuceeded = false } return } @@ -199,7 +188,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject { ssh_channel_free(testChannel) logger.critical("session opening error") logSshGetError() - withAnimation { testSuceeded = false } return } @@ -215,7 +203,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject { ssh_channel_free(testChannel) logger.critical("didnt read?") logSshGetError() - withAnimation { testSuceeded = false } return } @@ -223,7 +210,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { ssh_channel_close(testChannel) ssh_channel_free(testChannel) print("testExec succeeded") - withAnimation { testSuceeded = true } + success = true return } diff --git a/ShhShell/SSH/SSHState.swift b/ShhShell/SSH/SSHState.swift index 3eed7b3..a3fc09d 100644 --- a/ShhShell/SSH/SSHState.swift +++ b/ShhShell/SSH/SSHState.swift @@ -13,6 +13,9 @@ enum SSHState { case authorizing case authorized case shellOpen + + case connectionFailed + case authFailed } func checkConnected(_ state: SSHState) -> Bool { diff --git a/ShhShell/Views/ConnectionView.swift b/ShhShell/Views/ConnectionView.swift index fa810af..17bf70e 100644 --- a/ShhShell/Views/ConnectionView.swift +++ b/ShhShell/Views/ConnectionView.swift @@ -76,8 +76,9 @@ struct ConnectionView: View { TextField("", text: $passphrase, prompt: Text("Passphrase (Optional)")) } - if handler.host.key != nil { - Text("Hostkey: \(handler.host.key!.base64EncodedString())") + if handler.host.key != nil, + let hostkeyString = String(data: handler.host.key!, encoding: .utf8) { + Text("Hostkey: "+hostkeyString) .onChange(of: handler.host.key) { _ in guard let previousKnownHost = hostsManager.getHostMatching(handler.host) else { return } guard handler.host.key == previousKnownHost.key else {