mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 21:36:17 +00:00
migrated from bunch of bools to an enum of ssh states
This commit is contained in:
@@ -18,8 +18,13 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
// @Published var hostsManager = HostsManager()
|
// @Published var hostsManager = HostsManager()
|
||||||
|
|
||||||
@Published var title: String = ""
|
@Published var title: String = ""
|
||||||
@Published var connected: Bool = false
|
@Published var state: SSHState = .idle
|
||||||
@Published var authorized: Bool = false
|
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 testSuceeded: Bool? = nil
|
||||||
|
|
||||||
@Published var bell: UUID? = nil
|
@Published var bell: UUID? = nil
|
||||||
@@ -90,6 +95,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func connect() throws(SSHError) {
|
func connect() throws(SSHError) {
|
||||||
|
withAnimation { state = .connecting }
|
||||||
defer {
|
defer {
|
||||||
try? authWithNone()
|
try? authWithNone()
|
||||||
getAuthMethods()
|
getAuthMethods()
|
||||||
@@ -101,7 +107,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
|
|
||||||
session = ssh_new()
|
session = ssh_new()
|
||||||
guard session != nil else {
|
guard session != nil else {
|
||||||
withAnimation { connected = false }
|
withAnimation { state = .idle }
|
||||||
throw .backendError("Failed opening session")
|
throw .backendError("Failed opening session")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,17 +120,18 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
if status != SSH_OK {
|
if status != SSH_OK {
|
||||||
logger.critical("connection not ok: \(status)")
|
logger.critical("connection not ok: \(status)")
|
||||||
logSshGetError()
|
logSshGetError()
|
||||||
withAnimation { connected = false }
|
withAnimation { state = .idle }
|
||||||
throw .connectionFailed("Failed connecting")
|
throw .connectionFailed("Failed connecting")
|
||||||
}
|
}
|
||||||
withAnimation { connected = true }
|
withAnimation { state = .authorizing }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func disconnect() async {
|
func disconnect() async {
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
withAnimation { connected = false }
|
withAnimation { state = .idle }
|
||||||
withAnimation { authorized = false }
|
// withAnimation { connected = false }
|
||||||
|
// withAnimation { authorized = false }
|
||||||
withAnimation { testSuceeded = nil }
|
withAnimation { testSuceeded = nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,12 +171,16 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if state == .authorized {} else {
|
||||||
|
go()
|
||||||
|
}
|
||||||
|
|
||||||
if ssh_is_connected(session) == 0 {
|
if ssh_is_connected(session) == 0 {
|
||||||
withAnimation { testSuceeded = false }
|
withAnimation { testSuceeded = false }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
guard authorized else {
|
guard state == .authorized else {
|
||||||
withAnimation { testSuceeded = false }
|
withAnimation { testSuceeded = false }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -233,7 +244,6 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
//MARK: auth
|
//MARK: auth
|
||||||
func authWithPubkey(pub pubInp: Data, priv privInp: Data, pass: String) throws(KeyError) {
|
func authWithPubkey(pub pubInp: Data, priv privInp: Data, pass: String) throws(KeyError) {
|
||||||
guard session != nil else {
|
guard session != nil else {
|
||||||
withAnimation { authorized = false }
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,12 +286,11 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ssh_userauth_publickey(session, nil, privkey) != 0) {
|
if (ssh_userauth_publickey(session, nil, privkey) != 0) {
|
||||||
withAnimation { authorized = false }
|
|
||||||
throw .privkeyRejected
|
throw .privkeyRejected
|
||||||
}
|
}
|
||||||
|
|
||||||
//if u got this far, youre authed!
|
//if u got this far, youre authed!
|
||||||
withAnimation { authorized = true }
|
withAnimation { state = .authorized }
|
||||||
|
|
||||||
ssh_key_free(pubkey)
|
ssh_key_free(pubkey)
|
||||||
ssh_key_free(privkey)
|
ssh_key_free(privkey)
|
||||||
@@ -302,7 +311,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
logSshGetError()
|
logSshGetError()
|
||||||
throw .rejectedCredentials
|
throw .rejectedCredentials
|
||||||
}
|
}
|
||||||
withAnimation { authorized = true }
|
withAnimation { state = .authorized }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,7 +320,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
guard status == SSH_AUTH_SUCCESS.rawValue else { throw .rejectedCredentials }
|
guard status == SSH_AUTH_SUCCESS.rawValue else { throw .rejectedCredentials }
|
||||||
|
|
||||||
logCritical("no security moment lol")
|
logCritical("no security moment lol")
|
||||||
withAnimation { authorized = true }
|
withAnimation { state = .authorized }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,5 +13,4 @@ enum SSHState {
|
|||||||
case authorizing
|
case authorizing
|
||||||
case authorized
|
case authorized
|
||||||
case shellOpen
|
case shellOpen
|
||||||
// case unauthorized
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ struct ConnectionView: View {
|
|||||||
Text(handler.connected ? "connected" : "not connected")
|
Text(handler.connected ? "connected" : "not connected")
|
||||||
.modifier(foregroundColorStyle(handler.connected ? .green : .red))
|
.modifier(foregroundColorStyle(handler.connected ? .green : .red))
|
||||||
|
|
||||||
Text(handler.authorized ? "authorized" : "unauthorized")
|
Text(handler.state == .authorized ? "authorized" : "unauthorized")
|
||||||
.modifier(foregroundColorStyle(handler.authorized ? .green : .red))
|
.modifier(foregroundColorStyle(handler.state == .authorized ? .green : .red))
|
||||||
}
|
}
|
||||||
TextField("address", text: $handler.host.address)
|
TextField("address", text: $handler.host.address)
|
||||||
.textFieldStyle(.roundedBorder)
|
.textFieldStyle(.roundedBorder)
|
||||||
@@ -91,13 +91,9 @@ struct ConnectionView: View {
|
|||||||
} label: {
|
} label: {
|
||||||
Label("Show Terminal", systemImage: "apple.terminal")
|
Label("Show Terminal", systemImage: "apple.terminal")
|
||||||
}
|
}
|
||||||
.disabled(!handler.connected || !handler.authorized)
|
.disabled(!handler.connected || !(handler.state == .authorized))
|
||||||
|
|
||||||
Button() {
|
Button() {
|
||||||
if handler.authorized && handler.connected {
|
|
||||||
} else {
|
|
||||||
handler.go()
|
|
||||||
}
|
|
||||||
handler.testExec()
|
handler.testExec()
|
||||||
} label: {
|
} label: {
|
||||||
if let testResult = handler.testSuceeded {
|
if let testResult = handler.testSuceeded {
|
||||||
@@ -127,7 +123,7 @@ struct ConnectionView: View {
|
|||||||
ToolbarItem() {
|
ToolbarItem() {
|
||||||
Button() {
|
Button() {
|
||||||
handler.go()
|
handler.go()
|
||||||
showTerminal = handler.connected && handler.authorized
|
showTerminal = handler.connected && handler.state == .authorized
|
||||||
} label: {
|
} label: {
|
||||||
Label(
|
Label(
|
||||||
handler.connected ? "Disconnect" : "Connect",
|
handler.connected ? "Disconnect" : "Connect",
|
||||||
|
|||||||
Reference in New Issue
Block a user