rogueserver/api/daily/common.go

106 lines
2.3 KiB
Go
Raw Permalink Normal View History

2024-04-29 14:26:46 -07:00
/*
Copyright (C) 2024 Pagefault Games
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-04-29 12:32:58 -07:00
package daily
2024-03-16 18:51:13 -07:00
import (
2024-03-18 16:55:02 -07:00
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/binary"
2024-03-17 10:18:51 -07:00
"fmt"
2024-03-16 18:51:13 -07:00
"log"
2024-03-18 16:55:02 -07:00
"os"
"time"
2024-03-16 18:51:13 -07:00
2024-04-29 12:22:27 -07:00
"github.com/pagefaultgames/rogueserver/db"
2024-04-16 18:01:24 -07:00
"github.com/robfig/cron/v3"
2024-03-16 18:51:13 -07:00
)
2024-03-18 16:55:02 -07:00
const secondsPerDay = 60 * 60 * 24
2024-03-16 18:51:13 -07:00
var (
2024-04-17 16:03:25 -07:00
scheduler = cron.New(cron.WithLocation(time.UTC))
2024-04-14 17:13:45 -07:00
secret []byte
2024-03-16 18:51:13 -07:00
)
func Init() error {
2024-04-14 17:13:45 -07:00
var err error
secret, err = os.ReadFile("secret.key")
2024-03-16 18:51:13 -07:00
if err != nil {
2024-03-18 16:55:02 -07:00
if !os.IsNotExist(err) {
2024-04-08 15:15:09 -07:00
return fmt.Errorf("failed to read daily seed secret: %s", err)
2024-03-18 16:55:02 -07:00
}
2024-03-18 16:55:02 -07:00
newSecret := make([]byte, 32)
_, err := rand.Read(newSecret)
if err != nil {
2024-04-08 15:15:09 -07:00
return fmt.Errorf("failed to generate daily seed secret: %s", err)
}
2024-03-18 16:55:02 -07:00
err = os.WriteFile("secret.key", newSecret, 0400)
if err != nil {
2024-04-08 15:15:09 -07:00
return fmt.Errorf("failed to write daily seed secret: %s", err)
2024-03-18 16:55:02 -07:00
}
secret = newSecret
}
seed, err := recordNewDaily()
2024-03-18 16:55:02 -07:00
if err != nil {
2024-04-01 19:54:55 -07:00
log.Print(err)
2024-03-16 18:51:13 -07:00
}
2024-04-01 19:54:55 -07:00
log.Printf("Daily Run Seed: %s", seed)
_, err = scheduler.AddFunc("@daily", func() {
time.Sleep(time.Second)
seed, err = recordNewDaily()
2024-04-16 18:01:24 -07:00
if err != nil {
log.Printf("error while recording new daily: %s", err)
} else {
log.Printf("Daily Run Seed: %s", seed)
2024-04-16 18:01:24 -07:00
}
})
if err != nil {
return err
}
2024-04-16 18:01:24 -07:00
scheduler.Start()
2024-04-08 15:15:09 -07:00
return nil
2024-03-16 18:51:13 -07:00
}
func Seed() string {
2024-04-14 17:13:45 -07:00
return base64.StdEncoding.EncodeToString(deriveSeed(time.Now().UTC()))
}
2024-04-14 17:13:45 -07:00
func deriveSeed(seedTime time.Time) []byte {
2024-03-18 16:55:02 -07:00
day := make([]byte, 8)
binary.BigEndian.PutUint64(day, uint64(seedTime.Unix()/secondsPerDay))
2024-04-14 17:13:45 -07:00
hashedSeed := md5.Sum(append(day, secret...))
2024-03-18 16:55:02 -07:00
return hashedSeed[:]
}
2024-04-14 17:13:45 -07:00
func recordNewDaily() (string, error) {
return db.TryAddDailyRun(Seed())
2024-04-14 17:13:45 -07:00
}