test fixup

This commit is contained in:
adman
2026-03-20 17:57:03 +01:00
parent 43fea40d08
commit 29baf8da9f
6 changed files with 57 additions and 4 deletions
+43
View File
@@ -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
}
+2
View File
@@ -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 {
+1 -1
View File
@@ -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
}
+2 -2
View File
@@ -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)
}
}
+1 -1
View File
@@ -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
}
+8
View File
@@ -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 {