diff --git a/cmd/kubevpn/cmds/connect.go b/cmd/kubevpn/cmds/connect.go index d1b39fef..abeefa92 100644 --- a/cmd/kubevpn/cmds/connect.go +++ b/cmd/kubevpn/cmds/connect.go @@ -80,7 +80,7 @@ func CmdConnect(f cmdutil.Factory) *cobra.Command { if err != nil { return err } - if !sshConf.IsEmpty() { + if !sshConf.IsEmpty() && !sshConf.IsLoopback() { if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil { extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String()) } diff --git a/cmd/kubevpn/cmds/proxy.go b/cmd/kubevpn/cmds/proxy.go index fa977f05..61a0b937 100644 --- a/cmd/kubevpn/cmds/proxy.go +++ b/cmd/kubevpn/cmds/proxy.go @@ -106,7 +106,7 @@ func CmdProxy(f cmdutil.Factory) *cobra.Command { if err != nil { return err } - if !sshConf.IsEmpty() { + if !sshConf.IsEmpty() && !sshConf.IsLoopback() { if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil { extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String()) } diff --git a/cmd/kubevpn/cmds/sync.go b/cmd/kubevpn/cmds/sync.go index fee1bc7f..e1461d45 100644 --- a/cmd/kubevpn/cmds/sync.go +++ b/cmd/kubevpn/cmds/sync.go @@ -94,7 +94,7 @@ func CmdSync(f cmdutil.Factory) *cobra.Command { if err != nil { return err } - if !sshConf.IsEmpty() { + if !sshConf.IsEmpty() && !sshConf.IsLoopback() { if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil { extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String()) } diff --git a/pkg/run/options.go b/pkg/run/options.go index 80b7311d..c1d028c3 100644 --- a/pkg/run/options.go +++ b/pkg/run/options.go @@ -99,7 +99,7 @@ func (option *Options) Connect(ctx context.Context, sshConfig *pkgssh.SshConfig, if err != nil { return err } - if !sshConfig.IsEmpty() { + if !sshConfig.IsEmpty() && !sshConfig.IsLoopback() { if ip := util.GetAPIServerFromKubeConfigBytes(kubeConfigBytes); ip != nil { option.ExtraRouteInfo.ExtraCIDR = append(option.ExtraRouteInfo.ExtraCIDR, ip.String()) } diff --git a/pkg/ssh/config.go b/pkg/ssh/config.go index 99adfe80..6c25adc2 100644 --- a/pkg/ssh/config.go +++ b/pkg/ssh/config.go @@ -113,6 +113,28 @@ func (conf SshConfig) IsEmpty() bool { return conf.ConfigAlias == "" && conf.Addr == "" && conf.Jump == "" } +// IsLoopback TODO support alias and proxyJump +func (conf SshConfig) IsLoopback() bool { + if conf.Addr != "" { + var host string + var err error + if host, _, err = net.SplitHostPort(conf.Addr); err != nil { + host = conf.Addr + } + ip, err := net.LookupIP(host) + if err != nil { + return false + } + for _, i := range ip { + if i.IsLoopback() { + return true + } + } + return false + } + return false +} + func (conf SshConfig) GetAuth() ([]ssh.AuthMethod, error) { host, _, _ := net.SplitHostPort(conf.Addr) var auth []ssh.AuthMethod