/
main.go
142 lines (114 loc) · 2.43 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"time"
"github.com/jessevdk/go-flags"
"github.com/mcstatus-io/mcutil/v3"
"github.com/mcstatus-io/mcutil/v3/options"
)
var (
host string
port uint16
opts Options = Options{}
)
type Options struct {
Type string `short:"t" long:"type" description:"The type of status to retrieve" default:"java"`
Timeout uint `short:"T" long:"timeout" description:"The amount of seconds before the status retrieval times out" default:"5"`
DisableSRV bool `short:"S" long:"disable-srv" description:"Disables SRV lookup"`
}
func init() {
args, err := flags.Parse(&opts)
if err != nil {
if flags.WroteHelp(err) {
os.Exit(0)
return
}
panic(err)
}
if len(args) < 1 {
fmt.Println("missing host argument")
os.Exit(1)
}
host = args[0]
if len(args) < 2 {
switch opts.Type {
case "java", "legacy", "raw":
{
port = 25565
break
}
case "bedrock":
{
port = 19132
break
}
default:
{
fmt.Printf("unknown --type value: %s\n", opts.Type)
}
}
} else {
value, err := strconv.ParseUint(args[1], 10, 16)
if err != nil {
panic(err)
}
port = uint16(value)
}
}
func main() {
var (
result interface{}
err error
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(opts.Timeout))
defer cancel()
switch opts.Type {
case "java":
{
result, err = mcutil.Status(ctx, host, port, options.JavaStatus{
EnableSRV: !opts.DisableSRV,
Timeout: time.Duration(opts.Timeout) * time.Second,
ProtocolVersion: 47,
})
break
}
case "raw":
{
result, err = mcutil.StatusRaw(ctx, host, port, options.JavaStatus{
EnableSRV: !opts.DisableSRV,
Timeout: time.Duration(opts.Timeout) * time.Second,
ProtocolVersion: 47,
})
break
}
case "legacy":
{
result, err = mcutil.StatusLegacy(ctx, host, port, options.JavaStatusLegacy{
EnableSRV: !opts.DisableSRV,
Timeout: time.Duration(opts.Timeout) * time.Second,
ProtocolVersion: 47,
})
break
}
case "bedrock":
{
result, err = mcutil.StatusBedrock(ctx, host, port, options.BedrockStatus{
EnableSRV: !opts.DisableSRV,
Timeout: time.Duration(opts.Timeout) * time.Second,
})
break
}
}
if err != nil {
panic(err)
}
out, err := json.MarshalIndent(result, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", out)
}