Skip to content

Commit 32d6524

Browse files
authored
Merge pull request #10 from mcstatus-io/v4
Merge v4 into main
2 parents 427b3bc + 34927b5 commit 32d6524

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

cmd/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type passedOptions struct {
2121
Type string `short:"t" long:"type" description:"The type of status to retrieve" default:"java"`
2222
Timeout uint `short:"T" long:"timeout" description:"The amount of seconds before the status retrieval times out" default:"5"`
2323
DisableSRV bool `short:"S" long:"disable-srv" description:"Disables SRV lookup"`
24+
Debug bool `short:"D" long:"debug" description:"Enables debug printing to the console"`
2425
}
2526

2627
func init() {
@@ -62,6 +63,8 @@ func main() {
6263
EnableSRV: !opts.DisableSRV,
6364
Timeout: time.Duration(opts.Timeout) * time.Second,
6465
ProtocolVersion: 47,
66+
Ping: true,
67+
Debug: opts.Debug,
6568
})
6669

6770
break

options/status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type StatusModern struct {
1010
Timeout time.Duration
1111
ProtocolVersion int
1212
Ping bool
13+
Debug bool
1314
}
1415

1516
// StatusLegacy is the options used by the status.Legacy() function.

status/modern.go

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"fmt"
99
"io"
10+
"log"
1011
"math/rand"
1112
"net"
1213
"time"
@@ -21,8 +22,9 @@ import (
2122
var defaultJavaStatusOptions = options.StatusModern{
2223
EnableSRV: true,
2324
Timeout: time.Second * 5,
24-
ProtocolVersion: 47,
25+
ProtocolVersion: -1,
2526
Ping: true,
27+
Debug: false,
2628
}
2729

2830
type rawJavaStatus struct {
@@ -92,19 +94,30 @@ func Modern(ctx context.Context, host string, options ...options.StatusModern) (
9294

9395
func getStatusModern(host string, options ...options.StatusModern) (*response.StatusModern, error) {
9496
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
100103
)
101104

102-
connectionHostname, port, err := util.ParseAddress(host)
105+
hostname, port, err := util.ParseAddress(host)
103106

104107
if err != nil {
105108
return nil, err
106109
}
107110

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+
108121
if opts.EnableSRV && port == nil && net.ParseIP(connectionHostname) == nil {
109122
record, err := util.LookupSRV(host)
110123

@@ -116,6 +129,12 @@ func getStatusModern(host string, options ...options.StatusModern) (*response.St
116129
Host: record.Target,
117130
Port: record.Port,
118131
}
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")
119138
}
120139
}
121140

@@ -125,37 +144,61 @@ func getStatusModern(host string, options ...options.StatusModern) (*response.St
125144
return nil, err
126145
}
127146

147+
if opts.Debug {
148+
log.Printf("Successfully connected to %s:%d\n", connectionHostname, connectionPort)
149+
}
150+
128151
defer conn.Close()
129152

130153
if err = conn.SetDeadline(time.Now().Add(opts.Timeout)); err != nil {
131154
return nil, err
132155
}
133156

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 {
135158
return nil, err
136159
}
137160

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+
138165
if err = writeJavaStatusStatusRequestPacket(conn); err != nil {
139166
return nil, err
140167
}
141168

169+
if opts.Debug {
170+
log.Println("[S <- C] Wrote status request packet")
171+
}
172+
142173
if err = readJavaStatusStatusResponsePacket(conn, &rawResponse); err != nil {
143174
return nil, err
144175
}
145176

177+
if opts.Debug {
178+
log.Println("[S -> C] Read status response packet")
179+
}
180+
146181
if opts.Ping {
147182
payload := rand.Int63()
148183

149184
if err = writeJavaStatusPingPacket(conn, payload); err != nil {
150185
return nil, err
151186
}
152187

188+
if opts.Debug {
189+
log.Printf("[S <- C] Wrote ping packet (payload=%d)\n", payload)
190+
}
191+
153192
pingStart := time.Now()
154193

155194
if err = readJavaStatusPongPacket(conn, payload); err != nil {
156195
return nil, err
157196
}
158197

198+
if opts.Debug {
199+
log.Printf("[S -> C] Read ping packet (payload=%d)\n", payload)
200+
}
201+
159202
latency = time.Since(pingStart)
160203
}
161204

@@ -171,7 +214,7 @@ func parseJavaStatusOptions(opts ...options.StatusModern) options.StatusModern {
171214
}
172215

173216
// 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 {
175218
buf := &bytes.Buffer{}
176219

177220
// Packet ID - varint

status/modern_raw.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func getStatusRaw(host string, options ...options.StatusModern) (map[string]inte
7575
return nil, err
7676
}
7777

78-
if err = writeJavaStatusHandshakeRequestPacket(conn, int32(opts.ProtocolVersion), connectionHostname, connectionPort); err != nil {
78+
if err = writeJavaStatusHandshakePacket(conn, int32(opts.ProtocolVersion), connectionHostname, connectionPort); err != nil {
7979
return nil, err
8080
}
8181

status/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ func parsePlayerID(value interface{}) (string, bool) {
5454
func pointerOf[T any](v T) *T {
5555
return &v
5656
}
57+
58+
func nilValueSwap[T any](a *T, b T) T {
59+
if a == nil {
60+
return b
61+
}
62+
63+
return *a
64+
}

0 commit comments

Comments
 (0)