From 5b2e88ec324e0ee1b30dcead4d54f12bad36a28e Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 4 Jul 2022 22:18:01 -0700 Subject: [PATCH] checkpoint --- cmd/main-server.go | 12 +++++++++++ pkg/remote/remote.go | 28 ++++++++++++++++++++++++ pkg/sstore/dbops.go | 51 ++++++++++++++++++-------------------------- pkg/sstore/sstore.go | 22 +++++++++---------- 4 files changed, 72 insertions(+), 41 deletions(-) diff --git a/cmd/main-server.go b/cmd/main-server.go index 982a83e97..ce720d412 100644 --- a/cmd/main-server.go +++ b/cmd/main-server.go @@ -278,6 +278,17 @@ func HandleGetSession(w http.ResponseWriter, r *http.Request) { return } +// params: [none] +func HandleGetRemotes(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") + w.Header().Set("Vary", "Origin") + w.Header().Set("Cache-Control", "no-cache") + remotes := remote.GetAllRemoteState() + WriteJsonSuccess(w, remotes) + return +} + // params: sessionid, windowid func HandleGetWindowLines(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) @@ -570,6 +581,7 @@ func main() { gr.HandleFunc("/api/ptyout", HandleGetPtyOut) gr.HandleFunc("/api/get-session", HandleGetSession) gr.HandleFunc("/api/get-window-lines", HandleGetWindowLines) + gr.HandleFunc("/api/get-remotes", HandleGetRemotes) gr.HandleFunc("/api/run-command", HandleRunCommand).Methods("GET", "POST", "OPTIONS") server := &http.Server{ Addr: MainServerAddr, diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index 5123e5ff0..98634dcdc 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -32,6 +32,14 @@ type Store struct { Map map[string]*MShellProc } +type RemoteState struct { + RemoteType string `json:"remotetype"` + RemoteId string `json:"remoteid"` + RemoteName string `json:"remotename"` + Status string `json:"status"` + Cwd string `json:"cwd"` +} + type MShellProc struct { Lock *sync.Mutex Remote *sstore.RemoteType @@ -78,6 +86,26 @@ func GetRemote(name string) *MShellProc { return GlobalStore.Map[name] } +func GetAllRemoteState() []RemoteState { + GlobalStore.Lock.Lock() + defer GlobalStore.Lock.Unlock() + + var rtn []RemoteState + for _, proc := range GlobalStore.Map { + state := RemoteState{ + RemoteType: proc.Remote.RemoteType, + RemoteId: proc.Remote.RemoteId, + RemoteName: proc.Remote.RemoteName, + Status: proc.Status, + } + if proc.InitPk != nil { + state.Cwd = proc.InitPk.HomeDir + } + rtn = append(rtn, state) + } + return rtn +} + func MakeMShell(r *sstore.RemoteType) *MShellProc { rtn := &MShellProc{Lock: &sync.Mutex{}, Remote: r, Status: StatusInit} return rtn diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index decff430b..5f9dbd864 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -97,31 +97,19 @@ func InsertRemote(ctx context.Context, remote *RemoteType) error { } func GetSessionById(ctx context.Context, id string) (*SessionType, error) { - db, err := GetDB() - query := `SELECT * FROM session WHERE sessionid = ?` - var session SessionType - err = db.GetContext(ctx, &session, query, id) - if err == sql.ErrNoRows { - return nil, nil - } - if err != nil { - return nil, err - } - return &session, nil -} - -func GetSessionByName(ctx context.Context, name string) (*SessionType, error) { var rtnSession *SessionType err := WithTx(ctx, func(tx *TxWrap) error { var session SessionType - query := `SELECT * FROM session WHERE name = ?` - found := tx.GetWrap(&session, query, name) + query := `SELECT * FROM session WHERE sessionid = ?` + found := tx.GetWrap(&session, query, id) if !found { return nil } rtnSession = &session query = `SELECT sessionid, windowid, name, curremote, version FROM window WHERE sessionid = ?` tx.SelectWrap(&session.Windows, query, session.SessionId) + query = `SELECT * FROM session_remote WHERE sessionid = ?` + tx.SelectWrap(&session.Remotes, query, session.SessionId) return nil }) if err != nil { @@ -130,6 +118,23 @@ func GetSessionByName(ctx context.Context, name string) (*SessionType, error) { return rtnSession, nil } +func GetSessionByName(ctx context.Context, name string) (*SessionType, error) { + db, err := GetDB() + if err != nil { + return nil, err + } + var sessionId string + query := `SELECT sessionid FROM session WHERE name = ?` + err = db.GetContext(ctx, &sessionId, query, name) + if err != nil { + if err == sql.ErrNoRows { + return nil, nil + } + return nil, err + } + return GetSessionById(ctx, sessionId) +} + func GetWindowLines(ctx context.Context, sessionId string, windowId string) ([]*LineType, error) { var lines []*LineType db, err := GetDB() @@ -153,10 +158,6 @@ func InsertSessionWithName(ctx context.Context, sessionName string) error { SessionId: uuid.New().String(), Name: sessionName, } - localRemote, err := GetRemoteByName(ctx, LocalRemoteName) - if err != nil { - return err - } return WithTx(ctx, func(tx *TxWrap) error { query := `INSERT INTO session (sessionid, name) VALUES (:sessionid, :name)` tx.NamedExecWrap(query, session) @@ -169,16 +170,6 @@ func InsertSessionWithName(ctx context.Context, sessionName string) error { } query = `INSERT INTO window (sessionid, windowid, name, curremote, version) VALUES (:sessionid, :windowid, :name, :curremote, :version)` tx.NamedExecWrap(query, window) - - sr := &SessionRemote{ - SessionId: session.SessionId, - WindowId: window.WindowId, - RemoteName: localRemote.RemoteName, - RemoteId: localRemote.RemoteId, - Cwd: DefaultCwd, - } - query = `INSERT INTO session_remote (sessionid, windowid, remotename, remoteid, cwd) VALUES (:sessionid, :windowid, :remotename, :remoteid, :cwd)` - tx.NamedExecWrap(query, sr) return nil }) } diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index 1a4b1f5db..d8696c8fb 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -48,20 +48,20 @@ func GetDB() (*sqlx.DB, error) { } type SessionType struct { - SessionId string `json:"sessionid"` - Name string `json:"name"` - Windows []*WindowType `json:"windows"` - Cmds []*CmdType `json:"cmds"` + SessionId string `json:"sessionid"` + Name string `json:"name"` + Windows []*WindowType `json:"windows"` + Cmds []*CmdType `json:"cmds"` + Remotes []*SessionRemote `json:"remotes"` } type WindowType struct { - SessionId string `json:"sessionid"` - WindowId string `json:"windowid"` - Name string `json:"name"` - CurRemote string `json:"curremote"` - Remotes []*SessionRemote `json:"remotes"` - Lines []*LineType `json:"lines"` - Version int `json:"version"` + SessionId string `json:"sessionid"` + WindowId string `json:"windowid"` + Name string `json:"name"` + CurRemote string `json:"curremote"` + Lines []*LineType `json:"lines"` + Version int `json:"version"` } type SessionRemote struct {