From f0a2d7f7bb4fb34c7e8f84800613e25454816a94 Mon Sep 17 00:00:00 2001 From: Gerryl Talon Date: Wed, 1 Feb 2023 09:53:03 +0100 Subject: [PATCH] [feature/browser] add open url with a browser --- apparrayhub.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/apparrayhub.go b/apparrayhub.go index b4ae4f2..0441f98 100644 --- a/apparrayhub.go +++ b/apparrayhub.go @@ -9,6 +9,8 @@ import ( "log" "strings" "sync" + "os/exec" + "runtime" ) type AppArrayHub struct { @@ -56,6 +58,7 @@ func (h *AppArrayHub) OnConnected(string) { h.InitializeConnections() } } + } func (h *AppArrayHub) RunCommand(cmd string, client *ssh.Client) (int, string) { @@ -84,6 +87,29 @@ func (h *AppArrayHub) RunCommand(cmd string, client *ssh.Client) (int, string) { io.Copy(buf, stdout) return res, buf.String() } +// open browser with url +func (h *AppArrayHub) OpenUrl(url string) (int, string) { + var err error + res := 0 + + switch runtime.GOOS { + case "linux": + err = exec.Command("xdg-open", url).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + err = exec.Command("open", url).Start() + default: + err = fmt.Errorf("unsupported platform") + } + if err != nil { + res = 1 + } + + buf := new(strings.Builder) + + return res, buf.String() +} func (h *AppArrayHub) DownloadFile(path string, client *ssh.Client) (int, string, string) { sftpClient, err := sftp.NewClient(client) @@ -113,6 +139,9 @@ func (h *AppArrayHub) SendCommand(message string) { if req.CommandId == model.CommandDownload { status, res, filename = h.DownloadFile(req.Command, client) h.SendResponseCaller(NewCommandDownloadResponse(status, res, filename, req), CommandResultListener) + } else if req.CommandId == model.CommandWebsite { + status, res = h.OpenUrl(req.Command) + h.SendResponseCaller(NewCommandResponse(status, res, req), CommandResultListener) } else { status, res = h.RunCommand(req.Command, client) h.SendResponseCaller(NewCommandResponse(status, res, req), CommandResultListener)