From 66547dc58eae5ef5e63c2cc66bdc2f2ca6f2ebc5 Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Wed, 25 Jun 2025 12:26:29 +0100 Subject: [PATCH] migrated from bunch of bools to an enum of ssh states --- ShhShell/SSH/SSHHandler.swift | 35 ++++++++++++++++++----------- ShhShell/SSH/SSHState.swift | 1 - ShhShell/Views/ConnectionView.swift | 12 ++++------ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/ShhShell/SSH/SSHHandler.swift b/ShhShell/SSH/SSHHandler.swift index b408779..e3a57ee 100644 --- a/ShhShell/SSH/SSHHandler.swift +++ b/ShhShell/SSH/SSHHandler.swift @@ -18,8 +18,13 @@ class SSHHandler: @unchecked Sendable, ObservableObject { // @Published var hostsManager = HostsManager() @Published var title: String = "" - @Published var connected: Bool = false - @Published var authorized: Bool = false + @Published var state: SSHState = .idle + var connected: Bool { + return !(state == .idle || state == .connecting) +// return state == .authorized || state == .shellOpen || state == .authorizing + } +// @Published var connected: Bool = false +// @Published var authorized: Bool = false @Published var testSuceeded: Bool? = nil @Published var bell: UUID? = nil @@ -90,6 +95,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { } func connect() throws(SSHError) { + withAnimation { state = .connecting } defer { try? authWithNone() getAuthMethods() @@ -101,7 +107,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { session = ssh_new() guard session != nil else { - withAnimation { connected = false } + withAnimation { state = .idle } throw .backendError("Failed opening session") } @@ -114,17 +120,18 @@ class SSHHandler: @unchecked Sendable, ObservableObject { if status != SSH_OK { logger.critical("connection not ok: \(status)") logSshGetError() - withAnimation { connected = false } + withAnimation { state = .idle } throw .connectionFailed("Failed connecting") } - withAnimation { connected = true } + withAnimation { state = .authorizing } return } func disconnect() async { await MainActor.run { - withAnimation { connected = false } - withAnimation { authorized = false } + withAnimation { state = .idle } +// withAnimation { connected = false } +// withAnimation { authorized = false } withAnimation { testSuceeded = nil } } @@ -164,12 +171,16 @@ class SSHHandler: @unchecked Sendable, ObservableObject { } } + if state == .authorized {} else { + go() + } + if ssh_is_connected(session) == 0 { withAnimation { testSuceeded = false } return } - guard authorized else { + guard state == .authorized else { withAnimation { testSuceeded = false } return } @@ -233,7 +244,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject { //MARK: auth func authWithPubkey(pub pubInp: Data, priv privInp: Data, pass: String) throws(KeyError) { guard session != nil else { - withAnimation { authorized = false } return } @@ -276,12 +286,11 @@ class SSHHandler: @unchecked Sendable, ObservableObject { } if (ssh_userauth_publickey(session, nil, privkey) != 0) { - withAnimation { authorized = false } throw .privkeyRejected } //if u got this far, youre authed! - withAnimation { authorized = true } + withAnimation { state = .authorized } ssh_key_free(pubkey) ssh_key_free(privkey) @@ -302,7 +311,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { logSshGetError() throw .rejectedCredentials } - withAnimation { authorized = true } + withAnimation { state = .authorized } return } @@ -311,7 +320,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject { guard status == SSH_AUTH_SUCCESS.rawValue else { throw .rejectedCredentials } logCritical("no security moment lol") - withAnimation { authorized = true } + withAnimation { state = .authorized } return } diff --git a/ShhShell/SSH/SSHState.swift b/ShhShell/SSH/SSHState.swift index 120440e..db31e61 100644 --- a/ShhShell/SSH/SSHState.swift +++ b/ShhShell/SSH/SSHState.swift @@ -13,5 +13,4 @@ enum SSHState { case authorizing case authorized case shellOpen -// case unauthorized } diff --git a/ShhShell/Views/ConnectionView.swift b/ShhShell/Views/ConnectionView.swift index 4b45748..f9aeae6 100644 --- a/ShhShell/Views/ConnectionView.swift +++ b/ShhShell/Views/ConnectionView.swift @@ -29,8 +29,8 @@ struct ConnectionView: View { Text(handler.connected ? "connected" : "not connected") .modifier(foregroundColorStyle(handler.connected ? .green : .red)) - Text(handler.authorized ? "authorized" : "unauthorized") - .modifier(foregroundColorStyle(handler.authorized ? .green : .red)) + Text(handler.state == .authorized ? "authorized" : "unauthorized") + .modifier(foregroundColorStyle(handler.state == .authorized ? .green : .red)) } TextField("address", text: $handler.host.address) .textFieldStyle(.roundedBorder) @@ -91,13 +91,9 @@ struct ConnectionView: View { } label: { Label("Show Terminal", systemImage: "apple.terminal") } - .disabled(!handler.connected || !handler.authorized) + .disabled(!handler.connected || !(handler.state == .authorized)) Button() { - if handler.authorized && handler.connected { - } else { - handler.go() - } handler.testExec() } label: { if let testResult = handler.testSuceeded { @@ -127,7 +123,7 @@ struct ConnectionView: View { ToolbarItem() { Button() { handler.go() - showTerminal = handler.connected && handler.authorized + showTerminal = handler.connected && handler.state == .authorized } label: { Label( handler.connected ? "Disconnect" : "Connect",