mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 13:26:16 +00:00
removed lots of conurrency
its prob not needed tbh
This commit is contained in:
@@ -15,15 +15,12 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
private var channel: ssh_channel?
|
private var channel: ssh_channel?
|
||||||
private let sshQueue = DispatchQueue(label: "SSH Queue")
|
private let sshQueue = DispatchQueue(label: "SSH Queue")
|
||||||
|
|
||||||
// @Published var hostsManager = HostsManager()
|
|
||||||
|
|
||||||
@Published var title: String = ""
|
@Published var title: String = ""
|
||||||
@Published var state: SSHState = .idle
|
@Published var state: SSHState = .idle
|
||||||
var connected: Bool {
|
var connected: Bool {
|
||||||
return checkConnected(state)
|
return checkConnected(state)
|
||||||
}
|
}
|
||||||
// @Published var connected: Bool = false
|
|
||||||
// @Published var authorized: Bool = false
|
|
||||||
@Published var testSuceeded: Bool? = nil
|
@Published var testSuceeded: Bool? = nil
|
||||||
|
|
||||||
@Published var bell: UUID? = nil
|
@Published var bell: UUID? = nil
|
||||||
@@ -33,12 +30,8 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
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")
|
||||||
|
|
||||||
init(
|
init(host: Host) {
|
||||||
host: Host
|
|
||||||
// hostsManager: HostsManager
|
|
||||||
) {
|
|
||||||
self.host = host
|
self.host = host
|
||||||
// self.hostsManager = hostsManager
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getHostkey() -> Data? {
|
func getHostkey() -> Data? {
|
||||||
@@ -56,9 +49,11 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
|
|
||||||
func go() {
|
func go() {
|
||||||
guard !connected else {
|
guard !connected else {
|
||||||
withAnimation { state = .idle }
|
Task { @MainActor in
|
||||||
|
withAnimation { state = .idle }
|
||||||
|
}
|
||||||
Task {
|
Task {
|
||||||
await disconnect()
|
disconnect()
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -110,12 +105,12 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
withAnimation { state = .idle }
|
withAnimation { state = .idle }
|
||||||
throw .backendError("Failed opening session")
|
throw .backendError("Failed opening session")
|
||||||
}
|
}
|
||||||
|
|
||||||
ssh_options_set(session, SSH_OPTIONS_HOST, host.address)
|
ssh_options_set(session, SSH_OPTIONS_HOST, host.address)
|
||||||
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity)
|
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity)
|
||||||
ssh_options_set(session, SSH_OPTIONS_PORT, &host.port)
|
ssh_options_set(session, SSH_OPTIONS_PORT, &host.port)
|
||||||
ssh_options_set(session, SSH_OPTIONS_USER, host.username)
|
ssh_options_set(session, SSH_OPTIONS_USER, host.username)
|
||||||
|
|
||||||
let status = ssh_connect(session)
|
let status = ssh_connect(session)
|
||||||
if status != SSH_OK {
|
if status != SSH_OK {
|
||||||
logger.critical("connection not ok: \(status)")
|
logger.critical("connection not ok: \(status)")
|
||||||
@@ -126,11 +121,11 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
withAnimation { state = .authorizing }
|
withAnimation { state = .authorizing }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func disconnect() async {
|
func disconnect() {
|
||||||
await MainActor.run {
|
DispatchQueue.main.async {
|
||||||
withAnimation { state = .idle }
|
withAnimation { self.state = .idle }
|
||||||
withAnimation { testSuceeded = nil }
|
withAnimation { self.testSuceeded = nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
//send eof if open
|
//send eof if open
|
||||||
@@ -140,7 +135,9 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
ssh_channel_free(self.channel)
|
ssh_channel_free(self.channel)
|
||||||
self.channel = nil
|
self.channel = nil
|
||||||
|
|
||||||
ssh_disconnect(self.session)
|
if connected && (ssh_is_connected(session) == 1) {
|
||||||
|
ssh_disconnect(self.session)
|
||||||
|
}
|
||||||
ssh_free(self.session)
|
ssh_free(self.session)
|
||||||
self.session = nil
|
self.session = nil
|
||||||
}
|
}
|
||||||
@@ -157,14 +154,16 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setTitle(_ newTitle: String) {
|
func setTitle(_ newTitle: String) {
|
||||||
self.title = newTitle
|
DispatchQueue.main.async {
|
||||||
|
self.title = newTitle
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testExec() {
|
func testExec() {
|
||||||
defer {
|
defer {
|
||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
let result = self.testSuceeded
|
let result = self.testSuceeded
|
||||||
await disconnect()
|
disconnect()
|
||||||
withAnimation { testSuceeded = result }
|
withAnimation { testSuceeded = result }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -182,17 +181,17 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
withAnimation { testSuceeded = false }
|
withAnimation { testSuceeded = false }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var status: CInt
|
var status: CInt
|
||||||
var buffer: [CChar] = Array(repeating: 0, count: 256)
|
var buffer: [CChar] = Array(repeating: 0, count: 256)
|
||||||
var nbytes: CInt
|
var nbytes: CInt
|
||||||
|
|
||||||
let testChannel = ssh_channel_new(session)
|
let testChannel = ssh_channel_new(session)
|
||||||
guard testChannel != nil else {
|
guard testChannel != nil else {
|
||||||
withAnimation { testSuceeded = false }
|
withAnimation { testSuceeded = false }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
status = ssh_channel_open_session(testChannel)
|
status = ssh_channel_open_session(testChannel)
|
||||||
guard status == SSH_OK else {
|
guard status == SSH_OK else {
|
||||||
ssh_channel_free(testChannel)
|
ssh_channel_free(testChannel)
|
||||||
@@ -201,7 +200,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
withAnimation { testSuceeded = false }
|
withAnimation { testSuceeded = false }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
status = ssh_channel_request_exec(testChannel, "uptime")
|
status = ssh_channel_request_exec(testChannel, "uptime")
|
||||||
guard status == SSH_OK else {
|
guard status == SSH_OK else {
|
||||||
ssh_channel_close(testChannel)
|
ssh_channel_close(testChannel)
|
||||||
@@ -221,7 +220,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
while nbytes > 0 {
|
while nbytes > 0 {
|
||||||
nbytes = ssh_channel_read_nonblocking(testChannel, &buffer, UInt32(buffer.count), 0)
|
nbytes = ssh_channel_read_nonblocking(testChannel, &buffer, UInt32(buffer.count), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if nbytes < 0 {
|
if nbytes < 0 {
|
||||||
ssh_channel_close(testChannel)
|
ssh_channel_close(testChannel)
|
||||||
ssh_channel_free(testChannel)
|
ssh_channel_free(testChannel)
|
||||||
@@ -382,7 +381,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
func readFromChannel() -> String? {
|
func readFromChannel() -> String? {
|
||||||
guard connected else { return nil }
|
guard connected else { return nil }
|
||||||
guard ssh_channel_is_open(channel) == 1 && ssh_channel_is_eof(channel) == 0 else {
|
guard ssh_channel_is_open(channel) == 1 && ssh_channel_is_eof(channel) == 0 else {
|
||||||
Task { await disconnect() }
|
Task { disconnect() }
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -404,7 +403,7 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
|
|||||||
func writeToChannel(_ string: String?) {
|
func writeToChannel(_ string: String?) {
|
||||||
guard let string else { return }
|
guard let string else { return }
|
||||||
guard ssh_channel_is_open(channel) == 1 && ssh_channel_is_eof(channel) == 0 else {
|
guard ssh_channel_is_open(channel) == 1 && ssh_channel_is_eof(channel) == 0 else {
|
||||||
Task { await disconnect() }
|
Task { disconnect() }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,10 +111,8 @@ struct ConnectionView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Button("Disconnect", role: .cancel) {
|
Button("Disconnect", role: .cancel) {
|
||||||
Task {
|
handler.disconnect()
|
||||||
await handler.disconnect()
|
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")
|
||||||
|
|||||||
@@ -32,10 +32,8 @@ struct ShellView: View {
|
|||||||
.toolbar {
|
.toolbar {
|
||||||
ToolbarItem {
|
ToolbarItem {
|
||||||
Button() {
|
Button() {
|
||||||
Task {
|
handler.disconnect()
|
||||||
await handler.disconnect()
|
if !handler.connected { dismiss() }
|
||||||
if !handler.connected { dismiss() }
|
|
||||||
}
|
|
||||||
} label: {
|
} label: {
|
||||||
Label("Disconnect", systemImage: "xmark.app.fill")
|
Label("Disconnect", systemImage: "xmark.app.fill")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user