tf2-checker/cmd/main.go

134 lines
3.3 KiB
Go
Raw Normal View History

2024-10-26 21:46:38 +02:00
package main
import (
2024-10-26 22:25:44 +02:00
"context"
2024-10-26 21:46:38 +02:00
"encoding/json"
"flag"
"io"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
2024-10-26 22:25:44 +02:00
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/webhook"
2024-10-26 21:46:38 +02:00
"github.com/rumblefrog/go-a2s"
)
type apiResponse struct {
Response struct {
Success bool `json:"success"`
UpToDate bool `json:"up_to_date"`
VersionIsListable bool `json:"version_is_listable"`
2024-10-26 22:25:44 +02:00
RequiredVersion int `json:"required_version"`
2024-10-26 21:46:38 +02:00
Message string `json:"message"`
} `json:"response"`
}
func main() {
// Steam API takes version to check if newer version is available
url := "https://api.steampowered.com/ISteamApps/UpToDateCheck/v1/?appid=440&version="
var ip string
var port int
var name string
var command string
2024-10-26 22:25:44 +02:00
var discordwebhook string
2024-10-26 21:46:38 +02:00
// Defining Flags
flag.StringVar(&ip, "ip", "127.0.0.1", "Specify ip to use.")
flag.IntVar(&port, "port", 27015, "Specify port to use.")
flag.StringVar(&name, "name", "Server", "Specify Server Name to use in the Webhook.")
flag.StringVar(&command, "command", "", "Specify Command to run if the Server is not on the newest Version.")
2024-10-26 22:25:44 +02:00
flag.StringVar(&discordwebhook, "webhook", "", "Specify Discord Webhook URL to use.")
2024-10-26 21:46:38 +02:00
flag.Parse()
2024-10-26 22:25:44 +02:00
// Creating Discord Webhook Client
whclient, err := webhook.NewWithURL(discordwebhook)
defer whclient.Close(context.TODO())
if err != nil {
log.Fatal("Failed to create Webhook message...")
}
2024-10-26 21:46:38 +02:00
// Creating A2S Request to Server
client, err := a2s.NewClient(ip + ":" + strconv.FormatUint(uint64(port), 10))
if err != nil {
log.Fatal("Creation of client failed: %w", err)
return
}
defer client.Close()
info, err := client.QueryInfo()
// Checking if we received an error while peforming A2S request
if err != nil {
if strings.HasSuffix(err.Error(), "connection refused") {
log.Fatal("Server refused connection.")
}
log.Fatal("Querying failed: %s", err)
}
log.Printf("Current Server Version: %s", info.Version)
// Creating httpClient and sending Request to Steam API
httpClient := http.Client{}
2024-10-26 22:25:44 +02:00
info.Version = "123"
2024-10-26 21:46:38 +02:00
req, err := http.NewRequest(http.MethodGet, url+info.Version, nil)
if err != nil {
log.Fatal(err)
}
res, err := httpClient.Do(req)
if err != nil {
log.Fatal(err)
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
response := apiResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
log.Fatal(err)
}
// Checking if Server is on the newest Server version, if false execute Command given by the user
if response.Response.UpToDate {
log.Printf("Server is already on newest version.")
} else {
log.Printf("Server needs to be updated, executing command %s", command)
cmd := exec.Command("bash", "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
2024-10-26 22:25:44 +02:00
discordEmbed := []discord.Embed{}
discordEmbed = append(discordEmbed, discord.NewEmbedBuilder().
SetTitlef("Update for Server %s", name).
SetDescriptionf("Updating to the newest Server Version %d", response.Response.RequiredVersion).
SetFooterTextf("Current Version %s", info.Version).
Build())
if _, err := whclient.CreateEmbeds(discordEmbed); err != nil {
log.Fatal("error sending message to webhook %s", err)
}
2024-10-26 21:46:38 +02:00
if err != nil {
log.Fatal(err)
}
}
}