2025-04-12 16:45:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2025-04-17 16:12:10 +02:00
|
|
|
"strings"
|
2025-04-17 16:13:29 +02:00
|
|
|
"sync"
|
2025-04-12 16:45:01 +02:00
|
|
|
"time"
|
|
|
|
|
|
2025-04-12 16:51:40 +02:00
|
|
|
_ "github.com/jackc/pgx/v4/stdlib"
|
2025-04-12 16:45:01 +02:00
|
|
|
"github.com/joho/godotenv"
|
2025-04-17 16:12:10 +02:00
|
|
|
"gopkg.in/gomail.v2"
|
2025-04-12 16:45:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Application struct {
|
|
|
|
|
ID int
|
2025-04-17 16:20:24 +02:00
|
|
|
Name string
|
2025-04-12 16:45:01 +02:00
|
|
|
PublicURL string
|
|
|
|
|
Online bool
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 16:12:10 +02:00
|
|
|
type Notification struct {
|
|
|
|
|
ID int
|
|
|
|
|
Enabled bool
|
|
|
|
|
Type string
|
|
|
|
|
SMTPHost sql.NullString
|
|
|
|
|
SMTPPort sql.NullInt64
|
|
|
|
|
SMTPFrom sql.NullString
|
|
|
|
|
SMTPUser sql.NullString
|
|
|
|
|
SMTPPass sql.NullString
|
|
|
|
|
SMTPSecure sql.NullBool
|
|
|
|
|
SMTPTo sql.NullString
|
|
|
|
|
TelegramChatID sql.NullString
|
|
|
|
|
TelegramToken sql.NullString
|
|
|
|
|
DiscordWebhook sql.NullString
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 16:13:29 +02:00
|
|
|
var (
|
|
|
|
|
notifications []Notification
|
|
|
|
|
notifMutex sync.RWMutex
|
|
|
|
|
)
|
2025-04-17 16:12:10 +02:00
|
|
|
|
2025-04-12 16:45:01 +02:00
|
|
|
func main() {
|
|
|
|
|
if err := godotenv.Load(); err != nil {
|
|
|
|
|
fmt.Println("No env vars found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbURL := os.Getenv("DATABASE_URL")
|
|
|
|
|
if dbURL == "" {
|
|
|
|
|
panic("DATABASE_URL not set")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db, err := sql.Open("pgx", dbURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("Database connection failed: %v\n", err))
|
|
|
|
|
}
|
|
|
|
|
defer db.Close()
|
|
|
|
|
|
2025-04-17 16:13:29 +02:00
|
|
|
// initial load
|
|
|
|
|
notifs, err := loadNotifications(db)
|
2025-04-17 16:12:10 +02:00
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("Failed to load notifications: %v", err))
|
|
|
|
|
}
|
2025-04-17 16:13:29 +02:00
|
|
|
notifMutex.Lock()
|
|
|
|
|
notifications = notifMutexCopy(notifs)
|
|
|
|
|
notifMutex.Unlock()
|
|
|
|
|
|
|
|
|
|
// reload notification configs every minute
|
|
|
|
|
go func() {
|
|
|
|
|
reloadTicker := time.NewTicker(time.Minute)
|
|
|
|
|
defer reloadTicker.Stop()
|
|
|
|
|
|
|
|
|
|
for range reloadTicker.C {
|
|
|
|
|
newNotifs, err := loadNotifications(db)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Failed to reload notifications: %v\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
notifMutex.Lock()
|
|
|
|
|
notifications = notifMutexCopy(newNotifs)
|
|
|
|
|
notifMutex.Unlock()
|
|
|
|
|
fmt.Println("Reloaded notification configurations")
|
|
|
|
|
}
|
|
|
|
|
}()
|
2025-04-17 16:12:10 +02:00
|
|
|
|
2025-04-17 16:13:29 +02:00
|
|
|
// clean up old entries hourly
|
2025-04-15 11:37:30 +02:00
|
|
|
go func() {
|
2025-04-17 16:12:10 +02:00
|
|
|
deletionTicker := time.NewTicker(time.Hour)
|
2025-04-15 11:37:30 +02:00
|
|
|
defer deletionTicker.Stop()
|
|
|
|
|
|
|
|
|
|
for range deletionTicker.C {
|
|
|
|
|
if err := deleteOldEntries(db); err != nil {
|
|
|
|
|
fmt.Printf("Error deleting old entries: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2025-04-17 16:12:10 +02:00
|
|
|
ticker := time.NewTicker(time.Second)
|
2025-04-12 16:45:01 +02:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
|
Timeout: 4 * time.Second,
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 11:37:30 +02:00
|
|
|
for now := range ticker.C {
|
|
|
|
|
if now.Second()%10 != 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 16:45:01 +02:00
|
|
|
apps := getApplications(db)
|
|
|
|
|
checkAndUpdateStatus(db, client, apps)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 16:13:29 +02:00
|
|
|
// helper to safely copy slice
|
|
|
|
|
func notifMutexCopy(src []Notification) []Notification {
|
|
|
|
|
copyDst := make([]Notification, len(src))
|
|
|
|
|
copy(copyDst, src)
|
|
|
|
|
return copyDst
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 16:12:10 +02:00
|
|
|
func loadNotifications(db *sql.DB) ([]Notification, error) {
|
|
|
|
|
rows, err := db.Query(
|
|
|
|
|
`SELECT id, enabled, type, "smtpHost", "smtpPort", "smtpFrom", "smtpUser", "smtpPass", "smtpSecure", "smtpTo",
|
|
|
|
|
"telegramChatId", "telegramToken", "discordWebhook"
|
|
|
|
|
FROM notification
|
|
|
|
|
WHERE enabled = true`,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
var configs []Notification
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var n Notification
|
|
|
|
|
if err := rows.Scan(
|
|
|
|
|
&n.ID, &n.Enabled, &n.Type,
|
|
|
|
|
&n.SMTPHost, &n.SMTPPort, &n.SMTPFrom, &n.SMTPUser, &n.SMTPPass, &n.SMTPSecure, &n.SMTPTo,
|
|
|
|
|
&n.TelegramChatID, &n.TelegramToken, &n.DiscordWebhook,
|
|
|
|
|
); err != nil {
|
|
|
|
|
fmt.Printf("Error scanning notification: %v\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
configs = append(configs, n)
|
|
|
|
|
}
|
|
|
|
|
return configs, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 11:37:30 +02:00
|
|
|
func deleteOldEntries(db *sql.DB) error {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
res, err := db.ExecContext(ctx,
|
2025-04-17 16:12:10 +02:00
|
|
|
`DELETE FROM uptime_history WHERE "createdAt" < now() - interval '30 days'`,
|
|
|
|
|
)
|
2025-04-15 11:37:30 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
affected, _ := res.RowsAffected()
|
|
|
|
|
fmt.Printf("Deleted %d old entries from uptime_history\n", affected)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 16:45:01 +02:00
|
|
|
func getApplications(db *sql.DB) []Application {
|
2025-04-17 16:12:10 +02:00
|
|
|
rows, err := db.Query(
|
2025-04-17 16:20:24 +02:00
|
|
|
`SELECT id, name, "publicURL", online FROM application WHERE "publicURL" IS NOT NULL`,
|
2025-04-17 16:12:10 +02:00
|
|
|
)
|
2025-04-12 16:45:01 +02:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Error fetching applications: %v\n", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
var apps []Application
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var app Application
|
2025-04-17 16:20:24 +02:00
|
|
|
if err := rows.Scan(&app.ID, &app.Name, &app.PublicURL, &app.Online); err != nil {
|
2025-04-12 16:45:01 +02:00
|
|
|
fmt.Printf("Error scanning row: %v\n", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
apps = append(apps, app)
|
|
|
|
|
}
|
|
|
|
|
return apps
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkAndUpdateStatus(db *sql.DB, client *http.Client, apps []Application) {
|
2025-04-17 17:36:58 +02:00
|
|
|
var notificationTemplate string
|
|
|
|
|
err := db.QueryRow("SELECT notification_text FROM settings LIMIT 1").Scan(¬ificationTemplate)
|
|
|
|
|
if err != nil {
|
|
|
|
|
notificationTemplate = "The application '!name' (!url) went !status!"
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 16:45:01 +02:00
|
|
|
for _, app := range apps {
|
2025-04-17 11:36:35 +02:00
|
|
|
httpCtx, httpCancel := context.WithTimeout(context.Background(), 4*time.Second)
|
2025-04-17 15:34:45 +02:00
|
|
|
req, err := http.NewRequestWithContext(httpCtx, "GET", app.PublicURL, nil)
|
2025-04-12 16:45:01 +02:00
|
|
|
if err != nil {
|
2025-04-17 16:20:24 +02:00
|
|
|
httpCancel()
|
2025-04-12 16:45:01 +02:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
2025-04-17 16:20:24 +02:00
|
|
|
httpCancel()
|
|
|
|
|
|
|
|
|
|
var isOnline bool
|
|
|
|
|
if err == nil {
|
|
|
|
|
isOnline = resp.StatusCode >= 200 && resp.StatusCode < 300 ||
|
|
|
|
|
resp.StatusCode == 405
|
|
|
|
|
resp.Body.Close()
|
|
|
|
|
}
|
2025-04-17 16:12:10 +02:00
|
|
|
|
|
|
|
|
if isOnline != app.Online {
|
|
|
|
|
status := "offline"
|
|
|
|
|
if isOnline {
|
|
|
|
|
status = "online"
|
|
|
|
|
}
|
2025-04-17 17:36:58 +02:00
|
|
|
|
|
|
|
|
message := notificationTemplate
|
|
|
|
|
message = strings.ReplaceAll(message, "!name", app.Name)
|
|
|
|
|
message = strings.ReplaceAll(message, "!url", app.PublicURL)
|
|
|
|
|
message = strings.ReplaceAll(message, "!status", status)
|
|
|
|
|
|
|
|
|
|
sendNotifications(message)
|
2025-04-12 16:45:01 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-17 11:36:35 +02:00
|
|
|
dbCtx, dbCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
|
_, err = db.ExecContext(dbCtx,
|
2025-04-15 11:37:30 +02:00
|
|
|
`UPDATE application SET online = $1 WHERE id = $2`,
|
2025-04-17 16:12:10 +02:00
|
|
|
isOnline, app.ID,
|
2025-04-12 16:45:01 +02:00
|
|
|
)
|
2025-04-17 16:20:24 +02:00
|
|
|
dbCancel()
|
2025-04-12 16:45:01 +02:00
|
|
|
if err != nil {
|
2025-04-17 17:36:58 +02:00
|
|
|
fmt.Printf("Error updating application %d: %v\n", app.ID, err)
|
2025-04-12 16:45:01 +02:00
|
|
|
}
|
2025-04-15 11:37:30 +02:00
|
|
|
|
2025-04-17 16:20:24 +02:00
|
|
|
dbCtx2, dbCancel2 := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
|
_, err = db.ExecContext(dbCtx2,
|
|
|
|
|
`INSERT INTO uptime_history("applicationId", online, "createdAt") VALUES ($1, $2, now())`,
|
2025-04-17 16:12:10 +02:00
|
|
|
app.ID, isOnline,
|
2025-04-15 11:37:30 +02:00
|
|
|
)
|
2025-04-17 16:20:24 +02:00
|
|
|
dbCancel2()
|
2025-04-15 11:37:30 +02:00
|
|
|
if err != nil {
|
2025-04-17 17:36:58 +02:00
|
|
|
fmt.Printf("Error uptime_history %d: %v\n", app.ID, err)
|
2025-04-15 11:37:30 +02:00
|
|
|
}
|
2025-04-12 16:45:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-04-17 16:12:10 +02:00
|
|
|
|
|
|
|
|
func sendNotifications(message string) {
|
2025-04-17 16:13:29 +02:00
|
|
|
notifMutex.RLock()
|
|
|
|
|
notifs := notifMutexCopy(notifications)
|
|
|
|
|
notifMutex.RUnlock()
|
|
|
|
|
|
|
|
|
|
for _, n := range notifs {
|
2025-04-17 16:12:10 +02:00
|
|
|
switch n.Type {
|
|
|
|
|
case "email":
|
|
|
|
|
if n.SMTPHost.Valid && n.SMTPTo.Valid {
|
|
|
|
|
sendEmail(n, message)
|
|
|
|
|
}
|
|
|
|
|
case "telegram":
|
|
|
|
|
if n.TelegramToken.Valid && n.TelegramChatID.Valid {
|
|
|
|
|
sendTelegram(n, message)
|
|
|
|
|
}
|
|
|
|
|
case "discord":
|
|
|
|
|
if n.DiscordWebhook.Valid {
|
|
|
|
|
sendDiscord(n, message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendEmail(n Notification, body string) {
|
|
|
|
|
// Initialize SMTP dialer with host, port, user, pass
|
|
|
|
|
d := gomail.NewDialer(
|
|
|
|
|
n.SMTPHost.String,
|
|
|
|
|
int(n.SMTPPort.Int64),
|
|
|
|
|
n.SMTPUser.String,
|
|
|
|
|
n.SMTPPass.String,
|
|
|
|
|
)
|
|
|
|
|
if n.SMTPSecure.Valid && n.SMTPSecure.Bool {
|
|
|
|
|
d.SSL = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m := gomail.NewMessage()
|
|
|
|
|
m.SetHeader("From", n.SMTPFrom.String)
|
|
|
|
|
m.SetHeader("To", n.SMTPTo.String)
|
|
|
|
|
m.SetHeader("Subject", "Uptime Notification")
|
|
|
|
|
m.SetBody("text/plain", body)
|
|
|
|
|
|
|
|
|
|
if err := d.DialAndSend(m); err != nil {
|
|
|
|
|
fmt.Printf("Email send failed: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendTelegram(n Notification, message string) {
|
|
|
|
|
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s",
|
|
|
|
|
n.TelegramToken.String,
|
|
|
|
|
n.TelegramChatID.String,
|
|
|
|
|
message,
|
|
|
|
|
)
|
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Telegram send failed: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp.Body.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sendDiscord(n Notification, message string) {
|
|
|
|
|
payload := fmt.Sprintf(`{"content": "%s"}`, message)
|
|
|
|
|
req, err := http.NewRequest("POST", n.DiscordWebhook.String, strings.NewReader(payload))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Discord request creation failed: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("Discord send failed: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp.Body.Close()
|
|
|
|
|
}
|