1
0
mirror of https://github.com/mgerb/ServerStatus synced 2026-01-10 03:03:04 +00:00

12 Commits

Author SHA1 Message Date
25ab128a58 update docker files 2024-03-03 16:09:20 -06:00
Ethorbit
e4320bcecb Make docker image unprivileged 2024-03-03 16:00:31 -06:00
50a06b98be Updates and maintenance
- switch from dep to go modules
- update to discordgo v0.27.1
- fix offline/online message bug
2024-03-03 11:32:08 -06:00
Ethorbit
ed232fc785 Fix outdated broken bot
discordgo upgraded from 0.20.0 to 0.24.0 which fixed the bot starting with a websocket error and remaining offline. Additionally, the command prefix and MessageHandling commands have been replaced with Discord's newer slash commands.
2024-03-03 11:24:22 -06:00
12c20b928c v0.8.0 2020-06-13 12:53:51 -05:00
2fdc0eddd3 update docker files
closes #27
2020-06-13 12:52:52 -05:00
18b1a0b6e3 fix: add retry to RCON servers
closes #28
2020-06-13 12:52:32 -05:00
bf79d376f3 fix: date format
closes #29
2020-06-13 12:47:00 -05:00
8f430b5982 update readme 2020-06-10 18:53:49 -05:00
56770b7b66 v0.7.0 2020-06-10 18:30:40 -05:00
f8d72bc297 feat: new features/improvements
- update dependencies
- add up/down time #14
- add workers with retry to help with spam
2020-06-10 18:28:41 -05:00
68bfed3f3b update readme 2019-01-19 20:30:27 -06:00
14 changed files with 222 additions and 212 deletions

View File

@@ -1,17 +1,26 @@
FROM golang:1.11.1-alpine
FROM golang:1.22-alpine3.19
WORKDIR /go/src/github.com/mgerb/ServerStatus
ADD . .
RUN apk add --no-cache git alpine-sdk
RUN go get -u github.com/golang/dep/cmd/dep
RUN dep ensure
RUN go get
RUN make linux
FROM alpine:3.8
FROM alpine:3.19
ARG UNAME="server-status"
ARG GNAME="server-status"
ARG UID=1000
ARG GID=1000
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
WORKDIR /server-status
COPY --from=0 /go/src/github.com/mgerb/ServerStatus/dist/ServerStatus-linux .
ENTRYPOINT ./ServerStatus-linux
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
RUN addgroup -g ${GID} "${GNAME}"
RUN adduser -D -u ${UID} -G "${GNAME}" "${UNAME}" &&\
chown "${UNAME}":"${GNAME}" -R /server-status/
USER ${UNAME}
ENTRYPOINT ./ServerStatus-linux

89
Gopkg.lock generated
View File

