diff --git a/pkg/kt/command/connect/common.go b/pkg/kt/command/connect/common.go index c9503bed..62e85548 100644 --- a/pkg/kt/command/connect/common.go +++ b/pkg/kt/command/connect/common.go @@ -6,6 +6,7 @@ import ( opt "github.com/alibaba/kt-connect/pkg/kt/options" "github.com/alibaba/kt-connect/pkg/kt/service/cluster" "github.com/alibaba/kt-connect/pkg/kt/service/dns" + "github.com/alibaba/kt-connect/pkg/kt/transmission" "github.com/alibaba/kt-connect/pkg/kt/util" "github.com/rs/zerolog/log" coreV1 "k8s.io/api/core/v1" @@ -13,9 +14,10 @@ import ( "time" ) -func setupDns(shadowPodIp string) error { +func setupDns(shadowPodName, shadowPodIp string) error { namespace := opt.Get().Namespace if strings.HasPrefix(opt.Get().ConnectOptions.DnsMode, util.DnsModeHosts) { + log.Info().Msgf("Setting up dns in hosts mode") dump2HostsNamespaces := "" pos := len(util.DnsModeHosts) if len(opt.Get().ConnectOptions.DnsMode) > pos + 1 && opt.Get().ConnectOptions.DnsMode[pos:pos+1] == ":" { @@ -25,8 +27,10 @@ func setupDns(shadowPodIp string) error { return err } } else if opt.Get().ConnectOptions.DnsMode == util.DnsModePodDns { + log.Info().Msgf("Setting up dns in pod mode") return dns.Ins().SetNameServer(shadowPodIp) } else if opt.Get().ConnectOptions.DnsMode == util.DnsModeLocalDns { + log.Info().Msgf("Setting up dns in local mode") headlessPods, err := dumpCurrentNamespaceToHost(namespace) if err != nil { return err @@ -49,13 +53,20 @@ func setupDns(shadowPodIp string) error { headlessPods, _ = dumpCurrentNamespaceToHost(namespace) } }, nil) + + forwardedPodPort, err := util.GetRandomTcpPort() + if err != nil { + return err + } + transmission.SetupPortForwardToLocal(shadowPodName, common.StandardDnsPort, forwardedPodPort) + dnsPort := util.AlternativeDnsPort if util.IsWindows() { dnsPort = common.StandardDnsPort } // must setup name server before change dns config // otherwise the upstream name server address will be incorrect in linux - if err = dns.SetupLocalDns(shadowPodIp, dnsPort); err != nil { + if err = dns.SetupLocalDns(forwardedPodPort, dnsPort); err != nil { log.Error().Err(err).Msgf("Failed to setup local dns server") return err } diff --git a/pkg/kt/command/connect/sshuttle.go b/pkg/kt/command/connect/sshuttle.go index 937263bb..8f03aa68 100644 --- a/pkg/kt/command/connect/sshuttle.go +++ b/pkg/kt/command/connect/sshuttle.go @@ -2,6 +2,7 @@ package connect import ( "context" + "github.com/alibaba/kt-connect/pkg/common" opt "github.com/alibaba/kt-connect/pkg/kt/options" "github.com/alibaba/kt-connect/pkg/kt/service/cluster" "github.com/alibaba/kt-connect/pkg/kt/service/sshuttle" @@ -27,7 +28,7 @@ func BySshuttle() error { if err != nil { return err } - stop, err := transmission.ForwardSSHTunnelToLocal(podName, localSshPort) + stop, err := transmission.SetupPortForwardToLocal(podName, common.StandardSshPort, localSshPort) if err != nil { return err } @@ -41,7 +42,7 @@ func BySshuttle() error { return err } - return setupDns(podIP) + return setupDns(podName, podIP) } func checkSshuttleInstalled() { diff --git a/pkg/kt/command/connect/tun2socks.go b/pkg/kt/command/connect/tun2socks.go index a09df8bf..4b4beb20 100644 --- a/pkg/kt/command/connect/tun2socks.go +++ b/pkg/kt/command/connect/tun2socks.go @@ -2,6 +2,7 @@ package connect import ( "fmt" + "github.com/alibaba/kt-connect/pkg/common" opt "github.com/alibaba/kt-connect/pkg/kt/options" "github.com/alibaba/kt-connect/pkg/kt/service/cluster" "github.com/alibaba/kt-connect/pkg/kt/service/sshchannel" @@ -24,7 +25,7 @@ func ByTun2Socks() error { if err != nil { return err } - if _, err = transmission.ForwardSSHTunnelToLocal(podName, localSshPort); err != nil { + if _, err = transmission.SetupPortForwardToLocal(podName, common.StandardSshPort, localSshPort); err != nil { return err } if err = startSocks5Connection(privateKeyPath, localSshPort); err != nil { @@ -34,7 +35,7 @@ func ByTun2Socks() error { if opt.Get().ConnectOptions.DisableTunDevice { showSetupSocksMessage(opt.Get().ConnectOptions.SocksPort) if strings.HasPrefix(opt.Get().ConnectOptions.DnsMode, util.DnsModeHosts) { - return setupDns(podIP) + return setupDns(podName, podIP) } else { return nil } @@ -54,7 +55,7 @@ func ByTun2Socks() error { } log.Info().Msgf("Route to tun device completed") } - return setupDns(podIP) + return setupDns(podName, podIP) } } diff --git a/pkg/kt/service/dns/dnsserver.go b/pkg/kt/service/dns/dnsserver.go index ae0270fc..8ddb2a7c 100644 --- a/pkg/kt/service/dns/dnsserver.go +++ b/pkg/kt/service/dns/dnsserver.go @@ -13,7 +13,7 @@ type DnsServer struct { upstreamDnsAddr string } -func SetupLocalDns(shadowPodIp string, dnsPort int) error { +func SetupLocalDns(remoteDnsPort, localDnsPort int) error { var success = make(chan error) go func() { time.Sleep(1 * time.Second) @@ -21,11 +21,12 @@ func SetupLocalDns(shadowPodIp string, dnsPort int) error { }() go func() { nameserver := GetNameServer() - log.Debug().Msgf("Setup local DNS with shadow pod %s and upstream %s", shadowPodIp, nameserver) + log.Debug().Msgf("Setup local DNS with shadow pod %s:%d and upstream %s:%d", + common.Localhost, remoteDnsPort, nameserver, common.StandardDnsPort) success <-common.SetupDnsServer(&DnsServer{ - clusterDnsAddr: fmt.Sprintf("%s:%d", shadowPodIp, common.StandardDnsPort), + clusterDnsAddr: fmt.Sprintf("%s:%d", common.Localhost, remoteDnsPort), upstreamDnsAddr: fmt.Sprintf("%s:%d", nameserver, common.StandardDnsPort), - }, dnsPort, "udp") + }, localDnsPort, "udp") }() return <-success } diff --git a/pkg/kt/transmission/inbound.go b/pkg/kt/transmission/inbound.go index f9449fb6..ab290238 100644 --- a/pkg/kt/transmission/inbound.go +++ b/pkg/kt/transmission/inbound.go @@ -2,6 +2,7 @@ package transmission import ( "fmt" + "github.com/alibaba/kt-connect/pkg/common" opt "github.com/alibaba/kt-connect/pkg/kt/options" "github.com/alibaba/kt-connect/pkg/kt/service/sshchannel" "github.com/alibaba/kt-connect/pkg/kt/util" @@ -19,7 +20,7 @@ func ForwardPodToLocal(exposePorts, podName, privateKey string) (int, error) { } // port forward pod 22 -> local - if _, err = ForwardSSHTunnelToLocal(podName, localSSHPort); err != nil { + if _, err = SetupPortForwardToLocal(podName, common.StandardSshPort, localSSHPort); err != nil { return -1, err } diff --git a/pkg/kt/transmission/outbound.go b/pkg/kt/transmission/portforward.go similarity index 87% rename from pkg/kt/transmission/outbound.go rename to pkg/kt/transmission/portforward.go index 945370bd..55da9d12 100644 --- a/pkg/kt/transmission/outbound.go +++ b/pkg/kt/transmission/portforward.go @@ -3,7 +3,6 @@ package transmission import ( "context" "fmt" - "github.com/alibaba/kt-connect/pkg/common" opt "github.com/alibaba/kt-connect/pkg/kt/options" "github.com/alibaba/kt-connect/pkg/kt/process" "github.com/alibaba/kt-connect/pkg/kt/service/cluster" @@ -19,8 +18,8 @@ import ( "strings" ) -// ForwardSSHTunnelToLocal mapping local port to shadow pod ssh port -func ForwardSSHTunnelToLocal(podName string, localPort int) (chan struct{}, error) { +// SetupPortForwardToLocal mapping local port to shadow pod ssh port +func SetupPortForwardToLocal(podName string, remotePort, localPort int) (chan struct{}, error) { stop := make(chan struct{}) _, cancel := context.WithCancel(context.Background()) // one of the background process start failed and will cancel the started process @@ -28,8 +27,8 @@ func ForwardSSHTunnelToLocal(podName string, localPort int) (chan struct{}, erro process.Stop(<-stop, cancel) }() go func() { - if err := portForward(podName, common.StandardSshPort, localPort, stop); err != nil { - log.Error().Err(err).Msgf("Port forward to %d -> %d pod %s interrupted", localPort, common.StandardSshPort, podName) + if err := portForward(podName, remotePort, localPort, stop); err != nil { + log.Error().Err(err).Msgf("Port forward to %d -> %d pod %s interrupted", localPort, remotePort, podName) stop <- struct{}{} } }() diff --git a/pkg/kt/transmission/outbound_test.go b/pkg/kt/transmission/portforward_test.go similarity index 100% rename from pkg/kt/transmission/outbound_test.go rename to pkg/kt/transmission/portforward_test.go