Skip to content

Commit

Permalink
fix: Compatibility with newer FML network markers
Browse files Browse the repository at this point in the history
  • Loading branch information
layou233 committed Nov 4, 2024
1 parent f14f5c2 commit 57d61b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 12 additions & 3 deletions adapter/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type MinecraftMetadata struct {
PlayerName string
OriginDestination string
RewrittenDestination string
fmlMarkup string
OriginPort uint16
RewrittenPort uint16
UUID [16]byte
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions protocol/minecraft/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 57d61b1

Please sign in to comment.