This commit is contained in:
adman
2026-03-20 17:08:54 +01:00
parent b107eba37c
commit 43fea40d08
2 changed files with 18 additions and 29 deletions
+6 -23
View File
@@ -9,34 +9,18 @@ import (
"time" "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 type DNSCheckFunc func(addr string) error
// DoDns resolves a DNS name to an IP address // DoDns resolves a DNS name to an IP address
// It should resolve a minecraft compatible address into something net.Dial // 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) return DoDNSChecked(in, nil)
} }
// DoDNSChecked resolves a DNS name to an IP address // DoDNSChecked resolves a DNS name to an IP address
// It should resolve a minecraft compatible address into something net.Dial // It should resolve a minecraft compatible address into something net.Dial
// It also optionally accepts a function to "check" the address // 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 // https://mcsrc.dev/1/26.1-pre-1/net/minecraft/client/multiplayer/resolver/ServerAddress#L34
host, port, err := net.SplitHostPort(in) host, port, err := net.SplitHostPort(in)
if err != nil { if err != nil {
@@ -61,7 +45,7 @@ func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad co
if err != nil { if err != nil {
return return
} }
portN := uint16(porti) portN = uint16(porti)
outIP, err := net.ResolveIPAddr("ip", host) outIP, err := net.ResolveIPAddr("ip", host)
if err != nil { if err != nil {
return return
@@ -70,12 +54,11 @@ func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad co
if err != nil { if err != nil {
return return
} }
out = outIP.String() + ":" + port connectTo = outIP.String() + ":" + port
if portN != 25565 { if portN != 25565 {
return 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() defer cancel()
r := &net.Resolver{} r := &net.Resolver{}
@@ -95,6 +78,6 @@ func DoDNSChecked(in string, f DNSCheckFunc) (out string, err error) { // Bad co
if err != nil { if err != nil {
return return
} }
out = outIP.String() + ":" + port connectTo = outIP.String() + ":" + port
return return
} }
+11 -5
View File
@@ -5,17 +5,23 @@ import "testing"
func TestDoDNS(t *testing.T) { func TestDoDNS(t *testing.T) {
type Case struct { type Case struct {
Input string Input string
Output 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) t.Logf("Testing %s", c.Input)
out, err := DoDns(c.Input) _, host, portN, err := DoDns(c.Input)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
continue continue
} }
if out != c.Output { if host != c.Host || portN != c.portN {
t.Errorf("Expected %s, got %s", c.Output, out) t.Errorf("expected %s:%d, got %s:%d", c.Host, c.portN, host, portN)
} }
} }
} }