From 8147520c418e5e9764146afa4bd9204c5ac6074e Mon Sep 17 00:00:00 2001 From: duoduo song Date: Wed, 13 Mar 2024 11:04:10 +0800 Subject: [PATCH] add support for newer minecraft versions (tested in 1.18.2) --- core/common.go | 7 ++++--- core/core.go | 6 +++--- core/forward.go | 4 ++-- core/ping.go | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/core/common.go b/core/common.go index e59d136..8c94caf 100644 --- a/core/common.go +++ b/core/common.go @@ -8,6 +8,7 @@ import ( ) const VERSION_1_8_9 = 47 +const VERSION_1_18_2 = 758 var onlineCount atomic.Int32 @@ -51,11 +52,11 @@ type statusResponse struct { } // write ping response packet -func sendResponse(w io.Writer) error { +func sendResponse(w io.Writer, protocol int) error { resp, err := json.Marshal(statusResponse{ Version: statusVersion{ - Name: "1.8.9", - Protocol: VERSION_1_8_9, + Name: "gomcproxy", + Protocol: protocol, }, Players: statusPlayers{ Max: cfg.MaxPlayer, diff --git a/core/core.go b/core/core.go index 1716df5..da0b5a5 100644 --- a/core/core.go +++ b/core/core.go @@ -59,14 +59,14 @@ func handler(conn net.Conn) { switch nextState { case 1: // status - err := handlePing(reader, conn) + err := handlePing(reader, conn, int(protocol)) if err != nil { log.Println("handle ping error:", err) } case 2: // login - if protocol != VERSION_1_8_9 { + if protocol < VERSION_1_8_9 { err := sendDisconnect(conn, "unsupported client version") if err != nil { log.Println("disconnect error:", err) @@ -83,7 +83,7 @@ func handler(conn net.Conn) { return } - err := handleForward(reader, conn, strings.HasSuffix(string(address), "\x00FML\x00")) + err := handleForward(reader, conn, strings.HasSuffix(string(address), "\x00FML\x00"), int(protocol)) if err != nil { log.Println("handle forward error:", err) } diff --git a/core/forward.go b/core/forward.go index eb0d69c..5228149 100644 --- a/core/forward.go +++ b/core/forward.go @@ -7,7 +7,7 @@ import ( "sync" ) -func handleForward(reader io.Reader, writer io.Writer, fml bool) error { +func handleForward(reader io.Reader, writer io.Writer, fml bool, protocol int) error { onlineCount.Add(1) defer onlineCount.Add(-1) @@ -59,7 +59,7 @@ func handleForward(reader io.Reader, writer io.Writer, fml bool) error { } pktHandshake, err := Pack( - VarInt(VERSION_1_8_9), + VarInt(protocol), String(rewriteHost), UShort(cfg.RewirtePort), VarInt(2), // next state login diff --git a/core/ping.go b/core/ping.go index a8b2203..14cc689 100644 --- a/core/ping.go +++ b/core/ping.go @@ -7,7 +7,7 @@ import ( "time" ) -func handlePing(reader io.Reader, writer io.Writer) error { +func handlePing(reader io.Reader, writer io.Writer, protocol int) error { // request pkt, err := ReadPacket(reader) @@ -19,7 +19,7 @@ func handlePing(reader io.Reader, writer io.Writer) error { } // response - err = sendResponse(writer) + err = sendResponse(writer, protocol) if err != nil { return err }