2022-06-23 17:04:53 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2023-03-02 13:28:14 +01:00
|
|
|
|
2023-06-23 16:27:10 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-03-02 13:28:14 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/internal"
|
|
|
|
nbssh "github.com/netbirdio/netbird/client/ssh"
|
|
|
|
"github.com/netbirdio/netbird/util"
|
2022-06-23 17:04:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
port int
|
2022-07-07 11:24:38 +02:00
|
|
|
user = "root"
|
2022-06-23 17:04:53 +02:00
|
|
|
host string
|
|
|
|
)
|
|
|
|
|
|
|
|
var sshCmd = &cobra.Command{
|
2024-04-23 14:42:53 +02:00
|
|
|
Use: "ssh [user@]host",
|
2022-06-23 17:04:53 +02:00
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) < 1 {
|
|
|
|
return errors.New("requires a host argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
split := strings.Split(args[0], "@")
|
|
|
|
if len(split) == 2 {
|
|
|
|
user = split[0]
|
|
|
|
host = split[1]
|
|
|
|
} else {
|
|
|
|
host = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Short: "connect to a remote SSH server",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-01-17 19:16:50 +01:00
|
|
|
SetFlagsFromEnvVars(rootCmd)
|
|
|
|
SetFlagsFromEnvVars(cmd)
|
2022-06-23 17:04:53 +02:00
|
|
|
|
|
|
|
cmd.SetOut(cmd.OutOrStdout())
|
|
|
|
|
|
|
|
err := util.InitLog(logLevel, "console")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed initializing log %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !util.IsAdmin() {
|
|
|
|
cmd.Printf("error: you must have Administrator privileges to run this command\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := internal.CtxInitState(cmd.Context())
|
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
config, err := internal.UpdateConfig(internal.ConfigInput{
|
2023-01-08 12:57:28 +01:00
|
|
|
ConfigPath: configPath,
|
|
|
|
})
|
2022-06-23 17:04:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
sshctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
go func() {
|
2022-06-29 14:03:30 +02:00
|
|
|
// blocking
|
|
|
|
if err := runSSH(sshctx, host, []byte(config.SSHKey), cmd); err != nil {
|
2023-06-23 16:27:10 +02:00
|
|
|
log.Debug(err)
|
2023-06-23 12:20:14 +02:00
|
|
|
os.Exit(1)
|
2022-06-23 17:04:53 +02:00
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sig:
|
|
|
|
cancel()
|
|
|
|
case <-sshctx.Done():
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-29 14:03:30 +02:00
|
|
|
func runSSH(ctx context.Context, addr string, pemKey []byte, cmd *cobra.Command) error {
|
2022-06-23 17:04:53 +02:00
|
|
|
c, err := nbssh.DialWithKey(fmt.Sprintf("%s:%d", addr, port), user, pemKey)
|
|
|
|
if err != nil {
|
2022-06-29 14:03:30 +02:00
|
|
|
cmd.Printf("Error: %v\n", err)
|
2023-06-23 12:20:14 +02:00
|
|
|
cmd.Printf("Couldn't connect. Please check the connection status or if the ssh server is enabled on the other peer" +
|
2024-04-23 14:42:53 +02:00
|
|
|
"\nYou can verify the connection by running:\n\n" +
|
2023-06-23 12:20:14 +02:00
|
|
|
" netbird status\n\n")
|
2023-06-23 16:27:10 +02:00
|
|
|
return err
|
2022-06-23 17:04:53 +02:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
err = c.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = c.OpenTerminal()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-31 16:06:42 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
sshCmd.PersistentFlags().IntVarP(&port, "port", "p", nbssh.DefaultSSHPort, "Sets remote SSH port. Defaults to "+fmt.Sprint(nbssh.DefaultSSHPort))
|
|
|
|
}
|