refactoring code

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2020-11-05 16:51:20 +00:00
parent 4a44bbe89d
commit b33b6bee1d
8 changed files with 722 additions and 178 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
mcstatus
run.sh
go.sum

28
Makefile Normal file
View File

@ -0,0 +1,28 @@
APP=mcstatus
run: build
./$(APP)
doc:
godoc -http=:6090 -index
build:
go build
debs:
go get ./...
update-debs:
go get -u ./...
fmt:
gofmt -l -s .
clean:
go clean
proto:
protoc --go_out=/home/michael/go/src response.proto
.PHONY: run doc build debs update-debs fmt proto

101
client.go Normal file
View File

@ -0,0 +1,101 @@
package main
import (
"bytes"
"encoding/binary"
"net"
"strconv"
"time"
"git.0cd.xyz/michael/mcstatus/pb"
"github.com/golang/protobuf/jsonpb"
)
type client struct {
cmd *cmd
conn net.Conn
}
func newConn(cmd *cmd) (*client, error) {
conn, err := net.Dial("tcp", cmd.addr+":"+strconv.Itoa(cmd.port))
if err != nil {
return nil, err
}
return &client{
cmd: cmd,
conn: conn,
}, nil
}
func (client *client) write() error {
if err := client.conn.SetWriteDeadline(time.Now().Add(10 * time.Second)); err != nil {
client.conn.Close()
return err
}
client.conn.Write(handshake(client.cmd.addr, client.cmd.port, client.cmd.version))
client.conn.Write([]byte{0x01, 0x00})
return nil
}
func (client *client) read() (*pb.Response, error) {
var response pb.Response
if err := client.conn.SetReadDeadline(time.Now().Add(10 * time.Second)); err != nil {
client.conn.Close()
return nil, err
}
recvBuf := make([]byte, 512)
n, err := client.conn.Read(recvBuf)
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
client.conn.Close()
return nil, err
}
client.conn.Close()
return nil, err
}
b := bytes.Split(recvBuf[:n], []byte("{"))
ne := bytes.SplitAfterN(recvBuf[:n], b[0], 2)
trim := bytes.TrimSuffix(ne[1], []byte("\x00"))
js := &jsonpb.Unmarshaler{AllowUnknownFields: true}
if err := js.Unmarshal(bytes.NewBuffer(trim), &response); err != nil {
return nil, err
}
return &response, nil
}
func (client *client) pingServer() time.Duration {
ping := make([]byte, 1)
start := time.Now()
client.conn.Write([]byte{0x01, 0x00})
_, _ = client.conn.Read(ping[:])
diff := time.Now().Sub(start)
return diff
}
func packetlength(b ...[]byte) (length int) {
for _, bytes := range b {
length += len(bytes)
}
return length
}
func handshake(addr string, port int, ver uint64) []byte {
id := []byte{0x00}
state := []byte{0x01}
version := make([]byte, 2)
binary.PutUvarint(version, ver)
p := make([]byte, 2)
binary.BigEndian.PutUint16(p, uint16(port))
length := packetlength(id, version, []byte(addr), []byte(p), state) + 1
var handshake bytes.Buffer
handshake.WriteByte(byte(length))
handshake.Write(id)
handshake.Write(version)
handshake.WriteByte(byte(len(addr)))
handshake.WriteString(addr)
handshake.Write(p)
handshake.Write(state)
return handshake.Bytes()
}

8
go.mod Normal file
View File

@ -0,0 +1,8 @@
module git.0cd.xyz/michael/mcstatus
go 1.15
require (
github.com/golang/protobuf v1.4.3
google.golang.org/protobuf v1.25.0
)

224
main.go
View File

