cspj-application/server/internal/telegram/telegram.go

125 lines
2.7 KiB
Go
Raw Normal View History

2025-02-09 16:34:46 +08:00
package telegram
import (
"bufio"
2025-02-09 17:05:57 +08:00
"encoding/json"
2025-02-09 16:34:46 +08:00
"fmt"
"log"
"os"
2025-02-09 17:05:57 +08:00
"github.com/fsnotify/fsnotify"
2025-02-09 16:34:46 +08:00
tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const (
2025-02-09 17:05:57 +08:00
modsecLogFile = "/home/vomitblood/build/cspj-application/docker/chungus/logs/host-fs-auditlog.log"
2025-02-09 16:34:46 +08:00
telegramToken = "7215466800:AAGwjZnXEfbbjQiA0y7qtSzbSZNUWQJnyjo"
telegramChatID = 622943829
)
2025-02-09 17:05:57 +08:00
var lastReadPosition int64 = 0
type LogEntry struct {
AuditData struct {
Messages []string `json:"messages"`
} `json:"audit_data"`
}
2025-02-09 16:34:46 +08:00
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)
}
2025-02-09 17:05:57 +08:00
// Start watching the log file for changes
watchLogFile(bot)
}
func watchLogFile(bot *tg.BotAPI) {
watcher, err := fsnotify.NewWatcher()
2025-02-09 16:34:46 +08:00
if err != nil {
2025-02-09 17:05:57 +08:00
log.Fatal("Failed to initialize watcher:", err)
2025-02-09 16:34:46 +08:00
}
2025-02-09 17:05:57 +08:00
defer watcher.Close()
2025-02-09 16:34:46 +08:00
2025-02-09 17:05:57 +08:00
// Add log file to watcher
err = watcher.Add(modsecLogFile)
if err != nil {
log.Fatal("Failed to watch log file:", err)
}
log.Println("🔍 Monitoring log file for changes...")
2025-02-09 16:34:46 +08:00
for {
2025-02-09 17:05:57 +08:00
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("📄 Log file updated, reading new entries...")
readNewLines(bot)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("⚠️ Watcher error:", err)
2025-02-09 16:34:46 +08:00
}
2025-02-09 17:05:57 +08:00
}
}
func readNewLines(bot *tg.BotAPI) {
file, err := os.Open(modsecLogFile)
if err != nil {
log.Println("❌ Failed to reopen log file:", err)
return
}
defer file.Close()
// Move to the last read position
file.Seek(lastReadPosition, os.SEEK_SET)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
var logEntry LogEntry
// Try to parse JSON
if err := json.Unmarshal([]byte(line), &logEntry); err != nil {
log.Println("⚠️ Failed to parse JSON:", err)
continue // Skip invalid JSON lines
}
// Send only the first message from messages[]
if len(logEntry.AuditData.Messages) > 0 {
sendTelegramAlert(bot, logEntry.AuditData.Messages[0])
}
}
// Update last read position
lastReadPosition, _ = file.Seek(0, os.SEEK_CUR)
if err := scanner.Err(); err != nil {
log.Println("❌ Error reading log file:", err)
2025-02-09 16:34:46 +08:00
}
}
func sendTelegramAlert(bot *tg.BotAPI, message string) {
2025-02-09 17:05:57 +08:00
msg := tg.NewMessage(telegramChatID, fmt.Sprintf("🚨 *WEEWOO ALERT* 🚨\n%s", message))
_, err := bot.Send(msg)
if err != nil {
log.Println("❌ Failed to send Telegram message:", err)
}
2025-02-09 16:34:46 +08:00
}