diff --git a/ShhShell.xcodeproj/project.pbxproj b/ShhShell.xcodeproj/project.pbxproj index ba1a3bd..2ced66a 100644 --- a/ShhShell.xcodeproj/project.pbxproj +++ b/ShhShell.xcodeproj/project.pbxproj @@ -107,8 +107,8 @@ A92538912DEE06DC007E0A18 = { isa = PBXGroup; children = ( - A95FAA582DF4B71F00DE2F5A /* ci_scripts */, A95FAA462DF3884B00DE2F5A /* Config.xcconfig */, + A95FAA582DF4B71F00DE2F5A /* ci_scripts */, A92538C72DEE0742007E0A18 /* ShhShell */, A92538D42DEE0756007E0A18 /* Resources */, A92538CC2DEE0744007E0A18 /* ShhShellTests */, diff --git a/ShhShell/SSHHandler.swift b/ShhShell/SSHHandler.swift index 5bc065f..7dcbd6c 100644 --- a/ShhShell/SSHHandler.swift +++ b/ShhShell/SSHHandler.swift @@ -261,9 +261,9 @@ class SSHHandler: ObservableObject { interactiveShellSession(channel: channel) - ssh_channel_close(channel) - ssh_channel_send_eof(channel) - ssh_channel_free(channel) +// ssh_channel_close(channel) +// ssh_channel_send_eof(channel) +// ssh_channel_free(channel) } private func interactiveShellSession(channel: ssh_channel) { @@ -271,6 +271,8 @@ class SSHHandler: ObservableObject { var buffer: [CChar] = Array(repeating: 0, count: 256) var nbytes: CInt = 0 + let timer: Timer? + status = ssh_channel_request_pty(channel) guard status == SSH_OK else { return } @@ -280,19 +282,29 @@ class SSHHandler: ObservableObject { status = ssh_channel_request_shell(channel) guard status == SSH_OK else { return } -// Task { - while (ssh_channel_is_open(channel) != 0) && (ssh_channel_is_eof(channel) == 0) { - nbytes = ssh_channel_read(channel, &buffer, UInt32(MemoryLayout.size(ofValue: buffer)), 0) - if nbytes < 0 { - return - } - if nbytes > 0 { - write(1, buffer, Int(nbytes)) - } - sleep(1) + timer = Timer(timeInterval: 0.01, repeats: true) { _ in +// guard let self = self else { +// timer?.invalidate() +// return +// } + guard ssh_channel_is_open(channel) != 0 else { return } + guard ssh_channel_is_eof(channel) == 0 else { return } + + nbytes = ssh_channel_read(channel, &buffer, UInt32(buffer.count), 0) + guard nbytes > 0 else { return } + + if nbytes > 0 { + write(1, buffer, Int(nbytes)) } -// } - print(String(utf8String: buffer)) + print(String(cString: buffer)) + } + + DispatchQueue.global().async { + RunLoop.current.add(timer!, forMode: .common) + } + +// ssh_channel_close(channel) +// ssh_channel_free(channel) } private func logSshGetError() { diff --git a/ShhShell/Views/ContentView.swift b/ShhShell/Views/ContentView.swift index 6783f7d..a0ff550 100644 --- a/ShhShell/Views/ContentView.swift +++ b/ShhShell/Views/ContentView.swift @@ -71,10 +71,10 @@ struct ContentView: View { } } .disabled(!(connected && handler.authorized)) -// -// Button("request a shell") { -// handler.openShell() -// } + + Button("request a shell") { + handler.openShell() + } } } }