forked from andrewtian/minepong
/
ping.go
189 lines (150 loc) · 3.58 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package minepong
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"io"
"net"
"strconv"
"time"
)
const (
protocolVersion = 573
)
var (
connectionTimeout = 2 * time.Second
)
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"`
ResolvedHost string `json:"resolved_host"`
}
func resolveSRV(addr string) (host string, err error) {
h, _, err := net.SplitHostPort(addr)
if err != nil {
h = addr
}
_, addrs, err := net.LookupSRV("minecraft", "tcp", h)
if err != nil || len(addrs) == 0 {
return host, errors.New("unable to find SRV record")
}
return net.JoinHostPort(addrs[0].Target, strconv.Itoa(int(addrs[0].Port))), nil
}
func Ping(host string) (*Pong, error) {
srvHost, err := resolveSRV(host)
if err == nil {
host = srvHost
}
conn, err := net.DialTimeout("tcp", host, connectionTimeout)
if err != nil {
return nil, err
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(connectionTimeout))
conn.SetWriteDeadline(time.Now().Add(connectionTimeout))
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
}
pong.ResolvedHost = host
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.Write(encodeVarint(uint64(protocolVersion)))
// server address
host, port, err := net.SplitHostPort(host)
if err != nil {
return errors.New("cannot split host and port")
}
pl.Write(encodeVarint(uint64(len(host))))
pl.WriteString(host)
// server port
iPort, err := strconv.Atoi(port)
if err != nil {
return errors.New("cannot convert port to int")
}
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
}