Skip to content

Commit

Permalink
Fix small bug, clean up variable declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
PassTheMayo committed Jul 18, 2024
1 parent 88db267 commit 9323756
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
11 changes: 6 additions & 5 deletions status/bedrock.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ func Bedrock(ctx context.Context, host string, options ...options.StatusBedrock)
}

func getStatusBedrock(host string, options ...options.StatusBedrock) (*response.StatusBedrock, error) {
opts := parseBedrockStatusOptions(options...)
var (
opts = parseBedrockStatusOptions(options...)
connectionPort uint16 = uint16(util.DefaultBedrockPort)
)

connectionPort := uint16(util.DefaultBedrockPort)

connectionHost, port, err := util.ParseAddress(host)
connectionHostname, port, err := util.ParseAddress(host)

if err != nil {
return nil, err
Expand All @@ -71,7 +72,7 @@ func getStatusBedrock(host string, options ...options.StatusBedrock) (*response.
connectionPort = *port
}

conn, err := net.DialTimeout("udp", fmt.Sprintf("%s:%d", connectionHost, connectionPort), opts.Timeout)
conn, err := net.DialTimeout("udp", fmt.Sprintf("%s:%d", connectionHostname, connectionPort), opts.Timeout)

if err != nil {
return nil, err
Expand Down
11 changes: 5 additions & 6 deletions status/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,23 @@ func Legacy(ctx context.Context, host string, options ...options.StatusLegacy) (
}

func getStatusLegacy(host string, options ...options.StatusLegacy) (*response.StatusLegacy, error) {
opts := parseJavaStatusLegacyOptions(options...)

var (
opts = parseJavaStatusLegacyOptions(options...)
connectionPort uint16 = util.DefaultJavaPort
srvRecord *response.SRVRecord = nil
)

connectionHost, port, err := util.ParseAddress(host)
connectionHostname, port, err := util.ParseAddress(host)

if err != nil {
return nil, err
}

if opts.EnableSRV && port == nil && net.ParseIP(connectionHost) == nil {
if opts.EnableSRV && port == nil && net.ParseIP(connectionHostname) == nil {
record, err := util.LookupSRV(host)

if err == nil && record != nil {
connectionHost = record.Target
connectionHostname = record.Target
connectionPort = record.Port

srvRecord = &response.SRVRecord{
Expand All @@ -80,7 +79,7 @@ func getStatusLegacy(host string, options ...options.StatusLegacy) (*response.St
}
}

conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", connectionHost, connectionPort), opts.Timeout)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", connectionHostname, connectionPort), opts.Timeout)

if err != nil {
return nil, err
Expand Down
13 changes: 6 additions & 7 deletions status/modern.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,25 @@ func Modern(ctx context.Context, host string, options ...options.StatusModern) (
}

func getStatusModern(host string, options ...options.StatusModern) (*response.StatusModern, error) {
opts := parseJavaStatusOptions(options...)

var (
opts = parseJavaStatusOptions(options...)
connectionPort uint16 = util.DefaultJavaPort
srvRecord *response.SRVRecord = nil
rawResponse rawJavaStatus = rawJavaStatus{}
latency time.Duration = 0
)

connectionHost, port, err := util.ParseAddress(host)
connectionHostname, port, err := util.ParseAddress(host)

if err != nil {
return nil, err
}

if opts.EnableSRV && port == nil && net.ParseIP(connectionHost) == nil {
if opts.EnableSRV && port == nil && net.ParseIP(connectionHostname) == nil {
record, err := util.LookupSRV(host)

if err == nil && record != nil {
connectionHost = record.Target
connectionHostname = record.Target
connectionPort = record.Port

srvRecord = &response.SRVRecord{
Expand All @@ -120,7 +119,7 @@ func getStatusModern(host string, options ...options.StatusModern) (*response.St
}
}

conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", connectionHost, connectionPort), opts.Timeout)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", connectionHostname, connectionPort), opts.Timeout)

if err != nil {
return nil, err
Expand All @@ -132,7 +131,7 @@ func getStatusModern(host string, options ...options.StatusModern) (*response.St
return nil, err
}

if err = writeJavaStatusHandshakeRequestPacket(conn, int32(opts.ProtocolVersion), host, 0); err != nil {
if err = writeJavaStatusHandshakeRequestPacket(conn, int32(opts.ProtocolVersion), connectionHostname, connectionPort); err != nil {
return nil, err
}

Expand Down
15 changes: 6 additions & 9 deletions status/modern_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,29 @@ func ModernRaw(ctx context.Context, host string, options ...options.StatusModern
}

func getStatusRaw(host string, options ...options.StatusModern) (map[string]interface{}, error) {
opts := parseJavaStatusOptions(options...)

var (
opts = parseJavaStatusOptions(options...)
connectionPort uint16 = util.DefaultJavaPort
result map[string]interface{} = make(map[string]interface{})
payload int64 = rand.Int63()
)

// Parse the provided host into a split hostname and port.
connectionHost, port, err := util.ParseAddress(host)
connectionHostname, port, err := util.ParseAddress(host)

if err != nil {
return nil, err
}

// Perform an SRV lookup on the
if opts.EnableSRV && port == nil && net.ParseIP(connectionHost) == nil {
if opts.EnableSRV && port == nil && net.ParseIP(connectionHostname) == nil {
record, err := util.LookupSRV(host)

if err == nil && record != nil {
connectionHost = record.Target
connectionHostname = record.Target
connectionPort = record.Port
}
}

conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", connectionHost, connectionPort), opts.Timeout)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", connectionHostname, connectionPort), opts.Timeout)

if err != nil {
return nil, err
Expand All @@ -78,7 +75,7 @@ func getStatusRaw(host string, options ...options.StatusModern) (map[string]inte
return nil, err
}

if err = writeJavaStatusHandshakeRequestPacket(conn, int32(opts.ProtocolVersion), host, connectionPort); err != nil {
if err = writeJavaStatusHandshakeRequestPacket(conn, int32(opts.ProtocolVersion), connectionHostname, connectionPort); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion status/modern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestModern(t *testing.T) {
resp, err := status.Modern(context.Background(), "demo.mcstatus.io")
resp, err := status.Modern(context.Background(), "play.cobbletwo.com")

if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 9323756

Please sign in to comment.