fix: ignore loopback ip

This commit is contained in:
fengcaiwen
2025-12-22 11:02:04 +08:00
parent 5bdbdc9f56
commit 0d3882ed3c
5 changed files with 26 additions and 4 deletions

View File

@@ -80,7 +80,7 @@ func CmdConnect(f cmdutil.Factory) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
if !sshConf.IsEmpty() { if !sshConf.IsEmpty() && !sshConf.IsLoopback() {
if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil { if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil {
extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String()) extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String())
} }

View File

@@ -106,7 +106,7 @@ func CmdProxy(f cmdutil.Factory) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
if !sshConf.IsEmpty() { if !sshConf.IsEmpty() && !sshConf.IsLoopback() {
if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil { if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil {
extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String()) extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String())
} }

View File

@@ -94,7 +94,7 @@ func CmdSync(f cmdutil.Factory) *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
if !sshConf.IsEmpty() { if !sshConf.IsEmpty() && !sshConf.IsLoopback() {
if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil { if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil {
extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String()) extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String())
} }

View File

@@ -99,7 +99,7 @@ func (option *Options) Connect(ctx context.Context, sshConfig *pkgssh.SshConfig,
if err != nil { if err != nil {
return err return err
} }
if !sshConfig.IsEmpty() { if !sshConfig.IsEmpty() && !sshConfig.IsLoopback() {
if ip := util.GetAPIServerFromKubeConfigBytes(kubeConfigBytes); ip != nil { if ip := util.GetAPIServerFromKubeConfigBytes(kubeConfigBytes); ip != nil {
option.ExtraRouteInfo.ExtraCIDR = append(option.ExtraRouteInfo.ExtraCIDR, ip.String()) option.ExtraRouteInfo.ExtraCIDR = append(option.ExtraRouteInfo.ExtraCIDR, ip.String())
} }

View File

@@ -113,6 +113,28 @@ func (conf SshConfig) IsEmpty() bool {
return conf.ConfigAlias == "" && conf.Addr == "" && conf.Jump == "" 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) { func (conf SshConfig) GetAuth() ([]ssh.AuthMethod, error) {
host, _, _ := net.SplitHostPort(conf.Addr) host, _, _ := net.SplitHostPort(conf.Addr)
var auth []ssh.AuthMethod var auth []ssh.AuthMethod