mineauth/ping.go

70 lines
1.5 KiB
Go
Raw Normal View History

package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/net"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/google/uuid"
"go.jolheiser.com/beaver"
)
func (s *Server) acceptListPing(conn net.Conn) {
var p pk.Packet
for i := 0; i < 2; i++ {
err := conn.ReadPacket(&p)
if err != nil {
return
}
switch p.ID {
case 0x00:
err = conn.WritePacket(pk.Marshal(0x00, pk.String(listResp())))
case 0x01:
err = conn.WritePacket(p)
}
if err != nil {
return
}
}
}
type player struct {
Name string `json:"name"`
ID uuid.UUID `json:"id"`
}
// listResp return server status as JSON string
func listResp() string {
var list struct {
Version struct {
Name string `json:"name"`
Protocol int `json:"protocol"`
} `json:"version"`
Players struct {
Max int `json:"max"`
Online int `json:"online"`
Sample []player `json:"sample"`
} `json:"players"`
Description chat.Message `json:"description"`
FavIcon string `json:"favicon,omitempty"`
}
list.Version.Name = "Register Server"
list.Version.Protocol = bot.ProtocolVersion
list.Players.Max = 0
list.Players.Online = 0
list.Players.Sample = []player{}
list.Description = chat.Message{Text: "Login to register!"}
list.FavIcon = fmt.Sprintf("data:image/png;base64,%s", base64.StdEncoding.EncodeToString(favicon))
data, err := json.Marshal(list)
if err != nil {
beaver.Errorf("could not marshal JSON: %v", err)
}
return string(data)
}