From f86de49e3123c6afb482e48e560fd71fb61304a6 Mon Sep 17 00:00:00 2001 From: sawka Date: Tue, 16 Aug 2022 15:08:28 -0700 Subject: [PATCH] updates to remote --- db/migrations/000001_init.up.sql | 7 ++- pkg/remote/remote.go | 38 ++++++++++------ pkg/sstore/dbops.go | 20 ++++----- pkg/sstore/sstore.go | 74 +++++++++++++++++++++----------- 4 files changed, 91 insertions(+), 48 deletions(-) diff --git a/db/migrations/000001_init.up.sql b/db/migrations/000001_init.up.sql index 24c6de5c0..4854b7bf3 100644 --- a/db/migrations/000001_init.up.sql +++ b/db/migrations/000001_init.up.sql @@ -71,8 +71,13 @@ CREATE TABLE line ( CREATE TABLE remote ( remoteid varchar(36) PRIMARY KEY, + physicalid varchar(36) NOT NULL, remotetype varchar(10) NOT NULL, - remotename varchar(50) NOT NULL, + remotealias varchar(50) NOT NULL, + remotecanonicalname varchar(200) NOT NULL, + remotesudo boolean NOT NULL, + remoteuser varchar(50) NOT NULL, + remotehost varchar(200) NOT NULL, autoconnect boolean NOT NULL, initpk json NOT NULL, sshopts json NOT NULL, diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index a43742c99..7d1687366 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -35,12 +35,14 @@ type Store struct { } type RemoteState struct { - RemoteType string `json:"remotetype"` - RemoteId string `json:"remoteid"` - RemoteName string `json:"remotename"` - RemoteVars map[string]string `json:"remotevars"` - Status string `json:"status"` - DefaultState *sstore.RemoteState `json:"defaultstate"` + RemoteType string `json:"remotetype"` + RemoteId string `json:"remoteid"` + PhysicalId string `json:"physicalremoteid"` + RemoteAlias string `json:"remotealias"` + RemoteCanonicalName string `json:"remotecanonicalname"` + RemoteVars map[string]string `json:"remotevars"` + Status string `json:"status"` + DefaultState *sstore.RemoteState `json:"defaultstate"` } type MShellProc struct { @@ -78,7 +80,7 @@ func GetRemoteByName(name string) *MShellProc { GlobalStore.Lock.Lock() defer GlobalStore.Lock.Unlock() for _, msh := range GlobalStore.Map { - if msh.Remote.RemoteName == name { + if msh.Remote.RemoteAlias == name || msh.Remote.GetName() == name { return msh } } @@ -98,13 +100,25 @@ func GetAllRemoteState() []RemoteState { 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, + RemoteType: proc.Remote.RemoteType, + RemoteId: proc.Remote.RemoteId, + RemoteAlias: proc.Remote.RemoteAlias, + RemoteCanonicalName: proc.Remote.RemoteCanonicalName, + PhysicalId: proc.Remote.PhysicalId, + Status: proc.Status, } vars := make(map[string]string) - vars["user"], vars["host"] = proc.Remote.GetUserHost() + vars["user"] = proc.Remote.RemoteUser + vars["host"] = proc.Remote.RemoteHost + if proc.Remote.RemoteSudo { + vars["sudo"] = "1" + } + vars["alias"] = proc.Remote.RemoteAlias + vars["cname"] = proc.Remote.RemoteCanonicalName + vars["physicalid"] = proc.Remote.PhysicalId + vars["remoteid"] = proc.Remote.RemoteId + vars["status"] = proc.Status + vars["type"] = proc.Remote.RemoteType if proc.ServerProc != nil && proc.ServerProc.InitPk != nil { state.DefaultState = &sstore.RemoteState{Cwd: proc.ServerProc.InitPk.HomeDir} vars["home"] = proc.ServerProc.InitPk.HomeDir diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index e09a6285d..a9b5c434a 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -40,11 +40,11 @@ func GetAllRemotes(ctx context.Context) ([]*RemoteType, error) { return rtn, nil } -func GetRemoteByName(ctx context.Context, remoteName string) (*RemoteType, error) { +func GetRemoteById(ctx context.Context, remoteId string) (*RemoteType, error) { var remote *RemoteType err := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT * FROM remote WHERE remotename = ?` - m := tx.GetMap(query, remoteName) + query := `SELECT * FROM remote WHERE remoteid = ?` + m := tx.GetMap(query, remoteId) remote = RemoteFromMap(m) return nil }) @@ -54,11 +54,11 @@ func GetRemoteByName(ctx context.Context, remoteName string) (*RemoteType, error return remote, nil } -func GetRemoteById(ctx context.Context, remoteId string) (*RemoteType, error) { +func GetRemoteByPhysicalId(ctx context.Context, physicalId string) (*RemoteType, error) { var remote *RemoteType err := WithTx(ctx, func(tx *TxWrap) error { - query := `SELECT * FROM remote WHERE remoteid = ?` - m := tx.GetMap(query, remoteId) + query := `SELECT * FROM remote WHERE physicalid = ?` + m := tx.GetMap(query, physicalId) remote = RemoteFromMap(m) return nil }) @@ -76,8 +76,8 @@ func InsertRemote(ctx context.Context, remote *RemoteType) error { if err != nil { return err } - query := `INSERT INTO remote ( remoteid, remotetype, remotename, autoconnect, initpk, sshopts, lastconnectts) VALUES - (:remoteid,:remotetype,:remotename,:autoconnect,:initpk,:sshopts,:lastconnectts)` + query := `INSERT INTO remote ( remoteid, physicalid, remotetype, remotealias, remotecanonicalname, remotesudo, remoteuser, remotehost, autoconnect, initpk, sshopts, lastconnectts) VALUES + (:remoteid,:physicalid,:remotetype,:remotealias,:remotecanonicalname,:remotesudo,:remoteuser,:remotehost,:autoconnect,:initpk,:sshopts,:lastconnectts)` _, err = db.NamedExec(query, remote.ToMap()) if err != nil { return err @@ -533,8 +533,8 @@ func GetRemoteState(ctx context.Context, rname string, sessionId string, windowI remoteState = &ri.State return nil } - query = `SELECT remoteid FROM remote WHERE remotename = ?` - remoteId = tx.GetString(query, rname) + query = `SELECT remoteid FROM remote WHERE remotealias = ? OR remotecanonicalname = ?` + remoteId = tx.GetString(query, rname, rname) if remoteId == "" { return fmt.Errorf("remote not found", rname) } diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index f2f10d036..47401eaa9 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -9,8 +9,9 @@ import ( "database/sql/driver" "fmt" "log" + "os" + "os/user" "path" - "strings" "sync" "time" @@ -277,27 +278,28 @@ type SSHOpts struct { } type RemoteType struct { - RemoteId string `json:"remoteid"` - RemoteType string `json:"remotetype"` - RemoteName string `json:"remotename"` - AutoConnect bool `json:"autoconnect"` - InitPk *packet.InitPacketType `json:"inipk"` - SSHOpts *SSHOpts `json:"sshopts"` - LastConnectTs int64 `json:"lastconnectts"` + RemoteId string `json:"remoteid"` + PhysicalId string `json:"physicalid"` + RemoteType string `json:"remotetype"` + RemoteAlias string `json:"remotealias"` + RemoteCanonicalName string `json:"remotecanonicalname"` + RemoteSudo bool `json:"remotesudo"` + RemoteUser string `json:"remoteuser"` + RemoteHost string `json:"remotehost"` + AutoConnect bool `json:"autoconnect"` + InitPk *packet.InitPacketType `json:"inipk"` + SSHOpts *SSHOpts `json:"sshopts"` + LastConnectTs int64 `json:"lastconnectts"` } -func (r *RemoteType) GetUserHost() (string, string) { - if r.SSHOpts == nil { - return "", "" +func (r *RemoteType) GetName() string { + if r.RemoteAlias != "" { + return r.RemoteAlias } - if r.SSHOpts.SSHUser != "" { - return r.SSHOpts.SSHUser, r.SSHOpts.SSHHost + if r.RemoteUser == "" { + return r.RemoteHost } - atIdx := strings.Index(r.SSHOpts.SSHHost, "@") - if atIdx == -1 { - return "", r.SSHOpts.SSHHost - } - return r.SSHOpts.SSHHost[0:atIdx], r.SSHOpts.SSHHost[atIdx+1:] + return fmt.Sprintf("%s@%s", r.RemoteUser, r.RemoteHost) } type CmdType struct { @@ -318,8 +320,13 @@ type CmdType struct { func (r *RemoteType) ToMap() map[string]interface{} { rtn := make(map[string]interface{}) rtn["remoteid"] = r.RemoteId + rtn["physicalid"] = r.PhysicalId rtn["remotetype"] = r.RemoteType - rtn["remotename"] = r.RemoteName + rtn["remotealias"] = r.RemoteAlias + rtn["remotecanonicalname"] = r.RemoteCanonicalName + rtn["remotesudo"] = r.RemoteSudo + rtn["remoteuser"] = r.RemoteUser + rtn["remotehost"] = r.RemoteHost rtn["autoconnect"] = r.AutoConnect rtn["initpk"] = quickJson(r.InitPk) rtn["sshopts"] = quickJson(r.SSHOpts) @@ -333,8 +340,13 @@ func RemoteFromMap(m map[string]interface{}) *RemoteType { } var r RemoteType quickSetStr(&r.RemoteId, m, "remoteid") + quickSetStr(&r.PhysicalId, m, "physicalid") quickSetStr(&r.RemoteType, m, "remotetype") - quickSetStr(&r.RemoteName, m, "remotename") + quickSetStr(&r.RemoteAlias, m, "remotealias") + quickSetStr(&r.RemoteCanonicalName, m, "remotecanonicalname") + quickSetBool(&r.RemoteSudo, m, "remotesudo") + quickSetStr(&r.RemoteUser, m, "remoteuser") + quickSetStr(&r.RemoteHost, m, "remotehost") quickSetBool(&r.AutoConnect, m, "autoconnect") quickSetJson(&r.InitPk, m, "initpk") quickSetJson(&r.SSHOpts, m, "sshopts") @@ -431,18 +443,30 @@ func EnsureLocalRemote(ctx context.Context) error { if remote != nil { return nil } + hostName, err := os.Hostname() + if err != nil { + return fmt.Errorf("getting hostname: %w", err) + } + user, err := user.Current() + if err != nil { + return fmt.Errorf("getting user: %w", err) + } // create the local remote localRemote := &RemoteType{ - RemoteId: remoteId, - RemoteType: "ssh", - RemoteName: LocalRemoteName, - AutoConnect: true, + RemoteId: remoteId, + RemoteType: "ssh", + RemoteAlias: LocalRemoteName, + RemoteCanonicalName: fmt.Sprintf("%s@%s", user.Username, hostName), + RemoteSudo: false, + RemoteUser: user.Username, + RemoteHost: hostName, + AutoConnect: true, } err = InsertRemote(ctx, localRemote) if err != nil { return err } - log.Printf("[db] added remote '%s', id=%s\n", localRemote.RemoteName, localRemote.RemoteId) + log.Printf("[db] added remote '%s', id=%s\n", localRemote.GetName(), localRemote.RemoteId) return nil }