2022-08-10 05:10:47 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2024-05-30 06:20:24 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/encryption/coldstorage"
|
2023-09-19 00:22:50 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/encryption/keygen"
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2024-07-21 02:44:22 +00:00
|
|
|
"github.com/SherClockHolmes/webpush-go"
|
2022-10-21 04:02:30 +00:00
|
|
|
"github.com/google/uuid"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
2023-08-17 05:09:04 +00:00
|
|
|
// Version of the config format - when new fields are added, it will attempt
|
|
|
|
// to write the settings.toml to disk so new defaults populate.
|
2024-10-04 04:23:12 +00:00
|
|
|
var currentVersion = 5
|
2023-08-17 05:09:04 +00:00
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
// Current loaded settings.json
|
|
|
|
var Current = DefaultVariable()
|
|
|
|
|
|
|
|
// Variable configuration attributes (loaded from settings.json).
|
|
|
|
type Variable struct {
|
2023-08-17 05:09:04 +00:00
|
|
|
Version int
|
2022-09-27 02:12:24 +00:00
|
|
|
BaseURL string
|
|
|
|
AdminEmail string
|
2022-10-21 04:02:30 +00:00
|
|
|
CronAPIKey string
|
2022-09-27 02:12:24 +00:00
|
|
|
Mail Mail
|
|
|
|
Redis Redis
|
|
|
|
Database Database
|
2023-02-06 04:26:36 +00:00
|
|
|
BareRTC BareRTC
|
2023-08-17 05:09:04 +00:00
|
|
|
Maintenance Maintenance
|
2023-09-19 00:22:50 +00:00
|
|
|
Encryption Encryption
|
2024-10-04 04:23:12 +00:00
|
|
|
SignedPhoto SignedPhoto
|
2024-07-21 02:44:22 +00:00
|
|
|
WebPush WebPush
|
2024-05-20 01:33:28 +00:00
|
|
|
Turnstile Turnstile
|
2022-09-27 02:12:24 +00:00
|
|
|
UseXForwardedFor bool
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultVariable returns the default settings.json data.
|
|
|
|
func DefaultVariable() Variable {
|
|
|
|
return Variable{
|
|
|
|
BaseURL: "http://localhost:8080",
|
|
|
|
Mail: Mail{
|
|
|
|
Enabled: false,
|
|
|
|
Host: "localhost",
|
|
|
|
Port: 25,
|
|
|
|
From: "no-reply@localhost",
|
|
|
|
},
|
|
|
|
Redis: Redis{
|
|
|
|
Host: "localhost",
|
|
|
|
Port: 6379,
|
|
|
|
},
|
|
|
|
Database: Database{
|
|
|
|
SQLite: "database.sqlite",
|
2022-08-26 04:21:46 +00:00
|
|
|
Postgres: "host=localhost user=nonshy password=nonshy dbname=nonshy port=5679 sslmode=disable TimeZone=America/Los_Angeles",
|
2022-08-10 05:10:47 +00:00
|
|
|
},
|
2022-10-21 04:02:30 +00:00
|
|
|
CronAPIKey: uuid.New().String(),
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadSettings loads the settings.json file or, if not existing, creates it with the default settings.
|
|
|
|
func LoadSettings() {
|
2023-09-19 00:22:50 +00:00
|
|
|
var writeSettings bool
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
if _, err := os.Stat(SettingsPath); !os.IsNotExist(err) {
|
|
|
|
log.Info("Loading settings from %s", SettingsPath)
|
2024-05-20 01:33:28 +00:00
|
|
|
content, err := os.ReadFile(SettingsPath)
|
2022-08-10 05:10:47 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("LoadSettings: couldn't read settings.json: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
var v Variable
|
|
|
|
err = json.Unmarshal(content, &v)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("LoadSettings: couldn't parse settings.json: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
Current = v
|
|
|
|
} else {
|
2023-08-17 05:09:04 +00:00
|
|
|
WriteSettings()
|
2022-08-10 05:10:47 +00:00
|
|
|
log.Warn("NOTICE: Created default settings.json file - review it and configure mail servers and database!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no DB configured, exit now.
|
|
|
|
if !Current.Database.IsSQLite && !Current.Database.IsPostgres {
|
|
|
|
log.Error("No database configured in settings.json. Choose SQLite or Postgres and update the DB connector string!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-08-17 05:09:04 +00:00
|
|
|
|
2023-09-19 00:22:50 +00:00
|
|
|
// Initialize the AES encryption key.
|
|
|
|
if len(Current.Encryption.AESKey) == 0 {
|
|
|
|
log.Warn("NOTICE: rolling a random 32-byte (256-bit) AES encryption key for the settings file")
|
|
|
|
aesKey, err := keygen.NewAESKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Couldn't generate AES key: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
Current.Encryption.AESKey = aesKey
|
|
|
|
writeSettings = true
|
|
|
|
}
|
|
|
|
|
2024-05-30 07:12:41 +00:00
|
|
|
// Initialize the cold storage RSA keys.
|
2024-05-30 06:20:24 +00:00
|
|
|
if len(Current.Encryption.ColdStorageRSAPublicKey) == 0 {
|
|
|
|
x509publicKey, err := coldstorage.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Initializing cold storage: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the public key in the settings.json.
|
|
|
|
Current.Encryption.ColdStorageRSAPublicKey = x509publicKey
|
|
|
|
writeSettings = true
|
|
|
|
}
|
|
|
|
|
2024-07-21 02:44:22 +00:00
|
|
|
// Initialize the VAPID keys for Web Push Notification.
|
|
|
|
if len(Current.WebPush.VAPIDPublicKey) == 0 {
|
|
|
|
privateKey, publicKey, err := webpush.GenerateVAPIDKeys()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Initializing VAPID keys for Web Push: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
Current.WebPush.VAPIDPrivateKey = privateKey
|
|
|
|
Current.WebPush.VAPIDPublicKey = publicKey
|
|
|
|
writeSettings = true
|
|
|
|
}
|
|
|
|
|
2024-10-04 04:23:12 +00:00
|
|
|
// Initialize JWT token for SignedPhoto feature.
|
|
|
|
if Current.SignedPhoto.JWTSecret == "" {
|
|
|
|
Current.SignedPhoto.JWTSecret = uuid.New().String()
|
|
|
|
writeSettings = true
|
|
|
|
}
|
|
|
|
|
2023-08-17 05:09:04 +00:00
|
|
|
// Have we added new config fields? Save the settings.json.
|
2023-09-19 00:22:50 +00:00
|
|
|
if Current.Version != currentVersion || writeSettings {
|
2023-08-17 05:09:04 +00:00
|
|
|
log.Warn("New options are available for your settings.json file. Your settings will be re-saved now.")
|
|
|
|
Current.Version = currentVersion
|
|
|
|
if err := WriteSettings(); err != nil {
|
|
|
|
log.Error("Couldn't write your settings.json file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteSettings will commit the settings.json to disk.
|
|
|
|
func WriteSettings() error {
|
|
|
|
log.Error("Note: initial settings.json was written to disk.")
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
err := enc.Encode(Current)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("WriteSettings: couldn't marshal settings: %s", err))
|
|
|
|
}
|
|
|
|
|
2024-05-20 01:33:28 +00:00
|
|
|
return os.WriteFile(SettingsPath, buf.Bytes(), 0600)
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mail settings.
|
|
|
|
type Mail struct {
|
|
|
|
Enabled bool
|
|
|
|
Host string // localhost
|
|
|
|
Port int // 25
|
|
|
|
From string // noreply@localhost
|
|
|
|
Username string // SMTP credentials
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redis settings.
|
|
|
|
type Redis struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
DB int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Database settings.
|
|
|
|
type Database struct {
|
|
|
|
IsSQLite bool
|
|
|
|
IsPostgres bool
|
|
|
|
SQLite string
|
|
|
|
Postgres string
|
|
|
|
}
|
2023-02-06 04:26:36 +00:00
|
|
|
|
|
|
|
// BareRTC chat room settings.
|
|
|
|
type BareRTC struct {
|
|
|
|
JWTSecret string
|
|
|
|
URL string
|
|
|
|
}
|
2023-08-17 05:09:04 +00:00
|
|
|
|
|
|
|
// Maintenance mode settings.
|
|
|
|
type Maintenance struct {
|
|
|
|
PauseSignup bool
|
|
|
|
PauseLogin bool
|
|
|
|
PauseChat bool
|
|
|
|
PauseInteraction bool
|
|
|
|
}
|
2023-09-19 00:22:50 +00:00
|
|
|
|
|
|
|
// Encryption settings.
|
|
|
|
type Encryption struct {
|
2024-05-30 06:20:24 +00:00
|
|
|
AESKey []byte
|
|
|
|
ColdStorageRSAPublicKey []byte
|
2023-09-19 00:22:50 +00:00
|
|
|
}
|
2024-05-20 01:33:28 +00:00
|
|
|
|
2024-10-04 04:23:12 +00:00
|
|
|
// SignedPhoto settings.
|
|
|
|
type SignedPhoto struct {
|
|
|
|
Enabled bool
|
|
|
|
JWTSecret string
|
|
|
|
}
|
|
|
|
|
2024-07-21 02:44:22 +00:00
|
|
|
// WebPush settings.
|
|
|
|
type WebPush struct {
|
|
|
|
VAPIDPublicKey string
|
|
|
|
VAPIDPrivateKey string
|
|
|
|
}
|
|
|
|
|
2024-05-20 01:33:28 +00:00
|
|
|
// Turnstile (Cloudflare CAPTCHA) settings.
|
|
|
|
type Turnstile struct {
|
|
|
|
Enabled bool
|
|
|
|
SiteKey string
|
|
|
|
SecretKey string
|
|
|
|
}
|