From 61c8ba07bb0c01fb18c95de3f9a1dbf1f35f3a01 Mon Sep 17 00:00:00 2001 From: NyanCatda Date: Wed, 10 Jul 2024 17:59:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9F=BA=E5=B2=A9=E7=89=88Mo?= =?UTF-8?q?td=E6=95=B0=E6=8D=AE=E8=AF=B7=E6=B1=82=E5=8F=8A=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=EF=BC=8C=E6=B7=BB=E5=8A=A0ServerUniqueID=E8=BF=94?= =?UTF-8?q?=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MotdBEAPI/MotdBEAPI.go | 155 ++++++++++++++++++++++++++--------------- MotdBEAPI/Motd_test.go | 23 ++++-- go.mod | 2 +- 3 files changed, 120 insertions(+), 60 deletions(-) diff --git a/MotdBEAPI/MotdBEAPI.go b/MotdBEAPI/MotdBEAPI.go index 01655a0..8e7ece9 100644 --- a/MotdBEAPI/MotdBEAPI.go +++ b/MotdBEAPI/MotdBEAPI.go @@ -2,7 +2,7 @@ package MotdBEAPI import ( - "encoding/hex" + "encoding/binary" "net" "strconv" "strings" @@ -11,76 +11,121 @@ import ( // MotdBE信息 type MotdBEInfo struct { - Status string `json:"status"` //服务器状态 - Host string `json:"host"` //服务器Host - Motd string `json:"motd"` //Motd信息 - Agreement int `json:"agreement"` //协议版本 - Version string `json:"version"` //支持的游戏版本 - Online int `json:"online"` //在线人数 - Max int `json:"max"` //最大在线人数 - LevelName string `json:"level_name"` //存档名字 - GameMode string `json:"gamemode"` //游戏模式 - Delay int64 `json:"delay"` //连接延迟 + Status string `json:"status"` //服务器状态 (online/offline) + Host string `json:"host"` //服务器Host + Motd string `json:"motd"` //Motd信息 + Agreement int `json:"agreement"` //协议版本 + Version string `json:"version"` //支持的游戏版本 + Online int `json:"online"` //在线人数 + Max int `json:"max"` //最大在线人数 + LevelName string `json:"level_name"` //存档名字 + GameMode string `json:"gamemode"` //游戏模式 + ServerUniqueID int `json:"server_unique_id"` //服务器唯一ID + Delay int64 `json:"delay"` //连接延迟 } -// @description: 通过UDP请求获取MotdBE信息 -// @param {string} Host 服务器地址,nyan.xyz:19132 -// @return {*MotdBEInfo} -// @return {error} +/** + * @description: 通过UDP请求获取MotdBE信息 + * @param {string} Host 服务器地址,nyan.xyz:19132 + * @return {*MotdBEInfo} MotdBE信息 + * @return {error} 错误信息 + */ func MotdBE(Host string) (*MotdBEInfo, error) { + errorReturn := &MotdBEInfo{ + Status: "offline", + } + if Host == "" { - MotdInfo := &MotdBEInfo{ - Status: "offline", - } - return MotdInfo, nil + return errorReturn, nil } // 创建连接 - socket, err := net.Dial("udp", Host) + Socket, err := net.Dial("udp", Host) if err != nil { - MotdInfo := &MotdBEInfo{ - Status: "offline", - } - return MotdInfo, err + return errorReturn, err } - defer socket.Close() + defer Socket.Close() + + // 组成发送数据 + PacketID := []byte{0x01} // Packet ID + // 获取当前时间戳 + ClientSendTime := make([]byte, 8) // 客户端发送时间 + binary.BigEndian.PutUint64(ClientSendTime, uint64(time.Now().Unix())) + Magic := []byte{0x00, 0xFF, 0xFF, 0x00, 0xFE, 0xFE, 0xFE, 0xFE, 0xFD, 0xFD, 0xFD, 0xFD} // Magic Number + ClientID := []byte{0x00, 0x00, 0x00, 0x00} // 客户端ID + // 组合数据 + SendData := append(PacketID, ClientSendTime...) + SendData = append(SendData, Magic...) + SendData = append(SendData, ClientID...) + // 发送数据 - time1 := time.Now().UnixNano() / 1e6 //记录发送时间 - senddata, _ := hex.DecodeString("0100000000240D12D300FFFF00FEFEFEFEFDFDFDFD12345678") - _, err = socket.Write(senddata) + StartTime := time.Now().UnixNano() / 1e6 // 记录发送时间 + _, err = Socket.Write(SendData) if err != nil { - MotdInfo := &MotdBEInfo{ - Status: "offline", - } - return MotdInfo, err + return errorReturn, err } + // 接收数据 - UDPdata := make([]byte, 4096) - socket.SetReadDeadline(time.Now().Add(5 * time.Second)) //设置读取五秒超时 - _, err = socket.Read(UDPdata) + UDPdata := make([]byte, 256) + Socket.SetReadDeadline(time.Now().Add(5 * time.Second)) // 设置读取五秒超时 + // 读取数据 + _, err = Socket.Read(UDPdata) + if err != nil { + return errorReturn, err + } + EndTime := time.Now().UnixNano() / 1e6 // 记录接收时间 + + // 读取数据 + // PacketID = UDPdata[:1] // Packet ID + // ServerSendTime := UDPdata[1:9] // 服务器发送时间 + // ServerGUID := UDPdata[9:17] // 服务器GUID + // Magic = UDPdata[17:33] // Magic Number + ServerInfo := UDPdata[33:] // 服务器信息 + + // 按;分割数据 + MotdData := strings.Split(string(ServerInfo), ";") + + // 解析数据 + MOTD1 := MotdData[1] // 服务器MOTD line 1 + ProtocolVersion := MotdData[2] // 协议版本 + VersionName := MotdData[3] // 服务器游戏版本 + PlayerCount := MotdData[4] // 在线人数 + MaxPlayerCount := MotdData[5] // 最大在线人数 + ServerUniqueID := MotdData[6] // 服务器唯一ID + MOTD2 := MotdData[7] // 服务器MOTD line 2 + GameMode := MotdData[8] // 游戏模式 + // GameModeNumeric := MotdData[9] // 游戏模式数字 + + // 转换数据 + ProtocolVersionInt, err := strconv.Atoi(ProtocolVersion) if err != nil { - MotdInfo := &MotdBEInfo{ - Status: "offline", - } - return MotdInfo, err + return errorReturn, err } - time2 := time.Now().UnixNano() / 1e6 //记录接收时间 - //解析数据 - MotdData := strings.Split(string(UDPdata), ";") - Agreement, _ := strconv.Atoi(MotdData[2]) - Online, _ := strconv.Atoi(MotdData[4]) - Max, _ := strconv.Atoi(MotdData[5]) + PlayerCountInt, err := strconv.Atoi(PlayerCount) + if err != nil { + return errorReturn, err + } + MaxPlayerCountInt, err := strconv.Atoi(MaxPlayerCount) + if err != nil { + return errorReturn, err + } + ServerUniqueIDInt, err := strconv.Atoi(ServerUniqueID) + if err != nil { + return errorReturn, err + } + MotdInfo := &MotdBEInfo{ - Status: "online", - Host: Host, - Motd: MotdData[1], - Agreement: Agreement, - Version: MotdData[3], - Online: Online, - Max: Max, - LevelName: MotdData[7], - GameMode: MotdData[8], - Delay: time2 - time1, + Status: "online", + Host: Host, + Motd: MOTD1, + Agreement: ProtocolVersionInt, + Version: VersionName, + Online: PlayerCountInt, + Max: MaxPlayerCountInt, + LevelName: MOTD2, + GameMode: GameMode, + ServerUniqueID: ServerUniqueIDInt, + Delay: EndTime - StartTime, } return MotdInfo, nil } diff --git a/MotdBEAPI/Motd_test.go b/MotdBEAPI/Motd_test.go index e310e18..f657698 100644 --- a/MotdBEAPI/Motd_test.go +++ b/MotdBEAPI/Motd_test.go @@ -1,14 +1,17 @@ /* * @Author: NyanCatda * @Date: 2024-03-30 02:33:22 - * @LastEditTime: 2024-03-30 02:34:31 + * @LastEditTime: 2024-07-10 17:32:54 * @LastEditors: NyanCatda * @Description: 测试用例 * @FilePath: \MCBE-Server-Motd\MotdBEAPI\Motd_test.go */ package MotdBEAPI -import "testing" +import ( + "encoding/json" + "testing" +) func TestBE(t *testing.T) { Host := "" @@ -19,7 +22,13 @@ func TestBE(t *testing.T) { return } - t.Log(Data) + DataJson, err := json.Marshal(Data) + if err != nil { + t.Error(err) + return + } + + t.Log(string(DataJson)) } func TestJava(t *testing.T) { @@ -31,5 +40,11 @@ func TestJava(t *testing.T) { return } - t.Log(Data) + DataJson, err := json.Marshal(Data) + if err != nil { + t.Error(err) + return + } + + t.Log(string(DataJson)) } diff --git a/go.mod b/go.mod index a6bb1b8..d63d391 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( ) require ( - github.com/BlackBEDevelopment/MCBE-Server-Motd/MotdBEAPI v0.0.0-20211228052232-4fa847734aff + github.com/BlackBEDevelopment/MCBE-Server-Motd/MotdBEAPI v1.1.0 github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect