-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bb937a7
Showing
16 changed files
with
1,238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Go test and build | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: "1.20" | ||
|
||
- name: Test | ||
run: go test -v ./... | ||
|
||
- name: Build | ||
run: go build -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
|
||
# GoLand | ||
.idea/ | ||
|
||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Sch8ill | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
bin_name=mcli | ||
target=cmd/cli/main.go | ||
|
||
all: build | ||
|
||
run: | ||
go run $(target) | ||
|
||
build: | ||
go build -o build/$(bin_name) $(target) | ||
|
||
multi-arch: | ||
scripts/build-multi-arch.sh $(target) build/$(bin_name) | ||
|
||
clean: | ||
rm -rf build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# mclib | ||
|
||
[![Release](https://img.shields.io/github/release/sch8ill/mclib.svg?style=flat-square)](https://github.com/sch8ill/mclib/releases) | ||
[![doc](https://img.shields.io/badge/go.dev-doc-007d9c?style=flat-square&logo=read-the-docs)](https://pkg.go.dev/github.com/sch8ill/mclib) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/sch8ill/mclib)](https://goreportcard.com/report/github.com/sch8ill/mclib) | ||
![MIT license](https://img.shields.io/badge/license-MIT-green) | ||
|
||
--- | ||
|
||
The `mclib` package provides utilities for interacting with Minecraft servers using the Server List Ping (SLP) protocol. | ||
It includes functionality to query Minecraft servers for status and latency information. | ||
|
||
## Installation | ||
|
||
To use this package in your Go project, simply install it: | ||
|
||
```bash | ||
go get github.com/sch8ill/mclib | ||
``` | ||
|
||
## Usage | ||
|
||
### MCServer | ||
|
||
`MCServer` represents a Minecraft server with its address and client. It provides methods to retrieve server status and | ||
perform a status ping. | ||
|
||
#### Creating an MCServer Instance | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/sch8ill/mclib/server" | ||
) | ||
|
||
func main() { | ||
srv, err := server.New("example.com:25565") | ||
if err != nil { | ||
// handle error | ||
} | ||
} | ||
``` | ||
|
||
#### StatusPing | ||
|
||
```go | ||
res, err := srv.StatusPing() | ||
if err != nil { | ||
// handle error | ||
} | ||
|
||
fmt.Printf("version: %s\n", res.Version.Name) | ||
fmt.Printf("protocol: %d\n", res.Version.Protocol) | ||
fmt.Printf("online players: %d\n", res.Players.Online) | ||
fmt.Printf("max players: %d\n", res.Players.Max) | ||
fmt.Printf("sample players: %+q\n", res.Players.Sample) | ||
fmt.Printf("description: %s\n", res.Description.String()) | ||
fmt.Printf("latency: %dms\n", res.Latency) | ||
// ... | ||
``` | ||
|
||
#### Ping | ||
|
||
```go | ||
latency, err := srv.ping() | ||
if err != nil { | ||
// handle error | ||
} | ||
|
||
fmt.Printf("latency: %dms\n", latency) | ||
``` | ||
|
||
### Cli | ||
|
||
#### Build | ||
|
||
requires: | ||
|
||
``` | ||
make | ||
go >= 1.20 | ||
``` | ||
|
||
build: | ||
|
||
```bash | ||
make build && mv build/mcli mcli | ||
``` | ||
|
||
#### Usage | ||
|
||
`mclib` also provides a simple command line interface: | ||
|
||
``` | ||
-addr string | ||
the server address (default "localhost") | ||
-srv | ||
whether a srv lookup should be made (default true) | ||
-timeout duration | ||
the connection timeout (default 5s) | ||
``` | ||
|
||
For example: | ||
|
||
```bash | ||
mcli --addr hypixel.net --timeout 10s | ||
``` | ||
|
||
## License | ||
|
||
This package is licensed under the [MIT License](LICENSE). | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Package address provides utilities for working with Minecraft server addresses. | ||
package address | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const DefaultPort uint16 = 25565 | ||
|
||
// Address represents a Minecraft server address with a host, port and srv record. | ||
type Address struct { | ||
Host string | ||
Port uint16 | ||
SRVHost string | ||
SRVPort uint16 | ||
SRV bool | ||
} | ||
|
||
// New creates a new Address from a given address string, | ||
// which can include the host and port separated by a colon (e.g., "example.com:25565"). | ||
// If no port is specified, it uses the default Minecraft port. | ||
func New(addr string) (*Address, error) { | ||
if !strings.Contains(addr, ":") { | ||
return &Address{ | ||
Host: addr, | ||
Port: DefaultPort, | ||
}, nil | ||
} | ||
|
||
splitAddr := strings.Split(addr, ":") | ||
if len(splitAddr) != 2 { | ||
return nil, fmt.Errorf("invalid address: %s", addr) | ||
} | ||
|
||
port, err := strconv.Atoi(splitAddr[1]) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid port: %s", splitAddr[1]) | ||
} | ||
|
||
return &Address{ | ||
Host: splitAddr[0], | ||
Port: uint16(port), | ||
}, nil | ||
} | ||
|
||
// ResolveSRV resolves the SRV record for the Address's host and updates its SRV fields. | ||
func (a *Address) ResolveSRV() error { | ||
if a.IsIP() { | ||
return nil | ||
} | ||
|
||
_, records, err := net.LookupSRV("minecraft", "tcp", a.Host) | ||
if err != nil { | ||
return fmt.Errorf("failed to resolve SRV record: %w", err) | ||
} | ||
|
||
if len(records) > 0 { | ||
srvRecord := records[0] | ||
a.SRVPort = srvRecord.Port | ||
a.SRVHost = srvRecord.Target | ||
a.SRV = true | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IsIP checks if the host in the Address is an IP address. | ||
func (a *Address) IsIP() bool { | ||
return net.ParseIP(a.Host) != nil | ||
} | ||
|
||
// Addr returns the address string based on whether SRV record resolution is enabled. | ||
// If SRV resolution is enabled, it returns the SRV address; otherwise, the original address. | ||
func (a *Address) Addr() string { | ||
if a.SRV { | ||
return a.SRVAddr() | ||
} | ||
return a.OGAddr() | ||
} | ||
|
||
// SRVAddr returns the address string in the format "hostname:port" based on SRV record values. | ||
func (a *Address) SRVAddr() string { | ||
return fmt.Sprintf("%s:%d", a.SRVHost, a.SRVPort) | ||
} | ||
|
||
// OGAddr returns the address string in the format "hostname:port". | ||
func (a *Address) OGAddr() string { | ||
return fmt.Sprintf("%s:%d", a.Host, a.Port) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/sch8ill/mclib/server" | ||
"github.com/sch8ill/mclib/slp" | ||
) | ||
|
||
func main() { | ||
addr := flag.String("addr", "localhost", "the server address") | ||
timeout := flag.Duration("timeout", slp.DefaultTimeout, "the connection timeout") | ||
srv := flag.Bool("srv", true, "whether a srv lookup should be made") | ||
flag.Parse() | ||
|
||
opts := []server.MCServerOption{server.WithTimeout(*timeout)} | ||
if !*srv { | ||
opts = append(opts, server.WithoutSRV()) | ||
} | ||
|
||
mcs, err := server.New(*addr, opts...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
res, err := mcs.StatusPing() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("version: %s\n", res.Version.Name) | ||
fmt.Printf("protocol: %d\n", res.Version.Protocol) | ||
fmt.Printf("description: %s\n", res.Description.String()) | ||
fmt.Printf("online players: %d\n", res.Players.Online) | ||
fmt.Printf("max players: %d\n", res.Players.Max) | ||
fmt.Printf("sample players: %+q\n", res.Players.Sample) | ||
fmt.Printf("latency: %dms\n", res.Latency) | ||
fmt.Printf("favicon: %t\n", res.Favicon != "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/sch8ill/mclib | ||
|
||
go 1.20 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env bash | ||
|
||
target="$1" | ||
output_prefix="$2" | ||
|
||
platforms=("windows/amd64" "windows/arm" "linux/amd64" "linux/arm64" "linux/arm") | ||
|
||
# Ensure that the target and output_prefix are provided | ||
if [ -z "$target" ] || [ -z "$output_prefix" ]; then | ||
echo "Usage: $0 <target> <output_prefix>" | ||
exit 1 | ||
fi | ||
|
||
for platform in "${!platforms[@]}"; do | ||
output_name="$output_prefix-${platforms[$platform]}" | ||
|
||
if [[ "$platform" == "windows"* ]]; then | ||
output_name+='.exe' | ||
fi | ||
|
||
if env GOOS="${platforms[$platform]%%/*}" GOARCH="${platforms[$platform]##*/}" go build -o "$output_name" "$target"; then | ||
echo "Built $output_name" | ||
else | ||
echo "Error building $output_name" | ||
exit 1 | ||
fi | ||
done |
Oops, something went wrong.