diff --git a/pkg/webconsole/command/command.go b/pkg/webconsole/command/command.go index faccd18e88..58930a31cb 100644 --- a/pkg/webconsole/command/command.go +++ b/pkg/webconsole/command/command.go @@ -16,6 +16,7 @@ type ICommand interface { GetCommand() *exec.Cmd Cleanup() error GetData(string) (isShow bool, ouput string, command string) + Connect() error ShowInfo() string } @@ -42,6 +43,10 @@ func (c BaseCommand) GetCommand() *exec.Cmd { return exec.Command(c.name, c.args...) } +func (c BaseCommand) Connect() error { + return nil +} + func (c BaseCommand) GetData(comand string) (isShow bool, ouput string, command string) { return true, "", "" } diff --git a/pkg/webconsole/command/ssh_command.go b/pkg/webconsole/command/ssh_command.go index 3bc166b313..b126ce05f5 100644 --- a/pkg/webconsole/command/ssh_command.go +++ b/pkg/webconsole/command/ssh_command.go @@ -105,6 +105,15 @@ func (c *SSHtoolSol) GetProtocol() string { return PROTOCOL_TTY } +func (c *SSHtoolSol) Connect() error { + conn, err := net.DialTimeout("tcp", c.IP+":22", time.Second*2) + if err != nil { + return err + } + defer conn.Close() + return nil +} + func (c *SSHtoolSol) GetData(data string) (isShow bool, ouput string, command string) { if len(c.Username) == 0 { if len(data) == 0 { diff --git a/pkg/webconsole/server/tty_server.go b/pkg/webconsole/server/tty_server.go index 09955dcc2e..5d693b7de0 100644 --- a/pkg/webconsole/server/tty_server.go +++ b/pkg/webconsole/server/tty_server.go @@ -19,9 +19,10 @@ const ( ON_DISCONNECTION = "disconnection" ON_ERROR = "error" - OUTPUT_EVENT = "output" - INPUT_EVENT = "input" - RESIZE_EVENT = "resize" + DISCONNECT_EVENT = "disconnect" + OUTPUT_EVENT = "output" + INPUT_EVENT = "input" + RESIZE_EVENT = "resize" COMMAND_QUERY = "command" ARGS_QUERY = "args" @@ -72,6 +73,11 @@ func initSocketHandler(so socketio.Socket, p *session.Pty) { so.Emit(OUTPUT_EVENT, string(buf[0:n])) } if !p.IsOk { + if err := p.Session.Connect(); err != nil { + so.Emit(DISCONNECT_EVENT, "") + cleanUp(so, p) + return + } p.Stop() if info := p.Session.ShowInfo(); len(info) > 0 { so.Emit(OUTPUT_EVENT, info) @@ -89,6 +95,11 @@ func initSocketHandler(so socketio.Socket, p *session.Pty) { // handle write so.On(INPUT_EVENT, func(data string) { if !p.IsOk { + if err := p.Session.Connect(); err != nil { + so.Emit(DISCONNECT_EVENT, "") + cleanUp(so, p) + return + } if data == "\r" { p.Show, p.Output, p.Command = p.Session.GetData(p.Buffer) so.Emit(OUTPUT_EVENT, "\r\n") diff --git a/pkg/webconsole/session/remote_console.go b/pkg/webconsole/session/remote_console.go index 985d2a2fef..f3a4f40ba2 100644 --- a/pkg/webconsole/session/remote_console.go +++ b/pkg/webconsole/session/remote_console.go @@ -64,6 +64,11 @@ func (info *RemoteConsoleInfo) Cleanup() error { return nil } +// GetData implements ISessionData interface +func (info *RemoteConsoleInfo) Connect() error { + return nil +} + // GetData implements ISessionData interface func (info *RemoteConsoleInfo) GetData(s string) (bool, string, string) { return false, "", ""