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
This commit is contained in:
neon443
2025-06-25 13:47:07 +01:00
parent 046dbbb06c
commit 43b407b038
3 changed files with 17 additions and 26 deletions

View File

@@ -40,10 +40,8 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
var hostkeyB64: UnsafeMutablePointer<CChar>? = 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
}

View File

@@ -13,6 +13,9 @@ enum SSHState {
case authorizing
case authorized
case shellOpen
case connectionFailed
case authFailed
}
func checkConnected(_ state: SSHState) -> Bool {

View File

@@ -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 {