mirror of
https://github.com/wavetermdev/waveterm.git
synced 2025-01-03 18:47:56 +01:00
2913babea7
* feat: share sudo between pty sessions This is a first pass at a feature to cache the sudo password and share it between different pty sessions. This makes it possible to not require manual password entry every time sudo is used. * feat: allow error handling and canceling sudo cmds This adds the missing functionality that prevented failed sudo commands from automatically closing. * feat: restrict sudo caching to dev mode for now * modify fullCmdStr not pk.Command * refactor: condense ecdh encryptor creation This refactors the common pieces needed to create an encryptor from an ecdh key pair into a common function. * refactor: rename promptenc to waveenc * feat: add command to clear sudo password We currently do not provide use of the sudo -k and sudo -K commands to clear the sudo password. This adds a /sudo:clear command to handle it in the meantime. * feat: add kwarg to force sudo In cases where parsing for sudo doesn't work, this provides an alternate wave kwarg to use instead. It can be used with [sudo=1] at the beginning of a command. * refactor: simplify sudoArg parsing * feat: allow user to clear all sudo passwords This introduces the "all" kwarg for the sudo:clear command in order to clear all sudo passwords. * fix: handle deadline with real time Golang's time module uses monatomic time by default, but that is not desired for the password timeout since we want the timer to continue even if the computer is asleep. We now avoid this by directly comparing the unix timestamps. * fix: remove sudo restriction to dev mode This allows it to be used in regular builds as well. * fix: switch to password timeout without wait group This removes an unnecessary waiting period for sudo password entry. * fix: update deadline in sudo:clear This allows sudo:clear to cancel the goroutine for watching the password timer. * fix: pluralize sudo:clear message when all=1 This changes the output message for /sudo:clear to indicate multiple passwords cleared if the all=1 kwarg is used. * fix: use GetRemoteMap for getting remotes in clear The sudo:clear command was directly looping over the GlobalStore.Map which is not thread safe. Switched to GetRemoteMap which uses a lock internally. * fix: allow sudo metacmd to set sudo false This fixes the logic for resolving if a command is a sudo command. This change makes it possible for the sudo metacmd kwarg to force sudo to be false.
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
// Copyright 2024, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Provides a mechanism for the backend to request user input from the frontend.
|
|
package userinput
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"github.com/wavetermdev/waveterm/waveshell/pkg/packet"
|
|
"github.com/wavetermdev/waveterm/wavesrv/pkg/scbus"
|
|
)
|
|
|
|
const UserInputRequestStr = "userinputrequest"
|
|
|
|
// An RpcPacket for requesting user input from the client
|
|
type UserInputRequestType struct {
|
|
RequestId string `json:"requestid"`
|
|
QueryText string `json:"querytext"`
|
|
ResponseType string `json:"responsetype"`
|
|
Title string `json:"title"`
|
|
Markdown bool `json:"markdown"`
|
|
TimeoutMs int `json:"timeoutms"`
|
|
CheckBoxMsg string `json:"checkboxmsg"`
|
|
PublicText bool `json:"publictext"`
|
|
}
|
|
|
|
func (*UserInputRequestType) GetType() string {
|
|
return UserInputRequestStr
|
|
}
|
|
|
|
func (req *UserInputRequestType) SetReqId(reqId string) {
|
|
req.RequestId = reqId
|
|
}
|
|
|
|
func (req *UserInputRequestType) SetTimeoutMs(timeoutMs int) {
|
|
req.TimeoutMs = timeoutMs
|
|
}
|
|
|
|
const UserInputResponsePacketStr = "userinputresp"
|
|
|
|
// An RpcResponse for user input requests
|
|
type UserInputResponsePacketType struct {
|
|
Type string `json:"type"`
|
|
RequestId string `json:"requestid"`
|
|
Text string `json:"text,omitempty"`
|
|
Confirm bool `json:"confirm,omitempty"`
|
|
ErrorMsg string `json:"errormsg,omitempty"`
|
|
CheckboxStat bool `json:"checkboxstat,omitempty"`
|
|
}
|
|
|
|
func (*UserInputResponsePacketType) GetType() string {
|
|
return UserInputResponsePacketStr
|
|
}
|
|
|
|
func (pk *UserInputResponsePacketType) GetError() string {
|
|
return pk.ErrorMsg
|
|
}
|
|
|
|
func (pk *UserInputResponsePacketType) SetError(err string) {
|
|
pk.ErrorMsg = err
|
|
}
|
|
|
|
// Send a user input request to the frontend and wait for a response
|
|
func GetUserInput(ctx context.Context, bus *scbus.RpcBus, userInputRequest *UserInputRequestType) (*UserInputResponsePacketType, error) {
|
|
resp, err := scbus.MainRpcBus.DoRpc(ctx, userInputRequest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if ret, ok := resp.(*UserInputResponsePacketType); !ok {
|
|
return nil, fmt.Errorf("unexpected response type: %v", reflect.TypeOf(resp))
|
|
} else {
|
|
return ret, nil
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
// Register the user input request packet type
|
|
packet.RegisterPacketType(UserInputResponsePacketStr, reflect.TypeOf(UserInputResponsePacketType{}))
|
|
}
|