From 29baf8da9fc15a51cdce70f9f6d5aac646de76bd Mon Sep 17 00:00:00 2001 From: adman Date: Fri, 20 Mar 2026 17:57:03 +0100 Subject: [PATCH] test fixup --- client/dns.go | 43 ++++++++++++++++++++++++++++++ client/status.go | 2 ++ event/event_bus.go | 2 +- text/text_test.go | 4 +-- tools/cli/status/main.go | 2 +- tools/registry_data_hunter/main.go | 8 ++++++ 6 files changed, 57 insertions(+), 4 deletions(-) diff --git a/client/dns.go b/client/dns.go index dbba70f..b8e8b8d 100644 --- a/client/dns.go +++ b/client/dns.go @@ -81,3 +81,46 @@ func DoDNSChecked(in string, f DNSCheckFunc) (connectTo string, host string, por connectTo = outIP.String() + ":" + port return } + +// DoDnsSimple resolves a DNS name to an IP address +// It should resolve a minecraft compatible address into something net.Dial +// It does not check the address +func DoDnsSimple(in string) (out string, err error) { + host, port, err := net.SplitHostPort(in) + if err != nil { + var addrErr *net.AddrError + if errors.As(err, &addrErr) && addrErr.Err == "missing port in address" { + err = nil + host = in + port = "25565" + } else { + return + } + } + porti, err := strconv.Atoi(port) + if err != nil { + return + } + portN := uint16(porti) + _, err = net.ResolveIPAddr("ip", host) + if err != nil { + return + } + out = in + if portN != 25565 { + return + } + ctx, cancel := context.WithTimeout(context.Background(), time.Second) // otherwise this takes 10+ seconds + defer cancel() + + r := &net.Resolver{} + + _, srvs, _ := r.LookupSRV(ctx, "minecraft", "tcp", host) + if len(srvs) == 0 { + return + } + host = strings.TrimSuffix(srvs[0].Target, ".") + portN = srvs[0].Port + out = host + ":" + strconv.Itoa(int(portN)) + return +} diff --git a/client/status.go b/client/status.go index 282a3a2..14aa67c 100644 --- a/client/status.go +++ b/client/status.go @@ -48,6 +48,8 @@ func (s *StatusClient) OnPong(p *v1_21_8.StatusToClientPacketPing) (err error) { return } +// StatusRaw returns a StatusClient that is not connected to a server. +// So ignore the resource leak warning. And maybe attach a warn ignore comment func StatusRaw(addr string) (s *StatusClient, err error) { c, err := ConnectVersionLess(addr) if err != nil { diff --git a/event/event_bus.go b/event/event_bus.go index e855825..9938cf0 100644 --- a/event/event_bus.go +++ b/event/event_bus.go @@ -90,7 +90,7 @@ func (l *Loop) RegisterCustomType(eventType reflect.Type, hv reflect.Value) { handleFunc := func(packet any) error { v := hv.Call([]reflect.Value{reflect.ValueOf(packet)})[0] if !v.IsNil() { - slog.Error("handler failed", v.Interface().(error)) + slog.Error("handler failed", "err", v.Interface().(error)) } return nil } diff --git a/text/text_test.go b/text/text_test.go index 0b7ca10..bd64a84 100644 --- a/text/text_test.go +++ b/text/text_test.go @@ -167,14 +167,14 @@ func TestPretty(t *testing.T) { func TestPrettyF(t *testing.T) { got := PrettyF("Hello §b%s", "World") - want := Component{ + want := &Component{ Text: "Hello ", Extra: []Component{ {Text: "World", Color: "aqua"}, }, } if !reflect.DeepEqual(got, want) { - t.Errorf("PrettyF() = %+v, want %+v", got, want) + t.Errorf("PrettyF() = \n%+v\n, want \n%+v", got, want) } } diff --git a/tools/cli/status/main.go b/tools/cli/status/main.go index 3314045..089f555 100644 --- a/tools/cli/status/main.go +++ b/tools/cli/status/main.go @@ -17,7 +17,7 @@ func Run(args []string) (err error) { return } if *srvFlag { - *addrFlag, err = client.DoDNS(*addrFlag) + *addrFlag, err = client.DoDnsSimple(*addrFlag) if err != nil { return } diff --git a/tools/registry_data_hunter/main.go b/tools/registry_data_hunter/main.go index 556f87f..50cd467 100644 --- a/tools/registry_data_hunter/main.go +++ b/tools/registry_data_hunter/main.go @@ -48,6 +48,14 @@ type Proxy struct { Data nbt.Tag } +func (p *Proxy) OnStart() (err error) { + p.Client.RegisterCritical(p.OnAnything) + p.Servee.RegisterCritical(p.OnAnything) + + p.Client.RegisterCriticalUntilLatest(p.OnFinishConfiguration) + return +} + func (p *Proxy) OnAnything(e event.Anything) (err error) { packet, ok := e.Val.(proto_base.EncodeDecodeAble) if !ok {