From 3ad0b5d1a3ea95553a8032bc55cc610f4ded8289 Mon Sep 17 00:00:00 2001 From: naison <895703375@qq.com> Date: Sat, 8 Feb 2025 12:04:25 +0000 Subject: [PATCH] hotfix: add platform for cmd ssh --- cmd/kubevpn/cmds/ssh.go | 8 ++++++++ pkg/daemon/handler/ssh.go | 9 +++++++-- pkg/ssh/ssh.go | 16 ++++++++++++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/cmd/kubevpn/cmds/ssh.go b/cmd/kubevpn/cmds/ssh.go index c7a4e3cc..42cf71ff 100644 --- a/cmd/kubevpn/cmds/ssh.go +++ b/cmd/kubevpn/cmds/ssh.go @@ -7,6 +7,7 @@ import ( "io" "os" + "github.com/containerd/containerd/platforms" "github.com/google/uuid" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -28,6 +29,7 @@ import ( func CmdSSH(_ cmdutil.Factory) *cobra.Command { var sshConf = &pkgssh.SshConfig{} var extraCIDR []string + var platform string cmd := &cobra.Command{ Use: "ssh", Short: "Ssh to jump server", @@ -54,6 +56,10 @@ func CmdSSH(_ cmdutil.Factory) *cobra.Command { return daemon.StartupDaemon(cmd.Context()) }, RunE: func(cmd *cobra.Command, args []string) error { + parse, err2 := platforms.Parse(platform) + if err2 != nil { + return err2 + } config, err := websocket.NewConfig("ws://test/ws", "http://test") if err != nil { return err @@ -77,6 +83,7 @@ func CmdSSH(_ cmdutil.Factory) *cobra.Command { ExtraCIDR: extraCIDR, Width: width, Height: height, + Platform: platforms.Format(platforms.Normalize(parse)), SessionID: sessionID, } bytes, err := json.Marshal(ssh) @@ -117,6 +124,7 @@ func CmdSSH(_ cmdutil.Factory) *cobra.Command { } pkgssh.AddSshFlags(cmd.Flags(), sshConf) cmd.Flags().StringArrayVar(&extraCIDR, "extra-cidr", []string{}, "Extra network CIDR string, eg: --extra-cidr 192.168.0.159/24 --extra-cidr 192.168.1.160/32") + cmd.Flags().StringVar(&platform, "platform", util.If(os.Getenv("KUBEVPN_DEFAULT_PLATFORM") != "", os.Getenv("KUBEVPN_DEFAULT_PLATFORM"), "linux/amd64"), "Set ssh server platform if needs to install command kubevpn") return cmd } diff --git a/pkg/daemon/handler/ssh.go b/pkg/daemon/handler/ssh.go index 7e98055d..810c0ea3 100644 --- a/pkg/daemon/handler/ssh.go +++ b/pkg/daemon/handler/ssh.go @@ -17,6 +17,8 @@ import ( "sync" "time" + "github.com/containerd/containerd/platforms" + specs "github.com/opencontainers/image-spec/specs-go/v1" log "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" "golang.org/x/net/websocket" @@ -38,6 +40,7 @@ type wsHandler struct { width int height int sessionId string + platform specs.Platform condReady context.CancelFunc } @@ -240,7 +243,7 @@ func (w *wsHandler) terminal(ctx context.Context, cli *ssh.Client, conn io.ReadW ssh.TTY_OP_ISPEED: 14400, ssh.TTY_OP_OSPEED: 14400, } - if err = session.RequestPty("xterm", height, width, modes); err != nil { + if err = session.RequestPty("xterm-256color", height, width, modes); err != nil { w.Log("Request pty error: %v", err) return err } @@ -270,7 +273,7 @@ func (w *wsHandler) installKubevpnOnRemote(ctx context.Context, sshClient *ssh.C if config.GitHubOAuthToken != "" { client = oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: config.GitHubOAuthToken, TokenType: "Bearer"})) } - latestVersion, url, err := util.GetManifest(client, "linux", "amd64") + latestVersion, url, err := util.GetManifest(client, w.platform.OS, w.platform.Architecture) if err != nil { w.Log("Get latest kubevpn version failed: %v", err) return err @@ -346,6 +349,7 @@ type Ssh struct { ExtraCIDR []string Width int Height int + Platform string SessionID string } @@ -370,6 +374,7 @@ func init() { width: conf.Width, height: conf.Height, sessionId: conf.SessionID, + platform: platforms.MustParse(conf.Platform), condReady: cancelFunc, } CondReady[conf.SessionID] = ctx diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index 6ae2883e..7de1f1fb 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -430,8 +430,8 @@ func (config SshConfig) Dial(ctx context.Context, stopChan <-chan struct{}) (cli User: config.User, Auth: authMethod, HostKeyCallback: ssh.InsecureIgnoreHostKey(), - BannerCallback: ssh.BannerDisplayStderr(), - Timeout: time.Second * 10, + //BannerCallback: ssh.BannerDisplayStderr(), + Timeout: time.Second * 10, }) if err != nil { return nil, err @@ -484,8 +484,8 @@ func JumpTo(ctx context.Context, bClient *ssh.Client, to SshConfig, stopChan <-c User: to.User, Auth: authMethod, HostKeyCallback: ssh.InsecureIgnoreHostKey(), - BannerCallback: ssh.BannerDisplayStderr(), - Timeout: time.Second * 10, + //BannerCallback: ssh.BannerDisplayStderr(), + Timeout: time.Second * 10, }) if err != nil { return @@ -596,6 +596,14 @@ func PortMapUntil(ctx context.Context, conf *SshConfig, remote, local netip.Addr defer cancelFunc3() conn, err = client.DialContext(ctx3, "tcp", remote.String()) if err != nil { + var openChannelError *ssh.OpenChannelError + // if ssh server not permitted ssh port-forward, do nothing until exit + if errors.As(err, &openChannelError) && openChannelError.Reason == ssh.Prohibited { + _ = client.Close() + log.Debugf("Failed to open ssh port-forward: %s: %v", remote.String(), err) + <-connCtx.Done() + return nil, err + } log.Debugf("Failed to dial remote addr: %s: %v", remote.String(), err) client.Close() return nil, err