From b45b20161919d54f2733539af5ae2c360e090582 Mon Sep 17 00:00:00 2001 From: Jacob Gunther Date: Sat, 20 Jul 2024 10:38:45 -0500 Subject: [PATCH 1/2] Add extra duration to cmd context timeout --- cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index 6233274..922b66e 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -52,7 +52,7 @@ func main() { err error ) - ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(opts.Timeout)) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(opts.Timeout)+time.Millisecond*500) defer cancel() From f16693f600ea3c6c1363a3eb3300655e08a1d07e Mon Sep 17 00:00:00 2001 From: Jacob Gunther Date: Sat, 10 Aug 2024 09:39:28 -0500 Subject: [PATCH 2/2] Add port argument back to methods --- cmd/main.go | 39 +++++++++++++++++++++++++++++++++---- query/query.go | 41 ++++++++------------------------------- query/query_test.go | 5 +++-- rcon/client.go | 4 ++-- status/bedrock.go | 24 +++++------------------ status/bedrock_test.go | 3 ++- status/legacy.go | 23 +++++++++------------- status/modern.go | 36 ++++++++++------------------------ status/modern_raw.go | 25 ++++++++++-------------- status/modern_raw_test.go | 3 ++- status/modern_test.go | 6 +++++- status/util.go | 8 -------- 12 files changed, 91 insertions(+), 126 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 922b66e..8ed183f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "os" + "strconv" "time" "github.com/jessevdk/go-flags" @@ -14,6 +15,7 @@ import ( var ( host string + port uint16 opts passedOptions = passedOptions{} ) @@ -44,6 +46,35 @@ func init() { } 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() { @@ -59,7 +90,7 @@ func main() { switch opts.Type { case "java": { - result, err = status.Modern(ctx, host, options.StatusModern{ + result, err = status.Modern(ctx, host, port, options.StatusModern{ EnableSRV: !opts.DisableSRV, Timeout: time.Duration(opts.Timeout) * time.Second, ProtocolVersion: 47, @@ -71,7 +102,7 @@ func main() { } case "raw": { - result, err = status.ModernRaw(ctx, host, options.StatusModern{ + result, err = status.ModernRaw(ctx, host, port, options.StatusModern{ EnableSRV: !opts.DisableSRV, Timeout: time.Duration(opts.Timeout) * time.Second, ProtocolVersion: 47, @@ -81,7 +112,7 @@ func main() { } case "legacy": { - result, err = status.Legacy(ctx, host, options.StatusLegacy{ + result, err = status.Legacy(ctx, host, port, options.StatusLegacy{ EnableSRV: !opts.DisableSRV, Timeout: time.Duration(opts.Timeout) * time.Second, ProtocolVersion: 47, @@ -91,7 +122,7 @@ func main() { } case "bedrock": { - result, err = status.Bedrock(ctx, host, options.StatusBedrock{ + result, err = status.Bedrock(ctx, host, port, options.StatusBedrock{ Timeout: time.Duration(opts.Timeout) * time.Second, }) diff --git a/query/query.go b/query/query.go index 55941e2..75998d1 100644 --- a/query/query.go +++ b/query/query.go @@ -15,7 +15,6 @@ import ( "github.com/mcstatus-io/mcutil/v4/formatting" "github.com/mcstatus-io/mcutil/v4/options" "github.com/mcstatus-io/mcutil/v4/response" - "github.com/mcstatus-io/mcutil/v4/util" ) var ( @@ -27,12 +26,12 @@ var ( ) // Basic runs a query on the server and returns basic information. -func Basic(ctx context.Context, host string, options ...options.Query) (*response.QueryBasic, error) { +func Basic(ctx context.Context, hostname string, port uint16, options ...options.Query) (*response.QueryBasic, error) { r := make(chan *response.QueryBasic, 1) e := make(chan error, 1) go func() { - result, err := performBasicQuery(host, options...) + result, err := performBasicQuery(hostname, port, options...) if err != nil { e <- err @@ -56,12 +55,12 @@ func Basic(ctx context.Context, host string, options ...options.Query) (*respons } // Full runs a query on the server and returns the full information. -func Full(ctx context.Context, host string, options ...options.Query) (*response.QueryFull, error) { +func Full(ctx context.Context, hostname string, port uint16, options ...options.Query) (*response.QueryFull, error) { r := make(chan *response.QueryFull, 1) e := make(chan error, 1) go func() { - result, err := performFullQuery(host, options...) + result, err := performFullQuery(hostname, port, options...) if err != nil { e <- err @@ -84,22 +83,10 @@ func Full(ctx context.Context, host string, options ...options.Query) (*response } } -func performBasicQuery(host string, options ...options.Query) (*response.QueryBasic, error) { +func performBasicQuery(hostname string, port uint16, options ...options.Query) (*response.QueryBasic, error) { opts := parseQueryOptions(options...) - connectionPort := uint16(util.DefaultJavaPort) - - connectionHost, port, err := util.ParseAddress(host) - - if err != nil { - return nil, err - } - - if port != nil { - connectionPort = *port - } - - conn, err := net.DialTimeout("udp", fmt.Sprintf("%s:%d", connectionHost, connectionPort), opts.Timeout) + conn, err := net.DialTimeout("udp", fmt.Sprintf("%s:%d", hostname, port), opts.Timeout) if err != nil { return nil, err @@ -144,22 +131,10 @@ func performBasicQuery(host string, options ...options.Query) (*response.QueryBa return response, err } -func performFullQuery(host string, options ...options.Query) (*response.QueryFull, error) { +func performFullQuery(hostname string, port uint16, options ...options.Query) (*response.QueryFull, error) { opts := parseQueryOptions(options...) - connectionPort := uint16(util.DefaultJavaPort) - - connectionHost, port, err := util.ParseAddress(host) - - if err != nil { - return nil, err - } - - if port != nil { - connectionPort = *port - } - - conn, err := net.DialTimeout("udp", fmt.Sprintf("%s:%d", connectionHost, connectionPort), opts.Timeout) + conn, err := net.DialTimeout("udp", fmt.Sprintf("%s:%d", hostname, port), opts.Timeout) if err != nil { return nil, err diff --git a/query/query_test.go b/query/query_test.go index 5a8cfa0..8aa90ef 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -5,10 +5,11 @@ import ( "testing" "github.com/mcstatus-io/mcutil/v4/query" + "github.com/mcstatus-io/mcutil/v4/util" ) func TestBasic(t *testing.T) { - resp, err := query.Basic(context.Background(), "demo.mcstatus.io") + resp, err := query.Basic(context.Background(), "demo.mcstatus.io", util.DefaultJavaPort) if err != nil { t.Fatal(err) @@ -18,7 +19,7 @@ func TestBasic(t *testing.T) { } func TestFull(t *testing.T) { - resp, err := query.Full(context.Background(), "demo.mcstatus.io") + resp, err := query.Full(context.Background(), "demo.mcstatus.io", util.DefaultJavaPort) if err != nil { t.Fatal(err) diff --git a/rcon/client.go b/rcon/client.go index 11b3493..de7fa62 100644 --- a/rcon/client.go +++ b/rcon/client.go @@ -40,10 +40,10 @@ type Client struct { } // Dial connects to the server using the address provided and returns a new client. -func Dial(host string, port uint16, options ...options.RCON) (*Client, error) { +func Dial(hostname string, port uint16, options ...options.RCON) (*Client, error) { opts := parseOptions(options...) - conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), opts.Timeout) + conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", hostname, port), opts.Timeout) if err != nil { return nil, err diff --git a/status/bedrock.go b/status/bedrock.go index 64f5962..5680687 100644 --- a/status/bedrock.go +++ b/status/bedrock.go @@ -16,7 +16,6 @@ import ( "github.com/mcstatus-io/mcutil/v4/formatting" "github.com/mcstatus-io/mcutil/v4/options" "github.com/mcstatus-io/mcutil/v4/response" - "github.com/mcstatus-io/mcutil/v4/util" ) var ( @@ -28,12 +27,12 @@ var ( ) // Bedrock retrieves the status of a Bedrock Edition Minecraft server. -func Bedrock(ctx context.Context, host string, options ...options.StatusBedrock) (*response.StatusBedrock, error) { +func Bedrock(ctx context.Context, hostname string, port uint16, options ...options.StatusBedrock) (*response.StatusBedrock, error) { r := make(chan *response.StatusBedrock, 1) e := make(chan error, 1) go func() { - result, err := getStatusBedrock(host, options...) + result, err := getStatusBedrock(hostname, port, options...) if err != nil { e <- err @@ -56,23 +55,10 @@ func Bedrock(ctx context.Context, host string, options ...options.StatusBedrock) } } -func getStatusBedrock(host string, options ...options.StatusBedrock) (*response.StatusBedrock, error) { - var ( - opts = parseBedrockStatusOptions(options...) - connectionPort uint16 = uint16(util.DefaultBedrockPort) - ) +func getStatusBedrock(hostname string, port uint16, options ...options.StatusBedrock) (*response.StatusBedrock, error) { + opts := parseBedrockStatusOptions(options...) - connectionHostname, port, err := util.ParseAddress(host) - - if err != nil { - return nil, err - } - - if port != nil { - connectionPort = *port - } - - conn, err := net.DialTimeout("udp", fmt.Sprintf("%s:%d", connectionHostname, connectionPort), opts.Timeout) + conn, err := net.DialTimeout("udp", fmt.Sprintf("%s:%d", hostname, port), opts.Timeout) if err != nil { return nil, err diff --git a/status/bedrock_test.go b/status/bedrock_test.go index ff312ac..7234e96 100644 --- a/status/bedrock_test.go +++ b/status/bedrock_test.go @@ -5,10 +5,11 @@ import ( "testing" "github.com/mcstatus-io/mcutil/v4/status" + "github.com/mcstatus-io/mcutil/v4/util" ) func TestBedrock(t *testing.T) { - resp, err := status.Bedrock(context.Background(), "demo.mcstatus.io") + resp, err := status.Bedrock(context.Background(), "demo.mcstatus.io", util.DefaultBedrockPort) if err != nil { t.Fatal(err) diff --git a/status/legacy.go b/status/legacy.go index a39898d..2776f5b 100644 --- a/status/legacy.go +++ b/status/legacy.go @@ -24,12 +24,12 @@ var ( ) // Legacy retrieves the status of any Java Edition Minecraft server, but with reduced properties compared to Modern(). -func Legacy(ctx context.Context, host string, options ...options.StatusLegacy) (*response.StatusLegacy, error) { +func Legacy(ctx context.Context, hostname string, port uint16, options ...options.StatusLegacy) (*response.StatusLegacy, error) { r := make(chan *response.StatusLegacy, 1) e := make(chan error, 1) go func() { - result, err := getStatusLegacy(host, options...) + result, err := getStatusLegacy(hostname, port, options...) if err != nil { e <- err @@ -52,21 +52,16 @@ func Legacy(ctx context.Context, host string, options ...options.StatusLegacy) ( } } -func getStatusLegacy(host string, options ...options.StatusLegacy) (*response.StatusLegacy, error) { +func getStatusLegacy(hostname string, port uint16, options ...options.StatusLegacy) (*response.StatusLegacy, error) { var ( - opts = parseJavaStatusLegacyOptions(options...) - connectionPort uint16 = util.DefaultJavaPort - srvRecord *response.SRVRecord = nil + opts = parseJavaStatusLegacyOptions(options...) + connectionHostname = hostname + connectionPort uint16 = port + srvRecord *response.SRVRecord = nil ) - connectionHostname, port, err := util.ParseAddress(host) - - if err != nil { - return nil, err - } - - if opts.EnableSRV && port == nil && net.ParseIP(connectionHostname) == nil { - record, err := util.LookupSRV(host) + if opts.EnableSRV && port == util.DefaultJavaPort && net.ParseIP(connectionHostname) == nil { + record, err := util.LookupSRV(hostname) if err == nil && record != nil { connectionHostname = record.Target diff --git a/status/modern.go b/status/modern.go index de54d27..50c1da7 100644 --- a/status/modern.go +++ b/status/modern.go @@ -64,12 +64,12 @@ type rawJavaStatus struct { } // Modern retrieves the status of any 1.7+ Minecraft server. -func Modern(ctx context.Context, host string, options ...options.StatusModern) (*response.StatusModern, error) { +func Modern(ctx context.Context, hostname string, port uint16, options ...options.StatusModern) (*response.StatusModern, error) { r := make(chan *response.StatusModern, 1) e := make(chan error, 1) go func() { - result, err := getStatusModern(host, options...) + result, err := getStatusModern(hostname, port, options...) if err != nil { e <- err @@ -92,34 +92,18 @@ func Modern(ctx context.Context, host string, options ...options.StatusModern) ( } } -func getStatusModern(host string, options ...options.StatusModern) (*response.StatusModern, error) { +func getStatusModern(hostname string, port uint16, options ...options.StatusModern) (*response.StatusModern, error) { var ( opts = parseJavaStatusOptions(options...) - connectionHostname string = "" - connectionPort uint16 = util.DefaultJavaPort + connectionHostname string = hostname + connectionPort uint16 = port srvRecord *response.SRVRecord = nil rawResponse rawJavaStatus = rawJavaStatus{} latency time.Duration = 0 ) - hostname, port, err := util.ParseAddress(host) - - if err != nil { - return nil, err - } - - connectionHostname = hostname - - if port != nil { - connectionPort = *port - } - - if opts.Debug { - log.Printf("Parsed address into split hostname and port (host=%s, port=%v, default_port=%d)", connectionHostname, nilValueSwap(port, util.DefaultJavaPort), util.DefaultJavaPort) - } - - if opts.EnableSRV && port == nil && net.ParseIP(connectionHostname) == nil { - record, err := util.LookupSRV(host) + if opts.EnableSRV && port == util.DefaultJavaPort && net.ParseIP(connectionHostname) == nil { + record, err := util.LookupSRV(hostname) if err == nil && record != nil { connectionHostname = record.Target @@ -131,7 +115,7 @@ func getStatusModern(host string, options ...options.StatusModern) (*response.St } if opts.Debug { - log.Printf("Found an SRV record (target_host=%s, target_port=%d)", record.Target, record.Port) + log.Printf("Found an SRV record (host=%s, port=%d)", record.Target, record.Port) } } else if opts.Debug { log.Println("Could not find an SRV record for this host") @@ -154,12 +138,12 @@ func getStatusModern(host string, options ...options.StatusModern) (*response.St return nil, err } - if err = writeJavaStatusHandshakePacket(conn, int32(opts.ProtocolVersion), hostname, nilValueSwap(port, util.DefaultJavaPort)); err != nil { + if err = writeJavaStatusHandshakePacket(conn, int32(opts.ProtocolVersion), hostname, port); err != nil { return nil, err } if opts.Debug { - log.Printf("[S <- C] Wrote handshake packet (proto=%d, host=%s, port=%d, next_state=0)\n", opts.ProtocolVersion, hostname, nilValueSwap(port, util.DefaultJavaPort)) + log.Printf("[S <- C] Wrote handshake packet (proto=%d, host=%s, port=%d, next_state=0)\n", opts.ProtocolVersion, hostname, port) } if err = writeJavaStatusStatusRequestPacket(conn); err != nil { diff --git a/status/modern_raw.go b/status/modern_raw.go index 7964f8d..11db736 100644 --- a/status/modern_raw.go +++ b/status/modern_raw.go @@ -12,12 +12,12 @@ import ( ) // ModernRaw returns the raw status data of any 1.7+ Java Edition Minecraft server. -func ModernRaw(ctx context.Context, host string, options ...options.StatusModern) (map[string]interface{}, error) { +func ModernRaw(ctx context.Context, hostname string, port uint16, options ...options.StatusModern) (map[string]interface{}, error) { r := make(chan map[string]interface{}, 1) e := make(chan error, 1) go func() { - result, err := getStatusRaw(host, options...) + result, err := getStatusRaw(hostname, port, options...) if err != nil { e <- err @@ -40,22 +40,17 @@ func ModernRaw(ctx context.Context, host string, options ...options.StatusModern } } -func getStatusRaw(host string, options ...options.StatusModern) (map[string]interface{}, error) { +func getStatusRaw(hostname string, port uint16, options ...options.StatusModern) (map[string]interface{}, error) { var ( - opts = parseJavaStatusOptions(options...) - connectionPort uint16 = util.DefaultJavaPort - result map[string]interface{} = make(map[string]interface{}) - payload int64 = rand.Int63() + opts = parseJavaStatusOptions(options...) + connectionHostname = hostname + connectionPort uint16 = port + result map[string]interface{} = make(map[string]interface{}) + payload int64 = rand.Int63() ) - connectionHostname, port, err := util.ParseAddress(host) - - if err != nil { - return nil, err - } - - if opts.EnableSRV && port == nil && net.ParseIP(connectionHostname) == nil { - record, err := util.LookupSRV(host) + if opts.EnableSRV && port == util.DefaultJavaPort && net.ParseIP(connectionHostname) == nil { + record, err := util.LookupSRV(hostname) if err == nil && record != nil { connectionHostname = record.Target diff --git a/status/modern_raw_test.go b/status/modern_raw_test.go index a909cec..41c0cc3 100644 --- a/status/modern_raw_test.go +++ b/status/modern_raw_test.go @@ -5,10 +5,11 @@ import ( "testing" "github.com/mcstatus-io/mcutil/v4/status" + "github.com/mcstatus-io/mcutil/v4/util" ) func TestModernRaw(t *testing.T) { - resp, err := status.ModernRaw(context.Background(), "demo.mcstatus.io") + resp, err := status.ModernRaw(context.Background(), "demo.mcstatus.io", util.DefaultJavaPort) if err != nil { t.Fatal(err) diff --git a/status/modern_test.go b/status/modern_test.go index 4b54ec8..aae549e 100644 --- a/status/modern_test.go +++ b/status/modern_test.go @@ -4,11 +4,15 @@ import ( "context" "testing" + "github.com/mcstatus-io/mcutil/v4/options" "github.com/mcstatus-io/mcutil/v4/status" + "github.com/mcstatus-io/mcutil/v4/util" ) func TestModern(t *testing.T) { - resp, err := status.Modern(context.Background(), "play.cobbletwo.com") + resp, err := status.Modern(context.Background(), "hypixel.net", util.DefaultJavaPort, options.StatusModern{ + Debug: true, + }) if err != nil { t.Fatal(err) diff --git a/status/util.go b/status/util.go index 1580d1e..2db4703 100644 --- a/status/util.go +++ b/status/util.go @@ -54,11 +54,3 @@ func parsePlayerID(value interface{}) (string, bool) { func pointerOf[T any](v T) *T { return &v } - -func nilValueSwap[T any](a *T, b T) T { - if a == nil { - return b - } - - return *a -}