still not worling

just hangs when trying to open a shell
This commit is contained in:
neon443
2025-06-19 16:28:12 +01:00
parent 869de8a47b
commit d2e2aed7ea
3 changed files with 86 additions and 20 deletions

View File

@@ -47,8 +47,8 @@ struct blankHost: HostPr {
struct debugHost: HostPr { struct debugHost: HostPr {
var address: String = "localhost" var address: String = "localhost"
var port: Int = 2222 var port: Int = 32222
var username: String = "root" var username: String = "default"
var password: String = "root" var password: String = ""
var key: Data? = nil var key: Data? = nil
} }

View File

@@ -15,8 +15,8 @@ class SSHHandler: ObservableObject {
private var channel: ssh_channel? private var channel: ssh_channel?
private var readTimer: Timer? private var readTimer: Timer?
@Published var authorized: Bool = false
@Published var connected: Bool = false @Published var connected: Bool = false
@Published var authorized: Bool = false
@Published var testSuceeded: Bool = false @Published var testSuceeded: Bool = false
@Published var host: HostPr @Published var host: HostPr
@@ -47,6 +47,10 @@ class SSHHandler: ObservableObject {
return Data(base64Encoded: String(cString: data)) return Data(base64Encoded: String(cString: data))
} }
// func connect(_: Host) {
//
// }
func connect() { func connect() {
defer { defer {
getAuthMethods() getAuthMethods()
@@ -85,6 +89,7 @@ class SSHHandler: ObservableObject {
ssh_free(session) ssh_free(session)
session = nil session = nil
withAnimation { authorized = false } withAnimation { authorized = false }
withAnimation { connected = false }
host.key = nil host.key = nil
} }
@@ -164,7 +169,13 @@ class SSHHandler: ObservableObject {
return 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 fileManager = FileManager.default
let tempDir = fileManager.temporaryDirectory let tempDir = fileManager.temporaryDirectory
let tempPubkey = tempDir.appendingPathComponent("key.pub") let tempPubkey = tempDir.appendingPathComponent("key.pub")
@@ -173,20 +184,35 @@ class SSHHandler: ObservableObject {
fileManager.createFile(atPath: tempPubkey.path(), contents: nil) fileManager.createFile(atPath: tempPubkey.path(), contents: nil)
fileManager.createFile(atPath: tempKey.path(), contents: nil) fileManager.createFile(atPath: tempKey.path(), contents: nil)
try? pub.data(using: .utf8)?.write(to: tempPubkey) try? pub.write(to: tempPubkey)
try? priv.data(using: .utf8)?.write(to: tempKey) try? priv.write(to: tempKey)
var pubkey: ssh_key? var pubkey: ssh_key?
ssh_pki_import_pubkey_file(tempPubkey.path(), &pubkey) 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) print(status)
var privkey: ssh_key? var privkey: ssh_key?
if ssh_pki_import_privkey_file(tempKey.path(), pass, nil, nil, &privkey) != 0 { if ssh_pki_import_privkey_file(tempKey.path(), pass, nil, nil, &privkey) != 0 {
print("help?!?") 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 { func authWithPw() -> Bool {

View File

@@ -14,11 +14,38 @@ struct ContentView: View {
@State var privkey: String = "" @State var privkey: String = ""
@State var passphrase: 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 { var body: some View {
NavigationStack { NavigationStack {
List { List {
TextField("", text: $pubkey) Button("Choose Publickey") {
TextField("", text: $privkey) 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) TextField("", text: $passphrase)
HStack { HStack {
Text(handler.connected ? "connected" : "not connected") Text(handler.connected ? "connected" : "not connected")
@@ -55,18 +82,31 @@ struct ContentView: View {
SecureField("password", text: $handler.host.password) SecureField("password", text: $handler.host.password)
.textFieldStyle(.roundedBorder) .textFieldStyle(.roundedBorder)
Button() { if handler.connected {
handler.connect() Button() {
if !pubkey.isEmpty && !privkey.isEmpty { handler.disconnect()
handler.authWithPubkey(pub: pubkey, priv: privkey, pass: passphrase) } label: {
} else { Label("Disconnect", systemImage: "xmark.app.fill")
let _ = handler.authWithPw()
} }
handler.openShell() } else {
} label: { Button() {
Label("Connect", systemImage: "powerplug.portrait") 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() { NavigationLink() {
TerminalView(handler: handler) TerminalView(handler: handler)