run command submitted via run-command

This commit is contained in:
sawka 2022-06-13 11:11:56 -07:00
parent 22aa999a7b
commit d80517caa0
2 changed files with 81 additions and 7 deletions

View File

@ -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
}

46
pkg/sstore/sstore.go Normal file
View File

@ -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
}