mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 13:26:16 +00:00
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:
@@ -40,10 +40,8 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
var hostkeyB64: UnsafeMutablePointer<CChar>? = nil
|
var hostkeyB64: UnsafeMutablePointer<CChar>? = nil
|
||||||
|
|
||||||
let status = ssh_pki_export_pubkey_base64(hostkey, &hostkeyB64)
|
let status = ssh_pki_export_pubkey_base64(hostkey, &hostkeyB64)
|
||||||
guard status == SSH_OK else { return nil }
|
guard status == SSH_OK, let cString = hostkeyB64 else { return nil }
|
||||||
guard let data = hostkeyB64 else { return nil }
|
return String(cString: cString).data(using: .utf8)
|
||||||
|
|
||||||
return Data(base64Encoded: String(cString: data))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func go() {
|
func go() {
|
||||||
@@ -60,6 +58,9 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
}
|
}
|
||||||
guard connected else { return }
|
guard connected else { return }
|
||||||
|
|
||||||
|
try? authWithNone()
|
||||||
|
getAuthMethods()
|
||||||
|
self.host.key = getHostkey()
|
||||||
|
|
||||||
if !host.password.isEmpty {
|
if !host.password.isEmpty {
|
||||||
do { try authWithPw() } catch {
|
do { try authWithPw() } catch {
|
||||||
@@ -86,11 +87,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
|
|
||||||
func connect() throws(SSHError) {
|
func connect() throws(SSHError) {
|
||||||
withAnimation { state = .connecting }
|
withAnimation { state = .connecting }
|
||||||
defer {
|
|
||||||
try? authWithNone()
|
|
||||||
getAuthMethods()
|
|
||||||
self.host.key = getHostkey()
|
|
||||||
}
|
|
||||||
|
|
||||||
var verbosity: Int = 0
|
var verbosity: Int = 0
|
||||||
// var verbosity: Int = SSH_LOG_FUNCTIONS
|
// var verbosity: Int = SSH_LOG_FUNCTIONS
|
||||||
@@ -154,25 +150,20 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testExec() {
|
func testExec() {
|
||||||
|
var success = false
|
||||||
defer {
|
defer {
|
||||||
let result = self.testSuceeded
|
|
||||||
disconnect()
|
disconnect()
|
||||||
withAnimation { testSuceeded = result }
|
withAnimation { testSuceeded = success }
|
||||||
}
|
}
|
||||||
|
|
||||||
if !checkAuth(state) {
|
if !checkAuth(state) {
|
||||||
go()
|
go()
|
||||||
}
|
}
|
||||||
|
guard checkAuth(state) else { return }
|
||||||
|
|
||||||
if ssh_is_connected(session) == 0 {
|
if ssh_is_connected(session) == 0 { return }
|
||||||
withAnimation { testSuceeded = false }
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
guard checkAuth(state) else {
|
guard checkAuth(state) else { return }
|
||||||
withAnimation { testSuceeded = false }
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var status: CInt
|
var status: CInt
|
||||||
var buffer: [CChar] = Array(repeating: 0, count: 256)
|
var buffer: [CChar] = Array(repeating: 0, count: 256)
|
||||||
@@ -180,7 +171,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
|
|
||||||
let testChannel = ssh_channel_new(session)
|
let testChannel = ssh_channel_new(session)
|
||||||
guard testChannel != nil else {
|
guard testChannel != nil else {
|
||||||
withAnimation { testSuceeded = false }
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +179,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
ssh_channel_free(testChannel)
|
ssh_channel_free(testChannel)
|
||||||
logger.critical("session opening error")
|
logger.critical("session opening error")
|
||||||
logSshGetError()
|
logSshGetError()
|
||||||
withAnimation { testSuceeded = false }
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,7 +188,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
ssh_channel_free(testChannel)
|
ssh_channel_free(testChannel)
|
||||||
logger.critical("session opening error")
|
logger.critical("session opening error")
|
||||||
logSshGetError()
|
logSshGetError()
|
||||||
withAnimation { testSuceeded = false }
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +203,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
ssh_channel_free(testChannel)
|
ssh_channel_free(testChannel)
|
||||||
logger.critical("didnt read?")
|
logger.critical("didnt read?")
|
||||||
logSshGetError()
|
logSshGetError()
|
||||||
withAnimation { testSuceeded = false }
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,7 +210,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
ssh_channel_close(testChannel)
|
ssh_channel_close(testChannel)
|
||||||
ssh_channel_free(testChannel)
|
ssh_channel_free(testChannel)
|
||||||
print("testExec succeeded")
|
print("testExec succeeded")
|
||||||
withAnimation { testSuceeded = true }
|
success = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ enum SSHState {
|
|||||||
case authorizing
|
case authorizing
|
||||||
case authorized
|
case authorized
|
||||||
case shellOpen
|
case shellOpen
|
||||||
|
|
||||||
|
case connectionFailed
|
||||||
|
case authFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkConnected(_ state: SSHState) -> Bool {
|
func checkConnected(_ state: SSHState) -> Bool {
|
||||||
|
|||||||
@@ -76,8 +76,9 @@ struct ConnectionView: View {
|
|||||||
TextField("", text: $passphrase, prompt: Text("Passphrase (Optional)"))
|
TextField("", text: $passphrase, prompt: Text("Passphrase (Optional)"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if handler.host.key != nil {
|
if handler.host.key != nil,
|
||||||
Text("Hostkey: \(handler.host.key!.base64EncodedString())")
|
let hostkeyString = String(data: handler.host.key!, encoding: .utf8) {
|
||||||
|
Text("Hostkey: "+hostkeyString)
|
||||||
.onChange(of: handler.host.key) { _ in
|
.onChange(of: handler.host.key) { _ in
|
||||||
guard let previousKnownHost = hostsManager.getHostMatching(handler.host) else { return }
|
guard let previousKnownHost = hostsManager.getHostMatching(handler.host) else { return }
|
||||||
guard handler.host.key == previousKnownHost.key else {
|
guard handler.host.key == previousKnownHost.key else {
|
||||||
|
|||||||
Reference in New Issue
Block a user