| 1 | package workers |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "log" |
| 10 | "net/http" |
| 11 | "net/url" |
| 12 | "os" |
| 13 | "regexp" |
| 14 | "strings" |
| 15 | "sync" |
| 16 | "time" |
| 17 | |
| 18 | "github.com/stripsior/struxa/watchkeeper/internal/crypto" |
| 19 | ) |
| 20 | |
| 21 | const notifyHTTPTimeout = 8 |
| 22 | |
| 23 | type notifConfig struct { |
| 24 | appName string |
| 25 | appURL string |
| 26 | discordEnabled bool |
| 27 | discordURL string |
| 28 | telegramEnabled bool |
| 29 | telegramToken string |
| 30 | telegramChatID string |
| 31 | userConfigEnabled bool |
| 32 | } |
| 33 | |
| 34 | func loadNotifConfig(db * |
| 35 | rows, err := |
| 36 | if err != nil |
| 37 | return notifConfig{}, |
| 38 | } |
| 39 | defer rows.Close() |
| 40 | s := map[string |
| 41 | for rows.Next() |
| 42 | var k, v |
| 43 | if err := rows |
| 44 | return notifConfig{}, |
| 45 | } |
| 46 | s[k.String |
| 47 | } |
| 48 | if err := rows |
| 49 | return notifConfig{}, |
| 50 | } |
| 51 | cfg := notifConfig |
| 52 | appName: s[" |
| 53 | appURL: os.Getenv |
| 54 | discordEnabled: s[" |
| 55 | telegramEnabled: s[" |
| 56 | userConfigEnabled: s[" |
| 57 | } |
| 58 | if cfg.appName |
| 59 | cfg.appName = |
| 60 | } |
| 61 | if cfg.appURL |
| 62 | cfg.appURL = |
| 63 | } |
| 64 | if v := s |
| 65 | if plain, err |
| 66 | cfg.discordURL = |
| 67 | } |
| 68 | } |
| 69 | if v := s |
| 70 | if plain, err |
| 71 | cfg.telegramToken = |
| 72 | } |
| 73 | } |
| 74 | cfg.telegramChatID = |
| 75 | return cfg, nil |
| 76 | } |
| 77 | |
| 78 | type ownerNotifConfig struct { |
| 79 | discordURL string |
| 80 | telegramToken string |
| 81 | telegramChatID string |
| 82 | } |
| 83 | |
| 84 | func loadOwnerConfig(db * |
| 85 | var webhook, token |
| 86 | err := db. |
| 87 | `SELECT notification_discord_webhook, notification_telegram_token, notification_telegram_chat_id FROM user WHERE id=?`, |
| 88 | userID, |
| 89 | ).Scan(&webhook |
| 90 | if err != nil |
| 91 | return ownerNotifConfig{}, |
| 92 | } |
| 93 | var c ownerNotifConfig |
| 94 | if webhook.Valid |
| 95 | if plain, err |
| 96 | c.discordURL = |
| 97 | } |
| 98 | } |
| 99 | if token.Valid |
| 100 | if plain, err |
| 101 | c.telegramToken = |
| 102 | } |
| 103 | } |
| 104 | if chatID.Valid |
| 105 | c.telegramChatID = |
| 106 | } |
| 107 | return c, nil |
| 108 | } |
| 109 | |
| 110 | type statusNotifyJob struct { |
| 111 | serverID string |
| 112 | serverName string |
| 113 | uuid string |
| 114 | ownerID string |
| 115 | state string |
| 116 | } |
| 117 | |
| 118 | var ( |
| 119 | statusNotifyMu sync. |
| 120 | statusNotifyJobs = map[string |
| 121 | statusNotifyWake = make( |
| 122 | ) |
| 123 | |
| 124 | func enqueueStatusNotify(job statusNotifyJob |
| 125 | statusNotifyMu.Lock() |
| 126 | statusNotifyJobs[job.serverID |
| 127 | statusNotifyMu.Unlock() |
| 128 | select { |
| 129 | case statusNotifyWake <- struct |
| 130 | default: |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func runStatusNotifyDispatcher(db * |
| 135 | for range statusNotifyWake { |
| 136 | for { |
| 137 | statusNotifyMu.Lock() |
| 138 | var job statusNotifyJob |
| 139 | ok := false |
| 140 | for k, v |
| 141 | job = v |
| 142 | delete(statusNotifyJobs, k |
| 143 | ok = true |
| 144 | break |
| 145 | } |
| 146 | statusNotifyMu.Unlock() |
| 147 | if !ok { |
| 148 | break |
| 149 | } |
| 150 | notifyServerStatus(db, job |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func notifyServerStatus(db * |
| 156 | if state != " |
| 157 | return |
| 158 | } |
| 159 | cfg, err := |
| 160 | if err != nil |
| 161 | log.Printf("[status] failed to load notification config: |
| 162 | return |
| 163 | } |
| 164 | label := "online" |
| 165 | title := "Server online" |
| 166 | if state == " |
| 167 | label = "offline" |
| 168 | title = "Server offline" |
| 169 | } |
| 170 | short := uuid |
| 171 | if len(short) |
| 172 | short = short[ |
| 173 | } |
| 174 | msg := fmt. |
| 175 | n := notifMessage |
| 176 | title: title, |
| 177 | subtitle: fmt.Sprintf |
| 178 | detail: fmt.Sprintf |
| 179 | buttonLabel: "View server", |
| 180 | } |
| 181 | if cfg.appURL |
| 182 | n.buttonURL = |
| 183 | } |
| 184 | |
| 185 | if cfg.discordEnabled |
| 186 | if err := postDiscord |
| 187 | log.Printf("[status] discord notify failed for server |
| 188 | } |
| 189 | } |
| 190 | if cfg.telegramEnabled |
| 191 | if err := postTelegram |
| 192 | log.Printf("[status] telegram notify failed for server |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if ownerID == "" |
| 197 | return |
| 198 | } |
| 199 | oc, err := |
| 200 | if err != nil |
| 201 | log.Printf("[status] failed to load owner notification config for |
| 202 | return |
| 203 | } |
| 204 | if oc.discordURL |
| 205 | if err := postDiscord |
| 206 | log.Printf("[status] discord notify failed for owner |
| 207 | } |
| 208 | } |
| 209 | if oc.telegramToken |
| 210 | if err := postTelegram |
| 211 | log.Printf("[status] telegram notify failed for owner |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | type notifMessage struct { |
| 217 | title string |
| 218 | subtitle string |
| 219 | detail string |
| 220 | buttonLabel string |
| 221 | buttonURL string |
| 222 | } |
| 223 | |
| 224 | var telegramTokenPattern = regexp |
| 225 | |
| 226 | func safeDiscordURL(raw string |
| 227 | u, err := |
| 228 | if err != nil |
| 229 | return false |
| 230 | } |
| 231 | if u.Scheme |
| 232 | return false |
| 233 | } |
| 234 | return strings.HasPrefix( |
| 235 | } |
| 236 | |
| 237 | func postDiscord(rawURL string |
| 238 | if !safeDiscordURL(rawURL |
| 239 | return errors.New( |
| 240 | } |
| 241 | header := fmt. |
| 242 | inner := []any |
| 243 | if v.buttonURL |
| 244 | inner = append( |
| 245 | map[string]any |
| 246 | "type": 9, |
| 247 | "components": []any |
| 248 | map[string]any |
| 249 | }, |
| 250 | "accessory": map[string |
| 251 | }, |
| 252 | map[string]any |
| 253 | ) |
| 254 | } else { |
| 255 | inner = append( |
| 256 | } |
| 257 | inner = append( |
| 258 | map[string]any |
| 259 | map[string]any |
| 260 | ) |
| 261 | payload := map[string |
| 262 | "flags": 32768, |
| 263 | "allowed_mentions": map[string |
| 264 | "components": []any |
| 265 | map[string]any |
| 266 | }, |
| 267 | } |
| 268 | data, _ := |
| 269 | return httpPostJSON(withComponentsParam( |
| 270 | } |
| 271 | |
| 272 | func withComponentsParam(rawURL string |
| 273 | if strings.Contains( |
| 274 | return rawURL + " |
| 275 | } |
| 276 | return rawURL + " |
| 277 | } |
| 278 | |
| 279 | func postTelegram(token, |
| 280 | if !telegramTokenPattern.MatchString |
| 281 | return errors.New( |
| 282 | } |
| 283 | body, _ := |
| 284 | return httpPostJSON(fmt. |
| 285 | } |
| 286 | |
| 287 | func httpPostJSON(target string |
| 288 | client := &http |
| 289 | resp, err := |
| 290 | if err != nil |
| 291 | var urlErr *url |
| 292 | if errors.As( |
| 293 | return fmt.Errorf( |
| 294 | } |
| 295 | return err |
| 296 | } |
| 297 | defer resp.Body. |
| 298 | if resp.StatusCode |
| 299 | return fmt.Errorf( |
| 300 | } |
| 301 | return nil |
| 302 | } |