Skip to content

Commit

Permalink
优化基岩版Motd数据请求及解析,添加ServerUniqueID返回
Browse files Browse the repository at this point in the history
  • Loading branch information
nyancatda committed Jul 10, 2024
1 parent ed0aed3 commit 61c8ba0
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 60 deletions.
155 changes: 100 additions & 55 deletions MotdBEAPI/MotdBEAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package MotdBEAPI

import (
"encoding/hex"
"encoding/binary"
"net"
"strconv"
"strings"
Expand All @@ -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
}
23 changes: 19 additions & 4 deletions MotdBEAPI/Motd_test.go
Original file line number Diff line number Diff line change
@@ -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 := ""
Expand All @@ -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) {
Expand All @@ -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))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 61c8ba0

Please sign in to comment.