Files
uptimesebi/internal/check/games_test.go
T
Sebastian UnterschützandClaude Opus 5.5 f14b760069
ci / build (pull_request) Successful in 5m10s
ci / image (pull_request) Successful in 6m44s
ci / test (pull_request) Canceled after 7m6s
ci / lint (pull_request) Canceled after 7m5s
feat: Steam and Minecraft game server checks
Steam servers are asked for A2S_INFO like the server browser does,
answering the challenge newer servers send. Minecraft Java Edition gets
the server list ping, Bedrock Edition a RakNet unconnected ping. A
minimum of free slots can be required. The protocols are implemented
directly; tested against protocol-accurate test servers and once by
hand against public Java and Bedrock servers.

Closes #96

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 16:56:40 +01:00

137 lines
4.1 KiB
Go

package check
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"io"
"net"
"testing"
)
func udpServer(t *testing.T, answer func(req []byte) []byte) string {
t.Helper()
pc, err := net.ListenPacket("udp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = pc.Close() })
go func() {
buf := make([]byte, 1500)
for {
n, addr, err := pc.ReadFrom(buf)
if err != nil {
return
}
if out := answer(append([]byte{}, buf[:n]...)); out != nil {
_, _ = pc.WriteTo(out, addr)
}
}
}()
return pc.LocalAddr().String()
}
// steamServer answers A2S_INFO after a challenge, like current servers.
func steamServer(t *testing.T, players, maxPlayers byte) string {
challenge := []byte{0x11, 0x22, 0x33, 0x44}
return udpServer(t, func(req []byte) []byte {
if !bytes.HasPrefix(req, a2sInfo) {
return nil
}
if !bytes.Equal(req[len(a2sInfo):], challenge) {
return append([]byte{0xFF, 0xFF, 0xFF, 0xFF, 'A'}, challenge...)
}
out := []byte{0xFF, 0xFF, 0xFF, 0xFF, 'I', 17}
for _, s := range []string{"Test Server", "de_dust2", "csgo", "Counter-Strike"} {
out = append(append(out, s...), 0)
}
out = binary.LittleEndian.AppendUint16(out, 730)
return append(out, players, maxPlayers, 0, 'd', 'l', 0, 1)
})
}
func javaServer(t *testing.T, online, maxPlayers int) string {
l := listen(t)
go func() {
for {
conn, err := l.Accept()
if err != nil {
return
}
go func() {
defer func() { _ = conn.Close() }()
r := bufio.NewReader(conn)
for range 2 { // handshake, status request
n, err := binary.ReadUvarint(r)
if err != nil {
return
}
if _, err := io.CopyN(io.Discard, r, int64(n)); err != nil {
return
}
}
status, _ := json.Marshal(map[string]any{
"version": map[string]any{"name": "1.21.4", "protocol": 769},
"players": map[string]any{"max": maxPlayers, "online": online},
"description": map[string]any{"text": "A Minecraft Server"},
})
body := append([]byte{0x00}, binary.AppendUvarint(nil, uint64(len(status)))...)
body = append(body, status...)
_, _ = conn.Write(append(binary.AppendUvarint(nil, uint64(len(body))), body...))
}()
}
}()
return l.Addr().String()
}
func bedrockServer(t *testing.T) string {
return udpServer(t, func(req []byte) []byte {
if len(req) < 25 || req[0] != 0x01 || !bytes.Equal(req[9:25], raknetMagic) {
return nil
}
motd := "MCPE;Dedicated Server;766;1.21.50;3;10;1234;Bedrock level;Survival;1;19132;19133;"
out := append([]byte{0x1C}, req[1:9]...)
out = binary.BigEndian.AppendUint64(out, 42)
out = append(out, raknetMagic...)
out = binary.BigEndian.AppendUint16(out, uint16(len(motd)))
return append(out, motd...)
})
}
func TestGameChecks(t *testing.T) {
expectOutcomes(t, Steam, steamServer(t, 3, 24), map[string]string{
`{}`: "",
`{"min_free_slots":21}`: "",
`{"min_free_slots":22}`: "3 of 24 slots taken",
})
expectOutcomes(t, Minecraft, javaServer(t, 19, 20), map[string]string{
`{}`: "",
`{"edition":"java","min_free_slots":2}`: "19 of 20 slots taken",
})
expectOutcomes(t, Minecraft, bedrockServer(t), map[string]string{
`{"edition":"bedrock"}`: "",
`{"edition":"bedrock","min_free_slots":8}`: "3 of 10 slots taken",
})
// A port that answers nothing, and one speaking another protocol.
silent := udpServer(t, func([]byte) []byte { return nil })
expectOutcomes(t, Steam, silent, map[string]string{`{}`: "no answer from the game server"})
expectOutcomes(t, Minecraft, steamServer(t, 0, 1), map[string]string{`{"edition":"bedrock"}`: "no answer"})
expectOutcomes(t, Minecraft, smtpServer(t, nil), map[string]string{`{}`: "not a Minecraft server"})
}
func TestGameValidate(t *testing.T) {
for _, tc := range []struct {
typ Type
cfg string
}{
{Minecraft, `{"edition":"pocket"}`},
{Steam, `{"edition":"java"}`},
{Steam, `{"min_free_slots":-1}`},
} {
if _, err := Validate(tc.typ, "game.example.com:27015", []byte(tc.cfg)); err == nil {
t.Errorf("Validate(%s, %s) accepted", tc.typ, tc.cfg)
}
}
}