2022-11-13 19:17:07 +01:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
scn "blackforestbytes.com/simplecloudnotifier"
|
2024-07-15 17:26:55 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/db"
|
2023-07-27 17:44:06 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/db/simplectx"
|
2022-11-25 22:42:21 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/google"
|
2022-11-19 15:13:47 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
2022-11-23 19:32:23 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/push"
|
2022-11-13 19:17:07 +01:00
|
|
|
"context"
|
2023-01-17 22:03:27 +01:00
|
|
|
"errors"
|
2024-09-16 20:11:28 +02:00
|
|
|
"fmt"
|
2022-11-13 19:17:07 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-09-16 15:17:20 +02:00
|
|
|
golock "github.com/viney-shih/go-lock"
|
2024-07-15 17:26:55 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
2022-11-30 23:46:28 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/syncext"
|
2022-11-13 19:17:07 +01:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-11-18 23:12:37 +01:00
|
|
|
"strings"
|
2022-11-13 19:17:07 +01:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Application struct {
|
2022-11-25 22:42:21 +01:00
|
|
|
Config scn.Config
|
2024-07-15 17:26:55 +02:00
|
|
|
Gin *ginext.GinWrapper
|
2023-01-06 00:39:21 +01:00
|
|
|
Database *DBPool
|
2022-11-30 17:58:04 +01:00
|
|
|
Pusher push.NotificationClient
|
2022-11-25 22:42:21 +01:00
|
|
|
AndroidPublisher google.AndroidPublisherClient
|
|
|
|
Jobs []Job
|
|
|
|
stopChan chan bool
|
|
|
|
Port string
|
2022-11-30 23:46:28 +01:00
|
|
|
IsRunning *syncext.AtomicBool
|
2023-01-13 17:17:17 +01:00
|
|
|
RequestLogQueue chan models.RequestLog
|
2024-09-16 15:17:20 +02:00
|
|
|
MainDatabaseLock golock.RWMutex
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 00:39:21 +01:00
|
|
|
func NewApp(db *DBPool) *Application {
|
2022-11-20 12:59:43 +01:00
|
|
|
return &Application{
|
2024-09-16 15:17:20 +02:00
|
|
|
Database: db,
|
|
|
|
stopChan: make(chan bool),
|
|
|
|
IsRunning: syncext.NewAtomicBool(false),
|
|
|
|
RequestLogQueue: make(chan models.RequestLog, 1024),
|
|
|
|
MainDatabaseLock: golock.NewCASMutex(),
|
2022-11-20 12:59:43 +01:00
|
|
|
}
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
func (app *Application) Init(cfg scn.Config, g *ginext.GinWrapper, fb push.NotificationClient, apc google.AndroidPublisherClient, jobs []Job) {
|
2022-11-13 19:17:07 +01:00
|
|
|
app.Config = cfg
|
|
|
|
app.Gin = g
|
2022-11-30 17:58:04 +01:00
|
|
|
app.Pusher = fb
|
2022-11-25 22:42:21 +01:00
|
|
|
app.AndroidPublisher = apc
|
2022-11-20 15:40:19 +01:00
|
|
|
app.Jobs = jobs
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 20:21:49 +01:00
|
|
|
func (app *Application) Stop() {
|
2022-11-30 23:46:28 +01:00
|
|
|
// non-blocking send
|
|
|
|
select {
|
|
|
|
case app.stopChan <- true:
|
|
|
|
}
|
2022-11-23 20:21:49 +01:00
|
|
|
}
|
|
|
|
|
2022-11-13 19:17:07 +01:00
|
|
|
func (app *Application) Run() {
|
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
// ================== START HTTP ==================
|
2022-11-13 19:17:07 +01:00
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
addr := net.JoinHostPort(app.Config.ServerIP, app.Config.ServerPort)
|
2022-11-23 20:21:49 +01:00
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
errChan, httpserver := app.Gin.ListenAndServeHTTP(addr, func(port string) {
|
2022-11-23 20:21:49 +01:00
|
|
|
app.Port = port
|
2024-07-15 17:26:55 +02:00
|
|
|
app.IsRunning.Set(true)
|
|
|
|
})
|
2022-11-23 20:21:49 +01:00
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
// ================== START JOBS ==================
|
2022-11-20 15:40:19 +01:00
|
|
|
|
|
|
|
for _, job := range app.Jobs {
|
2023-01-13 17:17:17 +01:00
|
|
|
err := job.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Failed to start job")
|
|
|
|
}
|
2022-11-20 15:40:19 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
// ================== LISTEN FOR SIGNALS ==================
|
|
|
|
|
|
|
|
sigstop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigstop, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
2022-11-13 19:17:07 +01:00
|
|
|
select {
|
2022-12-22 10:21:10 +01:00
|
|
|
case <-sigstop:
|
2022-11-13 19:17:07 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
2022-11-20 15:40:19 +01:00
|
|
|
|
2022-11-13 19:17:07 +01:00
|
|
|
log.Info().Msg("Stopping HTTP-Server")
|
2022-11-20 15:40:19 +01:00
|
|
|
|
2022-11-13 19:17:07 +01:00
|
|
|
err := httpserver.Shutdown(ctx)
|
2022-11-20 15:40:19 +01:00
|
|
|
|
2022-11-13 19:17:07 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Info().Err(err).Msg("Error while stopping the http-server")
|
2022-11-20 15:40:19 +01:00
|
|
|
} else {
|
|
|
|
log.Info().Msg("Stopped HTTP-Server")
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-errChan:
|
|
|
|
log.Error().Err(err).Msg("HTTP-Server failed")
|
2022-11-23 20:21:49 +01:00
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
case <-app.stopChan:
|
2022-11-23 20:21:49 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
log.Info().Msg("Manually stopping HTTP-Server")
|
|
|
|
|
|
|
|
err := httpserver.Shutdown(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Info().Err(err).Msg("Error while stopping the http-server")
|
|
|
|
} else {
|
|
|
|
log.Info().Msg("Manually stopped HTTP-Server")
|
|
|
|
}
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
// ================== STOP JOBS ==================
|
|
|
|
|
2022-11-20 15:40:19 +01:00
|
|
|
for _, job := range app.Jobs {
|
2022-11-23 20:21:49 +01:00
|
|
|
job.Stop()
|
2022-11-20 15:40:19 +01:00
|
|
|
}
|
2022-11-30 23:46:28 +01:00
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
// ================== STOP DB ==================
|
2023-05-28 12:31:14 +02:00
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
{
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err := app.Database.Stop(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Failed to stop database")
|
|
|
|
}
|
2022-12-22 10:21:10 +01:00
|
|
|
}
|
2024-07-15 17:26:55 +02:00
|
|
|
log.Info().Msg("Stopped Databases")
|
2022-12-22 10:21:10 +01:00
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
// ================== FINISH ==================
|
2023-05-28 12:31:14 +02:00
|
|
|
|
2022-11-30 23:46:28 +01:00
|
|
|
app.IsRunning.Set(false)
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
2022-11-13 22:31:28 +01:00
|
|
|
|
|
|
|
func (app *Application) GenerateRandomAuthKey() string {
|
2023-01-15 06:30:30 +01:00
|
|
|
return scn.RandomAuthKey()
|
2022-11-13 22:31:28 +01:00
|
|
|
}
|
|
|
|
|
2022-11-25 22:42:21 +01:00
|
|
|
func (app *Application) VerifyProToken(ctx *AppContext, token string) (bool, error) {
|
2023-01-17 22:03:27 +01:00
|
|
|
|
2023-01-15 06:30:30 +01:00
|
|
|
if strings.HasPrefix(token, "ANDROID|v1|") {
|
2023-01-17 22:03:27 +01:00
|
|
|
subToken := token[len("ANDROID|v1|"):]
|
2023-01-15 06:30:30 +01:00
|
|
|
return app.VerifyAndroidProToken(ctx, subToken)
|
|
|
|
}
|
2023-01-17 22:03:27 +01:00
|
|
|
|
2022-11-25 22:42:21 +01:00
|
|
|
if strings.HasPrefix(token, "ANDROID|v2|") {
|
|
|
|
subToken := token[len("ANDROID|v2|"):]
|
|
|
|
return app.VerifyAndroidProToken(ctx, subToken)
|
|
|
|
}
|
2023-01-17 22:03:27 +01:00
|
|
|
|
|
|
|
if strings.HasPrefix(token, "IOS|v1|") {
|
|
|
|
return false, errors.New("invalid token-version: ios-v1")
|
|
|
|
}
|
|
|
|
|
2022-11-25 22:42:21 +01:00
|
|
|
if strings.HasPrefix(token, "IOS|v2|") {
|
|
|
|
subToken := token[len("IOS|v2|"):]
|
|
|
|
return app.VerifyIOSProToken(ctx, subToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Application) VerifyAndroidProToken(ctx *AppContext, token string) (bool, error) {
|
|
|
|
|
|
|
|
purchase, err := app.AndroidPublisher.GetProductPurchase(ctx, app.Config.GooglePackageName, app.Config.GoogleProProductID, token)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if purchase == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if purchase.PurchaseState == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if *purchase.PurchaseState != google.PurchaseStatePurchased {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Application) VerifyIOSProToken(ctx *AppContext, token string) (bool, error) {
|
|
|
|
return false, nil //TODO IOS
|
2022-11-13 22:31:28 +01:00
|
|
|
}
|
2022-11-18 21:25:40 +01:00
|
|
|
|
|
|
|
func (app *Application) Migrate() error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 24*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return app.Database.Migrate(ctx)
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (app *Application) NewSimpleTransactionContext(timeout time.Duration) *simplectx.SimpleContext {
|
2022-11-20 15:40:19 +01:00
|
|
|
ictx, cancel := context.WithTimeout(context.Background(), timeout)
|
2023-07-27 17:44:06 +02:00
|
|
|
return simplectx.CreateSimpleContext(ictx, cancel)
|
2022-11-20 15:40:19 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 17:26:55 +02:00
|
|
|
func (app *Application) getPermissions(ctx db.TxContext, hdr string) (models.PermissionSet, error) {
|
2022-11-18 23:12:37 +01:00
|
|
|
if hdr == "" {
|
2023-01-13 17:17:17 +01:00
|
|
|
return models.NewEmptyPermissions(), nil
|
2022-11-18 23:12:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(hdr, "SCN ") {
|
2023-01-13 17:17:17 +01:00
|
|
|
return models.NewEmptyPermissions(), nil
|
2022-11-18 23:12:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
key := strings.TrimSpace(hdr[4:])
|
|
|
|
|
2023-04-21 21:45:16 +02:00
|
|
|
tok, err := app.Database.Primary.GetKeyTokenByToken(ctx, key)
|
2022-11-18 23:12:37 +01:00
|
|
|
if err != nil {
|
2023-01-13 17:17:17 +01:00
|
|
|
return models.PermissionSet{}, err
|
2022-11-18 23:12:37 +01:00
|
|
|
}
|
|
|
|
|
2023-04-21 21:45:16 +02:00
|
|
|
if tok != nil {
|
|
|
|
|
|
|
|
err = app.Database.Primary.UpdateKeyTokenLastUsed(ctx, tok.KeyTokenID)
|
|
|
|
if err != nil {
|
|
|
|
return models.PermissionSet{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.PermissionSet{Token: tok}, nil
|
2022-11-18 23:12:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 17:17:17 +01:00
|
|
|
return models.NewEmptyPermissions(), nil
|
2022-11-18 21:25:40 +01:00
|
|
|
}
|
2022-11-19 15:13:47 +01:00
|
|
|
|
2022-12-22 11:22:36 +01:00
|
|
|
func (app *Application) GetOrCreateChannel(ctx *AppContext, userid models.UserID, displayChanName string, intChanName string) (models.Channel, error) {
|
2023-01-06 00:39:21 +01:00
|
|
|
existingChan, err := app.Database.Primary.GetChannelByName(ctx, userid, intChanName)
|
2022-11-19 15:13:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.Channel{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingChan != nil {
|
|
|
|
return *existingChan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribeKey := app.GenerateRandomAuthKey()
|
|
|
|
|
2024-05-26 19:30:13 +02:00
|
|
|
newChan, err := app.Database.Primary.CreateChannel(ctx, userid, displayChanName, intChanName, subscribeKey, nil)
|
2022-11-19 15:13:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.Channel{}, err
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:39:21 +01:00
|
|
|
_, err = app.Database.Primary.CreateSubscription(ctx, userid, newChan, true)
|
2022-11-19 15:13:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.Channel{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newChan, nil
|
|
|
|
}
|
2022-11-19 23:16:54 +01:00
|
|
|
|
2022-12-22 11:22:36 +01:00
|
|
|
func (app *Application) NormalizeChannelDisplayName(v string) string {
|
2023-05-28 13:14:05 +02:00
|
|
|
return strings.TrimSpace(v)
|
2022-12-22 11:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Application) NormalizeChannelInternalName(v string) string {
|
2023-05-28 13:14:05 +02:00
|
|
|
return strings.TrimSpace(v)
|
2022-11-19 23:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Application) NormalizeUsername(v string) string {
|
2023-05-28 13:14:05 +02:00
|
|
|
return strings.TrimSpace(v)
|
2022-11-19 23:16:54 +01:00
|
|
|
}
|
2022-11-20 15:40:19 +01:00
|
|
|
|
2024-06-01 15:37:59 +02:00
|
|
|
func (app *Application) DeliverMessage(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, error) {
|
2024-09-16 20:11:28 +02:00
|
|
|
fcmDelivID, errCode, err := app.Pusher.SendNotification(ctx, user, client, channel, msg)
|
2023-05-28 23:25:18 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Warn().Str("MessageID", msg.MessageID.String()).Str("ClientID", client.ClientID.String()).Err(err).Msg("FCM Delivery failed")
|
2024-09-16 20:11:28 +02:00
|
|
|
|
|
|
|
if errCode == "UNREGISTERED" {
|
|
|
|
|
|
|
|
log.Warn().Msg(fmt.Sprintf("Auto-Delete client %s of user %s (FCM is UNREGISTERED)", client.ClientID, user.UserID))
|
|
|
|
|
|
|
|
_, _ = simplectx.Run(ctx, func(ctx db.TxContext) (any, error) {
|
|
|
|
err = app.Database.Primary.DeleteClient(ctx, client.ClientID)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Str("ClientID", client.ClientID.String()).Msg("Failed to delete client")
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-28 23:25:18 +02:00
|
|
|
return "", err
|
2022-11-20 15:40:19 +01:00
|
|
|
}
|
2023-05-28 23:25:18 +02:00
|
|
|
return fcmDelivID, nil
|
2022-11-20 15:40:19 +01:00
|
|
|
}
|
2023-01-13 17:17:17 +01:00
|
|
|
|
|
|
|
func (app *Application) InsertRequestLog(data models.RequestLog) {
|
|
|
|
ok := syncext.WriteNonBlocking(app.RequestLogQueue, data)
|
|
|
|
if !ok {
|
|
|
|
log.Error().Msg("failed to insert request-log (queue full)")
|
|
|
|
}
|
|
|
|
}
|