diff --git a/04rel64-java/Dockerfile b/04rel64-java/Dockerfile new file mode 100644 index 0000000..e2e19b2 --- /dev/null +++ b/04rel64-java/Dockerfile @@ -0,0 +1,54 @@ +FROM debian:stable + +MAINTAINER Thijn + +# 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"] diff --git a/04rel64-java/build.sh b/04rel64-java/build.sh new file mode 100755 index 0000000..ef6c016 --- /dev/null +++ b/04rel64-java/build.sh @@ -0,0 +1,2 @@ +#!/bin/bash +docker build -t vcmp_04rel64:java . diff --git a/04rel64-java/publish.sh b/04rel64-java/publish.sh new file mode 100755 index 0000000..a4bad9a --- /dev/null +++ b/04rel64-java/publish.sh @@ -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 diff --git a/04rel64-java/query b/04rel64-java/query new file mode 100755 index 0000000..9457ed6 Binary files /dev/null and b/04rel64-java/query differ diff --git a/04rel64-java/start.sh b/04rel64-java/start.sh new file mode 100644 index 0000000..26f02cd --- /dev/null +++ b/04rel64-java/start.sh @@ -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 diff --git a/04rel64-stable/Dockerfile b/04rel64-stable/Dockerfile new file mode 100644 index 0000000..0a83a00 --- /dev/null +++ b/04rel64-stable/Dockerfile @@ -0,0 +1,45 @@ +FROM debian:stable + +MAINTAINER Thijn + +# 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"] diff --git a/04rel64-stable/build.sh b/04rel64-stable/build.sh new file mode 100755 index 0000000..a67a67f --- /dev/null +++ b/04rel64-stable/build.sh @@ -0,0 +1,2 @@ +#!/bin/bash +docker build -t vcmp_04rel64:stable . diff --git a/04rel64-stable/publish.sh b/04rel64-stable/publish.sh new file mode 100755 index 0000000..d2fd93d --- /dev/null +++ b/04rel64-stable/publish.sh @@ -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 diff --git a/04rel64-stable/query b/04rel64-stable/query new file mode 100755 index 0000000..9457ed6 Binary files /dev/null and b/04rel64-stable/query differ diff --git a/04rel64-stable/start.sh b/04rel64-stable/start.sh new file mode 100644 index 0000000..26f02cd --- /dev/null +++ b/04rel64-stable/start.sh @@ -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 diff --git a/04rel64/Dockerfile b/04rel64/Dockerfile index 0d955a2..bc7ae9f 100644 --- a/04rel64/Dockerfile +++ b/04rel64/Dockerfile @@ -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"] diff --git a/04rel64/query b/04rel64/query new file mode 100755 index 0000000..9457ed6 Binary files /dev/null and b/04rel64/query differ diff --git a/ea/Dockerfile b/ea/Dockerfile new file mode 100644 index 0000000..9e74ae4 --- /dev/null +++ b/ea/Dockerfile @@ -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"] diff --git a/ea/build.sh b/ea/build.sh new file mode 100755 index 0000000..4b12eb1 --- /dev/null +++ b/ea/build.sh @@ -0,0 +1,2 @@ +#!/bin/bash +docker build -t vcmp_04rel64:ea . diff --git a/ea/dockerstart.sh b/ea/dockerstart.sh new file mode 100755 index 0000000..5a3e525 --- /dev/null +++ b/ea/dockerstart.sh @@ -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 diff --git a/ea/publish.sh b/ea/publish.sh new file mode 100755 index 0000000..79ed99d --- /dev/null +++ b/ea/publish.sh @@ -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 diff --git a/ea/query b/ea/query new file mode 100755 index 0000000..9457ed6 Binary files /dev/null and b/ea/query differ diff --git a/sqmod/Dockerfile b/sqmod/Dockerfile index 792fb8f..9b0581e 100644 --- a/sqmod/Dockerfile +++ b/sqmod/Dockerfile @@ -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"] diff --git a/sqmod/query b/sqmod/query new file mode 100755 index 0000000..9457ed6 Binary files /dev/null and b/sqmod/query differ diff --git a/vcmpquery/go.mod b/vcmpquery/go.mod new file mode 100644 index 0000000..a5cba69 --- /dev/null +++ b/vcmpquery/go.mod @@ -0,0 +1,7 @@ +module vcmpquery/query + +go 1.19 + +require ( + github.com/pkg/errors v0.8.0 +) diff --git a/vcmpquery/go.sum b/vcmpquery/go.sum new file mode 100644 index 0000000..3dfe462 --- /dev/null +++ b/vcmpquery/go.sum @@ -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= diff --git a/vcmpquery/main.go b/vcmpquery/main.go new file mode 100644 index 0000000..8326853 --- /dev/null +++ b/vcmpquery/main.go @@ -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) +} diff --git a/vcmpquery/nagios b/vcmpquery/nagios new file mode 100755 index 0000000..7ae7c41 Binary files /dev/null and b/vcmpquery/nagios differ diff --git a/vcmpquery/nagios.go b/vcmpquery/nagios.go new file mode 100644 index 0000000..c1427ea --- /dev/null +++ b/vcmpquery/nagios.go @@ -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) +}