Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge v4 #11

Merged
merged 2 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"strconv"
"time"

"github.com/jessevdk/go-flags"
Expand All @@ -14,6 +15,7 @@ import (

var (
host string
port uint16
opts passedOptions = passedOptions{}
)

Expand Down Expand Up @@ -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() {
Expand All @@ -52,14 +83,14 @@ 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()

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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
})

Expand Down
41 changes: 8 additions & 33 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions rcon/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 5 additions & 19 deletions status/bedrock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion status/bedrock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 9 additions & 14 deletions status/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading
Loading