WORKS!!!!! PUBKEY ON IOS!!!!!!

no file import rn
This commit is contained in:
neon443
2025-06-22 12:35:08 +01:00
parent 1cf9518e0f
commit e156d63cf6
3 changed files with 37 additions and 16 deletions

View File

@@ -171,29 +171,32 @@ class SSHHandler: ObservableObject {
withAnimation { authorized = false } withAnimation { authorized = false }
return return
} }
var status: Int32 var status: Int32
let fileManager = FileManager.default
let tempDir = fileManager.temporaryDirectory
let tempPubkey = tempDir.appendingPathComponent("key.pub")
let tempKey = tempDir.appendingPathComponent("key")
print(pubInp) fileManager.createFile(atPath: tempPubkey.path(), contents: nil)
var pubInpCChar: [Int8] = [] fileManager.createFile(atPath: tempKey.path(), contents: nil)
for byte in pubInp {
pubInpCChar.append(Int8(byte))
}
var privInpCChar: [Int8] = [] let attributes: [FileAttributeKey: Any] = [.posixPermissions: 0o600]
for byte in privInp { try? fileManager.setAttributes(attributes, ofItemAtPath: tempPubkey.path())
privInpCChar.append(Int8(byte)) try? fileManager.setAttributes(attributes, ofItemAtPath: tempKey.path())
}
try? pubInp.write(to: tempPubkey)
try? privInp.write(to: tempKey)
var pubkey: ssh_key? var pubkey: ssh_key?
ssh_pki_import_pubkey_file(tempPubkey.path(), &pubkey)
ssh_pki_import_pubkey_base64(&pubInpCChar, SSH_KEYTYPE_SK_ED25519, &pubkey)
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_base64(&privInpCChar, pass, nil, nil, &privkey) != 0 { if ssh_pki_import_privkey_file(tempKey.path(), pass, nil, nil, &privkey) != 0 {
print("help?!?") print("help?!?")
print("likeley passphrase is incorrect") print("likeley password is incorrect")
} }
status = ssh_userauth_publickey(session, nil, privkey) status = ssh_userauth_publickey(session, nil, privkey)
@@ -206,6 +209,11 @@ class SSHHandler: ObservableObject {
withAnimation { authorized = true } withAnimation { authorized = true }
return return
//if u got this far, youre authed! //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

@@ -64,7 +64,8 @@ struct ConnectionView: View {
HStack { HStack {
TextField("", text: $pubkeyStr, prompt: Text("Public Key")) TextField("", text: $pubkeyStr, prompt: Text("Public Key"))
.onSubmit { .onSubmit {
pubkey = Data(pubkeyStr.utf8) let newStr = pubkeyStr.replacingOccurrences(of: "\r\n", with: "")
pubkey = Data(newStr.utf8)
} }
Button() { Button() {
pubPickerPresented.toggle() pubPickerPresented.toggle()
@@ -75,6 +76,11 @@ struct ConnectionView: View {
.fileImporter(isPresented: $pubPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in .fileImporter(isPresented: $pubPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in
do { do {
let fileURL = try Result.get() let fileURL = try Result.get()
guard fileURL.startAccessingSecurityScopedResource() else {
print("cant acces file")
return
}
defer { fileURL.stopAccessingSecurityScopedResource() }
pubkey = try? Data(contentsOf: fileURL) pubkey = try? Data(contentsOf: fileURL)
print(fileURL) print(fileURL)
} catch { } catch {
@@ -86,7 +92,8 @@ struct ConnectionView: View {
HStack { HStack {
TextField("", text: $privkeyStr, prompt: Text("Private Key")) TextField("", text: $privkeyStr, prompt: Text("Private Key"))
.onSubmit { .onSubmit {
privkey = Data(privkeyStr.utf8) let newStr = privkeyStr.replacingOccurrences(of: "\r\n", with: "")
privkey = Data(newStr.utf8)
} }
Button() { Button() {
privPickerPresented.toggle() privPickerPresented.toggle()
@@ -97,6 +104,11 @@ struct ConnectionView: View {
.fileImporter(isPresented: $privPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in .fileImporter(isPresented: $privPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in
do { do {
let fileURL = try Result.get() let fileURL = try Result.get()
guard fileURL.startAccessingSecurityScopedResource() else {
print("cant access file")
return
}
defer { fileURL.stopAccessingSecurityScopedResource() }
privkey = try? Data(contentsOf: fileURL) privkey = try? Data(contentsOf: fileURL)
print(privkey) print(privkey)
print(fileURL) print(fileURL)
@@ -150,7 +162,7 @@ struct ConnectionView: View {
handler.host.key = hostsManager.getHostMatching(handler.host)?.key handler.host.key = hostsManager.getHostMatching(handler.host)?.key
} }
} message: { } message: {
Text("Expected \(hostsManager.getHostMatching(handler.host)!.key?.base64EncodedString() ?? "null")\nbut recieved \(handler.host.key?.base64EncodedString() ?? "null" ) from the server") Text("Expected \(hostsManager.getHostMatching(handler.host)?.key?.base64EncodedString() ?? "null")\nbut recieved \(handler.host.key?.base64EncodedString() ?? "null" ) from the server")
} }
.transition(.opacity) .transition(.opacity)
.toolbar { .toolbar {

View File

@@ -23,6 +23,7 @@ class ShhTerminalViewDelegate: TerminalViewDelegate {
func send(source: TerminalView, data: ArraySlice<UInt8>) { func send(source: TerminalView, data: ArraySlice<UInt8>) {
print(data) print(data)
print("send")
} }
func scrolled(source: TerminalView, position: Double) { func scrolled(source: TerminalView, position: Double) {