From 16810cd2115031274d0e009febbd8fdd830a8e1c Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 4 May 2020 15:42:01 -0700 Subject: [PATCH] etcdmain: best effort detection of self pointing in tcp proxy --- etcdmain/gateway.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/etcdmain/gateway.go b/etcdmain/gateway.go index 5cd6285d356..2654e30d267 100644 --- a/etcdmain/gateway.go +++ b/etcdmain/gateway.go @@ -119,6 +119,40 @@ func startGateway(cmd *cobra.Command, args []string) { } } + lhost, lport, err := net.SplitHostPort(gatewayListenAddr) + if err != nil { + fmt.Println("failed to validate listen address:", gatewayListenAddr) + os.Exit(1) + } + + laddrs, err := net.LookupHost(lhost) + if err != nil { + fmt.Println("failed to resolve listen host:", lhost) + os.Exit(1) + } + laddrsMap := make(map[string]bool) + for _, addr := range laddrs { + laddrsMap[addr] = true + } + + for _, srv := range srvs.SRVs { + eaddrs, err := net.LookupHost(srv.Target) + if err != nil { + fmt.Println("failed to resolve endpoint host:", srv.Target) + os.Exit(1) + } + if fmt.Sprintf("%d", srv.Port) != lport { + continue + } + + for _, ea := range eaddrs { + if laddrsMap[ea] { + fmt.Printf("SRV or endpoint (%s:%d->%s:%d) should not resolve to the gateway listen addr (%s)\n", srv.Target, srv.Port, ea, srv.Port, gatewayListenAddr) + os.Exit(1) + } + } + } + if len(srvs.Endpoints) == 0 { fmt.Println("no endpoints found") os.Exit(1)