mainacotr for sshhandler

added a cleanup to run after 10s clearing memory for keys and files
remove timer code
remove write(1 to stop console echo
open links
image -> systemImage
This commit is contained in:
neon443
2025-06-23 10:19:49 +01:00
parent ea857a8960
commit 01d569a859
3 changed files with 22 additions and 31 deletions

View File

@@ -10,18 +10,18 @@ import LibSSH
import OSLog import OSLog
import SwiftUI import SwiftUI
@MainActor
class SSHHandler: ObservableObject { class SSHHandler: ObservableObject {
private var session: ssh_session? private var session: ssh_session?
private var channel: ssh_channel? private var channel: ssh_channel?
private var readTimer: Timer?
@Published var connected: Bool = false @MainActor @Published var connected: Bool = false
@Published var authorized: Bool = false @MainActor @Published var authorized: Bool = false
@Published var testSuceeded: Bool? = nil @MainActor @Published var testSuceeded: Bool? = nil
@Published var bell: UUID? @MainActor @Published var bell: UUID?
@Published var host: Host @MainActor @Published var host: Host
private let userDefaults = NSUbiquitousKeyValueStore.default private let userDefaults = NSUbiquitousKeyValueStore.default
private let logger = Logger(subsystem: "xy", category: "sshHandler") private let logger = Logger(subsystem: "xy", category: "sshHandler")
@@ -208,14 +208,17 @@ class SSHHandler: ObservableObject {
return return
} }
withAnimation { authorized = true }
return
//if u got this far, youre authed! //if u got this far, youre authed!
//cleanpu here: withAnimation { authorized = true }
//ssh_key_free(pubkey)
//ssh_key_free(privkey) DispatchQueue.main.asyncAfter(deadline: .now()+10) {
//try? fileManager.removeItem(at: tempPubkey) ssh_key_free(pubkey)
//try? fileManager.removeItem(at: tempKey) ssh_key_free(privkey)
try? fileManager.removeItem(at: tempPubkey)
try? fileManager.removeItem(at: tempKey)
}
return
} }
func authWithPw() -> Bool { func authWithPw() -> Bool {
@@ -297,20 +300,6 @@ class SSHHandler: ObservableObject {
status = ssh_channel_request_shell(self.channel) status = ssh_channel_request_shell(self.channel)
guard status == SSH_OK else { return } guard status == SSH_OK else { return }
self.readTimer = Timer(timeInterval: 0.1, repeats: true) { timer in
guard self.connected else {
timer.invalidate()
self.readTimer = nil
return
}
guard ssh_channel_is_open(self.channel) != 0 && ssh_channel_is_eof(self.channel) == 0 else {
timer.invalidate()
self.readTimer = nil
return
}
}
// RunLoop.main.add(self.readTimer!, forMode: .common)
} }
func readFromChannel() -> String? { func readFromChannel() -> String? {
@@ -321,9 +310,8 @@ class SSHHandler: ObservableObject {
let nbytes = ssh_channel_read_nonblocking(channel, &buffer, UInt32(buffer.count), 0) let nbytes = ssh_channel_read_nonblocking(channel, &buffer, UInt32(buffer.count), 0)
guard nbytes > 0 else { return nil } guard nbytes > 0 else { return nil }
write(1, buffer, Int(nbytes))
let data = Data(bytes: buffer, count: buffer.count) let data = Data(bytes: buffer, count: Int(nbytes))
if let string = String(data: data, encoding: .utf8) { if let string = String(data: data, encoding: .utf8) {
return string return string
} }

View File

@@ -6,6 +6,7 @@
// //
import Foundation import Foundation
import UIKit
import SwiftTerm import SwiftTerm
class SSHTerminalView: TerminalView, TerminalViewDelegate { class SSHTerminalView: TerminalView, TerminalViewDelegate {
@@ -65,7 +66,9 @@ class SSHTerminalView: TerminalView, TerminalViewDelegate {
} }
public func requestOpenLink(source: TerminalView, link: String, params: [String : String]) { public func requestOpenLink(source: TerminalView, link: String, params: [String : String]) {
print("open link \(link) \(params)") guard let url = URL(string: link) else { return }
guard UIApplication.shared.canOpenURL(url) else { return }
UIApplication.shared.open(url, options: [:])
} }
public func rangeChanged(source: TerminalView, startY: Int, endY: Int) { public func rangeChanged(source: TerminalView, startY: Int, endY: Int) {

View File

@@ -25,7 +25,7 @@ struct ShellView: View {
} }
} label: { } label: {
if handler.connected { if handler.connected {
Label("Disconnect", image: "xmark.square.fill") Label("Disconnect", systemImage: "xmark.square.fill")
} else { } else {
Label("Connect", image: "power") Label("Connect", image: "power")
} }