-
Notifications
You must be signed in to change notification settings - Fork 4
/
status.go
65 lines (58 loc) · 1.11 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import "bufio"
import "fmt"
import "net"
import "bytes"
const STATUS_RESPONSE = `{
"version": {
"name": "eelbot",
"protocol": %d
},
"players": {
"online": %d,
"max": %d
},
"description": {"text": "\u00A74\u00A7leelbot <3"}
}`
func handleStatus(conn net.Conn, reader *bufio.Reader, writer *bufio.Writer, protocol int32, max int) {
// C->S 0x00 Status request
id, err := readHeader(reader)
if err != nil {
conn.Close()
return
}
if id != 0x00 {
conn.Close()
return
}
packetbuf := new(bytes.Buffer)
// S->C Status response
writeVarInt(packetbuf, 0x00)
writeVarString(packetbuf, fmt.Sprintf(STATUS_RESPONSE, protocol, max, max))
if err = writePacketBuf(writer, packetbuf); err != nil {
conn.Close()
return
}
// C->S 0x00 Ping
id, err = readHeader(reader)
if err != nil {
conn.Close()
return
}
if id != 0x01 {
conn.Close()
return
}
ping, err := readLong(reader)
if err != nil {
conn.Close()
return
}
// S->C Ping
writeVarInt(packetbuf, 0x01)
writeLong(packetbuf, ping)
if err = writePacketBuf(writer, packetbuf); err != nil {
conn.Close()
return
}
}