Created
May 5, 2020 08:05
-
-
Save keymon/173a2e0d470a52e7b7fe1843a9929797 to your computer and use it in GitHub Desktop.
DNS check of LB services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"net" | |
"sort" | |
"strings" | |
"time" | |
) | |
func findNS(name string) ([]string, error) { | |
nameSplit := strings.Split(name, ".") | |
if len(nameSplit) < 2 { | |
return nil, fmt.Errorf("NS not found") | |
} | |
ns, err := net.LookupNS(name) | |
if err != nil { | |
return findNS(strings.Join(nameSplit[1:], ".")) | |
} else { | |
hosts := []string{} | |
for _, host := range ns { | |
hosts = append(hosts, host.Host) | |
} | |
return hosts, nil | |
} | |
} | |
func buildResolver(ns string) *net.Resolver { | |
return &net.Resolver{ | |
PreferGo: true, | |
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | |
d := net.Dialer{ | |
Timeout: time.Millisecond * time.Duration(10000), | |
} | |
return d.DialContext(ctx, "udp", ns+":53") | |
}, | |
} | |
} | |
func main() { | |
broker := "pkc-4xwkx.eu-west-1.aws.confluent.cloud" | |
elb := "ae6fe4609709447c2a66f438fc093b72-612660764.eu-west-1.elb.amazonaws.com" | |
r, err := net.LookupTXT(broker) | |
fmt.Println("txt: ", r, err) | |
ns, err := findNS(broker) | |
fmt.Println("ns: ", ns, err) | |
if err != nil { | |
return | |
} | |
resolver := buildResolver(ns[0]) | |
brokerHosts, err := resolver.LookupHost(context.Background(), broker) | |
sort.Strings(brokerHosts) | |
fmt.Println(brokerHosts, err) | |
elbHosts, err := resolver.LookupHost(context.Background(), elb) | |
sort.Strings(elbHosts) | |
fmt.Println(elbHosts, err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment