From 069f858f609669e522e4cf016a93115b73bd1925 Mon Sep 17 00:00:00 2001 From: Mariotaku Date: Tue, 4 Apr 2023 13:36:27 +0900 Subject: [PATCH] fixed dumb shell added shell connecting state --- src-tauri/src/shell_manager/mod.rs | 3 +- src-tauri/src/shell_manager/shell.rs | 27 +++++++++++++----- src/app/core/services/remote-shell.service.ts | 1 + src/app/terminal/dumb/dumb.component.html | 6 ++-- src/app/terminal/dumb/dumb.component.ts | 10 ++++--- src/app/terminal/terminal.component.html | 28 ++++++++++++------- 6 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src-tauri/src/shell_manager/mod.rs b/src-tauri/src/shell_manager/mod.rs index 2b43bc06..fe33184c 100644 --- a/src-tauri/src/shell_manager/mod.rs +++ b/src-tauri/src/shell_manager/mod.rs @@ -22,7 +22,7 @@ pub struct Shell { pub token: ShellToken, created_at: Instant, device: Device, - has_pty: bool, + pub(crate) has_pty: Mutex, pub(crate) sender: Mutex>>, pub(crate) callback: Mutex>>, pub(crate) parser: Mutex, @@ -42,6 +42,7 @@ pub struct ShellToken(Uuid); pub struct ShellInfo { pub token: ShellToken, pub title: String, + pub ready: bool, #[serde(rename = "hasPty")] pub has_pty: bool, #[serde(skip_serializing)] diff --git a/src-tauri/src/shell_manager/shell.rs b/src-tauri/src/shell_manager/shell.rs index bb5e67bc..91029f3b 100644 --- a/src-tauri/src/shell_manager/shell.rs +++ b/src-tauri/src/shell_manager/shell.rs @@ -1,3 +1,5 @@ +use libssh_rs::Error::RequestDenied; +use libssh_rs::SshResult; use std::collections::HashMap; use std::io::Write; use std::sync::mpsc::channel; @@ -19,7 +21,7 @@ impl Shell { } pub fn resize(&self, rows: u16, cols: u16) -> Result<(), Error> { - if !self.has_pty { + if self.has_pty.lock().unwrap().eq(&false) { return Err(Error::Unsupported); } self.parser.lock().unwrap().set_size(rows, cols); @@ -33,7 +35,7 @@ impl Shell { } pub fn screen(&self, cols: u16) -> Result { - if !self.has_pty { + if self.has_pty.lock().unwrap().eq(&false) { return Err(Error::Unsupported); } let guard = self.parser.lock().unwrap(); @@ -72,7 +74,8 @@ impl Shell { return ShellInfo { token: self.token.clone(), title: self.title(), - has_pty: self.has_pty, + ready: self.sender.lock().unwrap().is_some(), + has_pty: *self.has_pty.lock().unwrap(), created_at: self.created_at, }; } @@ -88,7 +91,7 @@ impl Shell { token: ShellToken::new(), created_at: Instant::now(), device, - has_pty, + has_pty: Mutex::new(false), sender: Mutex::default(), callback: Mutex::new(None), parser: Mutex::new(Parser::new(rows, cols, 1000)), @@ -99,7 +102,7 @@ impl Shell { } fn process(&self, data: &[u8]) -> bool { - if !self.has_pty { + if self.has_pty.lock().unwrap().eq(&false) { return false; } let mut parser = self.parser.lock().unwrap(); @@ -128,13 +131,20 @@ impl Shell { fn worker(&self) -> Result<(), Error> { let (sender, receiver) = channel::(); - *self.sender.lock().unwrap() = Some(sender); let connection = DeviceConnection::new(self.device.clone())?; let channel = connection.new_channel()?; channel.open_session()?; let (rows, cols) = self.parser.lock().unwrap().screen().size(); - channel.request_pty("xterm", cols as u32, rows as u32)?; + match channel.request_pty("xterm", cols as u32, rows as u32) { + Ok(_) => *self.has_pty.lock().unwrap() = true, + Err(RequestDenied(s)) => log::warn!("Failed to request pty {:?}", s), + e => e?, + } channel.request_shell()?; + *self.sender.lock().unwrap() = Some(sender); + if let Some(callback) = self.callback.lock().unwrap().as_ref() { + callback.info(self.info()); + } let mut buf = [0; 8192]; while !channel.is_closed() { if let Ok(msg) = receiver.recv_timeout(Duration::from_micros(1)) { @@ -151,6 +161,9 @@ impl Shell { } } let size = channel.read_timeout(&mut buf, false, Some(Duration::from_micros(5)))?; + if size == 0 { + continue; + } if let Some(callback) = self.callback.lock().unwrap().as_ref() { callback.rx(&buf[..size]); } diff --git a/src/app/core/services/remote-shell.service.ts b/src/app/core/services/remote-shell.service.ts index 19682821..b324eb99 100644 --- a/src/app/core/services/remote-shell.service.ts +++ b/src/app/core/services/remote-shell.service.ts @@ -11,6 +11,7 @@ export type ShellToken = string; export interface ShellInfo { token: ShellToken; title: string; + ready: boolean; hasPty: boolean; } diff --git a/src/app/terminal/dumb/dumb.component.html b/src/app/terminal/dumb/dumb.component.html index b1b7ae2f..20024db8 100644 --- a/src/app/terminal/dumb/dumb.component.html +++ b/src/app/terminal/dumb/dumb.component.html @@ -1,10 +1,10 @@ -
-
+
+
Interactive terminal is not supported for this connection. Check here for more info.
-
+
{{item.input}}
diff --git a/src/app/terminal/dumb/dumb.component.ts b/src/app/terminal/dumb/dumb.component.ts index a3137749..d70ed94d 100644 --- a/src/app/terminal/dumb/dumb.component.ts +++ b/src/app/terminal/dumb/dumb.component.ts @@ -1,5 +1,5 @@ -import {Component, ElementRef, Input, NgZone, OnDestroy, OnInit, ViewChild} from '@angular/core'; -import {RemoteShellService, ShellObservable, ShellSubject, ShellToken} from "../../core/services/remote-shell.service"; +import {Component, ElementRef, Input, OnDestroy, OnInit, ViewChild} from '@angular/core'; +import {RemoteShellService, ShellObservable, ShellToken} from "../../core/services/remote-shell.service"; import {Subscription} from "rxjs"; import {Buffer} from "buffer"; @@ -72,8 +72,10 @@ export class DumbComponent implements OnInit, OnDestroy { str = str.substring(0, end); } lastLog.output = `${lastLog.output}${str}`; - const container = this.container!.nativeElement; - container.scrollTop = container.scrollHeight; + setTimeout(() => { + const container = this.container!.nativeElement; + container.scrollTop = container.scrollHeight; + }, 10); } } diff --git a/src/app/terminal/terminal.component.html b/src/app/terminal/terminal.component.html index e62946f8..aadb66b3 100644 --- a/src/app/terminal/terminal.component.html +++ b/src/app/terminal/terminal.component.html @@ -5,19 +5,27 @@ [animation]="false" [(activeId)]="currentShell">
  • +
    + Connecting... +
    {{shell.title}} ×
    - - - -
    - {{pendingResize.rows}} × {{pendingResize.cols}} -
    + + + + +
    + {{pendingResize.rows}} × {{pendingResize.cols}} +
    +
    +
    - +
    + Connecting to {{shell.title}}... +
  • @@ -34,8 +42,8 @@