mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 05:19:13 +00:00
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:
@@ -10,18 +10,18 @@ import LibSSH
|
||||
import OSLog
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
class SSHHandler: ObservableObject {
|
||||
private var session: ssh_session?
|
||||
private var channel: ssh_channel?
|
||||
private var readTimer: Timer?
|
||||
|
||||
@Published var connected: Bool = false
|
||||
@Published var authorized: Bool = false
|
||||
@Published var testSuceeded: Bool? = nil
|
||||
@MainActor @Published var connected: Bool = false
|
||||
@MainActor @Published var authorized: Bool = false
|
||||
@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 logger = Logger(subsystem: "xy", category: "sshHandler")
|
||||
@@ -208,14 +208,17 @@ class SSHHandler: ObservableObject {
|
||||
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)
|
||||
withAnimation { authorized = true }
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now()+10) {
|
||||
ssh_key_free(pubkey)
|
||||
ssh_key_free(privkey)
|
||||
try? fileManager.removeItem(at: tempPubkey)
|
||||
try? fileManager.removeItem(at: tempKey)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func authWithPw() -> Bool {
|
||||
@@ -297,20 +300,6 @@ class SSHHandler: ObservableObject {
|
||||
|
||||
status = ssh_channel_request_shell(self.channel)
|
||||
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? {
|
||||
@@ -321,9 +310,8 @@ class SSHHandler: ObservableObject {
|
||||
let nbytes = ssh_channel_read_nonblocking(channel, &buffer, UInt32(buffer.count), 0)
|
||||
|
||||
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) {
|
||||
return string
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import SwiftTerm
|
||||
|
||||
class SSHTerminalView: TerminalView, TerminalViewDelegate {
|
||||
@@ -65,7 +66,9 @@ class SSHTerminalView: TerminalView, TerminalViewDelegate {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
@@ -25,7 +25,7 @@ struct ShellView: View {
|
||||
}
|
||||
} label: {
|
||||
if handler.connected {
|
||||
Label("Disconnect", image: "xmark.square.fill")
|
||||
Label("Disconnect", systemImage: "xmark.square.fill")
|
||||
} else {
|
||||
Label("Connect", image: "power")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user