Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c5dd0c5f4 | |||
| cdf375ba80 | |||
| 39a3ceadbb | |||
| 8d7588ed18 | |||
| d6aceaea18 | |||
|
|
064c6c4464 |
25
.github/workflows/ci.yml
vendored
Normal file
25
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
|
||||
- name: Run tests
|
||||
run: go test ./...
|
||||
|
||||
- name: Build CLI
|
||||
run: go build ./cmd/gamestatus
|
||||
41
.github/workflows/release.yml
vendored
Normal file
41
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
release_ref:
|
||||
description: "Git ref to release, preferably a tag like v1.0.1"
|
||||
required: true
|
||||
type: string
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.release_ref || github.ref }}
|
||||
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
|
||||
- name: Run tests
|
||||
run: go test ./...
|
||||
|
||||
- name: GoReleaser
|
||||
uses: goreleaser/goreleaser-action@v6
|
||||
with:
|
||||
version: latest
|
||||
args: release --clean
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/gamestatus
|
||||
/gamestatus.exe
|
||||
30
.goreleaser.yml
Normal file
30
.goreleaser.yml
Normal file
@@ -0,0 +1,30 @@
|
||||
version: 2
|
||||
|
||||
project_name: gamestatus
|
||||
|
||||
builds:
|
||||
- id: gamestatus
|
||||
main: ./cmd/gamestatus
|
||||
binary: gamestatus
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
goos:
|
||||
- windows
|
||||
- linux
|
||||
- darwin
|
||||
goarch:
|
||||
- amd64
|
||||
- arm64
|
||||
|
||||
archives:
|
||||
- id: default
|
||||
formats: [zip]
|
||||
name_template: >-
|
||||
{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}
|
||||
|
||||
checksum:
|
||||
name_template: checksums.txt
|
||||
|
||||
changelog:
|
||||
use: git
|
||||
sort: asc
|
||||
72
README.md
72
README.md
@@ -1,8 +1,17 @@
|
||||
# mcstatus
|
||||
# gamestatus
|
||||
|
||||
Small Go library for querying Minecraft Java and Bedrock server status.
|
||||
Small Go library for querying game server status.
|
||||
|
||||
The package is extracted from `DuExpBot/backend/pkg/mcstatus`, but without any app-specific model/database layer.
|
||||
Current support:
|
||||
|
||||
- Minecraft Java
|
||||
- Minecraft Bedrock
|
||||
- Source / A2S servers
|
||||
- Rust servers
|
||||
- Counter-Strike 2 servers
|
||||
- Valve/Source servers
|
||||
|
||||
The project is structured so other game protocols can be added later. Rust currently uses the same A2S-based transport layer as Source-style servers.
|
||||
|
||||
## Features
|
||||
|
||||
@@ -10,15 +19,46 @@ The package is extracted from `DuExpBot/backend/pkg/mcstatus`, but without any a
|
||||
- Java full query
|
||||
- Bedrock RakNet ping
|
||||
- Bedrock full query
|
||||
- Source A2S info
|
||||
- Source A2S players and rules via full query
|
||||
- Rust query via the same Source/A2S layer
|
||||
- CS2 query via the same Source/A2S layer
|
||||
- Valve query via the same Source/A2S layer
|
||||
- Optional SRV lookup
|
||||
- Simple MOTD cleanup helper
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
go get github.com/VexoraDevelopment/mcstatus
|
||||
go get github.com/VexoraDevelopment/gamestatus
|
||||
```
|
||||
|
||||
## CLI
|
||||
|
||||
The repository also ships a small CLI in `cmd/gamestatus`.
|
||||
|
||||
Build locally:
|
||||
|
||||
```bash
|
||||
go build -o gamestatus ./cmd/gamestatus
|
||||
```
|
||||
|
||||
Examples:
|
||||
|
||||
```bash
|
||||
gamestatus mc.hypixel.net
|
||||
gamestatus java -json mc.hypixel.net
|
||||
gamestatus bedrock -query play.example.com:19132
|
||||
gamestatus source 127.0.0.1:27015
|
||||
gamestatus rust 127.0.0.1:28015
|
||||
gamestatus cs2 127.0.0.1:27015
|
||||
gamestatus valve 127.0.0.1:27015
|
||||
gamestatus auto -pretty localhost
|
||||
gamestatus java -motd-raw mc.hypixel.net
|
||||
```
|
||||
|
||||
If you prefer prebuilt binaries, use the files attached to GitHub Releases.
|
||||
|
||||
## Quick Start
|
||||
|
||||
```go
|
||||
@@ -28,11 +68,11 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/VexoraDevelopment/mcstatus"
|
||||
"github.com/VexoraDevelopment/gamestatus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
client := mcstatus.NewAll(3 * time.Second)
|
||||
client := gamestatus.NewAll(3 * time.Second)
|
||||
|
||||
info, err := client.Java.Connect("hypixel.net", 25565)
|
||||
if err != nil {
|
||||
@@ -49,12 +89,22 @@ func main() {
|
||||
|
||||
## Packages
|
||||
|
||||
- `github.com/VexoraDevelopment/mcstatus`
|
||||
- `github.com/VexoraDevelopment/mcstatus/java`
|
||||
- `github.com/VexoraDevelopment/mcstatus/bedrock`
|
||||
- `github.com/VexoraDevelopment/mcstatus/text`
|
||||
- `github.com/VexoraDevelopment/gamestatus`
|
||||
- `github.com/VexoraDevelopment/gamestatus/java`
|
||||
- `github.com/VexoraDevelopment/gamestatus/bedrock`
|
||||
- `github.com/VexoraDevelopment/gamestatus/source`
|
||||
- `github.com/VexoraDevelopment/gamestatus/text`
|
||||
|
||||
## Releases
|
||||
|
||||
Pushing a tag like `v1.0.1` triggers GitHub Actions and publishes release archives for:
|
||||
|
||||
- Windows
|
||||
- Linux
|
||||
- macOS
|
||||
|
||||
using the `gamestatus` CLI binary from `cmd/gamestatus`.
|
||||
|
||||
## Notes
|
||||
|
||||
- No GORM or persistence layer is included.
|
||||
- `ServerInfo` is a plain struct and can be stored however you want.
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/VexoraDevelopment/mcstatus/info"
|
||||
"github.com/VexoraDevelopment/gamestatus/info"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
|
||||
20
client.go
20
client.go
@@ -1,20 +1,34 @@
|
||||
package mcstatus
|
||||
package gamestatus
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/VexoraDevelopment/mcstatus/bedrock"
|
||||
"github.com/VexoraDevelopment/mcstatus/java"
|
||||
"github.com/VexoraDevelopment/gamestatus/bedrock"
|
||||
"github.com/VexoraDevelopment/gamestatus/java"
|
||||
"github.com/VexoraDevelopment/gamestatus/source"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Java *java.Client
|
||||
Bedrock *bedrock.Client
|
||||
Source *source.Client
|
||||
}
|
||||
|
||||
func NewAll(timeout time.Duration) *Client {
|
||||
return &Client{
|
||||
Java: java.New(java.Options{Timeout: timeout, EnableSRV: true}),
|
||||
Bedrock: bedrock.New(bedrock.Options{Timeout: timeout, EnableSRV: true}),
|
||||
Source: source.New(source.Options{Timeout: timeout}),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceDebug(debug bool) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
timeout := 2 * time.Second
|
||||
if c.Source != nil {
|
||||
timeout = c.Source.Timeout()
|
||||
}
|
||||
c.Source = source.New(source.Options{Timeout: timeout, Debug: debug})
|
||||
}
|
||||
|
||||
307
cmd/gamestatus/main.go
Normal file
307
cmd/gamestatus/main.go
Normal file
@@ -0,0 +1,307 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/VexoraDevelopment/gamestatus"
|
||||
"github.com/VexoraDevelopment/gamestatus/text"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, target, err := parseCLI(os.Args[1:])
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
if target == "" {
|
||||
printUsage()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
host, port, err := parseTarget(target)
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
client := gamestatus.NewAll(cfg.timeout)
|
||||
client.SetSourceDebug(cfg.debug)
|
||||
|
||||
info, err := connect(client, cfg.edition, host, port, cfg.useQuery)
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
if cfg.asJSON {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
if err := enc.Encode(info); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
printText(info, cfg.pretty, cfg.motdRaw)
|
||||
}
|
||||
|
||||
type cliConfig struct {
|
||||
edition string
|
||||
timeout time.Duration
|
||||
asJSON bool
|
||||
useQuery bool
|
||||
pretty bool
|
||||
motdRaw bool
|
||||
debug bool
|
||||
}
|
||||
|
||||
func parseCLI(args []string) (cliConfig, string, error) {
|
||||
cfg := cliConfig{
|
||||
edition: "auto",
|
||||
timeout: 3 * time.Second,
|
||||
}
|
||||
|
||||
fs := flag.NewFlagSet("gamestatus", flag.ContinueOnError)
|
||||
fs.SetOutput(os.Stderr)
|
||||
edition := fs.String("edition", "auto", "server edition: auto, java, bedrock, source, rust, cs2, valve")
|
||||
timeout := fs.Duration("timeout", 3*time.Second, "request timeout")
|
||||
asJSON := fs.Bool("json", false, "print JSON output")
|
||||
useQuery := fs.Bool("query", false, "use full query when supported")
|
||||
pretty := fs.Bool("pretty", false, "use compact human-friendly output")
|
||||
motdRaw := fs.Bool("motd-raw", false, "print raw MOTD without cleanup")
|
||||
debug := fs.Bool("debug", false, "print protocol-specific diagnostics")
|
||||
|
||||
if len(args) > 0 {
|
||||
switch strings.ToLower(strings.TrimSpace(args[0])) {
|
||||
case "auto", "java", "bedrock", "source", "rust", "cs2", "valve":
|
||||
cfg.edition = strings.ToLower(strings.TrimSpace(args[0]))
|
||||
args = args[1:]
|
||||
}
|
||||
}
|
||||
|
||||
if err := fs.Parse(args); err != nil {
|
||||
return cfg, "", err
|
||||
}
|
||||
|
||||
cfg.edition = strings.ToLower(strings.TrimSpace(*edition))
|
||||
if cfg.edition == "auto" && len(args) > 0 {
|
||||
// Preserve subcommand semantics while still allowing -edition overrides.
|
||||
switch strings.ToLower(strings.TrimSpace(os.Args[1])) {
|
||||
case "auto", "java", "bedrock", "source", "rust", "cs2", "valve":
|
||||
cfg.edition = strings.ToLower(strings.TrimSpace(os.Args[1]))
|
||||
}
|
||||
}
|
||||
cfg.timeout = *timeout
|
||||
cfg.asJSON = *asJSON
|
||||
cfg.useQuery = *useQuery
|
||||
cfg.pretty = *pretty
|
||||
cfg.motdRaw = *motdRaw
|
||||
cfg.debug = *debug
|
||||
|
||||
if fs.NArg() != 1 {
|
||||
return cfg, "", nil
|
||||
}
|
||||
return cfg, fs.Arg(0), nil
|
||||
}
|
||||
|
||||
func printUsage() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [auto|java|bedrock|source|rust|cs2|valve] [flags] <host[:port]>\n\n", os.Args[0])
|
||||
fmt.Fprintln(os.Stderr, "Examples:")
|
||||
fmt.Fprintf(os.Stderr, " %s mc.hypixel.net\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " %s java mc.hypixel.net\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " %s bedrock -query play.example.com:19132\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " %s source 127.0.0.1:27015\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " %s rust 127.0.0.1:28015\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " %s cs2 127.0.0.1:27015\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, " %s valve 127.0.0.1:27015\n\n", os.Args[0])
|
||||
fmt.Fprintln(os.Stderr, "Flags:")
|
||||
fmt.Fprintln(os.Stderr, " -edition string")
|
||||
fmt.Fprintln(os.Stderr, " server edition: auto, java, bedrock, source, rust, cs2, valve")
|
||||
fmt.Fprintln(os.Stderr, " -timeout duration")
|
||||
fmt.Fprintln(os.Stderr, " request timeout (default 3s)")
|
||||
fmt.Fprintln(os.Stderr, " -json")
|
||||
fmt.Fprintln(os.Stderr, " print JSON output")
|
||||
fmt.Fprintln(os.Stderr, " -query")
|
||||
fmt.Fprintln(os.Stderr, " use full query when supported")
|
||||
fmt.Fprintln(os.Stderr, " -pretty")
|
||||
fmt.Fprintln(os.Stderr, " use compact human-friendly output")
|
||||
fmt.Fprintln(os.Stderr, " -motd-raw")
|
||||
fmt.Fprintln(os.Stderr, " print raw MOTD without cleanup")
|
||||
fmt.Fprintln(os.Stderr, " -debug")
|
||||
fmt.Fprintln(os.Stderr, " print protocol-specific diagnostics")
|
||||
}
|
||||
|
||||
func connect(client *gamestatus.Client, edition, host string, port int, useQuery bool) (*gamestatus.ServerInfo, error) {
|
||||
switch edition {
|
||||
case "auto":
|
||||
if info, err := connectJava(client, host, port, useQuery); err == nil {
|
||||
return info, nil
|
||||
}
|
||||
if info, err := connectBedrock(client, host, port, useQuery); err == nil {
|
||||
return info, nil
|
||||
}
|
||||
return connectSource(client, host, port, useQuery)
|
||||
case "java":
|
||||
return connectJava(client, host, port, useQuery)
|
||||
case "bedrock":
|
||||
return connectBedrock(client, host, port, useQuery)
|
||||
case "source":
|
||||
return connectSource(client, host, port, useQuery)
|
||||
case "rust":
|
||||
return connectRust(client, host, port, useQuery)
|
||||
case "cs2":
|
||||
return connectCS2(client, host, port, useQuery)
|
||||
case "valve":
|
||||
return connectValve(client, host, port, useQuery)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported edition %q", edition)
|
||||
}
|
||||
}
|
||||
|
||||
func connectJava(client *gamestatus.Client, host string, port int, useQuery bool) (*gamestatus.ServerInfo, error) {
|
||||
if useQuery {
|
||||
return client.Java.QueryFull(host, port)
|
||||
}
|
||||
return client.Java.Connect(host, port)
|
||||
}
|
||||
|
||||
func connectBedrock(client *gamestatus.Client, host string, port int, useQuery bool) (*gamestatus.ServerInfo, error) {
|
||||
if useQuery {
|
||||
return client.Bedrock.QueryFull(host, port)
|
||||
}
|
||||
return client.Bedrock.Connect(host, port)
|
||||
}
|
||||
|
||||
func connectSource(client *gamestatus.Client, host string, port int, useQuery bool) (*gamestatus.ServerInfo, error) {
|
||||
if useQuery {
|
||||
return client.Source.QueryFull(host, port)
|
||||
}
|
||||
return client.Source.Connect(host, port)
|
||||
}
|
||||
|
||||
func connectRust(client *gamestatus.Client, host string, port int, useQuery bool) (*gamestatus.ServerInfo, error) {
|
||||
if useQuery {
|
||||
return client.Source.QueryFullRust(host, port)
|
||||
}
|
||||
return client.Source.ConnectRust(host, port)
|
||||
}
|
||||
|
||||
func connectCS2(client *gamestatus.Client, host string, port int, useQuery bool) (*gamestatus.ServerInfo, error) {
|
||||
if useQuery {
|
||||
return client.Source.QueryFullCS2(host, port)
|
||||
}
|
||||
return client.Source.ConnectCS2(host, port)
|
||||
}
|
||||
|
||||
func connectValve(client *gamestatus.Client, host string, port int, useQuery bool) (*gamestatus.ServerInfo, error) {
|
||||
if useQuery {
|
||||
return client.Source.QueryFullValve(host, port)
|
||||
}
|
||||
return client.Source.ConnectValve(host, port)
|
||||
}
|
||||
|
||||
func parseTarget(target string) (string, int, error) {
|
||||
host := strings.TrimSpace(target)
|
||||
if host == "" {
|
||||
return "", 0, fmt.Errorf("empty target")
|
||||
}
|
||||
|
||||
if strings.Count(host, ":") == 1 && !strings.HasSuffix(host, "]") {
|
||||
parts := strings.SplitN(host, ":", 2)
|
||||
p, err := strconv.Atoi(parts[1])
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("invalid port %q", parts[1])
|
||||
}
|
||||
return parts[0], p, nil
|
||||
}
|
||||
|
||||
if strings.HasPrefix(host, "[") {
|
||||
end := strings.LastIndex(host, "]")
|
||||
if end == -1 {
|
||||
return "", 0, fmt.Errorf("invalid IPv6 target %q", target)
|
||||
}
|
||||
ipv6Host := host[1:end]
|
||||
if len(host) == end+1 {
|
||||
return ipv6Host, 0, nil
|
||||
}
|
||||
if !strings.HasPrefix(host[end+1:], ":") {
|
||||
return "", 0, fmt.Errorf("invalid IPv6 target %q", target)
|
||||
}
|
||||
p, err := strconv.Atoi(host[end+2:])
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("invalid port %q", host[end+2:])
|
||||
}
|
||||
return ipv6Host, p, nil
|
||||
}
|
||||
|
||||
return host, 0, nil
|
||||
}
|
||||
|
||||
func printText(info *gamestatus.ServerInfo, pretty bool, motdRaw bool) {
|
||||
motd := info.MOTD
|
||||
if !motdRaw {
|
||||
motd = text.CleanMOTD(motd)
|
||||
}
|
||||
if pretty {
|
||||
printPretty(info, motd)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Edition: %s\n", info.Edition)
|
||||
fmt.Printf("Address: %s:%d\n", info.Address, info.Port)
|
||||
if info.IP != "" {
|
||||
fmt.Printf("IP: %s\n", info.IP)
|
||||
}
|
||||
if info.Version != "" {
|
||||
fmt.Printf("Version: %s\n", info.Version)
|
||||
}
|
||||
if motd != "" {
|
||||
fmt.Printf("MOTD: %s\n", motd)
|
||||
}
|
||||
fmt.Printf("Players: %d/%d\n", info.Online, info.Slots)
|
||||
if info.GameMode != "" {
|
||||
fmt.Printf("GameMode: %s\n", info.GameMode)
|
||||
}
|
||||
if info.Map != "" {
|
||||
fmt.Printf("Map: %s\n", info.Map)
|
||||
}
|
||||
if info.Software != "" {
|
||||
fmt.Printf("Software: %s\n", info.Software)
|
||||
}
|
||||
if len(info.Plugins) > 0 {
|
||||
fmt.Printf("Plugins: %s\n", strings.Join(info.Plugins, ", "))
|
||||
}
|
||||
if len(info.Players) > 0 {
|
||||
fmt.Printf("Sample Players: %s\n", strings.Join(info.Players, ", "))
|
||||
}
|
||||
if info.Latency > 0 {
|
||||
fmt.Printf("Latency: %s\n", info.Latency)
|
||||
}
|
||||
fmt.Printf("Query: %t\n", info.Query)
|
||||
}
|
||||
|
||||
func printPretty(info *gamestatus.ServerInfo, motd string) {
|
||||
fmt.Printf("%s %s:%d\n", strings.ToUpper(string(info.Edition)), info.Address, info.Port)
|
||||
if motd != "" {
|
||||
fmt.Printf("%s\n", motd)
|
||||
}
|
||||
fmt.Printf("Players %d/%d", info.Online, info.Slots)
|
||||
if info.Version != "" {
|
||||
fmt.Printf(" | Version %s", info.Version)
|
||||
}
|
||||
if info.Software != "" {
|
||||
fmt.Printf(" | %s", info.Software)
|
||||
}
|
||||
if info.Latency > 0 {
|
||||
fmt.Printf(" | %s", info.Latency)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
func fatal(err error) {
|
||||
fmt.Fprintln(os.Stderr, "gamestatus:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
4
go.mod
4
go.mod
@@ -1,3 +1,5 @@
|
||||
module github.com/VexoraDevelopment/mcstatus
|
||||
module github.com/VexoraDevelopment/gamestatus
|
||||
|
||||
go 1.25
|
||||
|
||||
require github.com/rumblefrog/go-a2s v1.0.3 // indirect
|
||||
|
||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/rumblefrog/go-a2s v1.0.3 h1:Y1r8oX5IOL8b3KHhN9RCY+2bdmpio52Acd1KsYg4efI=
|
||||
github.com/rumblefrog/go-a2s v1.0.3/go.mod h1:6nq//LMUMa3ElowQ7eH8atnDbQG+nVMFsaMFzSo8p/M=
|
||||
@@ -10,6 +10,10 @@ type Edition string
|
||||
const (
|
||||
EditionJava Edition = "java"
|
||||
EditionBedrock Edition = "bedrock"
|
||||
EditionSource Edition = "source"
|
||||
EditionRust Edition = "rust"
|
||||
EditionCS2 Edition = "cs2"
|
||||
EditionValve Edition = "valve"
|
||||
)
|
||||
|
||||
type ServerInfo struct {
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/VexoraDevelopment/mcstatus/info"
|
||||
"github.com/VexoraDevelopment/gamestatus/info"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/VexoraDevelopment/mcstatus/info"
|
||||
"github.com/VexoraDevelopment/gamestatus/info"
|
||||
)
|
||||
|
||||
func (c *Client) QueryFull(address string, port int) (*info.ServerInfo, error) {
|
||||
|
||||
222
source/source.go
Normal file
222
source/source.go
Normal file
@@ -0,0 +1,222 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
a2s "github.com/rumblefrog/go-a2s"
|
||||
|
||||
"github.com/VexoraDevelopment/gamestatus/info"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Timeout time.Duration
|
||||
Debug bool
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
opt Options
|
||||
}
|
||||
|
||||
func New(opt Options) *Client {
|
||||
if opt.Timeout <= 0 {
|
||||
opt.Timeout = 2 * time.Second
|
||||
}
|
||||
return &Client{opt: opt}
|
||||
}
|
||||
|
||||
func (c *Client) Timeout() time.Duration {
|
||||
if c == nil {
|
||||
return 0
|
||||
}
|
||||
return c.opt.Timeout
|
||||
}
|
||||
|
||||
func (c *Client) Connect(address string, port int) (*info.ServerInfo, error) {
|
||||
return c.query(address, port, false)
|
||||
}
|
||||
|
||||
func (c *Client) QueryFull(address string, port int) (*info.ServerInfo, error) {
|
||||
return c.query(address, port, true)
|
||||
}
|
||||
|
||||
func (c *Client) ConnectRust(address string, port int) (*info.ServerInfo, error) {
|
||||
out, err := c.query(address, port, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out.Edition = info.EditionRust
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) QueryFullRust(address string, port int) (*info.ServerInfo, error) {
|
||||
out, err := c.query(address, port, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out.Edition = info.EditionRust
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) ConnectCS2(address string, port int) (*info.ServerInfo, error) {
|
||||
out, err := c.query(address, port, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out.Edition = info.EditionCS2
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) QueryFullCS2(address string, port int) (*info.ServerInfo, error) {
|
||||
out, err := c.query(address, port, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out.Edition = info.EditionCS2
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) ConnectValve(address string, port int) (*info.ServerInfo, error) {
|
||||
out, err := c.query(address, port, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out.Edition = info.EditionValve
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) QueryFullValve(address string, port int) (*info.ServerInfo, error) {
|
||||
out, err := c.query(address, port, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out.Edition = info.EditionValve
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) query(address string, port int, full bool) (*info.ServerInfo, error) {
|
||||
addr := normalizeAddress(address, port)
|
||||
client, err := a2s.NewClient(addr, a2s.TimeoutOption(c.opt.Timeout))
|
||||
if err != nil {
|
||||
return nil, wrapQueryError(addr, "connect", err, c.opt.Debug)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
start := time.Now()
|
||||
infoResp, err := client.QueryInfo()
|
||||
if err != nil {
|
||||
return nil, wrapQueryError(addr, "info", err, c.opt.Debug)
|
||||
}
|
||||
|
||||
out := &info.ServerInfo{
|
||||
Edition: info.EditionSource,
|
||||
Address: address,
|
||||
IP: resolveIP(address),
|
||||
Port: effectivePort(address, port),
|
||||
MOTD: infoResp.Name,
|
||||
Version: infoResp.Version,
|
||||
Online: int(infoResp.Players),
|
||||
Slots: int(infoResp.MaxPlayers),
|
||||
Software: infoResp.Game,
|
||||
Game: infoResp.Folder,
|
||||
Map: infoResp.Map,
|
||||
Query: full,
|
||||
Latency: time.Since(start),
|
||||
}
|
||||
|
||||
if full {
|
||||
playerResp, err := client.QueryPlayer()
|
||||
if err == nil && playerResp != nil {
|
||||
names := make([]string, 0, len(playerResp.Players))
|
||||
for _, p := range playerResp.Players {
|
||||
if p == nil {
|
||||
continue
|
||||
}
|
||||
name := strings.TrimSpace(p.Name)
|
||||
if name != "" {
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
out.Players = names
|
||||
} else if err != nil && c.opt.Debug {
|
||||
return nil, wrapQueryError(addr, "players", err, true)
|
||||
}
|
||||
rulesResp, err := client.QueryRules()
|
||||
if err == nil && rulesResp != nil {
|
||||
out.RawQuery = rulesResp.Rules
|
||||
} else if err != nil && c.opt.Debug {
|
||||
return nil, wrapQueryError(addr, "rules", err, true)
|
||||
}
|
||||
}
|
||||
|
||||
raw, _ := json.Marshal(infoResp)
|
||||
out.Raw = raw
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func wrapQueryError(addr, stage string, err error, debug bool) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var netErr net.Error
|
||||
if errors.As(err, &netErr) && netErr.Timeout() {
|
||||
msg := fmt.Sprintf("no A2S response from %s", addr)
|
||||
if debug && strings.TrimSpace(stage) != "" {
|
||||
msg += fmt.Sprintf(" during %s", stage)
|
||||
}
|
||||
msg += " (server may have query disabled, blocked, or be using a different query port)"
|
||||
return errors.New(msg)
|
||||
}
|
||||
if debug && strings.TrimSpace(stage) != "" {
|
||||
return fmt.Errorf("source query %s failed for %s: %w", stage, addr, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func normalizeAddress(host string, port int) string {
|
||||
if port == 0 {
|
||||
port = a2s.DefaultPort
|
||||
}
|
||||
if strings.Contains(host, ":") {
|
||||
if _, _, err := net.SplitHostPort(host); err == nil {
|
||||
return host
|
||||
}
|
||||
if strings.Count(host, ":") > 1 && !strings.HasPrefix(host, "[") {
|
||||
return net.JoinHostPort(host, strconv.Itoa(port))
|
||||
}
|
||||
}
|
||||
return net.JoinHostPort(host, strconv.Itoa(port))
|
||||
}
|
||||
|
||||
func effectivePort(host string, port int) int {
|
||||
if port != 0 {
|
||||
return port
|
||||
}
|
||||
if _, p, err := net.SplitHostPort(host); err == nil {
|
||||
if v, convErr := strconv.Atoi(p); convErr == nil {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return a2s.DefaultPort
|
||||
}
|
||||
|
||||
func resolveIP(host string) string {
|
||||
if h, _, err := net.SplitHostPort(host); err == nil {
|
||||
host = h
|
||||
}
|
||||
host = strings.TrimPrefix(host, "[")
|
||||
host = strings.TrimSuffix(host, "]")
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
return ip.String()
|
||||
}
|
||||
ips, err := net.LookupIP(host)
|
||||
if err != nil || len(ips) == 0 {
|
||||
return ""
|
||||
}
|
||||
return ips[0].String()
|
||||
}
|
||||
Reference in New Issue
Block a user