diff --git a/ShhShell/SSH/Host.swift b/ShhShell/SSH/Host.swift index 8c9d9a9..9d85fc8 100644 --- a/ShhShell/SSH/Host.swift +++ b/ShhShell/SSH/Host.swift @@ -47,7 +47,7 @@ struct blankHost: HostPr { struct debugHost: HostPr { var address: String = "localhost" - var port: Int = 32222 + var port: Int = 22 var username: String = "default" var password: String = "" var key: Data? = nil diff --git a/ShhShell/SSH/SSHHandler.swift b/ShhShell/SSH/SSHHandler.swift index 118101c..872e1fb 100644 --- a/ShhShell/SSH/SSHHandler.swift +++ b/ShhShell/SSH/SSHHandler.swift @@ -169,7 +169,7 @@ class SSHHandler: ObservableObject { return } - func authWithPubkey(pub: Data, priv: Data, pass: String) { + func authWithPubkey(pub: String, priv: String, pass: String) { guard session != nil else { withAnimation { authorized = false } return @@ -184,8 +184,8 @@ class SSHHandler: ObservableObject { fileManager.createFile(atPath: tempPubkey.path(), contents: nil) fileManager.createFile(atPath: tempKey.path(), contents: nil) - try? pub.write(to: tempPubkey) - try? priv.write(to: tempKey) + try? Data(base64Encoded: pub)?.write(to: tempPubkey) + try? Data(base64Encoded: priv)?.write(to: tempKey) var pubkey: ssh_key? ssh_pki_import_pubkey_file(tempPubkey.path(), &pubkey) diff --git a/ShhShell/Views/ContentView.swift b/ShhShell/Views/ContentView.swift index 1f6c87f..5b62b7e 100644 --- a/ShhShell/Views/ContentView.swift +++ b/ShhShell/Views/ContentView.swift @@ -12,8 +12,8 @@ struct ContentView: View { @State var passphrase: String = "" - @State var pubkeyData: Data? - @State var privkeyData: Data? + @State var pubkey: String = "" + @State var privkey: String = "" @State var privPickerPresented: Bool = false @State var pubPickerPresented: Bool = false @@ -21,29 +21,43 @@ struct ContentView: View { var body: some View { NavigationStack { List { - Button("Choose Publickey") { - pubPickerPresented.toggle() - } + HStack { + TextField("", text: $pubkey, prompt: Text("Public Key")) + Button() { + pubPickerPresented.toggle() + } label: { + Image(systemName: "folder") + } + .buttonStyle(.plain) .fileImporter(isPresented: $pubPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in do { let fileURL = try Result.get() - pubkeyData = try? Data(contentsOf: fileURL) + let data = try! Data(contentsOf: fileURL) + pubkey = data.base64EncodedString() print(fileURL) } catch { print(error.localizedDescription) } } - - Button("Choose Privatekey") { - privPickerPresented.toggle() } - .fileImporter(isPresented: $privPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in - do { - let fileURL = try Result.get() - privkeyData = try? Data(contentsOf: fileURL) - print(fileURL) - } catch { - print(error.localizedDescription) + + HStack { + TextField("", text: $pubkey, prompt: Text("Public Key")) + Button() { + privPickerPresented.toggle() + } label: { + Image(systemName: "folder") + } + .buttonStyle(.plain) + .fileImporter(isPresented: $privPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in + do { + let fileURL = try Result.get() + let data = try! Data(contentsOf: fileURL) + privkey = data.base64EncodedString() + print(fileURL) + } catch { + print(error.localizedDescription) + } } } @@ -92,8 +106,8 @@ struct ContentView: View { } else { Button() { handler.connect() - if pubkeyData != nil && privkeyData != nil { - handler.authWithPubkey(pub: pubkeyData!, priv: privkeyData!, pass: passphrase) + if !pubkey.isEmpty && !privkey.isEmpty { + handler.authWithPubkey(pub: pubkey, priv: privkey, pass: passphrase) } else { let _ = handler.authWithPw() } @@ -102,8 +116,8 @@ struct ContentView: View { Label("Connect", systemImage: "powerplug.portrait") } .disabled( - pubkeyData == nil && privkeyData == nil || - handler.host.username.isEmpty && handler.host.password.isEmpty + pubkey.isEmpty && privkey.isEmpty || + handler.host.username.isEmpty && handler.host.password.isEmpty ) }