From d2e2aed7eab7db4eb022f127838d4f687750adba Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Thu, 19 Jun 2025 16:28:12 +0100 Subject: [PATCH] still not worling just hangs when trying to open a shell --- ShhShell/SSH/Host.swift | 6 +-- ShhShell/SSH/SSHHandler.swift | 36 +++++++++++++++--- ShhShell/Views/ContentView.swift | 64 ++++++++++++++++++++++++++------ 3 files changed, 86 insertions(+), 20 deletions(-) diff --git a/ShhShell/SSH/Host.swift b/ShhShell/SSH/Host.swift index 37b5257..8c9d9a9 100644 --- a/ShhShell/SSH/Host.swift +++ b/ShhShell/SSH/Host.swift @@ -47,8 +47,8 @@ struct blankHost: HostPr { struct debugHost: HostPr { var address: String = "localhost" - var port: Int = 2222 - var username: String = "root" - var password: String = "root" + var port: Int = 32222 + var username: String = "default" + var password: String = "" var key: Data? = nil } diff --git a/ShhShell/SSH/SSHHandler.swift b/ShhShell/SSH/SSHHandler.swift index 5b6d2e0..ffc5e4e 100644 --- a/ShhShell/SSH/SSHHandler.swift +++ b/ShhShell/SSH/SSHHandler.swift @@ -15,8 +15,8 @@ class SSHHandler: ObservableObject { private var channel: ssh_channel? private var readTimer: Timer? - @Published var authorized: Bool = false @Published var connected: Bool = false + @Published var authorized: Bool = false @Published var testSuceeded: Bool = false @Published var host: HostPr @@ -47,6 +47,10 @@ class SSHHandler: ObservableObject { return Data(base64Encoded: String(cString: data)) } +// func connect(_: Host) { +// +// } + func connect() { defer { getAuthMethods() @@ -85,6 +89,7 @@ class SSHHandler: ObservableObject { ssh_free(session) session = nil withAnimation { authorized = false } + withAnimation { connected = false } host.key = nil } @@ -164,7 +169,13 @@ class SSHHandler: ObservableObject { return } - func authWithPubkey(pub: String, priv: String, pass: String) { + func authWithPubkey(pub: Data, priv: Data, pass: String) { + guard session != nil else { + withAnimation { authorized = false } + return + } + + var status: Int32 let fileManager = FileManager.default let tempDir = fileManager.temporaryDirectory let tempPubkey = tempDir.appendingPathComponent("key.pub") @@ -173,20 +184,35 @@ class SSHHandler: ObservableObject { fileManager.createFile(atPath: tempPubkey.path(), contents: nil) fileManager.createFile(atPath: tempKey.path(), contents: nil) - try? pub.data(using: .utf8)?.write(to: tempPubkey) - try? priv.data(using: .utf8)?.write(to: tempKey) + try? pub.write(to: tempPubkey) + try? priv.write(to: tempKey) var pubkey: ssh_key? ssh_pki_import_pubkey_file(tempPubkey.path(), &pubkey) - let status = ssh_userauth_try_publickey(session, nil, pubkey) + status = ssh_userauth_try_publickey(session, nil, pubkey) print(status) var privkey: ssh_key? if ssh_pki_import_privkey_file(tempKey.path(), pass, nil, nil, &privkey) != 0 { print("help?!?") + print("likeley password is incorrect") } + status = ssh_userauth_publickey(session, nil, privkey) + if status != 0 { + withAnimation { authorized = false } + print("auth failed lol") + return + } + withAnimation { authorized = true } + return + //if u got this far, youre authed! + //cleanpu here: +// ssh_key_free(pubkey) +// ssh_key_free(privkey) +// try? fileManager.removeItem(at: tempPubkey) +// try? fileManager.removeItem(at: tempKey) } func authWithPw() -> Bool { diff --git a/ShhShell/Views/ContentView.swift b/ShhShell/Views/ContentView.swift index ceade9a..ccb7845 100644 --- a/ShhShell/Views/ContentView.swift +++ b/ShhShell/Views/ContentView.swift @@ -14,11 +14,38 @@ struct ContentView: View { @State var privkey: String = "" @State var passphrase: String = "" + @State var pubkeyData: Data? + @State var privkeyData: Data? + + @State var privPickerPresented: Bool = false + @State var pubPickerPresented: Bool = false + var body: some View { NavigationStack { List { - TextField("", text: $pubkey) - TextField("", text: $privkey) + Button("Choose Publickey") { + pubPickerPresented.toggle() + } + Image(systemName: "globe") + .dropDestination(for: Data.self) { items, location in + pubkeyData = items.first + return true + } + .fileImporter(isPresented: $pubPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in + do { + let fileURL = try Result.get() + print(fileURL) + } catch { + print(error.localizedDescription) + } + } + +// TextField("", text: $privkey) + Image(systemName: "key.viewfinder") + .dropDestination(for: Data.self) { items, location in + privkeyData = items.first + return true + } TextField("", text: $passphrase) HStack { Text(handler.connected ? "connected" : "not connected") @@ -55,18 +82,31 @@ struct ContentView: View { SecureField("password", text: $handler.host.password) .textFieldStyle(.roundedBorder) - Button() { - handler.connect() - if !pubkey.isEmpty && !privkey.isEmpty { - handler.authWithPubkey(pub: pubkey, priv: privkey, pass: passphrase) - } else { - let _ = handler.authWithPw() + if handler.connected { + Button() { + handler.disconnect() + } label: { + Label("Disconnect", systemImage: "xmark.app.fill") } - handler.openShell() - } label: { - Label("Connect", systemImage: "powerplug.portrait") + } else { + Button() { + handler.connect() + if !pubkey.isEmpty && !privkey.isEmpty { + handler.authWithPubkey(pub: pubkeyData!, priv: privkeyData!, pass: passphrase) + } else { + let _ = handler.authWithPw() + } +// DispatchQueue.main.asyncAfter(deadline: .now()+10) { + handler.openShell() +// } + } label: { + Label("Connect", systemImage: "powerplug.portrait") + } + .disabled( + pubkeyData == nil && privkeyData == nil || + handler.host.username.isEmpty && handler.host.password.isEmpty + ) } - .disabled(handler.connected) NavigationLink() { TerminalView(handler: handler)