From 57d61b1c6f3fefe24109db7e17c7c3448fcc1f77 Mon Sep 17 00:00:00 2001 From: layou233 Date: Mon, 4 Nov 2024 23:45:51 +0800 Subject: [PATCH] fix: Compatibility with newer FML network markers --- adapter/metadata.go | 15 ++++++++++++--- protocol/minecraft/outbound.go | 10 +++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/adapter/metadata.go b/adapter/metadata.go index ae1a726..7011c7a 100644 --- a/adapter/metadata.go +++ b/adapter/metadata.go @@ -41,6 +41,7 @@ type MinecraftMetadata struct { PlayerName string OriginDestination string RewrittenDestination string + fmlMarkup string OriginPort uint16 RewrittenPort uint16 UUID [16]byte @@ -49,11 +50,19 @@ type MinecraftMetadata struct { } func (m *MinecraftMetadata) IsFML() bool { - return strings.HasSuffix(m.OriginDestination, "\x00FML\x00") + return strings.IndexByte(m.OriginDestination, 0) != -1 } -func (m *MinecraftMetadata) CleanOriginDestination() string { - return strings.TrimSuffix(m.OriginDestination, "\x00FML\x00") +func (m *MinecraftMetadata) CleanOriginDestination() (clean string) { + clean, m.fmlMarkup, _ = strings.Cut(m.OriginDestination, "\x00") + return +} + +func (m *MinecraftMetadata) FMLMarkup() string { + if m.fmlMarkup == "" { + _, m.fmlMarkup, _ = strings.Cut(m.OriginDestination, "\x00") + } + return m.fmlMarkup } type TLSMetadata struct { diff --git a/protocol/minecraft/outbound.go b/protocol/minecraft/outbound.go index 98c9faa..3e6974f 100644 --- a/protocol/minecraft/outbound.go +++ b/protocol/minecraft/outbound.go @@ -250,7 +250,7 @@ func (o *Outbound) InjectConnection(ctx context.Context, conn *bufio.CachedConn, } //remoteConn.(*net.TCPConn).SetLinger(0) // for some reason if metadata.Minecraft.RewrittenDestination == "" { - metadata.Minecraft.RewrittenDestination = metadata.Minecraft.OriginDestination + metadata.Minecraft.RewrittenDestination = metadata.Minecraft.CleanOriginDestination() } if metadata.Minecraft.RewrittenPort == 0 { metadata.Minecraft.RewrittenPort = metadata.Minecraft.OriginPort @@ -265,10 +265,10 @@ func (o *Outbound) InjectConnection(ctx context.Context, conn *bufio.CachedConn, hostname = o.config.TargetAddress } } else if hostname == "" { - hostname = metadata.Minecraft.OriginDestination + hostname = metadata.Minecraft.CleanOriginDestination() } if !o.config.Minecraft.IgnoreFMLSuffix && metadata.Minecraft.IsFML() { - hostname += "\x00FML\x00" + hostname += "\x00" + metadata.Minecraft.FMLMarkup() } port := metadata.Minecraft.RewrittenPort if port <= 0 { @@ -391,10 +391,10 @@ func (o *Outbound) InjectConnection(ctx context.Context, conn *bufio.CachedConn, hostname = o.config.TargetAddress } } else if hostname == "" { - hostname = metadata.Minecraft.OriginDestination + hostname = metadata.Minecraft.CleanOriginDestination() } if !o.config.Minecraft.IgnoreFMLSuffix && metadata.Minecraft.IsFML() { - hostname += "\x00FML\x00" + hostname += "\x00" + metadata.Minecraft.FMLMarkup() } port := metadata.Minecraft.RewrittenPort if port <= 0 {