60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package telegram
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
)
|
|
|
|
const (
|
|
modsecLogFile = "/tmp/host-fs-auditlog.log"
|
|
telegramToken = "7215466800:AAGwjZnXEfbbjQiA0y7qtSzbSZNUWQJnyjo"
|
|
telegramChatID = 622943829
|
|
)
|
|
|
|
func TelegramBotInit() {
|
|
bot, err := tg.NewBotAPI(telegramToken)
|
|
if err != nil {
|
|
log.Fatal("Failed to create Telegram bot:", err)
|
|
}
|
|
|
|
log.Println("Telegram bot connected")
|
|
|
|
// send init message on startup
|
|
testMsg := tg.NewMessage(telegramChatID, "I'm in")
|
|
_, err = bot.Send(testMsg)
|
|
if err != nil {
|
|
log.Fatal("Failed to send test message:", err)
|
|
}
|
|
|
|
// open the log file
|
|
logFile, err := os.Open(modsecLogFile)
|
|
if err != nil {
|
|
log.Fatal("Failed to open log file:", err)
|
|
}
|
|
defer logFile.Close()
|
|
|
|
// seek to the end of the file to read only new entries
|
|
logFile.Seek(0, io.SeekEnd)
|
|
|
|
reader := bufio.NewReader(logFile)
|
|
for {
|
|
line, err := reader.ReadString('\n')
|
|
if err == nil {
|
|
sendTelegramAlert(bot, line)
|
|
}
|
|
// maybe change this logic? interrupt vs polling??????
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|
|
|
|
func sendTelegramAlert(bot *tg.BotAPI, message string) {
|
|
msg := tg.NewMessage(telegramChatID, fmt.Sprintf("*WEEWOO ALERT*\n%s", message))
|
|
bot.Send(msg)
|
|
}
|