checkpoint

This commit is contained in:
sawka 2022-07-03 12:01:56 -07:00
parent 17172b158c
commit fc18df0601
2 changed files with 25 additions and 25 deletions

View File

@ -258,7 +258,7 @@ func sendCmdInput(pk *packet.InputPacketType) error {
} }
// params: name // params: name
func GetSession(w http.ResponseWriter, r *http.Request) { func HandleGetSession(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true") w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Vary", "Origin") w.Header().Set("Vary", "Origin")
@ -279,7 +279,7 @@ func GetSession(w http.ResponseWriter, r *http.Request) {
} }
// params: sessionid, windowid // params: sessionid, windowid
func GetWindowLines(w http.ResponseWriter, r *http.Request) { func HandleGetWindowLines(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true") w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Vary", "Origin") w.Header().Set("Vary", "Origin")
@ -309,7 +309,7 @@ func GetPtyOutFile(sessionId string, cmdId string) string {
return pathStr return pathStr
} }
func GetPtyOut(w http.ResponseWriter, r *http.Request) { func HandleGetPtyOut(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true") w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Vary", "Origin") w.Header().Set("Vary", "Origin")
@ -364,12 +364,6 @@ func WriteJsonSuccess(w http.ResponseWriter, data interface{}) {
return return
} }
type runCommandParams struct {
SessionId string `json:"sessionid"`
WindowId string `json:"windowid"`
Command string `json:"command"`
}
type runCommandResponse struct { type runCommandResponse struct {
Line *sstore.LineType `json:"line"` Line *sstore.LineType `json:"line"`
} }
@ -395,16 +389,24 @@ func HandleRunCommand(w http.ResponseWriter, r *http.Request) {
WriteJsonError(w, fmt.Errorf("invalid sessionid '%s': %w", commandPk.SessionId, err)) WriteJsonError(w, fmt.Errorf("invalid sessionid '%s': %w", commandPk.SessionId, err))
return return
} }
commandStr := strings.TrimSpace(commandPk.CmdStr) line, err := ProcessFeCommandPacket(&commandPk)
if commandStr == "" { if err != nil {
WriteJsonError(w, fmt.Errorf("invalid emtpty command")) WriteJsonError(w, err)
return return
} }
WriteJsonSuccess(w, &runCommandResponse{Line: line})
return
}
func ProcessFeCommandPacket(pk *scpacket.FeCommandPacketType) (*sstore.LineType, error) {
commandStr := strings.TrimSpace(pk.CmdStr)
if commandStr == "" {
return nil, fmt.Errorf("invalid emtpty command")
}
if strings.HasPrefix(commandStr, "/comment ") { if strings.HasPrefix(commandStr, "/comment ") {
text := strings.TrimSpace(commandStr[9:]) text := strings.TrimSpace(commandStr[9:])
rtnLine := sstore.MakeNewLineText(commandPk.SessionId, commandPk.WindowId, text) rtnLine := sstore.MakeNewLineText(pk.SessionId, pk.WindowId, text)
WriteJsonSuccess(w, &runCommandResponse{Line: rtnLine}) return rtnLine, nil
return
} }
if strings.HasPrefix(commandStr, "cd ") { if strings.HasPrefix(commandStr, "cd ") {
newDir := strings.TrimSpace(commandStr[3:]) newDir := strings.TrimSpace(commandStr[3:])
@ -414,26 +416,23 @@ func HandleRunCommand(w http.ResponseWriter, r *http.Request) {
localRemote := remote.GetRemote("local") localRemote := remote.GetRemote("local")
if localRemote != nil { if localRemote != nil {
localRemote.Input.SendPacket(cdPacket) localRemote.Input.SendPacket(cdPacket)
return
} }
return return nil, nil
} }
rtnLine := sstore.MakeNewLineCmd(commandPk.SessionId, commandPk.WindowId) rtnLine := sstore.MakeNewLineCmd(pk.SessionId, pk.WindowId)
// rtnLine.CmdText = commandStr
runPacket := packet.MakeRunPacket() runPacket := packet.MakeRunPacket()
runPacket.CK = base.MakeCommandKey(commandPk.SessionId, rtnLine.CmdId) runPacket.CK = base.MakeCommandKey(pk.SessionId, rtnLine.CmdId)
runPacket.Cwd = "" runPacket.Cwd = ""
runPacket.Env = nil runPacket.Env = nil
runPacket.Command = commandStr runPacket.Command = commandStr
fmt.Printf("run-packet %v\n", runPacket) fmt.Printf("run-packet %v\n", runPacket)
WriteJsonSuccess(w, &runCommandResponse{Line: rtnLine})
go func() { go func() {
localRemote := remote.GetRemote("local") localRemote := remote.GetRemote("local")
if localRemote != nil { if localRemote != nil {
localRemote.Input.SendPacket(runPacket) localRemote.Input.SendPacket(runPacket)
} }
}() }()
return return rtnLine, nil
} }
// /api/start-session // /api/start-session
@ -568,9 +567,9 @@ func main() {
} }
go runWebSocketServer() go runWebSocketServer()
gr := mux.NewRouter() gr := mux.NewRouter()
gr.HandleFunc("/api/ptyout", GetPtyOut) gr.HandleFunc("/api/ptyout", HandleGetPtyOut)
gr.HandleFunc("/api/get-session", GetSession) gr.HandleFunc("/api/get-session", HandleGetSession)
gr.HandleFunc("/api/get-window-lines", GetWindowLines) gr.HandleFunc("/api/get-window-lines", HandleGetWindowLines)
gr.HandleFunc("/api/run-command", HandleRunCommand).Methods("GET", "POST", "OPTIONS") gr.HandleFunc("/api/run-command", HandleRunCommand).Methods("GET", "POST", "OPTIONS")
server := &http.Server{ server := &http.Server{
Addr: MainServerAddr, Addr: MainServerAddr,

View File

@ -18,6 +18,7 @@ type FeCommandPacketType struct {
Type string `json:"type"` Type string `json:"type"`
SessionId string `json:"sessionid"` SessionId string `json:"sessionid"`
WindowId string `json:"windowid"` WindowId string `json:"windowid"`
UserId string `json:"userid"`
CmdStr string `json:"cmdstr"` CmdStr string `json:"cmdstr"`
RemoteState RemoteState `json:"remotestate"` RemoteState RemoteState `json:"remotestate"`
} }