@ -1,198 +1,66 @@
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"flag"
"fmt"
"net"
"os"
"strconv"
"time"
)
func packetlength(b ...[]byte) (length int) {
for _, bytes := range b {
length += len(bytes)
}
return length
}
func handshake(addr string, port int, ver uint64) []byte {
id := []byte{0x00}
state := []byte{0x01}
version := make([]byte, 2)
binary.PutUvarint(version, ver)
p := make([]byte, 2)
binary.BigEndian.PutUint16(p, uint16(port))
length := packetlength(id, version, []byte(addr), []byte(p), state) + 1
var handshake bytes.Buffer
handshake.WriteByte(byte(length))
handshake.Write(id)
handshake.Write(version)
handshake.WriteByte(byte(len(addr)))
handshake.WriteString(addr)
handshake.Write(p)
handshake.Write(state)
return handshake.Bytes()
}
func pingServer(conn net.Conn) time.Duration {
ping := make([]byte, 1)
start := time.Now()
conn.Write([]byte{0x01, 0x00})
_, _ = conn.Read(ping[:])
diff := time.Now().Sub(start)
return diff
}
func orig() error {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
addr := flag.String("addr", "127.0.0.1", "Server address")
port := flag.Int("port", 25565, "Server Port")
ver := flag.Uint64("ver", 751, "Minecraft protocol version number")
raw := flag.Bool("raw", false, "Prints raw json")
ping := flag.Bool("ping", false, "Pings the server")
flag.Parse()
/*if len(os.Args) < 2 {
flag.Usage()
return
}*/
conn, err := net.Dial("tcp", *addr+":"+strconv.Itoa(*port))
if err != nil {
return err
}
for {
err := conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
if err != nil {
conn.Close()
return err
}
conn.Write(handshake(*addr, *port, *ver))
conn.Write([]byte{0x01, 0x00})
err = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil {
conn.Close()
return err
}
recvBuf := make([]byte, 512)
var resp response
n, err := conn.Read(recvBuf)
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
conn.Close()
return err
}
conn.Close()
return err
}
b := bytes.Split(recvBuf[:n], []byte("{"))
ne := bytes.SplitAfterN(recvBuf[:n], b[0], 2)
trim := bytes.TrimSuffix(ne[1], []byte("\x00"))
if *ping == false {
if err := json.Unmarshal(trim, &resp); err != nil {
conn.Close()
return err
}
if *raw == true {
json, err := json.MarshalIndent(resp, "", " ")
if err != nil {
conn.Close()
return err
}
fmt.Printf("%s\n", string(json))
conn.Close()
return err
}
fmt.Printf("Name: %s\nPlayers: %d/%d\nVersion: %s\n",
resp.Description.Text,
resp.Players.Online,
resp.Players.Max,
resp.Version.Name)
if resp.Players.Online >= 1 {
fmt.Println("Online:")
for _, player := range resp.Players.Sample {
fmt.Printf("\t%s\n", player.Name)
}
}
}
fmt.Printf("Ping: %+v\n", pingServer(conn))
if err = conn.Close(); err != nil {
conn.Close()
return err
}
return nil
}
}
func main() {
if err := orig(); err != nil {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run() error {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
addr := flag.String("addr", "127.0.0.1", "Server address")
port := flag.Int("port", 25565, "Server Port")
//ver := flag.Uint64("ver", 751, "Minecraft protocol version number")
flag.Parse()
conn, err := net.Dial("tcp", *addr+":"+strconv.Itoa(*port))
cmd := ui()
conn, err := newConn(cmd)
if err != nil {
return err
}
/*conn.Write(handshake(*addr, *port, *ver))
conn.Write([]byte{0x01, 0x00})*/
recvBuf := make([]byte, 512)
n, err := conn.Read(recvBuf)
if err != nil {
return err
for {
if err := conn.write(); err != nil {
return err
}
resp, err := conn.read()
if err != nil {
return err
}
if cmd.ping {
fmt.Printf("Ping: %+v\n", conn.pingServer())
if err = conn.conn.Close(); err != nil {
conn.conn.Close()
return err
}
return nil
}
if cmd.raw {
json, err := json.MarshalIndent(&resp, "", " ")
if err != nil {
conn.conn.Close()
return err
}
fmt.Printf("%s\n", string(json))
conn.conn.Close()
return err
}
fmt.Printf("Name: %s\nPlayers: %d/%d\nVersion: %s\n",
resp.Description.Text,
resp.Players.Online,
resp.Players.Max,
resp.Version.Name)
if resp.Players.Online >= 1 {
fmt.Println("Online:")
for _, player := range resp.Players.Sample {
fmt.Printf("\t%s\n", player.Name)
}
}
fmt.Printf("Ping: %+v\n", conn.pingServer())
if err = conn.conn.Close(); err != nil {
conn.conn.Close()
return err
}
return nil
}
conn.Write([]byte{0x00})
fmt.Println(string(recvBuf[:n]))
return nil
}
type response struct {
Version struct {
Name string `json:"name"`
Protocol int `json:"protocol"`
} `json:"version"`
Players struct {
Max int `json:"max"`
Online int `json:"online"`
Sample []struct {
Name string `json:"name"`
ID string `json:"id"`
} `json:"sample"`
} `json:"players"`
Description struct {
Text string `json:"text"`
} `json:"description"`
Favicon string `json:"favicon"`
}

473
pb/response.pb.go Normal file
View File

@ -0,0 +1,473 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0-devel
// protoc v3.7.1
// source: response.proto
package pb
import (
proto "github.com/golang/protobuf/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// This is a compile-time assertion that a sufficiently up-to-date version
// of the legacy proto package is being used.
const _ = proto.ProtoPackageIsVersion4
type Response struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Version *Response_Version `protobuf:"bytes,1,opt,name=version,proto3" json:"version,omitempty"`
Players *Response_Players `protobuf:"bytes,2,opt,name=players,proto3" json:"players,omitempty"`
Description *Response_Description `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"`
Favicon string `protobuf:"bytes,4,opt,name=favicon,proto3" json:"favicon,omitempty"`
}
func (x *Response) Reset() {
*x = Response{}
if protoimpl.UnsafeEnabled {
mi := &file_response_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Response) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Response) ProtoMessage() {}
func (x *Response) ProtoReflect() protoreflect.Message {
mi := &file_response_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Response.ProtoReflect.Descriptor instead.
func (*Response) Descriptor() ([]byte, []int) {
return file_response_proto_rawDescGZIP(), []int{0}
}
func (x *Response) GetVersion() *Response_Version {
if x != nil {
return x.Version
}
return nil
}
func (x *Response) GetPlayers() *Response_Players {
if x != nil {
return x.Players
}
return nil
}
func (x *Response) GetDescription() *Response_Description {
if x != nil {
return x.Description
}
return nil
}
func (x *Response) GetFavicon() string {
if x != nil {
return x.Favicon
}
return ""
}
type Response_Version struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Protocol int32 `protobuf:"varint,2,opt,name=protocol,proto3" json:"protocol,omitempty"`
}
func (x *Response_Version) Reset() {
*x = Response_Version{}
if protoimpl.UnsafeEnabled {
mi := &file_response_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Response_Version) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Response_Version) ProtoMessage() {}
func (x *Response_Version) ProtoReflect() protoreflect.Message {
mi := &file_response_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Response_Version.ProtoReflect.Descriptor instead.
func (*Response_Version) Descriptor() ([]byte, []int) {
return file_response_proto_rawDescGZIP(), []int{0, 0}
}
func (x *Response_Version) GetName() string {
if x != nil {
return x.Name
}
return ""
}
func (x *Response_Version) GetProtocol() int32 {
if x != nil {
return x.Protocol
}
return 0
}
type Response_Players struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Max int32 `protobuf:"varint,1,opt,name=max,proto3" json:"max,omitempty"`
Online int32 `protobuf:"varint,2,opt,name=online,proto3" json:"online,omitempty"`
Sample []*Response_Players_Sample `protobuf:"bytes,3,rep,name=sample,proto3" json:"sample,omitempty"`
}
func (x *Response_Players) Reset() {
*x = Response_Players{}
if protoimpl.UnsafeEnabled {
mi := &file_response_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Response_Players) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Response_Players) ProtoMessage() {}
func (x *Response_Players) ProtoReflect() protoreflect.Message {
mi := &file_response_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Response_Players.ProtoReflect.Descriptor instead.
func (*Response_Players) Descriptor() ([]byte, []int) {
return file_response_proto_rawDescGZIP(), []int{0, 1}
}
func (x *Response_Players) GetMax() int32 {
if x != nil {
return x.Max
}
return 0
}
func (x *Response_Players) GetOnline() int32 {
if x != nil {
return x.Online
}
return 0
}
func (x *Response_Players) GetSample() []*Response_Players_Sample {
if x != nil {
return x.Sample
}
return nil
}
type Response_Description struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Text string `protobuf:"bytes,1,opt,name=text,proto3" json:"text,omitempty"`
}
func (x *Response_Description) Reset() {
*x = Response_Description{}
if protoimpl.UnsafeEnabled {
mi := &file_response_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Response_Description) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Response_Description) ProtoMessage() {}
func (x *Response_Description) ProtoReflect() protoreflect.Message {
mi := &file_response_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Response_Description.ProtoReflect.Descriptor instead.
func (*Response_Description) Descriptor() ([]byte, []int) {
return file_response_proto_rawDescGZIP(), []int{0, 2}
}
func (x *Response_Description) GetText() string {
if x != nil {
return x.Text
}
return ""
}
type Response_Players_Sample struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Id string `protobuf:"bytes,2,opt,name=id,proto3" json:"id,omitempty"`
}
func (x *Response_Players_Sample) Reset() {
*x = Response_Players_Sample{}
if protoimpl.UnsafeEnabled {
mi := &file_response_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *Response_Players_Sample) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*Response_Players_Sample) ProtoMessage() {}
func (x *Response_Players_Sample) ProtoReflect() protoreflect.Message {
mi := &file_response_proto_msgTypes[4]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use Response_Players_Sample.ProtoReflect.Descriptor instead.
func (*Response_Players_Sample) Descriptor() ([]byte, []int) {
return file_response_proto_rawDescGZIP(), []int{0, 1, 0}
}
func (x *Response_Players_Sample) GetName() string {
if x != nil {
return x.Name
}
return ""
}
func (x *Response_Players_Sample) GetId() string {
if x != nil {
return x.Id
}
return ""
}
var File_response_proto protoreflect.FileDescriptor
var file_response_proto_rawDesc = []byte{
0x0a, 0x0e, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x12, 0x02, 0x70, 0x62, 0x22, 0xb7, 0x03, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x12, 0x2e, 0x0a, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01,
0x28, 0x0b, 0x32, 0x14, 0x2e, 0x70, 0x62, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
0x2e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f,
0x6e, 0x12, 0x2e, 0x0a, 0x07, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x18, 0x02, 0x20, 0x01,
0x28, 0x0b, 0x32, 0x14, 0x2e, 0x70, 0x62, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
0x2e, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x52, 0x07, 0x70, 0x6c, 0x61, 0x79, 0x65, 0x72,
0x73, 0x12, 0x3a, 0x0a, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e,
0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x70, 0x62, 0x2e, 0x52, 0x65, 0x73, 0x70,
0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e,
0x52, 0x0b, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x18, 0x0a,
0x07, 0x66, 0x61, 0x76, 0x69, 0x63, 0x6f, 0x6e, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07,
0x66, 0x61, 0x76, 0x69, 0x63, 0x6f, 0x6e, 0x1a, 0x39, 0x0a, 0x07, 0x56, 0x65, 0x72, 0x73, 0x69,
0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63,
0x6f, 0x6c, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63,
0x6f, 0x6c, 0x1a, 0x96, 0x01, 0x0a, 0x07, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x12, 0x10,
0x0a, 0x03, 0x6d, 0x61, 0x78, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x6d, 0x61, 0x78,
0x12, 0x16, 0x0a, 0x06, 0x6f, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05,
0x52, 0x06, 0x6f, 0x6e, 0x6c, 0x69, 0x6e, 0x65, 0x12, 0x33, 0x0a, 0x06, 0x73, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x70, 0x62, 0x2e, 0x52, 0x65,
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x2e, 0x50, 0x6c, 0x61, 0x79, 0x65, 0x72, 0x73, 0x2e, 0x53,
0x61, 0x6d, 0x70, 0x6c, 0x65, 0x52, 0x06, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x1a, 0x2c, 0x0a,
0x06, 0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18,
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69,
0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x1a, 0x21, 0x0a, 0x0b, 0x44,
0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65,
0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x42, 0x21,
0x5a, 0x1f, 0x67, 0x69, 0x74, 0x2e, 0x30, 0x63, 0x64, 0x2e, 0x78, 0x79, 0x7a, 0x2f, 0x6d, 0x69,
0x63, 0x68, 0x61, 0x65, 0x6c, 0x2f, 0x6d, 0x63, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x2f, 0x70,
0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_response_proto_rawDescOnce sync.Once
file_response_proto_rawDescData = file_response_proto_rawDesc
)
func file_response_proto_rawDescGZIP() []byte {
file_response_proto_rawDescOnce.Do(func() {
file_response_proto_rawDescData = protoimpl.X.CompressGZIP(file_response_proto_rawDescData)
})
return file_response_proto_rawDescData
}
var file_response_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
var file_response_proto_goTypes = []interface{}{
(*Response)(nil), // 0: pb.Response
(*Response_Version)(nil), // 1: pb.Response.Version
(*Response_Players)(nil), // 2: pb.Response.Players
(*Response_Description)(nil), // 3: pb.Response.Description
(*Response_Players_Sample)(nil), // 4: pb.Response.Players.Sample
}
var file_response_proto_depIdxs = []int32{
1, // 0: pb.Response.version:type_name -> pb.Response.Version
2, // 1: pb.Response.players:type_name -> pb.Response.Players
3, // 2: pb.Response.description:type_name -> pb.Response.Description
4, // 3: pb.Response.Players.sample:type_name -> pb.Response.Players.Sample
4, // [4:4] is the sub-list for method output_type
4, // [4:4] is the sub-list for method input_type
4, // [4:4] is the sub-list for extension type_name
4, // [4:4] is the sub-list for extension extendee
0, // [0:4] is the sub-list for field type_name
}
func init() { file_response_proto_init() }
func file_response_proto_init() {
if File_response_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_response_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Response); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_response_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Response_Version); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_response_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Response_Players); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_response_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Response_Description); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_response_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*Response_Players_Sample); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_response_proto_rawDesc,
NumEnums: 0,
NumMessages: 5,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_response_proto_goTypes,
DependencyIndexes: file_response_proto_depIdxs,
MessageInfos: file_response_proto_msgTypes,
}.Build()
File_response_proto = out.File
file_response_proto_rawDesc = nil
file_response_proto_goTypes = nil
file_response_proto_depIdxs = nil
}

28
response.proto Normal file
View File

@ -0,0 +1,28 @@
syntax="proto3";
package pb;
option go_package = "git.0cd.xyz/michael/mcstatus/pb";
message Response {
message Version {
string name = 1;
int32 protocol = 2;
}
Version version = 1;
message Players {
int32 max = 1;
int32 online = 2;
message Sample {
string name = 1;
string id = 2;
}
repeated Sample sample = 3;
}
Players players = 2;
message Description {
string text = 1;
}
Description description = 3;
string favicon = 4;
}

35
ui.go Normal file
View File

@ -0,0 +1,35 @@
package main
import (
"flag"
"fmt"
"os"
)
type cmd struct {
addr string
port int
version uint64
raw bool
ping bool
}
func ui() *cmd {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
addr := flag.String("addr", "127.0.0.1", "Server address")
port := flag.Int("port", 25565, "Server Port")
version := flag.Uint64("ver", 751, "Minecraft protocol version number")
raw := flag.Bool("raw", false, "Prints raw json")
ping := flag.Bool("ping", false, "Pings the server")
flag.Parse()
return &cmd{
addr: *addr,
port: *port,
version: *version,
raw: *raw,
ping: *ping,
}
}