forked from andrewtian/minepong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.go
152 lines (122 loc) · 2.73 KB
/
ping.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package minepong
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"io"
"net"
"strconv"
)
const (
protocolVersion = 0x47
)
type Pong struct {
Version struct {
Name string
Protocol int
} `json:"version"`
Players struct {
Max int `json:"max"`
Online int `json:"online"`
Sample []map[string]string
} `json:"players"`
Description interface{} `json:"description"`
FavIcon string `json:"favicon"`
}
func Ping(conn net.Conn, host string) (*Pong, error) {
if err := SendHandshake(conn, host); err != nil {
return nil, err
}
if err := SendStatusRequest(conn); err != nil {
return nil, err
}
pong, err := ReadPong(conn)
if err != nil {
return nil, err
}
return pong, nil
}
func makePacket(pl *bytes.Buffer) *bytes.Buffer {
var buf bytes.Buffer
// get payload length
buf.Write(encodeVarint(uint64(len(pl.Bytes()))))
// write payload
buf.Write(pl.Bytes())
return &buf
}
func SendHandshake(conn net.Conn, host string) error {
pl := &bytes.Buffer{}
// packet id
pl.WriteByte(0x00)
// protocol version
pl.WriteByte(protocolVersion)
// server address
host, port, err := net.SplitHostPort(host)
if err != nil {
panic(err)
}
pl.Write(encodeVarint(uint64(len(host))))
pl.WriteString(host)
// server port
iPort, err := strconv.Atoi(port)
if err != nil {
panic(err)
}
binary.Write(pl, binary.BigEndian, int16(iPort))
// next state (status)
pl.WriteByte(0x01)
if _, err := makePacket(pl).WriteTo(conn); err != nil {
return errors.New("cannot write handshake")
}
return nil
}
func SendStatusRequest(conn net.Conn) error {
pl := &bytes.Buffer{}
// send request zero
pl.WriteByte(0x00)
if _, err := makePacket(pl).WriteTo(conn); err != nil {
return errors.New("cannot write send status request")
}
return nil
}
// https://code.google.com/p/goprotobuf/source/browse/proto/encode.go#83
func encodeVarint(x uint64) []byte {
var buf [10]byte
var n int
for n = 0; x > 127; n++ {
buf[n] = 0x80 | uint8(x&0x7F)
x >>= 7
}
buf[n] = uint8(x)
n++
return buf[0:n]
}
func ReadPong(rd io.Reader) (*Pong, error) {
r := bufio.NewReader(rd)
nl, err := binary.ReadUvarint(r)
if err != nil {
return nil, errors.New("could not read length")
}
pl := make([]byte, nl)
_, err = io.ReadFull(r, pl)
if err != nil {
return nil, errors.New("could not read length given by length header")
}
// packet id
_, n := binary.Uvarint(pl)
if n <= 0 {
return nil, errors.New("could not read packet id")
}
// string varint
_, n2 := binary.Uvarint(pl[n:])
if n2 <= 0 {
return nil, errors.New("could not read string varint")
}
var pong Pong
if err := json.Unmarshal(pl[n+n2:], &pong); err != nil {
return nil, errors.New("could not read pong json")
}
return &pong, nil
}