connectmode, add stubs for export/setenv command

This commit is contained in:
sawka 2022-08-21 12:31:29 -07:00
parent b2a2b6252d
commit 9d150dc7e3
6 changed files with 43 additions and 16 deletions

View File

@ -78,7 +78,7 @@ CREATE TABLE remote (
remotesudo boolean NOT NULL, remotesudo boolean NOT NULL,
remoteuser varchar(50) NOT NULL, remoteuser varchar(50) NOT NULL,
remotehost varchar(200) NOT NULL, remotehost varchar(200) NOT NULL,
autoconnect boolean NOT NULL, connectmode varchar(20) NOT NULL,
initpk json NOT NULL, initpk json NOT NULL,
sshopts json NOT NULL, sshopts json NOT NULL,
remoteopts json NOT NULL, remoteopts json NOT NULL,

View File

@ -73,9 +73,10 @@ CREATE TABLE remote (
remotesudo boolean NOT NULL, remotesudo boolean NOT NULL,
remoteuser varchar(50) NOT NULL, remoteuser varchar(50) NOT NULL,
remotehost varchar(200) NOT NULL, remotehost varchar(200) NOT NULL,
autoconnect boolean NOT NULL, connectmode varchar(20) NOT NULL,
initpk json NOT NULL, initpk json NOT NULL,
sshopts json NOT NULL, sshopts json NOT NULL,
remoteopts json NOT NULL,
lastconnectts bigint NOT NULL lastconnectts bigint NOT NULL
); );
CREATE TABLE cmd ( CREATE TABLE cmd (

View File

@ -53,6 +53,8 @@ func SubMetaCmd(cmd string) string {
return "comment" return "comment"
case "e": case "e":
return "eval" return "eval"
case "export":
return "setenv"
default: default:
return cmd return cmd
} }
@ -66,6 +68,7 @@ var ValidCommands = []string{
"/comment", "/comment",
"/cd", "/cd",
"/compgen", "/compgen",
"/setenv",
} }
func HandleCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { func HandleCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
@ -94,6 +97,9 @@ func HandleCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstor
case "compgen": case "compgen":
return CompGenCommand(ctx, pk) return CompGenCommand(ctx, pk)
case "setenv":
return SetEnvCommand(ctx, pk)
default: default:
return nil, fmt.Errorf("invalid command '/%s', no handler", pk.MetaCmd) return nil, fmt.Errorf("invalid command '/%s', no handler", pk.MetaCmd)
} }
@ -338,6 +344,12 @@ func evalCommandInternal(ctx context.Context, pk *scpacket.FeCommandPacketType)
} else if commandStr == "cr" || strings.HasPrefix(commandStr, "cr ") { } else if commandStr == "cr" || strings.HasPrefix(commandStr, "cr ") {
metaCmd = "cr" metaCmd = "cr"
commandStr = strings.TrimSpace(commandStr[2:]) commandStr = strings.TrimSpace(commandStr[2:])
} else if commandStr == "export" || strings.HasPrefix(commandStr, "export ") {
metaCmd = "setenv"
commandStr = strings.TrimSpace(commandStr[6:])
} else if commandStr == "setenv" || strings.HasPrefix(commandStr, "setenv ") {
metaCmd = "setenv"
commandStr = strings.TrimSpace(commandStr[6:])
} else if commandStr[0] == '/' { } else if commandStr[0] == '/' {
spaceIdx := strings.Index(commandStr, " ") spaceIdx := strings.Index(commandStr, " ")
if spaceIdx == -1 { if spaceIdx == -1 {
@ -425,6 +437,10 @@ func ScreenCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstor
return update, nil return update, nil
} }
func SetEnvCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
return nil, nil
}
func CrCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { func CrCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
ids, err := resolveIds(ctx, pk, R_Session|R_Window) ids, err := resolveIds(ctx, pk, R_Session|R_Window)
if err != nil { if err != nil {

View File

@ -62,7 +62,7 @@ type RemoteState struct {
Status string `json:"status"` Status string `json:"status"`
ErrorStr string `json:"errorstr,omitempty"` ErrorStr string `json:"errorstr,omitempty"`
DefaultState *sstore.RemoteState `json:"defaultstate"` DefaultState *sstore.RemoteState `json:"defaultstate"`
AutoConnect bool `json:"autoconnect"` ConnectMode string `json:"connectmode"`
} }
type MShellProc struct { type MShellProc struct {
@ -91,7 +91,7 @@ func LoadRemotes(ctx context.Context) error {
for _, remote := range allRemotes { for _, remote := range allRemotes {
msh := MakeMShell(remote) msh := MakeMShell(remote)
GlobalStore.Map[remote.RemoteId] = msh GlobalStore.Map[remote.RemoteId] = msh
if remote.AutoConnect { if remote.ConnectMode == sstore.ConnectModeStartup {
go msh.Launch() go msh.Launch()
} }
} }
@ -128,7 +128,7 @@ func GetAllRemoteState() []RemoteState {
RemoteCanonicalName: proc.Remote.RemoteCanonicalName, RemoteCanonicalName: proc.Remote.RemoteCanonicalName,
PhysicalId: proc.Remote.PhysicalId, PhysicalId: proc.Remote.PhysicalId,
Status: proc.Status, Status: proc.Status,
AutoConnect: proc.Remote.AutoConnect, ConnectMode: proc.Remote.ConnectMode,
} }
if proc.Err != nil { if proc.Err != nil {
state.ErrorStr = proc.Err.Error() state.ErrorStr = proc.Err.Error()

View File

@ -90,8 +90,8 @@ func InsertRemote(ctx context.Context, remote *RemoteType) error {
if err != nil { if err != nil {
return err return err
} }
query := `INSERT INTO remote ( remoteid, physicalid, remotetype, remotealias, remotecanonicalname, remotesudo, remoteuser, remotehost, autoconnect, initpk, sshopts, remoteopts, lastconnectts) VALUES query := `INSERT INTO remote ( remoteid, physicalid, remotetype, remotealias, remotecanonicalname, remotesudo, remoteuser, remotehost, connectmode, initpk, sshopts, remoteopts, lastconnectts) VALUES
(:remoteid,:physicalid,:remotetype,:remotealias,:remotecanonicalname,:remotesudo,:remoteuser,:remotehost,:autoconnect,:initpk,:sshopts,:remoteopts,:lastconnectts)` (:remoteid,:physicalid,:remotetype,:remotealias,:remotecanonicalname,:remotesudo,:remoteuser,:remotehost,:connectmode,:initpk,:sshopts,:remoteopts,:lastconnectts)`
_, err = db.NamedExec(query, remote.ToMap()) _, err = db.NamedExec(query, remote.ToMap())
if err != nil { if err != nil {
return err return err

View File

@ -50,6 +50,16 @@ const (
ShareModeShared = "shared" ShareModeShared = "shared"
) )
const (
ConnectModeStartup = "startup"
ConnectModeAuto = "auto"
ConnectModeManual = "manual"
)
const (
RemoteTypeSsh = "ssh"
)
var globalDBLock = &sync.Mutex{} var globalDBLock = &sync.Mutex{}
var globalDB *sqlx.DB var globalDB *sqlx.DB
var globalDBErr error var globalDBErr error
@ -298,7 +308,7 @@ type RemoteType struct {
RemoteSudo bool `json:"remotesudo"` RemoteSudo bool `json:"remotesudo"`
RemoteUser string `json:"remoteuser"` RemoteUser string `json:"remoteuser"`
RemoteHost string `json:"remotehost"` RemoteHost string `json:"remotehost"`
AutoConnect bool `json:"autoconnect"` ConnectMode string `json:"connectmode"`
InitPk *packet.InitPacketType `json:"inipk"` InitPk *packet.InitPacketType `json:"inipk"`
SSHOpts *SSHOpts `json:"sshopts"` SSHOpts *SSHOpts `json:"sshopts"`
RemoteOpts *RemoteOptsType `json:"remoteopts"` RemoteOpts *RemoteOptsType `json:"remoteopts"`
@ -340,7 +350,7 @@ func (r *RemoteType) ToMap() map[string]interface{} {
rtn["remotesudo"] = r.RemoteSudo rtn["remotesudo"] = r.RemoteSudo
rtn["remoteuser"] = r.RemoteUser rtn["remoteuser"] = r.RemoteUser
rtn["remotehost"] = r.RemoteHost rtn["remotehost"] = r.RemoteHost
rtn["autoconnect"] = r.AutoConnect rtn["connectmode"] = r.ConnectMode
rtn["initpk"] = quickJson(r.InitPk) rtn["initpk"] = quickJson(r.InitPk)
rtn["sshopts"] = quickJson(r.SSHOpts) rtn["sshopts"] = quickJson(r.SSHOpts)
rtn["remoteopts"] = quickJson(r.RemoteOpts) rtn["remoteopts"] = quickJson(r.RemoteOpts)
@ -361,7 +371,7 @@ func RemoteFromMap(m map[string]interface{}) *RemoteType {
quickSetBool(&r.RemoteSudo, m, "remotesudo") quickSetBool(&r.RemoteSudo, m, "remotesudo")
quickSetStr(&r.RemoteUser, m, "remoteuser") quickSetStr(&r.RemoteUser, m, "remoteuser")
quickSetStr(&r.RemoteHost, m, "remotehost") quickSetStr(&r.RemoteHost, m, "remotehost")
quickSetBool(&r.AutoConnect, m, "autoconnect") quickSetStr(&r.ConnectMode, m, "connectmode")
quickSetJson(&r.InitPk, m, "initpk") quickSetJson(&r.InitPk, m, "initpk")
quickSetJson(&r.SSHOpts, m, "sshopts") quickSetJson(&r.SSHOpts, m, "sshopts")
quickSetJson(&r.RemoteOpts, m, "remoteopts") quickSetJson(&r.RemoteOpts, m, "remoteopts")
@ -470,13 +480,13 @@ func EnsureLocalRemote(ctx context.Context) error {
localRemote := &RemoteType{ localRemote := &RemoteType{
RemoteId: uuid.New().String(), RemoteId: uuid.New().String(),
PhysicalId: physicalId, PhysicalId: physicalId,
RemoteType: "ssh", RemoteType: RemoteTypeSsh,
RemoteAlias: LocalRemoteName, RemoteAlias: LocalRemoteName,
RemoteCanonicalName: fmt.Sprintf("%s@%s", user.Username, hostName), RemoteCanonicalName: fmt.Sprintf("%s@%s", user.Username, hostName),
RemoteSudo: false, RemoteSudo: false,
RemoteUser: user.Username, RemoteUser: user.Username,
RemoteHost: hostName, RemoteHost: hostName,
AutoConnect: true, ConnectMode: ConnectModeStartup,
} }
err = InsertRemote(ctx, localRemote) err = InsertRemote(ctx, localRemote)
if err != nil { if err != nil {
@ -496,7 +506,7 @@ func AddTest01Remote(ctx context.Context) error {
} }
testRemote := &RemoteType{ testRemote := &RemoteType{
RemoteId: uuid.New().String(), RemoteId: uuid.New().String(),
RemoteType: "ssh", RemoteType: RemoteTypeSsh,
RemoteAlias: "test01", RemoteAlias: "test01",
RemoteCanonicalName: "ubuntu@test01.ec2", RemoteCanonicalName: "ubuntu@test01.ec2",
RemoteSudo: false, RemoteSudo: false,
@ -507,7 +517,7 @@ func AddTest01Remote(ctx context.Context) error {
SSHUser: "ubuntu", SSHUser: "ubuntu",
SSHIdentity: "/Users/mike/aws/mfmt.pem", SSHIdentity: "/Users/mike/aws/mfmt.pem",
}, },
AutoConnect: true, ConnectMode: ConnectModeStartup,
} }
err = InsertRemote(ctx, testRemote) err = InsertRemote(ctx, testRemote)
if err != nil { if err != nil {
@ -527,7 +537,7 @@ func AddTest02Remote(ctx context.Context) error {
} }
testRemote := &RemoteType{ testRemote := &RemoteType{
RemoteId: uuid.New().String(), RemoteId: uuid.New().String(),
RemoteType: "ssh", RemoteType: RemoteTypeSsh,
RemoteAlias: "test2", RemoteAlias: "test2",
RemoteCanonicalName: "test2@test01.ec2", RemoteCanonicalName: "test2@test01.ec2",
RemoteSudo: false, RemoteSudo: false,
@ -537,7 +547,7 @@ func AddTest02Remote(ctx context.Context) error {
SSHHost: "test01.ec2", SSHHost: "test01.ec2",
SSHUser: "test2", SSHUser: "test2",
}, },
AutoConnect: true, ConnectMode: ConnectModeStartup,
} }
err = InsertRemote(ctx, testRemote) err = InsertRemote(ctx, testRemote)
if err != nil { if err != nil {