7
7
"encoding/json"
8
8
"fmt"
9
9
"io"
10
+ "log"
10
11
"math/rand"
11
12
"net"
12
13
"time"
@@ -21,8 +22,9 @@ import (
21
22
var defaultJavaStatusOptions = options.StatusModern {
22
23
EnableSRV : true ,
23
24
Timeout : time .Second * 5 ,
24
- ProtocolVersion : 47 ,
25
+ ProtocolVersion : - 1 ,
25
26
Ping : true ,
27
+ Debug : false ,
26
28
}
27
29
28
30
type rawJavaStatus struct {
@@ -92,19 +94,30 @@ func Modern(ctx context.Context, host string, options ...options.StatusModern) (
92
94
93
95
func getStatusModern (host string , options ... options.StatusModern ) (* response.StatusModern , error ) {
94
96
var (
95
- opts = parseJavaStatusOptions (options ... )
96
- connectionPort uint16 = util .DefaultJavaPort
97
- srvRecord * response.SRVRecord = nil
98
- rawResponse rawJavaStatus = rawJavaStatus {}
99
- latency time.Duration = 0
97
+ opts = parseJavaStatusOptions (options ... )
98
+ connectionHostname string = ""
99
+ connectionPort uint16 = util .DefaultJavaPort
100
+ srvRecord * response.SRVRecord = nil
101
+ rawResponse rawJavaStatus = rawJavaStatus {}
102
+ latency time.Duration = 0
100
103
)
101
104
102
- connectionHostname , port , err := util .ParseAddress (host )
105
+ hostname , port , err := util .ParseAddress (host )
103
106
104
107
if err != nil {
105
108
return nil , err
106
109
}
107
110
111
+ connectionHostname = hostname
112
+
113
+ if port != nil {
114
+ connectionPort = * port
115
+ }
116
+
117
+ if opts .Debug {
118
+ log .Printf ("Parsed address into split hostname and port (host=%s, port=%v, default_port=%d)" , connectionHostname , nilValueSwap (port , util .DefaultJavaPort ), util .DefaultJavaPort )
119
+ }
120
+
108
121
if opts .EnableSRV && port == nil && net .ParseIP (connectionHostname ) == nil {
109
122
record , err := util .LookupSRV (host )
110
123
@@ -116,6 +129,12 @@ func getStatusModern(host string, options ...options.StatusModern) (*response.St
116
129
Host : record .Target ,
117
130
Port : record .Port ,
118
131
}
132
+
133
+ if opts .Debug {
134
+ log .Printf ("Found an SRV record (target_host=%s, target_port=%d)" , record .Target , record .Port )
135
+ }
136
+ } else if opts .Debug {
137
+ log .Println ("Could not find an SRV record for this host" )
119
138
}
120
139
}
121
140
@@ -125,37 +144,61 @@ func getStatusModern(host string, options ...options.StatusModern) (*response.St
125
144
return nil , err
126
145
}
127
146
147
+ if opts .Debug {
148
+ log .Printf ("Successfully connected to %s:%d\n " , connectionHostname , connectionPort )
149
+ }
150
+
128
151
defer conn .Close ()
129
152
130
153
if err = conn .SetDeadline (time .Now ().Add (opts .Timeout )); err != nil {
131
154
return nil , err
132
155
}
133
156
134
- if err = writeJavaStatusHandshakeRequestPacket (conn , int32 (opts .ProtocolVersion ), connectionHostname , connectionPort ); err != nil {
157
+ if err = writeJavaStatusHandshakePacket (conn , int32 (opts .ProtocolVersion ), hostname , nilValueSwap ( port , util . DefaultJavaPort ) ); err != nil {
135
158
return nil , err
136
159
}
137
160
161
+ if opts .Debug {
162
+ log .Printf ("[S <- C] Wrote handshake packet (proto=%d, host=%s, port=%d, next_state=0)\n " , opts .ProtocolVersion , hostname , nilValueSwap (port , util .DefaultJavaPort ))
163
+ }
164
+
138
165
if err = writeJavaStatusStatusRequestPacket (conn ); err != nil {
139
166
return nil , err
140
167
}
141
168
169
+ if opts .Debug {
170
+ log .Println ("[S <- C] Wrote status request packet" )
171
+ }
172
+
142
173
if err = readJavaStatusStatusResponsePacket (conn , & rawResponse ); err != nil {
143
174
return nil , err
144
175
}
145
176
177
+ if opts .Debug {
178
+ log .Println ("[S -> C] Read status response packet" )
179
+ }
180
+
146
181
if opts .Ping {
147
182
payload := rand .Int63 ()
148
183
149
184
if err = writeJavaStatusPingPacket (conn , payload ); err != nil {
150
185
return nil , err
151
186
}
152
187
188
+ if opts .Debug {
189
+ log .Printf ("[S <- C] Wrote ping packet (payload=%d)\n " , payload )
190
+ }
191
+
153
192
pingStart := time .Now ()
154
193
155
194
if err = readJavaStatusPongPacket (conn , payload ); err != nil {
156
195
return nil , err
157
196
}
158
197
198
+ if opts .Debug {
199
+ log .Printf ("[S -> C] Read ping packet (payload=%d)\n " , payload )
200
+ }
201
+
159
202
latency = time .Since (pingStart )
160
203
}
161
204
@@ -171,7 +214,7 @@ func parseJavaStatusOptions(opts ...options.StatusModern) options.StatusModern {
171
214
}
172
215
173
216
// https://wiki.vg/Server_List_Ping#Handshake
174
- func writeJavaStatusHandshakeRequestPacket (w io.Writer , protocolVersion int32 , host string , port uint16 ) error {
217
+ func writeJavaStatusHandshakePacket (w io.Writer , protocolVersion int32 , host string , port uint16 ) error {
175
218
buf := & bytes.Buffer {}
176
219
177
220
// Packet ID - varint
0 commit comments