ssh config import (#156)

* create migrations for required database change

This is a first attempt that does not appear to be working properly. It
requires review.

* fix errors in db migrations

The previous commit had an extra json call that broke the update and did
not remove the imported interies during a downgrade.

* change migrations to use column instead of json

It makes more sense to associate the source of a config with the remote
type than the sshopts type. This change makes that clear in the database
structure.

* ensure adding a remote manually tags correctly

Using the usual way of adding a remote should result in a sshconfigsrc
of "waveterm-manual". This will be important for filtering out remotes
installed manually and remotes installed via import

* create basic structure for parsing ssh config

This entails creating a new command, making it possible to query only
the imported remotes from the database, and implementing the logic to
handle all of the updates needed.

This needs improvements in a few areas:
- the /etc/ssh/config needs to be parsed as well
- the logic for editing exisiting imported remotes needs to be written
- error handling needs to be improved
- update packet responses need to be provided

* add sshkey support and implement editing

We now search for the ssh identity keyfile and add it if it is found.
Additionally, the logic to edit previously imported ssh hosts has been
added.

* combine hosts from user and system ssh config

We now check both the user ~/.ssh/config as well as the /etc/ssh/config
for hosts. This loops through each file starting with the user one. For
each host, it selects the first pattern without a wildcard and chooses
that to be the alias. If any future hosts are found to have the same
alias, they are skipped. Errors are raised if neither config file can be
opened or no aliases were found.

* improve logging and error reporting

Error reporting is now shortcircuited in cases of individual remotes in
order to allow the other remotes to continue. These errors are now
printed to logs instead.

* allow imports to edit ssh port

Previously, ssh ports could not be edited after the fact. Unfortunately,
this can cause problems since the port can be changed in an ssh config
file. To address this, we allow imports to change the port if a host
with the same canonical name had previously been imported.

* fix response to parse command

* fix error handline for alias parsing

Small mistake of checking for equality instead of inequality

* fix the ability to overwrite hostName with alias

if ssh_config does not find Hostname, it won't output an error. Now we
compare against the result instead of looking for an error.

* fix the error catching for User and Port

This fixes the same problem where parsing the config doesn't give an
error in the case when nothing is found. As before, this checks for a
blank result instead.

* remove unused code

* remove repeated canonical name check

The logic that checks for an existing canonical name already exists in
the AddRemote function, so it is not needed here.

Secondly, we now only allow edits of previously created remotes if they
have not been archived. If they have, the usual logic for creating a new
remote takes precedence.

Lastly, there is no need to archive a remote that has already been
archived so an additional check has been added.

* allow archives to preserve the SSHConfigSrc

* add log message for archiving of imported remotes

* create variables for string variants

Matches existing code style

* add cleanup for opened files

* move migration 25 to migration 26 (already merged a migration 25)

* fix RemoteRuntimeState in ModelUpdate by moving type to sstore.go.  Fix some bugs in remote:parse.  Fix key/identityfile, return value, and remote editing (should go through msh).  remote sudo.  add info messages around parse status

* fix issue with archiving the sshconfigsrc

A bug in RemoteType's FromMap caused the loss of sshconfigsrc during the
conversion. This has been corrected and the schema has been updated.

* fix order of archiving removed imported remotes

Previously, if the canonical name changed, the code would try to create
a new remote before archiving the old one. This did not work if the
alias didn't change. Now we archive first and add a new remote after.

* fix ability to change port when importing config

Importing from sshconfig needs to allow the port to change. This was not
happening because of a bug that has been corrected.

* always use host in place of hostname

Since host is the key actually searched for in the ssh config file,
searching for user@hostName may not actually work. To avoid this, we now
always use user@host instead.

* automatically determine ConnectMode

This aims to select a connection mode based off what is provided in the
ssh config file. It aims for auto connections when possible but will
fall back to manual if we can't easily support it

* remove sshkeysource migration number confilict

Previously had conflicting migration numbers of 26. The change not in
the main branch has been moved to 27 to remove the conflict.

* move sshkeysource migration to migration 28

* add WaveOptions flag parsing for ssh config

This is currently being used to allow users to force manual connect mode
if desired. It will also be used to force skipping options in the future
but that is not complete in this commit.

* implement ignore flag for ssh config parsing

The ignore flag will now archive an imported remote if it previously
existed and not create a new remote in its place.

* fix discovery of identity file

Previously, a ~ in the identity file's path was not expanded to the home
dir. Because of this, files with a ~ were previously identified as
invalid files. By expanding it during the search, this is no longer the
case.

* disable frontend edit button for imported remotes

Imported Remotes should not be editable in waveterm by users. This edit
makes it clear that the button will not work for those cases. Further
edits may be needed to explain why it doesn't work and what to do
instead.

* add backend rejection of updating imported remote

As before, we don't want manual editing of an imported remote inside the
app. This ensures that it can't happen on the backend.

* create tooltips for sshconfig edit/delete buttons

For remotes that are imported, edits are not allowed. This adds a
tooltip that explains what to do instead.

Deleting remotes that are imported is allowed, but they will come back
if the user imports again. The tooltip explains a way to avoid this.

* add logo after name for imported remotes

In the connections screen, there previously was not a way to tell
imported connections from manually created connections. This change adds
a logo after the imported ones to differentiate them.

* small formatting updates

* add import tooltip to connection modal

Added the logo for an imported config to the connection modal. It also
provides a short description when it the mouse hovers over it.

* add button to import ssh config

Make the command into a button for a simple gui interface.

Also ran prettier to clean up some syntax.

* remove strict casing on WaveOptions

WaveOptions was previously very specific about the casing of the ignore
and connectmode subcommands. With this update, the casing is
automatically converted to lowercase and can be ignored.

* add status dot before name in connections screen

* add space and tooltip to connection imported icon

* re-prettier
This commit is contained in:
Sylvie Crowe 2023-12-28 11:09:41 -08:00 committed by GitHub
parent 2f7cce294c
commit 8d88e2cf94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 473 additions and 86 deletions

View File

@ -1,4 +1,6 @@
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

View File

@ -601,6 +601,11 @@
align-items: flex-start;
gap: 12px;
.name-wrapper {
display: flex;
flex-direction: row;
}
.rconndetail-name {
color: @term-bright-white;
font-size: 15px;

View File

@ -453,7 +453,9 @@ class AboutModal extends React.Component<{}, {}> {
</a>
<a
className="wave-button wave-button-link color-standard"
href={util.makeExternLink("https://github.com/wavetermdev/waveterm/blob/main/acknowledgements/README.md")}
href={util.makeExternLink(
"https://github.com/wavetermdev/waveterm/blob/main/acknowledgements/README.md"
)}
rel={"noopener"}
target="_blank"
>
@ -968,7 +970,7 @@ class ViewRemoteConnDetailModal extends React.Component<{}, {}> {
Install Now
</Button>
);
const archiveButton = (
let archiveButton = (
<Button theme="secondary" onClick={() => this.clickArchive()}>
Delete
</Button>
@ -983,6 +985,36 @@ class ViewRemoteConnDetailModal extends React.Component<{}, {}> {
updateAuthButton = <></>;
cancelInstallButton = <></>;
}
if (remote.sshconfigsrc == "sshconfig-import") {
updateAuthButton = (
<Button theme="secondary" disabled={true}>
Edit
<Tooltip
message={`Remotes imported from an ssh config file cannot be edited inside waveterm. To edit these, you must edit the config file and import it again.`}
icon={<i className="fa-sharp fa-regular fa-fw fa-ban" />}
>
<i className="fa-sharp fa-regular fa-fw fa-ban" />
</Tooltip>
</Button>
);
archiveButton = (
<Button theme="secondary" onClick={() => this.clickArchive()}>
Delete
<Tooltip
message={
<span>
Remotes imported from an ssh config file can be deleted, but will come back upon
importing again. They will stay removed if you follow{" "}
<a href="https://docs.waveterm.dev/features/sshconfig-imports">this procedure</a>.
</span>
}
icon={<i className="fa-sharp fa-regular fa-fw fa-triangle-exclamation" />}
>
<i className="fa-sharp fa-regular fa-fw fa-triangle-exclamation" />
</Tooltip>
</Button>
);
}
if (remote.status == "connected" || remote.status == "connecting") {
buttons.push(disconnectButton);
} else if (remote.status == "disconnected") {
@ -1057,7 +1089,9 @@ class ViewRemoteConnDetailModal extends React.Component<{}, {}> {
<Modal.Header title="Connection" onClose={this.model.closeModal} />
<div className="wave-modal-body">
<div className="name-header-actions-wrapper">
<div className="name text-primary">{getName(remote)}</div>
<div className="name text-primary name-wrapper">
{getName(remote)}&nbsp; {getImportTooltip(remote)}
</div>
<div className="header-actions">{this.renderHeaderBtns(remote)}</div>
</div>
<div className="remote-detail" style={{ overflow: "hidden" }}>
@ -1433,6 +1467,21 @@ const getName = (remote: T.RemoteType): string => {
return remotealias ? `${remotealias} [${remotecanonicalname}]` : remotecanonicalname;
};
const getImportTooltip = (remote: T.RemoteType): React.ReactElement<any, any> => {
if (remote.sshconfigsrc == "sshconfig-import") {
return (
<Tooltip
message={`This remote was imported from an SSH config file.`}
icon={<i className="fa-sharp fa-solid fa-file-import" />}
>
<i className="fa-sharp fa-solid fa-file-import" />
</Tooltip>
);
} else {
return <></>;
}
};
export {
LoadingSpinner,
ClientStopModal,

View File

@ -87,6 +87,12 @@
display: flex;
visibility: hidden;
}
&.col-name {
display: flex;
flex-direction: row;
align-items: center;
}
}
&.hovered {
@ -99,6 +105,9 @@
footer {
margin-left: 10px;
display: flex;
flex-direction: row;
gap: 8px;
}
.help-entry {

View File

@ -58,11 +58,26 @@ class ConnectionsView extends React.Component<{ model: RemotesModel }, { hovered
return remotealias ? `${remotealias} [${remotecanonicalname}]` : remotecanonicalname;
}
@boundMethod
getImportSymbol(item: T.RemoteType): React.ReactElement<any, any> {
const { sshconfigsrc } = item;
if (sshconfigsrc == "sshconfig-import") {
return <i title="Connection Imported from SSH Config" className="fa-sharp fa-solid fa-file-import" />;
} else {
return <></>;
}
}
@boundMethod
handleAddConnection(): void {
GlobalModel.remotesModel.openAddModal({ remoteedit: true });
}
@boundMethod
handleImportSshConfig(): void {
GlobalCommandRunner.importSshConfig();
}
@boundMethod
handleRead(remoteId: string): void {
GlobalModel.remotesModel.openReadModal(remoteId);
@ -148,7 +163,8 @@ class ConnectionsView extends React.Component<{ model: RemotesModel }, { hovered
onClick={() => this.handleRead(item.remoteid)} // Moved onClick here
>
<td className="col-name">
<div>{this.getName(item)}</div>
<Status status={this.getStatus(item.status)} text=""></Status>
{this.getName(item)}&nbsp;{this.getImportSymbol(item)}
</td>
<td className="col-type">
<div>{item.remotetype}</div>
@ -170,6 +186,13 @@ class ConnectionsView extends React.Component<{ model: RemotesModel }, { hovered
>
New Connection
</Button>
<Button
theme="secondary"
leftIcon={<i className="fa-sharp fa-solid fa-fw fa-file-import"></i>}
onClick={this.handleImportSshConfig}
>
Import Config
</Button>
</footer>
<If condition={items.length == 0}>
<div className="no-items">

View File

@ -4406,6 +4406,10 @@ class CommandRunner {
GlobalModel.submitCommand("remote", "archive", null, { remote: remoteid, nohist: "1" }, true);
}
importSshConfig() {
GlobalModel.submitCommand("remote", "parse", null, null, false);
}
screenSelectLine(lineArg: string, focusVal?: string) {
let kwargs: Record<string, string> = {
nohist: "1",

View File

@ -110,6 +110,7 @@ type RemoteType = {
connectmode: string;
autoinstall: boolean;
remoteidx: number;
sshconfigsrc: string;
archived: boolean;
uname: string;
mshellversion: string;

View File

@ -0,0 +1,3 @@
DELETE FROM remote WHERE sshconfigsrc != 'waveterm-manual';
ALTER TABLE remote DROP COLUMN sshconfigsrc;

View File

@ -0,0 +1 @@
ALTER TABLE remote ADD COLUMN sshconfigsrc varchar(36) NOT NULL DEFAULT 'waveterm-manual';

View File

@ -55,8 +55,10 @@ CREATE TABLE remote (
lastconnectts bigint NOT NULL,
local boolean NOT NULL,
archived boolean NOT NULL,
remoteidx int NOT NULL
, statevars json NOT NULL DEFAULT '{}', openaiopts json NOT NULL DEFAULT '{}', sshconfigsrc varchar(36) NOT NULL DEFAULT 'waveterm-manual');
remoteidx int NOT NULL,
statevars json NOT NULL DEFAULT '{}',
sshconfigsrc varchar(36) NOT NULL DEFAULT 'waveterm-manual',
openaiopts json NOT NULL DEFAULT '{}');
CREATE TABLE history (
historyid varchar(36) PRIMARY KEY,
ts bigint NOT NULL,

View File

@ -6,6 +6,7 @@ require (
github.com/alessio/shellescape v1.4.1
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2
github.com/creack/pty v1.1.18
github.com/kevinburke/ssh_config v1.2.0
github.com/golang-migrate/migrate/v4 v4.16.2
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0

View File

@ -7,6 +7,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
@ -23,6 +24,7 @@ import (
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/kevinburke/ssh_config"
"github.com/wavetermdev/waveterm/waveshell/pkg/base"
"github.com/wavetermdev/waveterm/waveshell/pkg/packet"
"github.com/wavetermdev/waveterm/waveshell/pkg/shexec"
@ -191,6 +193,7 @@ func init() {
registerCmdFn("remote:install", RemoteInstallCommand)
registerCmdFn("remote:installcancel", RemoteInstallCancelCommand)
registerCmdFn("remote:reset", RemoteResetCommand)
registerCmdFn("remote:parse", RemoteConfigParseCommand)
registerCmdFn("screen:resize", ScreenResizeCommand)
@ -1362,6 +1365,7 @@ func RemoteNewCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss
ConnectMode: editArgs.ConnectMode,
AutoInstall: editArgs.AutoInstall,
SSHOpts: editArgs.SSHOpts,
SSHConfigSrc: sstore.SSHConfigSrcTypeManual,
}
if editArgs.Color != "" {
r.RemoteOpts = &sstore.RemoteOptsType{Color: editArgs.Color}
@ -1383,6 +1387,9 @@ func RemoteSetCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss
if err != nil {
return nil, err
}
if ids.Remote.RState.SSHConfigSrc == sstore.SSHConfigSrcTypeImport {
return nil, fmt.Errorf("/remote:new cannot update imported remote")
}
visualEdit := resolveBool(pk.Kwargs["visual"], false)
isSubmitted := resolveBool(pk.Kwargs["submit"], false)
editArgs, err := parseRemoteEditArgs(false, pk, ids.Remote.MShell.IsLocal())
@ -1447,6 +1454,244 @@ func RemoteShowAllCommand(ctx context.Context, pk *scpacket.FeCommandPacketType)
}, nil
}
func resolveSshConfigPatterns(configFiles []string) ([]string, error) {
// using two separate containers to track order and have O(1) lookups
// since go does not have an ordered map primitive
var discoveredPatterns []string
alreadyUsed := make(map[string]bool)
alreadyUsed[""] = true // this excludes the empty string from potential alias
var openedFiles []fs.File
defer func() {
for _, openedFile := range openedFiles {
openedFile.Close()
}
}()
var errs []error
for _, configFile := range configFiles {
fd, openErr := os.Open(configFile)
openedFiles = append(openedFiles, fd)
if fd == nil {
errs = append(errs, openErr)
continue
}
cfg, _ := ssh_config.Decode(fd)
for _, host := range cfg.Hosts {
// for each host, find the first good alias
for _, hostPattern := range host.Patterns {
hostPatternStr := hostPattern.String()
if strings.Index(hostPatternStr, "*") == -1 || alreadyUsed[hostPatternStr] == true {
discoveredPatterns = append(discoveredPatterns, hostPatternStr)
alreadyUsed[hostPatternStr] = true
break
}
}
}
}
if len(errs) == len(configFiles) {
errs = append([]error{fmt.Errorf("no ssh config files could be opened:\n")}, errs...)
return nil, errors.Join(errs...)
}
if len(discoveredPatterns) == 0 {
return nil, fmt.Errorf("no compatible hostnames found in ssh config files")
}
return discoveredPatterns, nil
}
type HostInfoType struct {
Host string
User string
CanonicalName string
Port int
SshKeyFile string
ConnectMode string
Ignore bool
}
func NewHostInfo(hostName string) (*HostInfoType, error) {
userName, _ := ssh_config.GetStrict(hostName, "User")
if userName == "" {
// we cannot store a remote with a missing user
// in the current setup
return nil, fmt.Errorf("could not parse \"%s\" - no User in config\n", hostName)
}
canonicalName := userName + "@" + hostName
// check if user and host are okay
m := userHostRe.FindStringSubmatch(canonicalName)
if m == nil || m[2] == "" || m[3] == "" {
return nil, fmt.Errorf("could not parse \"%s\" - %s did not fit user@host requirement\n", hostName, canonicalName)
}
portStr, _ := ssh_config.GetStrict(hostName, "Port")
var portVal int
if portStr != "" {
var err error
portVal, err = strconv.Atoi(portStr)
if err != nil {
// do not make assumptions about port if incorrectly configured
return nil, fmt.Errorf("could not parse \"%s\" (%s) - %s could not be converted to a valid port\n", hostName, canonicalName, portStr)
}
if int(int16(portVal)) != portVal {
return nil, fmt.Errorf("could not parse port \"%d\": number is not valid for a port\n", portVal)
}
}
identityFile, _ := ssh_config.GetStrict(hostName, "IdentityFile")
passwordAuth, _ := ssh_config.GetStrict(hostName, "PasswordAuthentication")
cfgWaveOptionsStr, _ := ssh_config.GetStrict(hostName, "WaveOptions")
cfgWaveOptionsStr = strings.ToLower(cfgWaveOptionsStr)
cfgWaveOptions := make(map[string]string)
setBracketArgs(cfgWaveOptions, cfgWaveOptionsStr)
shouldIgnore := false
if result, _ := strconv.ParseBool(cfgWaveOptions["ignore"]); result {
shouldIgnore = true
}
var sshKeyFile string
connectMode := sstore.ConnectModeAuto
if cfgWaveOptions["connectmode"] == "manual" {
connectMode = sstore.ConnectModeManual
} else if _, err := os.Stat(base.ExpandHomeDir(identityFile)); err == nil {
sshKeyFile = identityFile
} else if passwordAuth == "yes" {
connectMode = sstore.ConnectModeManual
}
outHostInfo := new(HostInfoType)
outHostInfo.Host = hostName
outHostInfo.User = userName
outHostInfo.CanonicalName = canonicalName
outHostInfo.Port = portVal
outHostInfo.SshKeyFile = sshKeyFile
outHostInfo.ConnectMode = connectMode
outHostInfo.Ignore = shouldIgnore
return outHostInfo, nil
}
func RemoteConfigParseCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
home := base.GetHomeDir()
localConfig := filepath.Join(home, ".ssh", "config")
systemConfig := filepath.Join("/", "ssh", "config")
sshConfigFiles := []string{localConfig, systemConfig}
hostPatterns, hostPatternsErr := resolveSshConfigPatterns(sshConfigFiles)
if hostPatternsErr != nil {
return nil, hostPatternsErr
}
previouslyImportedRemotes, dbQueryErr := sstore.GetAllImportedRemotes(ctx)
if dbQueryErr != nil {
return nil, dbQueryErr
}
var parsedHostData []*HostInfoType
hostInfoInConfig := make(map[string]*HostInfoType)
for _, hostPattern := range hostPatterns {
hostInfo, hostInfoErr := NewHostInfo(hostPattern)
if hostInfoErr != nil {
log.Printf("sshconfig-import: %s", hostInfoErr)
continue
}
parsedHostData = append(parsedHostData, hostInfo)
hostInfoInConfig[hostInfo.CanonicalName] = hostInfo
}
// remove all previously imported remotes that
// no longer have a canonical pattern in the config files
for importedRemoteCanonicalName, importedRemote := range previouslyImportedRemotes {
var err error
hostInfo := hostInfoInConfig[importedRemoteCanonicalName]
if !importedRemote.Archived && (hostInfo == nil || hostInfo.Ignore) {
err = remote.ArchiveRemote(ctx, importedRemote.RemoteId)
if err != nil {
log.Printf("sshconfig-import: failed to remove remote \"%s\" (%s)\n", importedRemote.RemoteAlias, importedRemote.RemoteCanonicalName)
} else {
log.Printf("sshconfig-import: archived remote \"%s\" (%s)\n", importedRemote.RemoteAlias, importedRemote.RemoteCanonicalName)
}
}
}
var updatedRemotes []string
for _, hostInfo := range parsedHostData {
previouslyImportedRemote := previouslyImportedRemotes[hostInfo.CanonicalName]
updatedRemotes = append(updatedRemotes, hostInfo.CanonicalName)
if hostInfo.Ignore {
log.Printf("sshconfig-import: ignore remote[%s] as specified in config file\n", hostInfo.CanonicalName)
continue
}
if previouslyImportedRemote != nil && !previouslyImportedRemote.Archived {
// this already existed and was created via import
// it needs to be updated instead of created
editMap := make(map[string]interface{})
editMap[sstore.RemoteField_Alias] = hostInfo.Host
editMap[sstore.RemoteField_ConnectMode] = hostInfo.ConnectMode
// changing port is unique to imports because it lets us avoid conflicts
// if the port is changed in the ssh config
editMap[sstore.RemoteField_SSHPort] = hostInfo.Port
if hostInfo.SshKeyFile != "" {
editMap[sstore.RemoteField_SSHKey] = hostInfo.SshKeyFile
}
msh := remote.GetRemoteById(previouslyImportedRemote.RemoteId)
if msh == nil {
log.Printf("strange, msh for remote %s [%s] not found\n", hostInfo.CanonicalName, previouslyImportedRemote.RemoteId)
continue
} else {
err := msh.UpdateRemote(ctx, editMap)
if err != nil {
log.Printf("error updating remote[%s]: %v\n", hostInfo.CanonicalName, err)
continue
}
}
log.Printf("sshconfig-import: found previously imported remote with canonical name \"%s\": it has been updated\n", hostInfo.CanonicalName)
} else {
sshOpts := &sstore.SSHOpts{
Local: false,
SSHHost: hostInfo.Host,
SSHUser: hostInfo.User,
IsSudo: false,
SSHPort: hostInfo.Port,
}
if hostInfo.SshKeyFile != "" {
sshOpts.SSHIdentity = hostInfo.SshKeyFile
}
// this is new and must be created for the first time
r := &sstore.RemoteType{
RemoteId: scbase.GenWaveUUID(),
RemoteType: sstore.RemoteTypeSsh,
RemoteAlias: hostInfo.Host,
RemoteCanonicalName: hostInfo.CanonicalName,
RemoteUser: hostInfo.User,
RemoteHost: hostInfo.Host,
ConnectMode: hostInfo.ConnectMode,
AutoInstall: true,
SSHOpts: sshOpts,
SSHConfigSrc: sstore.SSHConfigSrcTypeImport,
}
err := remote.AddRemote(ctx, r, false)
if err != nil {
log.Printf("sshconfig-import: failed to add remote \"%s\" (%s): it is being skipped\n", hostInfo.Host, hostInfo.CanonicalName)
continue
}
log.Printf("sshconfig-import: created remote \"%s\" (%s)\n", hostInfo.Host, hostInfo.CanonicalName)
}
}
update := &sstore.ModelUpdate{Remotes: remote.GetAllRemoteRuntimeState()}
update.Info = &sstore.InfoMsgType{}
if len(updatedRemotes) == 0 {
update.Info.InfoMsg = "no connections imported from ssh config."
} else {
update.Info.InfoMsg = fmt.Sprintf("imported %d connection(s) from ssh config file: %s\n", len(updatedRemotes), strings.Join(updatedRemotes, ", "))
}
return update, nil
}
func ScreenShowAllCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
ids, err := resolveUiIds(ctx, pk, R_Session)
screenArr, err := sstore.GetSessionScreens(ctx, ids.SessionId)

View File

@ -79,10 +79,10 @@ func MakeServerCommandStr() string {
}
const (
StatusConnected = "connected"
StatusConnecting = "connecting"
StatusDisconnected = "disconnected"
StatusError = "error"
StatusConnected = sstore.RemoteStatus_Connected
StatusConnecting = sstore.RemoteStatus_Connecting
StatusDisconnected = sstore.RemoteStatus_Disconnected
StatusError = sstore.RemoteStatus_Error
)
func init() {
@ -141,36 +141,7 @@ type RunCmdType struct {
RunPacket *packet.RunPacketType
}
type RemoteRuntimeState struct {
RemoteType string `json:"remotetype"`
RemoteId string `json:"remoteid"`
RemoteAlias string `json:"remotealias,omitempty"`
RemoteCanonicalName string `json:"remotecanonicalname"`
RemoteVars map[string]string `json:"remotevars"`
DefaultFeState map[string]string `json:"defaultfestate"`
Status string `json:"status"`
ConnectTimeout int `json:"connecttimeout,omitempty"`
ErrorStr string `json:"errorstr,omitempty"`
InstallStatus string `json:"installstatus"`
InstallErrorStr string `json:"installerrorstr,omitempty"`
NeedsMShellUpgrade bool `json:"needsmshellupgrade,omitempty"`
NoInitPk bool `json:"noinitpk,omitempty"`
AuthType string `json:"authtype,omitempty"`
ConnectMode string `json:"connectmode"`
AutoInstall bool `json:"autoinstall"`
Archived bool `json:"archived,omitempty"`
RemoteIdx int64 `json:"remoteidx"`
UName string `json:"uname"`
MShellVersion string `json:"mshellversion"`
WaitingForPassword bool `json:"waitingforpassword,omitempty"`
Local bool `json:"local,omitempty"`
RemoteOpts *sstore.RemoteOptsType `json:"remoteopts,omitempty"`
CanComplete bool `json:"cancomplete,omitempty"`
}
func (state RemoteRuntimeState) IsConnected() bool {
return state.Status == StatusConnected
}
type RemoteRuntimeState = sstore.RemoteRuntimeState
func CanComplete(remoteType string) bool {
switch remoteType {
@ -225,21 +196,6 @@ func (msh *MShellProc) GetInstallStatus() string {
return msh.InstallStatus
}
func (state RemoteRuntimeState) GetBaseDisplayName() string {
if state.RemoteAlias != "" {
return state.RemoteAlias
}
return state.RemoteCanonicalName
}
func (state RemoteRuntimeState) GetDisplayName(rptr *sstore.RemotePtrType) string {
baseDisplayName := state.GetBaseDisplayName()
if rptr == nil {
return baseDisplayName
}
return rptr.GetDisplayName(baseDisplayName)
}
func LoadRemotes(ctx context.Context) error {
GlobalStore = &Store{
Lock: &sync.Mutex{},
@ -363,6 +319,7 @@ func ArchiveRemote(ctx context.Context, remoteId string) error {
RemoteCanonicalName: rcopy.RemoteCanonicalName,
ConnectMode: sstore.ConnectModeManual,
Archived: true,
SSHConfigSrc: rcopy.SSHConfigSrc,
}
err := sstore.UpsertRemote(ctx, archivedRemote)
if err != nil {
@ -549,6 +506,7 @@ func (msh *MShellProc) GetRemoteRuntimeState() RemoteRuntimeState {
AutoInstall: msh.Remote.AutoInstall,
Archived: msh.Remote.Archived,
RemoteIdx: msh.Remote.RemoteIdx,
SSHConfigSrc: msh.Remote.SSHConfigSrc,
UName: msh.UName,
InstallStatus: msh.InstallStatus,
NeedsMShellUpgrade: msh.NeedsMShellUpgrade,
@ -647,7 +605,7 @@ func (msh *MShellProc) GetRemoteRuntimeState() RemoteRuntimeState {
func (msh *MShellProc) NotifyRemoteUpdate() {
rstate := msh.GetRemoteRuntimeState()
update := &sstore.ModelUpdate{Remotes: []interface{}{rstate}}
update := &sstore.ModelUpdate{Remotes: []RemoteRuntimeState{rstate}}
sstore.MainBus.SendUpdate(update)
}
@ -1368,20 +1326,6 @@ func replaceHomePath(pathStr string, homeDir string) string {
return pathStr
}
func (state RemoteRuntimeState) ExpandHomeDir(pathStr string) (string, error) {
if pathStr != "~" && !strings.HasPrefix(pathStr, "~/") {
return pathStr, nil
}
homeDir := state.RemoteVars["home"]
if homeDir == "" {
return "", fmt.Errorf("remote does not have HOME set, cannot do ~ expansion")
}
if pathStr == "~" {
return homeDir, nil
}
return path.Join(homeDir, pathStr[2:]), nil
}
func (msh *MShellProc) IsCmdRunning(ck base.CommandKey) bool {
msh.Lock.Lock()
defer msh.Lock.Unlock()

View File

@ -166,11 +166,7 @@ func (ws *WSState) handleConnection() error {
return fmt.Errorf("getting sessions: %w", err)
}
remotes := remote.GetAllRemoteRuntimeState()
ifarr := make([]interface{}, len(remotes))
for idx, r := range remotes {
ifarr[idx] = r
}
update.Remotes = ifarr
update.Remotes = remotes
update.Connect = true
err = ws.Shell.WriteJson(update)
if err != nil {

View File

@ -113,6 +113,25 @@ func GetAllRemotes(ctx context.Context) ([]*RemoteType, error) {
return rtn, nil
}
func GetAllImportedRemotes(ctx context.Context) (map[string]*RemoteType, error) {
rtn := make(map[string]*RemoteType)
err := WithTx(ctx, func(tx *TxWrap) error {
query := `SELECT * FROM remote
WHERE sshconfigsrc = "sshconfig-import"
ORDER BY remoteidx`
marr := tx.SelectMaps(query)
for _, m := range marr {
remote := dbutil.FromMap[*RemoteType](m)
rtn[remote.RemoteCanonicalName] = remote
}
return nil
})
if err != nil {
return nil, err
}
return rtn, nil
}
func GetRemoteByAlias(ctx context.Context, alias string) (*RemoteType, error) {
var remote *RemoteType
err := WithTx(ctx, func(tx *TxWrap) error {
@ -198,8 +217,8 @@ func UpsertRemote(ctx context.Context, r *RemoteType) error {
maxRemoteIdx := tx.GetInt(query)
r.RemoteIdx = int64(maxRemoteIdx + 1)
query = `INSERT INTO remote
( remoteid, remotetype, remotealias, remotecanonicalname, remoteuser, remotehost, connectmode, autoinstall, sshopts, remoteopts, lastconnectts, archived, remoteidx, local, statevars, openaiopts) VALUES
(:remoteid,:remotetype,:remotealias,:remotecanonicalname,:remoteuser,:remotehost,:connectmode,:autoinstall,:sshopts,:remoteopts,:lastconnectts,:archived,:remoteidx,:local,:statevars,:openaiopts)`
( remoteid, remotetype, remotealias, remotecanonicalname, remoteuser, remotehost, connectmode, autoinstall, sshopts, remoteopts, lastconnectts, archived, remoteidx, local, statevars, sshconfigsrc, openaiopts) VALUES
(:remoteid,:remotetype,:remotealias,:remotecanonicalname,:remoteuser,:remotehost,:connectmode,:autoinstall,:sshopts,:remoteopts,:lastconnectts,:archived,:remoteidx,:local,:statevars,:sshconfigsrc,:openaiopts)`
tx.NamedExec(query, r.ToMap())
return nil
})
@ -1685,6 +1704,7 @@ const (
RemoteField_ConnectMode = "connectmode" // string
RemoteField_SSHKey = "sshkey" // string
RemoteField_SSHPassword = "sshpassword" // string
RemoteField_SSHPort = "sshport" // string
RemoteField_Color = "color" // string
)
@ -1716,6 +1736,10 @@ func UpdateRemote(ctx context.Context, remoteId string, editMap map[string]inter
query = `UPDATE remote SET sshopts = json_set(sshopts, '$.sshpassword', ?) WHERE remoteid = ?`
tx.Exec(query, sshPassword, remoteId)
}
if sshPort, found := editMap[RemoteField_SSHPort]; found {
query = `UPDATE remote SET sshopts = json_set(sshopts, '$.sshport', ?) WHERE remoteid = ?`
tx.Exec(query, sshPort, remoteId)
}
if color, found := editMap[RemoteField_Color]; found {
query = `UPDATE remote SET remoteopts = json_set(remoteopts, '$.color', ?) WHERE remoteid = ?`
tx.Exec(query, color, remoteId)

View File

@ -22,7 +22,7 @@ import (
"github.com/golang-migrate/migrate/v4"
)
const MaxMigration = 27
const MaxMigration = 28
const MigratePrimaryScreenVersion = 9
const CmdScreenSpecialMigration = 13
const CmdLineSpecialMigration = 20

View File

@ -93,6 +93,11 @@ const (
RemoteAuthTypeKeyPassword = "key+password"
)
const (
SSHConfigSrcTypeManual = "waveterm-manual"
SSHConfigSrcTypeImport = "sshconfig-import"
)
const (
ShareModeLocal = "local"
ShareModeWeb = "web"
@ -975,6 +980,74 @@ type OpenAIOptsType struct {
MaxChoices int `json:"maxchoices,omitempty"`
}
const (
RemoteStatus_Connected = "connected"
RemoteStatus_Connecting = "connecting"
RemoteStatus_Disconnected = "disconnected"
RemoteStatus_Error = "error"
)
type RemoteRuntimeState struct {
RemoteType string `json:"remotetype"`
RemoteId string `json:"remoteid"`
RemoteAlias string `json:"remotealias,omitempty"`
RemoteCanonicalName string `json:"remotecanonicalname"`
RemoteVars map[string]string `json:"remotevars"`
DefaultFeState map[string]string `json:"defaultfestate"`
Status string `json:"status"`
ConnectTimeout int `json:"connecttimeout,omitempty"`
ErrorStr string `json:"errorstr,omitempty"`
InstallStatus string `json:"installstatus"`
InstallErrorStr string `json:"installerrorstr,omitempty"`
NeedsMShellUpgrade bool `json:"needsmshellupgrade,omitempty"`
NoInitPk bool `json:"noinitpk,omitempty"`
AuthType string `json:"authtype,omitempty"`
ConnectMode string `json:"connectmode"`
AutoInstall bool `json:"autoinstall"`
Archived bool `json:"archived,omitempty"`
RemoteIdx int64 `json:"remoteidx"`
SSHConfigSrc string `json:"sshconfigsrc"`
UName string `json:"uname"`
MShellVersion string `json:"mshellversion"`
WaitingForPassword bool `json:"waitingforpassword,omitempty"`
Local bool `json:"local,omitempty"`
RemoteOpts *RemoteOptsType `json:"remoteopts,omitempty"`
CanComplete bool `json:"cancomplete,omitempty"`
}
func (state RemoteRuntimeState) IsConnected() bool {
return state.Status == RemoteStatus_Connected
}
func (state RemoteRuntimeState) GetBaseDisplayName() string {
if state.RemoteAlias != "" {
return state.RemoteAlias
}
return state.RemoteCanonicalName
}
func (state RemoteRuntimeState) GetDisplayName(rptr *RemotePtrType) string {
baseDisplayName := state.GetBaseDisplayName()
if rptr == nil {
return baseDisplayName
}
return rptr.GetDisplayName(baseDisplayName)
}
func (state RemoteRuntimeState) ExpandHomeDir(pathStr string) (string, error) {
if pathStr != "~" && !strings.HasPrefix(pathStr, "~/") {
return pathStr, nil
}
homeDir := state.RemoteVars["home"]
if homeDir == "" {
return "", fmt.Errorf("remote does not have HOME set, cannot do ~ expansion")
}
if pathStr == "~" {
return homeDir, nil
}
return path.Join(homeDir, pathStr[2:]), nil
}
type RemoteType struct {
RemoteId string `json:"remoteid"`
RemoteType string `json:"remotetype"`
@ -986,13 +1059,14 @@ type RemoteType struct {
Archived bool `json:"archived"`
// SSH fields
Local bool `json:"local"`
RemoteUser string `json:"remoteuser"`
RemoteHost string `json:"remotehost"`
ConnectMode string `json:"connectmode"`
AutoInstall bool `json:"autoinstall"`
SSHOpts *SSHOpts `json:"sshopts"`
StateVars map[string]string `json:"statevars"`
Local bool `json:"local"`
RemoteUser string `json:"remoteuser"`
RemoteHost string `json:"remotehost"`
ConnectMode string `json:"connectmode"`
AutoInstall bool `json:"autoinstall"`
SSHOpts *SSHOpts `json:"sshopts"`
StateVars map[string]string `json:"statevars"`
SSHConfigSrc string `json:"sshconfigsrc"`
// OpenAI fields
OpenAIOpts *OpenAIOptsType `json:"openaiopts,omitempty"`
@ -1048,6 +1122,7 @@ func (r *RemoteType) ToMap() map[string]interface{} {
rtn["remoteidx"] = r.RemoteIdx
rtn["local"] = r.Local
rtn["statevars"] = quickJson(r.StateVars)
rtn["sshconfigsrc"] = r.SSHConfigSrc
rtn["openaiopts"] = quickJson(r.OpenAIOpts)
return rtn
}
@ -1068,6 +1143,7 @@ func (r *RemoteType) FromMap(m map[string]interface{}) bool {
quickSetInt64(&r.RemoteIdx, m, "remoteidx")
quickSetBool(&r.Local, m, "local")
quickSetJson(&r.StateVars, m, "statevars")
quickSetStr(&r.SSHConfigSrc, m, "sshconfigsrc")
quickSetJson(&r.OpenAIOpts, m, "openaiopts")
return true
}
@ -1230,6 +1306,7 @@ func EnsureLocalRemote(ctx context.Context) error {
AutoInstall: true,
SSHOpts: &SSHOpts{Local: true},
Local: true,
SSHConfigSrc: SSHConfigSrcTypeManual,
}
err = UpsertRemote(ctx, localRemote)
if err != nil {
@ -1248,6 +1325,7 @@ func EnsureLocalRemote(ctx context.Context) error {
SSHOpts: &SSHOpts{Local: true, IsSudo: true},
RemoteOpts: &RemoteOptsType{Color: "red"},
Local: true,
SSHConfigSrc: SSHConfigSrcTypeManual,
}
err = UpsertRemote(ctx, sudoRemote)
if err != nil {

View File

@ -48,7 +48,7 @@ type ModelUpdate struct {
CmdLine *utilfn.StrWithPos `json:"cmdline,omitempty"`
Info *InfoMsgType `json:"info,omitempty"`
ClearInfo bool `json:"clearinfo,omitempty"`
Remotes []interface{} `json:"remotes,omitempty"` // []*remote.RemoteState
Remotes []RemoteRuntimeState `json:"remotes,omitempty"`
History *HistoryInfoType `json:"history,omitempty"`
Interactive bool `json:"interactive"`
Connect bool `json:"connect,omitempty"`