@@ -1,89 +0,0 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
digest = "1:87c2e02fb01c27060ccc5ba7c5a407cc91147726f8f40b70cceeedbc52b1f3a8"
name = "github.com/Sirupsen/logrus"
packages = ["."]
pruneopts = "UT"
revision = "e1e72e9de974bd926e5c56f83753fba2df402ce5"
version = "v1.3.0"
[[projects]]
branch = "master"
digest = "1:b7e05c8da029b985907b15e126cde5f42f2814b08211a184dac436eeec6fe780"
name = "github.com/anvie/port-scanner"
packages = [
".",
"predictors",
"predictors/webserver",
]
pruneopts = "UT"
revision = "8159197d3770eb6dbf3a9706a6d40462ebb69cec"
[[projects]]
digest = "1:4fd5ce7844c22e194005b9e12fee8adc70fb5ba0bbba9e1964d2e3d1f301d789"
name = "github.com/bwmarrin/discordgo"
packages = ["."]
pruneopts = "UT"
revision = "4a33b9bc7c56cfdb9bb244e33e83cb3941fe2bdc"
version = "v0.18.0"
[[projects]]
digest = "1:43dd08a10854b2056e615d1b1d22ac94559d822e1f8b6fcc92c1a1057e85188e"
name = "github.com/gorilla/websocket"
packages = ["."]
pruneopts = "UT"
revision = "ea4d1f681babbce9545c9c5f3d5194a789c89f5b"
version = "v1.2.0"
[[projects]]
branch = "master"
digest = "1:50993a8fbb3042b88dfecb8f4473a42f13ac70d6fd86f64df525a28b357c2d5b"
name = "github.com/kidoman/go-steam"
packages = ["."]
pruneopts = "UT"
revision = "2e40e0d508cbac591bab4ae18b231153295f3a0a"
[[projects]]
digest = "1:0a69a1c0db3591fcefb47f115b224592c8dfa4368b7ba9fae509d5e16cdc95c8"
name = "github.com/konsorten/go-windows-terminal-sequences"
packages = ["."]
pruneopts = "UT"
revision = "5c8c8bd35d3832f5d134ae1e1e375b69a4d25242"
version = "v1.0.1"
[[projects]]
branch = "master"
digest = "1:e035fb07be76ce6e4ce005add75491d9f0749403f595f153d6e2823a71c24149"
name = "golang.org/x/crypto"
packages = [
"nacl/secretbox",
"poly1305",
"salsa20/salsa",
"ssh/terminal",
]
pruneopts = "UT"
revision = "c4a91bd4f524f10d064139674cf55852e055ad01"
[[projects]]
branch = "master"
digest = "1:43cde116ff48f299eddb7e6515677e6d0a2c915854bb05a333877f07c3bb3033"
name = "golang.org/x/sys"
packages = [
"unix",
"windows",
]
pruneopts = "UT"
revision = "11f53e03133963fb11ae0588e08b5e0b85be8be5"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = [
"github.com/anvie/port-scanner",
"github.com/bwmarrin/discordgo",
"github.com/kidoman/go-steam",
]
solver-name = "gps-cdcl"
solver-version = 1

View File

@@ -1,42 +0,0 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
branch = "master"
name = "github.com/anvie/port-scanner"
[[constraint]]
name = "github.com/bwmarrin/discordgo"
version = "0.18.0"
[prune]
go-tests = true
unused-packages = true
[[constraint]]
branch = "master"
name = "github.com/kidoman/go-steam"

View File

@@ -1,18 +1,19 @@
{
"Token": "your bot token",
"RoomIDList":["room id list goes here"],
"RolesToNotify": ["<@&roleid>", "<@userid>"],
"GameStatus": "current playing game",
"PollingInterval": 10,
"Servers": [
{
"Name": "Your awesome server",
"Address": "game.server.com",
"Port": 80
}, {
"Name": "Another awesome server",
"Address": "awesome.server.com",
"Port": 8080
}
]
"Token": "your bot token",
"RoomIDList": ["room id list goes here"],
"RolesToNotify": ["<@&roleid>", "<@userid>"],
"GameStatus": "current playing game",
"PollingInterval": 10,
"Servers": [
{
"Name": "Your awesome server",
"Address": "game.server.com",
"Port": 80
},
{
"Name": "Another awesome server",
"Address": "awesome.server.com",
"Port": 8080
}
]
}

View File

