From 43fea40d08c04dd40a4f53428274cd73c64510cb Mon Sep 17 00:00:00 2001 From: adman Date: Fri, 20 Mar 2026 17:08:54 +0100 Subject: [PATCH] dns fix --- client/dns.go | 29 ++++++----------------------- client/dns_test.go | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/client/dns.go b/client/dns.go index 61492db..dbba70f 100644 --- a/client/dns.go +++ b/client/dns.go @@ -9,34 +9,18 @@ import ( "time" ) -func LookupMinecraftSRV(host string, port uint16) (hostRet string, portRet uint16) { - hostRet = host // in case of error - portRet = port - if port != 25565 { - return - } - _, srvs, err := net.LookupSRV("minecraft", "tcp", host) - if err != nil || len(srvs) == 0 { - return - } - target := strings.TrimSuffix(srvs[0].Target, ".") - hostRet = target - portRet = srvs[0].Port - return -} - type DNSCheckFunc func(addr string) error // DoDns resolves a DNS name to an IP address // It should resolve a minecraft compatible address into something net.Dial -func DoDns(in string) (out string, err error) { +func DoDns(in string) (out string, host string, portN uint16, err error) { return DoDNSChecked(in, nil) } // DoDNSChecked resolves a DNS name to an IP address // It should resolve a minecraft compatible address into something net.Dial // It also optionally accepts a function to "check" the address -func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad code should match minecraft +func DoDNSChecked(in string, f DNSCheckFunc) (connectTo string, host string, portN uint16, err error) { // https://mcsrc.dev/1/26.1-pre-1/net/minecraft/client/multiplayer/resolver/ServerAddress#L34 host, port, err := net.SplitHostPort(in) if err != nil { @@ -61,7 +45,7 @@ func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad co if err != nil { return } - portN := uint16(porti) + portN = uint16(porti) outIP, err := net.ResolveIPAddr("ip", host) if err != nil { return @@ -70,12 +54,11 @@ func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad co if err != nil { return } - out = outIP.String() + ":" + port + connectTo = outIP.String() + ":" + port if portN != 25565 { - return } - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) // otherwise this takes 10+ seconds + ctx, cancel := context.WithTimeout(context.Background(), time.Second) // otherwise this takes 10+ seconds defer cancel() r := &net.Resolver{} @@ -95,6 +78,6 @@ func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad co if err != nil { return } - out = outIP.String() + ":" + port + connectTo = outIP.String() + ":" + port return } diff --git a/client/dns_test.go b/client/dns_test.go index ad57cf8..bb5e87e 100644 --- a/client/dns_test.go +++ b/client/dns_test.go @@ -4,18 +4,24 @@ import "testing" func TestDoDNS(t *testing.T) { type Case struct { - Input string - Output string + Input string + Host string + portN uint16 } - for _, c := range []Case{{"localhost", "127.0.0.1:25565"}, {"1f2d.net", "50.114.4.126:25565"}} { + cases := []Case{ + {"localhost", "localhost", 25565}, + {"play.hypixel.net", "play.hypixel.net", 25565}, + {"1f2d.net", "tcpshield.1f2d.net", 25565}, + } + for _, c := range cases { t.Logf("Testing %s", c.Input) - out, err := DoDns(c.Input) + _, host, portN, err := DoDns(c.Input) if err != nil { t.Error(err) continue } - if out != c.Output { - t.Errorf("Expected %s, got %s", c.Output, out) + if host != c.Host || portN != c.portN { + t.Errorf("expected %s:%d, got %s:%d", c.Host, c.portN, host, portN) } } }