This commit is contained in:
2024-10-26 12:41:37 +02:00
parent 78b2a2f022
commit a6a7a94c20
5 changed files with 2021 additions and 135 deletions

View File

@@ -19,7 +19,6 @@ import (
"io"
"log"
"math"
"math/big"
"net/http"
"regexp"
"strings"
@@ -34,22 +33,6 @@ func generateRandomBytes(size int) ([]byte, error) {
return randomBytes, nil
}
func serverIDCreate() ([]byte, error) {
result := make([]byte, 20)
charRange := 0x7E - 0x21 + 1
for i := 0; i < 20; i++ {
// Generate a random number in the range [charStart, charEnd]
num, err := rand.Int(rand.Reader, big.NewInt(int64(charRange)))
if err != nil {
return []byte{}, err
}
result[i] = byte(0x21 + num.Int64())
}
return result, nil
}
type TextPiece struct {
Text string `json:"text,omitempty"`
Color string `json:"color,omitempty"`
@@ -62,6 +45,14 @@ type TextComponent struct {
CleanText string `json:"cleantext,omitempty"`
}
func getTextComponent(text string) TextComponent {
return TextComponent{
Text: text,
Extra: nil,
CleanText: text,
}
}
type RegistryEntry struct {
EntryID string `json:"entryID,omitempty"`
HasNBT bool `json:"hasNBT,omitempty"`
@@ -237,11 +228,51 @@ type MojangProperty struct {
Signature string `json:"signature"`
}
func (mojangProperty *MojangProperty) add(buf *bytes.Buffer) {
addString(buf, mojangProperty.Name)
addString(buf, mojangProperty.Value)
hasSignature := mojangProperty.Signature != ""
addBool(buf, hasSignature)
if hasSignature {
addString(buf, mojangProperty.Signature)
}
}
type PlayerAction struct {
ActionMask byte
Name string
Properties []MojangProperty
GameMode int32
Listed bool
Ping int32
DisplayName string
HasDisplayName bool
}
// Helper to add a player with properties
func (playerAction *PlayerAction) add(buf *bytes.Buffer) {
addString(buf, playerAction.Name)
// Add properties
addVarint(buf, int32(len(playerAction.Properties)))
for _, prop := range playerAction.Properties {
prop.add(buf)
}
}
// Helper to update game mode
// Helper to update listed state
// Helper to update latency (ping)
// Helper to update display name
func (player *Player) hasJoinedSession(sharedSecret []byte) (bool, error) {
serverKey, err := ExportRSAPublicKey(serverPublicKey)
serverIDHash := AuthDigest([][]byte{serverID, sharedSecret, serverKey})
remoteAddr := strings.Split(player.conn.RemoteAddr().String(), ":")[0]
url := fmt.Sprintf("https://sessionserver.mojang.com/session/minecraft/hasJoined?username=%s&serverId=%s&ip=%s", player.name, serverIDHash, remoteAddr)
url := fmt.Sprintf("https://sessionserver.mojang.com/session/minecraft/hasJoined?username=%s&serverId=%s&ip=%s", player.Entity.Name, serverIDHash, remoteAddr)
resp, err := http.Get(url)
if err != nil {
@@ -567,37 +598,6 @@ func receiveVarint(buf *bytes.Buffer) (value int32, err error) {
return
}
func receiveVarlong(buf *bytes.Buffer) (currentValue int64) {
var (
b byte
shift uint64
uValue uint64
length uint32
)
for i := 0; i < buf.Len(); i++ {
bx, err := buf.ReadByte()
b = bx
if err != nil {
return
}
length++
uValue |= (uint64(b) & 0x7F) << shift
shift += 7
if b&0x80 == 0 {
break
}
}
if shift < 8*uint64(length) && b&0x40 != 0 {
uValue |= ^uint64(0) << shift
}
currentValue = int64(uValue)
return currentValue
}
func addUUID(buffer *bytes.Buffer, inUUID uuid.UUID) int32 {
binaryUUID, err := inUUID.MarshalBinary()
if err != nil {
@@ -626,6 +626,10 @@ func addByte(buffer *bytes.Buffer, byte byte) {
buffer.WriteByte(byte)
}
func addBytes(buffer *bytes.Buffer, bytes []byte) {
buffer.Write(bytes)
}
func addBool(buffer *bytes.Buffer, boolean bool) {
var boolByte byte = 0x00
if boolean {
@@ -643,12 +647,6 @@ func receiveBool(buffer *bytes.Buffer) (boolean bool, err error) {
return
}
func addUint16(buffer *bytes.Buffer, value uint16) {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, value)
buffer.Write(b)
}
// Decodes an uint16 from the buffer.
func receiveUint16(buffer *bytes.Buffer) (value uint16) {
// Create a byte slice to hold the 2 bytes for the uint16.
@@ -665,6 +663,22 @@ func receiveUint16(buffer *bytes.Buffer) (value uint16) {
return value
}
// Decodes an uint16 from the buffer.
func receiveInt16(buffer *bytes.Buffer) (value int16) {
// Create a byte slice to hold the 2 bytes for the uint16.
b := make([]byte, 2)
// Read exactly 2 bytes from the buffer.
_, err := buffer.Read(b)
if err != nil {
return 0
}
// Convert the byte slice to uint16 using BigEndian.
value = int16(binary.BigEndian.Uint16(b))
return value
}
func addInt64(buffer *bytes.Buffer, value int64) {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(value))
@@ -707,15 +721,6 @@ func addFloat64(buffer *bytes.Buffer, value float64) {
}
// Decodes a float64 from the buffer.
func receiveFloat64(buffer *bytes.Buffer) (value float64) {
b := make([]byte, 8)
_, err := buffer.Read(b)
if err != nil {
return 0
}
value = math.Float64frombits(binary.BigEndian.Uint64(b))
return
}
func addInt32(buffer *bytes.Buffer, value int32) {
b := make([]byte, 4)
@@ -724,15 +729,6 @@ func addInt32(buffer *bytes.Buffer, value int32) {
}
// Decodes an int32 from the buffer.
func receiveInt32(buffer *bytes.Buffer) (value int32) {
b := make([]byte, 4)
_, err := buffer.Read(b)
if err != nil {
return 0
}
value = int32(binary.BigEndian.Uint32(b))
return
}
func addInt16(buffer *bytes.Buffer, value int16) {
b := make([]byte, 2)
@@ -793,20 +789,3 @@ func addTextComponent(buffer *bytes.Buffer, component TextComponent) {
addString(buffer, jsonString)
return
}
func receiveTextComponent(buf *bytes.Buffer) (component TextComponent, err error) {
jsonString, err := receiveString(buf)
if err != nil {
return
}
if strings.ContainsAny(jsonString, "") {
}
jsonDecoderText := json.NewDecoder(strings.NewReader(jsonString))
err = jsonDecoderText.Decode(&component)
if err != nil {
err = nil
component.Text = jsonString
component.CleanText = cleanMinecraftFormatting(jsonString)
}
return
}