@@ -16,16 +16,19 @@ type configStruct struct {
Token string `json:"Token"`
RoomIDList []string `json:"RoomIDList"`
RolesToNotify []string `json:"RolesToNotify"`
Servers []server `json:"Servers"`
Servers []Server `json:"Servers"`
GameStatus string `json:"GameStatus"`
PollingInterval time.Duration `json:"PollingInterval"`
}
type server struct {
type Server struct {
Name string `json:"Name"`
Address string `json:"Address"`
Port int `json:"Port"`
Online bool `json:"Online,omitempty"`
// OnlineTimestamp - time of when the server last came online
OnlineTimestamp time.Time
OfflineTimestamp time.Time
}
func Configure() {

View File

@@ -1,4 +1,4 @@
version: "2"
version: "3"
services:
server-status:

17
go.mod Normal file
View File

@@ -0,0 +1,17 @@
module github.com/mgerb/ServerStatus
go 1.22.0
require (
github.com/anvie/port-scanner v0.0.0-20180225151059-8159197d3770
github.com/bwmarrin/discordgo v0.27.1
github.com/kidoman/go-steam v0.0.0-20141221015629-2e40e0d508cb
)
require (
github.com/Sirupsen/logrus v1.0.6 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
)

19
go.sum Normal file
View File

@@ -0,0 +1,19 @@
github.com/Sirupsen/logrus v1.0.6 h1:HCAGQRk48dRVPA5Y+Yh0qdCSTzPOyU1tBJ7Q9YzotII=
github.com/Sirupsen/logrus v1.0.6/go.mod h1:rmk17hk6i8ZSAJkSDa7nOxamrG+SP4P0mm+DAvExv4U=
github.com/anvie/port-scanner v0.0.0-20180225151059-8159197d3770 h1:1KEvfMGAjISVzk3Ti6pfaOgtoC3naoU0LfiJooZDNO8=
github.com/anvie/port-scanner v0.0.0-20180225151059-8159197d3770/go.mod h1:QGzdstKeoHmMWwi9oNHZ7DQzEj9pi7H42171pkj9htk=
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kidoman/go-steam v0.0.0-20141221015629-2e40e0d508cb h1:+3H4rb1CvcN/BEuBlk774uxxab272M6YU9nb9p/GZm8=
github.com/kidoman/go-steam v0.0.0-20141221015629-2e40e0d508cb/go.mod h1:PKiM4eL8SN6mJ38F9m6ZJpVtJKnmSxOxBsi2p3TOru4=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View File

@@ -22,13 +22,13 @@ func main() {
bot.Connect(config.Config.Token)
// add handlers
bot.AddHandler(serverstatus.MessageHandler)
bot.AddHandler(serverstatus.InteractionHandler)
//start websocket to listen for messages
bot.Start()
//start server status task
serverstatus.Start()
serverstatus.Start()
// Simple way to keep program running until CTRL-C is pressed.
<-make(chan struct{})

View File

@@ -4,7 +4,7 @@ run:
go run ./src/main.go
linux:
go build -o ./dist/ServerStatus-linux -ldflags="-X main.version=${VERSION}" ./main.go
GOOS=linux GOARCH=amd64 go build -o ./dist/ServerStatus-linux -ldflags="-X main.version=${VERSION}" ./main.go
mac:
GOOS=darwin GOARCH=amd64 go build -o ./dist/ServerStatus-mac -ldflags="-X main.version=${VERSION}" ./main.go

View File

@@ -1,19 +1,27 @@
# Server Status
Scans a list of servers checking checking which are currently online.
This bot will send a chat notification when the status of a server changes (goes on or offline).
Monitors a list of servers and sends a chat notification when a server goes on or offline.
## Features
- send channel notifications
- track server up/down time
- **TCP** - should work with all servers
- **UDP** - [Source RCON Protocol](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol) is supported
- [Docker](https://hub.docker.com/r/mgerb/server-status)
### Want to see more features?
[Submit a new issue](https://github.com/mgerb/ServerStatus/issues/new/choose)
## Configuration
- Download the latest release [here](https://github.com/mgerb/ServerStatus/releases)
- Add your bot token as well as other configurations to config.json
- Add your bot token as well as other configurations to **config.json**
- Execute the OS specific binary!
### Mentioning Roles/Users
- you must first get your role/user id (see below of obtaining ID's)
- for user `<@userid>`
- for role `<@&roleid>`
- list of user/role ID's must be in the following format (see below for obtaining ID's)
- `<@userid>`
- `<@&roleid>`
### Polling Interval
The polling interval is how often the bot will try to ping the servers.
@@ -21,7 +29,7 @@ A good interval is 10 seconds, but this may need some adjustment if
it happens to be spamming notifications.
- time in seconds
- configurable in `config.json`
- configurable in **config.json**
## With Docker
@@ -42,9 +50,9 @@ services:
```
## Usage
To get the current status of your servers simply type `!ServerStatus` in chat.
To get the current status of your servers simply type `/server-status` in chat.
![Server Status](https://i.imgur.com/ZzQSBJp.png)
![Server Status](./readme_files/screenshot1.png)
## Compiling from source
- Make sure Go and Make are installed
@@ -56,5 +64,4 @@ https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-
### How to get your room ID
To get IDs, turn on Developer Mode in the Discord client (User Settings -> Appearance) and then right-click your name/icon anywhere in the client and select Copy ID.
<img src="https://camo.githubusercontent.com/9f759ec8b45a6e9dd2242bc64c82897c74f84a25/687474703a2f2f692e696d6775722e636f6d2f47684b70424d512e676966"/>
<img src="./readme_files/screenshot2.gif"/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 838 KiB

View File

@@ -1,12 +1,14 @@
package serverstatus
import (
"fmt"
"log"
"strconv"
"strings"
"sync"
"time"
"github.com/anvie/port-scanner"
portscanner "github.com/anvie/port-scanner"
"github.com/bwmarrin/discordgo"
steam "github.com/kidoman/go-steam"
"github.com/mgerb/ServerStatus/bot"
@@ -19,16 +21,36 @@ const (
blue = 0x42adf4
)
// Start - start port scanner and bot listeners
// Start - add command, start port scanner and bot listeners
func Start() {
//add command
_, err := bot.Session.ApplicationCommandCreate(bot.Session.State.User.ID, "", &discordgo.ApplicationCommand{
Name: "server-status",
Description: "Get the status of the servers.",
})
if err != nil {
log.Panicf("Cannot create status command '%v'", err)
}
//set each server status as online to start
for i := range config.Config.Servers {
config.Config.Servers[i].Online = true
config.Config.Servers[i].OnlineTimestamp = time.Now()
config.Config.Servers[i].OfflineTimestamp = time.Now()
}
err := bot.Session.UpdateStatus(0, config.Config.GameStatus)
err = bot.Session.UpdateStatusComplex(discordgo.UpdateStatusData{
Status: "online",
Activities: []*discordgo.Activity{
&discordgo.Activity{
Type: discordgo.ActivityTypeGame,
Name: config.Config.GameStatus,
},
},
})
sendMessageToRooms(blue, "Server Status", "Bot started! Type !ServerStatus to see the status of your servers :smiley:", false)
sendMessageToRooms(blue, "Server Status", "Bot started! Type /server-status to see the status of your servers :smiley:", false)
if err != nil {
log.Println(err)
@@ -48,49 +70,76 @@ func scanServers() {
for {
for index, server := range config.Config.Servers {
prevServerUp := server.Online //set value to previous server status
// use waitgroup to scan all servers concurrently
var wg sync.WaitGroup
serverScanner := portscanner.NewPortScanner(server.Address, time.Second*2, 1)
serverUp := serverScanner.IsOpen(server.Port) //check if the port is open
// if server isn't up check RCON protocol (UDP)
if !serverUp {
host := server.Address + ":" + strconv.Itoa(server.Port)
steamConnection, err := steam.Connect(host)
if err == nil {
defer steamConnection.Close()
_, err := steamConnection.Ping()
if err == nil {
serverUp = true
}
}
}
if serverUp && serverUp != prevServerUp {
sendMessageToRooms(green, server.Name, "Is now online :smiley:", true)
} else if !serverUp && serverUp != prevServerUp {
sendMessageToRooms(red, server.Name, "Has gone offline :frowning2:", true)
}
config.Config.Servers[index].Online = serverUp
for index := range config.Config.Servers {
wg.Add(1)
go worker(&config.Config.Servers[index], &wg)
}
wg.Wait()
time.Sleep(time.Second * config.Config.PollingInterval)
}
}
func worker(server *config.Server, wg *sync.WaitGroup) {
defer wg.Done()
prevServerUp := server.Online //set value to previous server status
var serverUp bool
retryCounter := 0
// try reconnecting 5 times if failure persists (every 2 seconds)
for {
serverScanner := portscanner.NewPortScanner(server.Address, time.Second*2, 1)
serverUp = serverScanner.IsOpen(server.Port) //check if the port is open
// if server isn't up check RCON protocol (UDP)
if !serverUp {
host := server.Address + ":" + strconv.Itoa(server.Port)
steamConnection, err := steam.Connect(host)
if err == nil {
defer steamConnection.Close()
_, err := steamConnection.Ping()
if err == nil {
serverUp = true
}
}
}
if serverUp || retryCounter >= 5 {
break
}
retryCounter++
time.Sleep(time.Second * 2)
}
if serverUp && serverUp != prevServerUp {
server.OnlineTimestamp = time.Now()
sendMessageToRooms(green, server.Name, "Is now online :smiley:", true)
} else if !serverUp && serverUp != prevServerUp {
server.OfflineTimestamp = time.Now()
sendMessageToRooms(red, server.Name, "Has gone offline :frowning2:", true)
}
server.Online = serverUp
}
func sendMessageToRooms(color int, title, description string, mentionRoles bool) {
for _, roomID := range config.Config.RoomIDList {
if mentionRoles {
content := strings.Join(config.Config.RolesToNotify, " ")
bot.Session.ChannelMessageSend(roomID, content)
}
sendEmbededMessage(roomID, color, title, description)
sendEmbeddedMessage(roomID, color, title, description)
}
}
func sendEmbededMessage(roomID string, color int, title, description string) {
func sendEmbeddedMessage(roomID string, color int, title, description string) {
embed := &discordgo.MessageEmbed{
Color: color,
@@ -101,22 +150,58 @@ func sendEmbededMessage(roomID string, color int, title, description string) {
bot.Session.ChannelMessageSendEmbed(roomID, embed)
}
// MessageHandler will be called every time a new
// message is created on any channel that the autenticated bot has access to.
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// InteractionHandler will be called every time an interaction from a user occurs
// Command interaction handling requires bot command scope
func InteractionHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
// A user is calling us with our status command
if i.ApplicationCommandData().Name == "server-status" {
online := ""
offline := ""
// Ignore all messages created by the bot itself
if m.Author.ID == bot.BotID {
return
}
if m.Content == "!ServerStatus" {
for _, server := range config.Config.Servers {
if server.Online {
sendEmbededMessage(m.ChannelID, green, server.Name, "Online!")
online = online + server.Name + " : " + fmtDuration(time.Since(server.OnlineTimestamp)) + "\n"
} else {
sendEmbededMessage(m.ChannelID, red, server.Name, "Offline!")
offline = offline + server.Name + " : " + fmtDuration(time.Since(server.OfflineTimestamp)) + "\n"
}
}
embeds := []*discordgo.MessageEmbed{}
if online != "" {
embeds = append(embeds, &discordgo.MessageEmbed{
Title: ":white_check_mark: Online",
Color: green,
Description: online,
})
}
if offline != "" {
embeds = append(embeds, &discordgo.MessageEmbed{
Title: ":x: Offline",
Color: red,
Description: offline,
})
}
// Only one message can be an interaction response. Messages can only contain up to 10 embeds.
// Our message will therefore instead be two embeds (online and offline), each with a list of servers in text.
// Embed descriptions can be ~4096 characters, so no limits should get hit with this.
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: embeds,
},
})
}
}
func fmtDuration(d time.Duration) string {
days := int(d.Hours()) / 24
hours := int(d.Hours()) % 24
minutes := int(d.Minutes()) % 60
seconds := int(d.Seconds()) % 60
return fmt.Sprintf("%dd %dh %dm %ds", days, hours, minutes, seconds)
}