Add SRV record to response data
This commit is contained in:
parent
247004f478
commit
8b6d648cae
@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"main/src/assets"
|
"main/src/assets"
|
||||||
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -35,13 +36,14 @@ type JavaStatusResponse struct {
|
|||||||
|
|
||||||
// JavaStatus is the status response properties for Java Edition.
|
// JavaStatus is the status response properties for Java Edition.
|
||||||
type JavaStatus struct {
|
type JavaStatus struct {
|
||||||
Version *JavaVersion `json:"version"`
|
Version *JavaVersion `json:"version"`
|
||||||
Players JavaPlayers `json:"players"`
|
Players JavaPlayers `json:"players"`
|
||||||
MOTD MOTD `json:"motd"`
|
MOTD MOTD `json:"motd"`
|
||||||
Icon *string `json:"icon"`
|
Icon *string `json:"icon"`
|
||||||
Mods []Mod `json:"mods"`
|
Mods []Mod `json:"mods"`
|
||||||
Software *string `json:"software"`
|
Software *string `json:"software"`
|
||||||
Plugins []Plugin `json:"plugins"`
|
Plugins []Plugin `json:"plugins"`
|
||||||
|
SRVRecord *SRVRecord `json:"srv_record"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// BedrockStatusResponse is the combined response of the root response and the Bedrock Edition status response.
|
// BedrockStatusResponse is the combined response of the root response and the Bedrock Edition status response.
|
||||||
@ -114,6 +116,12 @@ type Plugin struct {
|
|||||||
Version *string `json:"version"`
|
Version *string `json:"version"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SRVRecord is the result of the SRV lookup performed during status retrieval
|
||||||
|
type SRVRecord struct {
|
||||||
|
Host string `json:"host"`
|
||||||
|
Port uint16 `json:"port"`
|
||||||
|
}
|
||||||
|
|
||||||
// GetJavaStatus returns the status response of a Java Edition server, either using cache or fetching a fresh status.
|
// GetJavaStatus returns the status response of a Java Edition server, either using cache or fetching a fresh status.
|
||||||
func GetJavaStatus(host string, port uint16, opts *StatusOptions) (*JavaStatusResponse, time.Duration, error) {
|
func GetJavaStatus(host string, port uint16, opts *StatusOptions) (*JavaStatusResponse, time.Duration, error) {
|
||||||
cacheKey := GetCacheKey(host, port, opts)
|
cacheKey := GetCacheKey(host, port, opts)
|
||||||
@ -260,6 +268,8 @@ func GetServerIcon(host string, port uint16, opts *StatusOptions) ([]byte, time.
|
|||||||
|
|
||||||
// FetchJavaStatus fetches fresh information about a Java Edition Minecraft server.
|
// FetchJavaStatus fetches fresh information about a Java Edition Minecraft server.
|
||||||
func FetchJavaStatus(host string, port uint16, opts *StatusOptions) JavaStatusResponse {
|
func FetchJavaStatus(host string, port uint16, opts *StatusOptions) JavaStatusResponse {
|
||||||
|
srvRecord, _ := mcutil.LookupSRV("tcp", host, port)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
@ -285,10 +295,17 @@ func FetchJavaStatus(host string, port uint16, opts *StatusOptions) JavaStatusRe
|
|||||||
// Status
|
// Status
|
||||||
{
|
{
|
||||||
go func() {
|
go func() {
|
||||||
statusResult, _ = mcutil.Status(statusContext, host, port, options.JavaStatus{
|
if srvRecord != nil {
|
||||||
EnableSRV: true,
|
statusResult, _ = mcutil.Status(statusContext, srvRecord.Target, srvRecord.Port, options.JavaStatus{
|
||||||
Timeout: opts.Timeout - time.Millisecond*100,
|
EnableSRV: false,
|
||||||
})
|
Timeout: opts.Timeout - time.Millisecond*100,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
statusResult, _ = mcutil.Status(statusContext, host, port, options.JavaStatus{
|
||||||
|
EnableSRV: false,
|
||||||
|
Timeout: opts.Timeout - time.Millisecond*100,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
|
||||||
@ -305,10 +322,17 @@ func FetchJavaStatus(host string, port uint16, opts *StatusOptions) JavaStatusRe
|
|||||||
// Legacy Status
|
// Legacy Status
|
||||||
{
|
{
|
||||||
go func() {
|
go func() {
|
||||||
legacyStatusResult, _ = mcutil.StatusLegacy(legacyContext, host, port, options.JavaStatusLegacy{
|
if srvRecord != nil {
|
||||||
EnableSRV: true,
|
legacyStatusResult, _ = mcutil.StatusLegacy(legacyContext, srvRecord.Target, srvRecord.Port, options.JavaStatusLegacy{
|
||||||
Timeout: opts.Timeout - time.Millisecond*100,
|
EnableSRV: false,
|
||||||
})
|
Timeout: opts.Timeout - time.Millisecond*100,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
legacyStatusResult, _ = mcutil.StatusLegacy(legacyContext, host, port, options.JavaStatusLegacy{
|
||||||
|
EnableSRV: false,
|
||||||
|
Timeout: opts.Timeout - time.Millisecond*100,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
|
||||||
@ -333,7 +357,7 @@ func FetchJavaStatus(host string, port uint16, opts *StatusOptions) JavaStatusRe
|
|||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
return BuildJavaResponse(host, port, statusResult, legacyStatusResult, queryResult)
|
return BuildJavaResponse(host, port, statusResult, legacyStatusResult, queryResult, srvRecord)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchBedrockStatus fetches a fresh status of a Bedrock Edition server.
|
// FetchBedrockStatus fetches a fresh status of a Bedrock Edition server.
|
||||||
@ -348,7 +372,7 @@ func FetchBedrockStatus(host string, port uint16, opts *StatusOptions) BedrockSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BuildJavaResponse builds the response data from the status and query information.
|
// BuildJavaResponse builds the response data from the status and query information.
|
||||||
func BuildJavaResponse(host string, port uint16, status *response.JavaStatus, legacyStatus *response.JavaStatusLegacy, query *response.FullQuery) (result JavaStatusResponse) {
|
func BuildJavaResponse(host string, port uint16, status *response.JavaStatus, legacyStatus *response.JavaStatusLegacy, query *response.FullQuery, srvRecord *net.SRV) (result JavaStatusResponse) {
|
||||||
result = JavaStatusResponse{
|
result = JavaStatusResponse{
|
||||||
BaseStatus: BaseStatus{
|
BaseStatus: BaseStatus{
|
||||||
Online: false,
|
Online: false,
|
||||||
@ -529,6 +553,13 @@ func BuildJavaResponse(host string, port uint16, status *response.JavaStatus, le
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if result.JavaStatus != nil && srvRecord != nil {
|
||||||
|
result.SRVRecord = &SRVRecord{
|
||||||
|
Host: strings.Trim(srvRecord.Target, "."),
|
||||||
|
Port: srvRecord.Port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user