Add variants and EA image
This commit is contained in:
parent
3f606dd5c6
commit
bb2c817c0d
54
04rel64-java/Dockerfile
Normal file
54
04rel64-java/Dockerfile
Normal file
@ -0,0 +1,54 @@
|
||||
FROM debian:stable
|
||||
|
||||
MAINTAINER Thijn <thijn@viceunderdogs.com>
|
||||
|
||||
# Install packages
|
||||
RUN apt-get update && apt-get install -y \
|
||||
wget \
|
||||
unzip \
|
||||
p7zip \
|
||||
libstdc++6 \
|
||||
gcc \
|
||||
g++ \
|
||||
build-essential \
|
||||
libcurl4 \
|
||||
&& adduser --disabled-password --home /home/container container
|
||||
|
||||
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && \
|
||||
apt-get install -y nodejs
|
||||
|
||||
RUN cd /tmp && \
|
||||
wget 'http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.1_1.1.1n-0+deb11u5_amd64.deb' && \
|
||||
dpkg -i libssl1.1_1.1.1n-0+deb11u5_amd64.deb && rm libssl1.1_1.1.1n-0+deb11u5_amd64.deb
|
||||
|
||||
#RUN apt-get install -y screen gdb
|
||||
#RUN cd /tmp && wget 'http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.0.0_1.0.1t-1+deb8u12_amd64.deb' && \
|
||||
#dpkg -i libssl1.0.0_1.0.1t-1+deb8u12_amd64.deb && rm libssl1.0.0_1.0.1t-1+deb8u12_amd64.deb
|
||||
|
||||
RUN mkdir -p /usr/lib/jvm/jdk-15 && cd /usr/lib/jvm/jdk-15 && \
|
||||
wget 'https://download.java.net/java/GA/jdk15.0.2/0d1cfde4252546c6931946de8db48ee2/7/GPL/openjdk-15.0.2_linux-x64_bin.tar.gz' && \
|
||||
tar -xzv --strip-components=1 -f openjdk-15.0.2_linux-x64_bin.tar.gz
|
||||
|
||||
RUN mkdir -p /usr/lib/jvm/jdk-18 && cd /usr/lib/jvm/jdk-18 && \
|
||||
wget 'https://download.java.net/java/GA/jdk18.0.2/f6ad4b4450fd4d298113270ec84f30ee/9/GPL/openjdk-18.0.2_linux-x64_bin.tar.gz' && \
|
||||
tar -xzv --strip-components=1 -f openjdk-18.0.2_linux-x64_bin.tar.gz
|
||||
|
||||
RUN mkdir -p /usr/lib/jvm/jdk-8 && cd /usr/lib/jvm/jdk-8 && \
|
||||
wget 'https://builds.openlogic.com/downloadJDK/openlogic-openjdk-jre/8u412-b08/openlogic-openjdk-jre-8u412-b08-linux-x64.tar.gz' && \
|
||||
tar -xzv --strip-components=1 -f openlogic-openjdk-jre-8u412-b08-linux-x64.tar.gz
|
||||
|
||||
|
||||
USER container
|
||||
ENV USER container
|
||||
ENV HOME /home/container
|
||||
ENV VCMP_VERSION v30
|
||||
ENV VCMP_PLUGIN_PATCH patch2
|
||||
|
||||
WORKDIR /home/container
|
||||
|
||||
COPY ./start.sh /start.sh
|
||||
COPY ./query /bin/vcmp-query
|
||||
healthcheck --interval=1m --timeout=3s \
|
||||
cmd /bin/vcmp-query "127.0.0.1:${SERVER_PORT}"
|
||||
|
||||
CMD ["/bin/bash", "/start.sh"]
|
2
04rel64-java/build.sh
Executable file
2
04rel64-java/build.sh
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
docker build -t vcmp_04rel64:java .
|
3
04rel64-java/publish.sh
Executable file
3
04rel64-java/publish.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker tag vcmp_04rel64:java docker.panel2.viceunderdogs.com/vcmp-04rel64:java
|
||||
docker push docker.panel2.viceunderdogs.com/vcmp-04rel64:java
|
BIN
04rel64-java/query
Executable file
BIN
04rel64-java/query
Executable file
Binary file not shown.
42
04rel64-java/start.sh
Normal file
42
04rel64-java/start.sh
Normal file
@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
CHK_FILE="/home/container/mpsvrrel64"
|
||||
if [ -f $CHK_FILE ]; then
|
||||
echo "VC:MP executable exists, not downloading. To update, delete mpsvrrel64."
|
||||
else
|
||||
wget -q http://v04.thijn.ovh/server/VCMP04_server_${VCMP_VERSION}_linux64.zip
|
||||
unzip VCMP04_server_${VCMP_VERSION}_linux64.zip
|
||||
rm -f VCMP04_server_${VCMP_VERSION}_linux64.zip
|
||||
echo "Downloaded VC:MP Server version ${VCMP_VERSION}"
|
||||
|
||||
mkdir plugins
|
||||
cd plugins/
|
||||
wget -q http://v04.thijn.ovh/allplugins_04rel006_${VCMP_PLUGIN_PATCH}.7z
|
||||
p7zip -d allplugins_04rel006_${VCMP_PLUGIN_PATCH}.7z
|
||||
chmod 700 *
|
||||
rm -f allplugins_04rel006_${VCMP_PLUGIN_PATCH}.7z
|
||||
rm -f *rel32.*
|
||||
rm -f *.dll
|
||||
|
||||
cd /home/container
|
||||
chmod +x mpsvrrel64
|
||||
find
|
||||
fi
|
||||
|
||||
if [ -f "/home/container/server.cfg" ]; then
|
||||
echo "server.cfg exists, not generating file."
|
||||
else
|
||||
echo "
|
||||
gamemode Default mode
|
||||
plugins squirrel04rel64 sqlite04rel64 hashing04rel64 sockets04rel64
|
||||
port ${SERVER_PORT}
|
||||
sqgamemode main.nut
|
||||
maxplayers ${MAX_SLOTS}
|
||||
" > server.cfg
|
||||
fi
|
||||
|
||||
cd /home/container/
|
||||
MODIFIED_STARTUP=`echo ${STARTUP} | perl -pe 's@\{\{(.*?)\}\}@$ENV{$1}@g'`
|
||||
echo "Startup: ${MODIFIED_STARTUP}"
|
||||
eval ${MODIFIED_STARTUP}
|
||||
|
||||
exit 0
|
45
04rel64-stable/Dockerfile
Normal file
45
04rel64-stable/Dockerfile
Normal file
@ -0,0 +1,45 @@
|
||||
FROM debian:stable
|
||||
|
||||
MAINTAINER Thijn <thijn@viceunderdogs.com>
|
||||
|
||||
# Install packages
|
||||
RUN apt-get update && apt-get install -y \
|
||||
wget \
|
||||
unzip \
|
||||
p7zip \
|
||||
libstdc++6 \
|
||||
gcc \
|
||||
g++ \
|
||||
build-essential \
|
||||
libcurl4 \
|
||||
&& adduser --disabled-password --home /home/container container
|
||||
|
||||
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - && \
|
||||
apt-get install -y nodejs npm
|
||||
|
||||
RUN cd /tmp && \
|
||||
wget 'http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.1_1.1.1n-0+deb11u5_amd64.deb' && \
|
||||
dpkg -i libssl1.1_1.1.1n-0+deb11u5_amd64.deb && rm libssl1.1_1.1.1n-0+deb11u5_amd64.deb
|
||||
|
||||
#RUN apt-get install -y screen gdb
|
||||
#RUN cd /tmp && wget 'http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.0.0_1.0.1t-1+deb8u12_amd64.deb' && \
|
||||
#dpkg -i libssl1.0.0_1.0.1t-1+deb8u12_amd64.deb && rm libssl1.0.0_1.0.1t-1+deb8u12_amd64.deb
|
||||
|
||||
#RUN mkdir -p /usr/lib/jvm/jdk-15 && cd /usr/lib/jvm/jdk-15 && \
|
||||
# wget 'https://download.java.net/java/GA/jdk15.0.2/0d1cfde4252546c6931946de8db48ee2/7/GPL/openjdk-15.0.2_linux-x64_bin.tar.gz' && \
|
||||
# tar -xzv --strip-components=1 -f openjdk-15.0.2_linux-x64_bin.tar.gz
|
||||
|
||||
USER container
|
||||
ENV USER container
|
||||
ENV HOME /home/container
|
||||
ENV VCMP_VERSION v30
|
||||
ENV VCMP_PLUGIN_PATCH patch2
|
||||
|
||||
WORKDIR /home/container
|
||||
|
||||
COPY ./start.sh /start.sh
|
||||
COPY ./query /bin/vcmp-query
|
||||
healthcheck --interval=1m --timeout=3s \
|
||||
cmd /bin/vcmp-query "127.0.0.1:${SERVER_PORT}"
|
||||
|
||||
CMD ["/bin/bash", "/start.sh"]
|
2
04rel64-stable/build.sh
Executable file
2
04rel64-stable/build.sh
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
docker build -t vcmp_04rel64:stable .
|
3
04rel64-stable/publish.sh
Executable file
3
04rel64-stable/publish.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker tag vcmp_04rel64:stable docker.panel2.viceunderdogs.com/vcmp-04rel64:stable
|
||||
docker push docker.panel2.viceunderdogs.com/vcmp-04rel64:stable
|
BIN
04rel64-stable/query
Executable file
BIN
04rel64-stable/query
Executable file
Binary file not shown.
42
04rel64-stable/start.sh
Normal file
42
04rel64-stable/start.sh
Normal file
@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
CHK_FILE="/home/container/mpsvrrel64"
|
||||
if [ -f $CHK_FILE ]; then
|
||||
echo "VC:MP executable exists, not downloading. To update, delete mpsvrrel64."
|
||||
else
|
||||
wget -q http://v04.thijn.ovh/server/VCMP04_server_${VCMP_VERSION}_linux64.zip
|
||||
unzip VCMP04_server_${VCMP_VERSION}_linux64.zip
|
||||
rm -f VCMP04_server_${VCMP_VERSION}_linux64.zip
|
||||
echo "Downloaded VC:MP Server version ${VCMP_VERSION}"
|
||||
|
||||
mkdir plugins
|
||||
cd plugins/
|
||||
wget -q http://v04.thijn.ovh/allplugins_04rel006_${VCMP_PLUGIN_PATCH}.7z
|
||||
p7zip -d allplugins_04rel006_${VCMP_PLUGIN_PATCH}.7z
|
||||
chmod 700 *
|
||||
rm -f allplugins_04rel006_${VCMP_PLUGIN_PATCH}.7z
|
||||
rm -f *rel32.*
|
||||
rm -f *.dll
|
||||
|
||||
cd /home/container
|
||||
chmod +x mpsvrrel64
|
||||
find
|
||||
fi
|
||||
|
||||
if [ -f "/home/container/server.cfg" ]; then
|
||||
echo "server.cfg exists, not generating file."
|
||||
else
|
||||
echo "
|
||||
gamemode Default mode
|
||||
plugins squirrel04rel64 sqlite04rel64 hashing04rel64 sockets04rel64
|
||||
port ${SERVER_PORT}
|
||||
sqgamemode main.nut
|
||||
maxplayers ${MAX_SLOTS}
|
||||
" > server.cfg
|
||||
fi
|
||||
|
||||
cd /home/container/
|
||||
MODIFIED_STARTUP=`echo ${STARTUP} | perl -pe 's@\{\{(.*?)\}\}@$ENV{$1}@g'`
|
||||
echo "Startup: ${MODIFIED_STARTUP}"
|
||||
eval ${MODIFIED_STARTUP}
|
||||
|
||||
exit 0
|
@ -39,5 +39,8 @@ ENV VCMP_PLUGIN_PATCH patch2
|
||||
WORKDIR /home/container
|
||||
|
||||
COPY ./start.sh /start.sh
|
||||
COPY ./query /bin/vcmp-query
|
||||
healthcheck --interval=5m --timeout=3s \
|
||||
cmd /bin/vcmp-query "127.0.0.1:8192"
|
||||
|
||||
CMD ["/bin/bash", "/start.sh"]
|
||||
|
BIN
04rel64/query
Executable file
BIN
04rel64/query
Executable file
Binary file not shown.
30
ea/Dockerfile
Normal file
30
ea/Dockerfile
Normal file
@ -0,0 +1,30 @@
|
||||
FROM node:18.20.4-slim
|
||||
|
||||
# Cache apt-get update and package installs
|
||||
RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \
|
||||
--mount=target=/var/cache/apt,type=cache,sharing=locked \
|
||||
rm -f /etc/apt/apt.conf.d/docker-clean \
|
||||
&& apt-get update \
|
||||
&& apt-get -y --no-install-recommends install \
|
||||
wget libzmq3-dev libzmq5 libmariadb-dev libcurl4
|
||||
|
||||
# SSL 1.1 setup
|
||||
# download install package
|
||||
RUN wget http://ftp.us.debian.org/debian/pool/main/o/openssl/libssl1.1_1.1.1w-0+deb11u1_amd64.deb && dpkg -i libssl1.1_1.1.1w-0+deb11u1_amd64.deb
|
||||
|
||||
RUN npm i -g pm2
|
||||
|
||||
RUN adduser --disabled-password --home /home/container container
|
||||
|
||||
USER container
|
||||
|
||||
COPY dockerstart.sh /
|
||||
|
||||
WORKDIR /home/container
|
||||
|
||||
ENV PORT=8192
|
||||
ENV HOME=/home/container
|
||||
EXPOSE 8192/udp
|
||||
|
||||
# Set entrypoint
|
||||
CMD ["bash", "/dockerstart.sh"]
|
2
ea/build.sh
Executable file
2
ea/build.sh
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
docker build -t vcmp_04rel64:ea .
|
12
ea/dockerstart.sh
Executable file
12
ea/dockerstart.sh
Executable file
@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
mkdir -p /home/container/discord_bot /home/container/server
|
||||
cd '/home/container/discord_bot'
|
||||
npm install
|
||||
pm2 start bot.js
|
||||
|
||||
cd /home/container/server
|
||||
echo 'Starting server on' $PORT
|
||||
|
||||
# Using stdbuf to force unbuffered output since server is not directly connected to terminal and is buffering output
|
||||
stdbuf -oL ./server -allow-server-runas-root -port $PORT
|
3
ea/publish.sh
Executable file
3
ea/publish.sh
Executable file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker tag vcmp_04rel64:ea docker.panel2.viceunderdogs.com/vcmp-04rel64:ea
|
||||
docker push docker.panel2.viceunderdogs.com/vcmp-04rel64:ea
|
@ -12,6 +12,7 @@ RUN apt-get update && apt-get install -y \
|
||||
g++ \
|
||||
build-essential \
|
||||
libmariadb3 \
|
||||
libmariadb-dev \
|
||||
libcurl4 \
|
||||
libpq5 \
|
||||
&& adduser --disabled-password --home /home/container container
|
||||
@ -25,5 +26,6 @@ ENV VCMP_PLUGIN_PATCH patch2
|
||||
WORKDIR /home/container
|
||||
|
||||
COPY ./start.sh /start.sh
|
||||
COPY ./query /bin/vcmp-query
|
||||
|
||||
CMD ["/bin/bash", "/start.sh"]
|
||||
|
BIN
sqmod/query
Executable file
BIN
sqmod/query
Executable file
Binary file not shown.
7
vcmpquery/go.mod
Normal file
7
vcmpquery/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module vcmpquery/query
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/pkg/errors v0.8.0
|
||||
)
|
2
vcmpquery/go.sum
Normal file
2
vcmpquery/go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
193
vcmpquery/main.go
Normal file
193
vcmpquery/main.go
Normal file
@ -0,0 +1,193 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"math/rand"
|
||||
"net"
|
||||
// "strconv"
|
||||
"context"
|
||||
"time"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Server contains all the information retreived from the server query API.
|
||||
type Server struct {
|
||||
Address string `json:"address"`
|
||||
Hostname string `json:"hostname"`
|
||||
Players int `json:"players"`
|
||||
MaxPlayers int `json:"max_players"`
|
||||
Gamemode string `json:"gamemode"`
|
||||
Language string `json:"language"`
|
||||
Password bool `json:"password"`
|
||||
Rules map[string]string `json:"rules"`
|
||||
Ping int `json:"ping"`
|
||||
}
|
||||
|
||||
// QueryType represents a query method from the SA:MP set: i, r, c, d, x, p
|
||||
type QueryType uint8
|
||||
|
||||
const (
|
||||
// Info is the 'i' packet type
|
||||
Info QueryType = 'i'
|
||||
// Rules is the 'r' packet type
|
||||
Rules QueryType = 'r'
|
||||
// Players is the 'c' packet type
|
||||
Players QueryType = 'c'
|
||||
// Ping is the 'p' packet type
|
||||
Ping QueryType = 'p'
|
||||
)
|
||||
|
||||
// Query stores state for masterlist queries
|
||||
type Query struct {
|
||||
addr *net.UDPAddr
|
||||
Data Server
|
||||
}
|
||||
|
||||
// NewQuery creates a new query handler for a server
|
||||
func NewQuery(host string) (query *Query, err error) {
|
||||
query = new(Query)
|
||||
|
||||
query.addr, err = net.ResolveUDPAddr("udp", host)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to resolve host")
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// Close closes a query manager's connection
|
||||
func (query *Query) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (query *Query) SendQuery(ctx context.Context, opcode QueryType) (response []byte, err error) {
|
||||
request := new(bytes.Buffer)
|
||||
|
||||
port := [2]byte{
|
||||
byte(query.addr.Port & 0xFF),
|
||||
byte((query.addr.Port >> 8) & 0xFF),
|
||||
}
|
||||
|
||||
if err = binary.Write(request, binary.LittleEndian, []byte("VCMP")); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, query.addr.IP.To4()); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, port[0]); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, port[1]); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, opcode); err != nil {
|
||||
return
|
||||
}
|
||||
if opcode == Ping {
|
||||
p := make([]byte, 4)
|
||||
_, err = rand.Read(p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, p); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
conn, err := openConnection(query.addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
_, err = conn.Write(request.Bytes())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to write")
|
||||
}
|
||||
|
||||
type resultData struct {
|
||||
data []byte
|
||||
bytes int
|
||||
err error
|
||||
}
|
||||
waitResult := make(chan resultData, 1)
|
||||
|
||||
go func() {
|
||||
response := make([]byte, 2048)
|
||||
n, errInner := conn.Read(response)
|
||||
if errInner != nil {
|
||||
waitResult <- resultData{err: errors.Wrap(errInner, "failed to read response")}
|
||||
return
|
||||
}
|
||||
if n > cap(response) {
|
||||
waitResult <- resultData{err: errors.New("read response over buffer capacity")}
|
||||
return
|
||||
}
|
||||
waitResult <- resultData{data: response, bytes: n}
|
||||
}()
|
||||
|
||||
var result resultData
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, errors.New("socket read timed out")
|
||||
|
||||
case result = <-waitResult:
|
||||
break
|
||||
}
|
||||
|
||||
if result.err != nil {
|
||||
return nil, result.err
|
||||
}
|
||||
|
||||
return result.data[:result.bytes], nil
|
||||
}
|
||||
|
||||
// GetPing sends and receives a packet to measure ping
|
||||
func (query *Query) GetPing(ctx context.Context) (ping time.Duration, err error) {
|
||||
t := time.Now()
|
||||
_, err = query.SendQuery(ctx, Ping)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
ping = time.Now().Sub(t)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func openConnection(addr *net.UDPAddr) (conn *net.UDPConn, err error) {
|
||||
conn, err = net.DialUDP("udp", nil, addr)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to dial")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("Usage: ./vcmpquery ip:port")
|
||||
os.Exit(1)
|
||||
}
|
||||
qIp := args[1]
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
|
||||
defer cancel()
|
||||
query, err := NewQuery(qIp)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ping, err := query.GetPing(ctx)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("Server online, ping: ", ping)
|
||||
os.Exit(0)
|
||||
}
|
BIN
vcmpquery/nagios
Executable file
BIN
vcmpquery/nagios
Executable file
Binary file not shown.
236
vcmpquery/nagios.go
Normal file
236
vcmpquery/nagios.go
Normal file
@ -0,0 +1,236 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"math/rand"
|
||||
"net"
|
||||
// "strings"
|
||||
"context"
|
||||
"time"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Server contains all the information retreived from the server query API.
|
||||
type Server struct {
|
||||
Address string `json:"address"`
|
||||
Hostname string `json:"hostname"`
|
||||
Players int `json:"players"`
|
||||
MaxPlayers int `json:"max_players"`
|
||||
Gamemode string `json:"gamemode"`
|
||||
Language string `json:"language"`
|
||||
Password bool `json:"password"`
|
||||
Rules map[string]string `json:"rules"`
|
||||
Ping int `json:"ping"`
|
||||
}
|
||||
|
||||
// QueryType represents a query method from the SA:MP set: i, r, c, d, x, p
|
||||
type QueryType uint8
|
||||
|
||||
const (
|
||||
// Info is the 'i' packet type
|
||||
Info QueryType = 'i'
|
||||
// Rules is the 'r' packet type
|
||||
Rules QueryType = 'r'
|
||||
// Players is the 'c' packet type
|
||||
Players QueryType = 'c'
|
||||
// Ping is the 'p' packet type
|
||||
Ping QueryType = 'p'
|
||||
)
|
||||
|
||||
// Query stores state for masterlist queries
|
||||
type Query struct {
|
||||
addr *net.UDPAddr
|
||||
Data Server
|
||||
}
|
||||
|
||||
// NewQuery creates a new query handler for a server
|
||||
func NewQuery(host string) (query *Query, err error) {
|
||||
query = new(Query)
|
||||
|
||||
query.addr, err = net.ResolveUDPAddr("udp", host)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to resolve host")
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// Close closes a query manager's connection
|
||||
func (query *Query) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (query *Query) SendQuery(ctx context.Context, opcode QueryType) (response []byte, err error) {
|
||||
request := new(bytes.Buffer)
|
||||
|
||||
port := [2]byte{
|
||||
byte(query.addr.Port & 0xFF),
|
||||
byte((query.addr.Port >> 8) & 0xFF),
|
||||
}
|
||||
|
||||
if err = binary.Write(request, binary.LittleEndian, []byte("VCMP")); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, query.addr.IP.To4()); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, port[0]); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, port[1]); err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, opcode); err != nil {
|
||||
return
|
||||
}
|
||||
if opcode == Ping {
|
||||
p := make([]byte, 4)
|
||||
_, err = rand.Read(p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err = binary.Write(request, binary.LittleEndian, p); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
conn, err := openConnection(query.addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
_, err = conn.Write(request.Bytes())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to write")
|
||||
}
|
||||
|
||||
type resultData struct {
|
||||
data []byte
|
||||
bytes int
|
||||
err error
|
||||
}
|
||||
waitResult := make(chan resultData, 1)
|
||||
|
||||
go func() {
|
||||
response := make([]byte, 2048)
|
||||
n, errInner := conn.Read(response)
|
||||
if errInner != nil {
|
||||
waitResult <- resultData{err: errors.Wrap(errInner, "failed to read response")}
|
||||
return
|
||||
}
|
||||
if n > cap(response) {
|
||||
waitResult <- resultData{err: errors.New("read response over buffer capacity")}
|
||||
return
|
||||
}
|
||||
waitResult <- resultData{data: response, bytes: n}
|
||||
}()
|
||||
|
||||
var result resultData
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, errors.New("socket read timed out")
|
||||
|
||||
case result = <-waitResult:
|
||||
break
|
||||
}
|
||||
|
||||
if result.err != nil {
|
||||
return nil, result.err
|
||||
}
|
||||
|
||||
return result.data[:result.bytes], nil
|
||||
}
|
||||
|
||||
// GetPing sends and receives a packet to measure ping
|
||||
func (query *Query) GetPing(ctx context.Context) (ping time.Duration, err error) {
|
||||
t := time.Now()
|
||||
_, err = query.SendQuery(ctx, Ping)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
ping = time.Now().Sub(t)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (query *Query) GetInfo(ctx context.Context) (server Server, err error) {
|
||||
response, err := query.SendQuery(ctx, Info)
|
||||
if err != nil {
|
||||
return server, err
|
||||
}
|
||||
|
||||
ptr := 11
|
||||
ptr += 12
|
||||
|
||||
server.Password = (response[ptr] == 1)
|
||||
ptr++
|
||||
|
||||
server.Players = int(binary.LittleEndian.Uint16(response[ptr : ptr+2]))
|
||||
ptr += 2
|
||||
|
||||
server.MaxPlayers = int(binary.LittleEndian.Uint16(response[ptr : ptr+2]))
|
||||
ptr += 2
|
||||
|
||||
hostnameLen := int(binary.LittleEndian.Uint16(response[ptr : ptr+4]))
|
||||
ptr += 4
|
||||
|
||||
hostnameRaw := response[ptr : ptr+hostnameLen]
|
||||
ptr += hostnameLen
|
||||
|
||||
gamemodeLen := int(binary.LittleEndian.Uint16(response[ptr : ptr+4]))
|
||||
ptr += 4
|
||||
|
||||
gamemodeRaw := response[ptr : ptr+gamemodeLen]
|
||||
ptr += gamemodeLen
|
||||
|
||||
server.Gamemode = string(gamemodeRaw)
|
||||
server.Hostname = string(hostnameRaw)
|
||||
|
||||
server.Language = "-"
|
||||
return
|
||||
}
|
||||
|
||||
func openConnection(addr *net.UDPAddr) (conn *net.UDPConn, err error) {
|
||||
conn, err = net.DialUDP("udp", nil, addr)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to dial")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("Usage: ./vcmpquery ip:port")
|
||||
os.Exit(1)
|
||||
}
|
||||
qIp := args[1]
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
|
||||
defer cancel()
|
||||
query, err := NewQuery(qIp)
|
||||
if err != nil {
|
||||
fmt.Println("CRITICAL - Cannot connect;", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ping, err := query.GetPing(ctx)
|
||||
if err != nil {
|
||||
fmt.Println("CRITICAL - Cannot ping;", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
server, err := query.GetInfo(ctx)
|
||||
if err != nil {
|
||||
fmt.Println("CRITICAL - Cannot get info;", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("OK - Server online, ping: ", ping, "; Name: ", server.Hostname, ", Players: ", server.Players, "/", server.MaxPlayers)
|
||||
os.Exit(0)
|
||||
}
|
Loading…
Reference in New Issue
Block a user