From d80517caa06d54575024cc98d6e08a7e472221dd Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 13 Jun 2022 11:11:56 -0700 Subject: [PATCH] run command submitted via run-command --- cmd/main-server.go | 42 +++++++++++++++++++++++++++++++++------- pkg/sstore/sstore.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 pkg/sstore/sstore.go diff --git a/cmd/main-server.go b/cmd/main-server.go index cdc507d4a..4da0259ea 100644 --- a/cmd/main-server.go +++ b/cmd/main-server.go @@ -7,13 +7,16 @@ import ( "net/http" "os" "os/exec" + "strings" "time" "github.com/fsnotify/fsnotify" + "github.com/google/uuid" "github.com/gorilla/mux" "github.com/scripthaus-dev/sh2-runner/pkg/base" "github.com/scripthaus-dev/sh2-runner/pkg/packet" + "github.com/scripthaus-dev/sh2-server/pkg/sstore" "github.com/scripthaus-dev/sh2-server/pkg/wsshell" ) @@ -144,12 +147,8 @@ func GetPtyOut(w http.ResponseWriter, r *http.Request) { io.Copy(w, fd) } -type runCommandParams struct { - SessionId string `json:"sessionid"` - Command string `json:"command"` -} - func WriteJsonError(w http.ResponseWriter, errVal error) { + w.Header().Set("Content-Type", "application/json") w.WriteHeader(500) errMap := make(map[string]interface{}) errMap["error"] = errVal.Error() @@ -159,6 +158,7 @@ func WriteJsonError(w http.ResponseWriter, errVal error) { } func WriteJsonSuccess(w http.ResponseWriter, data interface{}) { + w.Header().Set("Content-Type", "application/json") rtnMap := make(map[string]interface{}) rtnMap["success"] = true if data != nil { @@ -174,6 +174,15 @@ func WriteJsonSuccess(w http.ResponseWriter, data interface{}) { return } +type runCommandParams struct { + SessionId string `json:"sessionid"` + Command string `json:"command"` +} + +type runCommandResponse struct { + Line *sstore.LineType `json:"line"` +} + func HandleRunCommand(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) w.Header().Set("Access-Control-Allow-Credentials", "true") @@ -191,8 +200,27 @@ func HandleRunCommand(w http.ResponseWriter, r *http.Request) { WriteJsonError(w, fmt.Errorf("error decoding json: %w", err)) return } - fmt.Printf("RUN COMMAND sessionid[%s] cmd[%s]\n", params.SessionId, params.Command) - WriteJsonSuccess(w, nil) + if _, err = uuid.Parse(params.SessionId); err != nil { + WriteJsonError(w, fmt.Errorf("invalid sessionid '%s': %w", params.SessionId, err)) + return + } + commandStr := strings.TrimSpace(params.Command) + if commandStr == "" { + WriteJsonError(w, fmt.Errorf("invalid emtpty command")) + return + } + rtnLine := sstore.MakeNewLineCmd(commandStr) + runPacket := packet.MakeRunPacket() + runPacket.SessionId = params.SessionId + runPacket.CmdId = rtnLine.CmdId + runPacket.ChDir = "" + runPacket.Env = nil + runPacket.Command = commandStr + fmt.Printf("run-packet %v\n", runPacket) + WriteJsonSuccess(w, &runCommandResponse{Line: rtnLine}) + go func() { + GlobalRunnerProc.Input.SendPacket(runPacket) + }() return } diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go new file mode 100644 index 000000000..1234a8626 --- /dev/null +++ b/pkg/sstore/sstore.go @@ -0,0 +1,46 @@ +package sstore + +import ( + "time" + + "github.com/google/uuid" +) + +var NextLineId = 10 + +const LineTypeCmd = "cmd" +const LineTypeText = "text" + +type LineType struct { + LineId int `json:"lineid"` + Ts int64 `json:"ts"` + UserId string `json:"userid"` + LineType string `json:"linetype"` + Text string `json:"text,omitempty"` + CmdId string `json:"cmdid,omitempty"` + CmdText string `json:"cmdtext,omitempty"` + CmdRemote string `json:"cmdremote,omitempty"` +} + +func MakeNewLineCmd(cmdText string) *LineType { + rtn := &LineType{} + rtn.LineId = NextLineId + NextLineId++ + rtn.Ts = time.Now().UnixMilli() + rtn.UserId = "mike" + rtn.LineType = LineTypeCmd + rtn.CmdId = uuid.New().String() + rtn.CmdText = cmdText + return rtn +} + +func MakeNewLineText(text string) *LineType { + rtn := &LineType{} + rtn.LineId = NextLineId + NextLineId++ + rtn.Ts = time.Now().UnixMilli() + rtn.UserId = "mike" + rtn.LineType = LineTypeText + rtn.Text = text + return rtn +}