2023-02-26 21:22:18 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-07-12 05:57:06 +00:00
|
|
|
|
2024-11-28 17:54:36 +00:00
|
|
|
"forge.lightcrystal.systems/nilix/quartzgun/cookie"
|
2023-02-26 21:22:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-07-10 05:41:30 +00:00
|
|
|
Port int
|
|
|
|
Uploads string
|
|
|
|
UploadMaxMB int
|
|
|
|
MongoURI string
|
|
|
|
RegistrationSecret string
|
2023-07-12 05:57:06 +00:00
|
|
|
RegistrationSalt []byte
|
2023-02-26 21:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfigLocation() string {
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
appdata := os.Getenv("APPDATA")
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
return filepath.Join(appdata, "felt")
|
|
|
|
case "darwin":
|
|
|
|
return filepath.Join(home, "Library", "Application Support", "felt")
|
|
|
|
case "plan9":
|
|
|
|
return filepath.Join(home, "lib", "felt")
|
|
|
|
default:
|
|
|
|
return filepath.Join(home, ".config", "felt")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureConfigLocationExists() {
|
|
|
|
fileInfo, err := os.Stat(GetConfigLocation())
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
os.MkdirAll(GetConfigLocation(), os.ModePerm)
|
|
|
|
} else if !fileInfo.IsDir() {
|
|
|
|
panic("Config location is not a directory!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadConfig() *Config {
|
|
|
|
ensureConfigLocationExists()
|
2023-07-12 05:57:06 +00:00
|
|
|
self := parseConfig(filepath.Join(GetConfigLocation(), "felt.conf"))
|
|
|
|
bytes, err := getSecrets(filepath.Join(GetConfigLocation(), "enclave"))
|
|
|
|
if err == nil {
|
|
|
|
self.RegistrationSalt = bytes[0:16]
|
|
|
|
self.RegistrationSecret = string(bytes[16:48])
|
|
|
|
} else {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
return self
|
2023-02-26 21:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Config) Write() error {
|
|
|
|
ensureConfigLocationExists()
|
|
|
|
return writeConfig(self, filepath.Join(GetConfigLocation(), "felt.conf"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Config) IsNull() bool {
|
2023-07-12 05:57:06 +00:00
|
|
|
return self.Port == 0 || self.UploadMaxMB == 0 || self.Uploads == "" || len(self.RegistrationSalt) != 16 || len(self.RegistrationSecret) != 32
|
2023-02-26 21:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Config) RunWizard() {
|
|
|
|
fmt.Printf("All options are required.\n")
|
|
|
|
defer func(cfg *Config) {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Printf("Invalid selection, starting over...")
|
|
|
|
cfg.RunWizard()
|
|
|
|
}
|
|
|
|
}(self)
|
|
|
|
inputBuf := ""
|
|
|
|
|
|
|
|
fmt.Printf("MongoDB URI? ")
|
|
|
|
ensureNonEmptyOption(&inputBuf)
|
|
|
|
self.MongoURI = inputBuf
|
|
|
|
|
|
|
|
inputBuf = ""
|
|
|
|
fmt.Printf("TCP port? ")
|
|
|
|
self.Port = ensurePortOption(&inputBuf)
|
|
|
|
|
|
|
|
fmt.Printf("file uploads location? ")
|
|
|
|
ensureNonEmptyOption(&inputBuf)
|
|
|
|
self.Uploads = inputBuf
|
|
|
|
|
|
|
|
inputBuf = ""
|
|
|
|
fmt.Printf("Max file upload size (MB)? ")
|
|
|
|
self.UploadMaxMB = ensureNumberOption(&inputBuf)
|
|
|
|
|
2023-07-12 05:57:06 +00:00
|
|
|
setSecrets([]byte(cookie.GenToken(48)), filepath.Join(GetConfigLocation(), "enclave"))
|
2023-07-10 05:41:30 +00:00
|
|
|
|
2023-07-12 05:57:06 +00:00
|
|
|
fmt.Printf("Configuration complete! Registration secrets have been updated as well!\n")
|
2023-02-26 21:22:18 +00:00
|
|
|
self.Write()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureNonEmptyOption(buffer *string) {
|
|
|
|
for {
|
|
|
|
fmt.Scanln(buffer)
|
|
|
|
if len(strings.TrimSpace(*buffer)) != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fmt.Println("Please enter a nonempty value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureBooleanOption(buffer *string) bool {
|
|
|
|
for {
|
|
|
|
fmt.Scanln(buffer)
|
|
|
|
trimmedBuf := strings.TrimSpace(*buffer)
|
|
|
|
v, err := strconv.ParseBool(trimmedBuf)
|
|
|
|
if err == nil {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
fmt.Println("Please enter a true or false value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureNumberOption(buffer *string) int {
|
|
|
|
for {
|
|
|
|
fmt.Scanln(buffer)
|
|
|
|
trimmedBuf := strings.TrimSpace(*buffer)
|
|
|
|
v, err := strconv.ParseInt(trimmedBuf, 10, 32)
|
|
|
|
if err == nil && v > 0 {
|
|
|
|
return int(v)
|
|
|
|
}
|
|
|
|
fmt.Println("Please enter a positive integer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensurePortOption(buffer *string) int {
|
|
|
|
for {
|
|
|
|
fmt.Scanln(buffer)
|
|
|
|
trimmedBuf := strings.TrimSpace(*buffer)
|
|
|
|
v, err := strconv.ParseInt(trimmedBuf, 10, 32)
|
|
|
|
if err == nil && v > 0 && v <= 65536 {
|
|
|
|
return int(v)
|
|
|
|
}
|
|
|
|
fmt.Println("Please enter a valid port [1, 65536]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeConfig(cfg *Config, configFile string) error {
|
|
|
|
f, err := os.Create(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
f.WriteString("mongoURI=" + cfg.MongoURI + "\n")
|
|
|
|
f.WriteString("uploads=" + cfg.Uploads + "\n")
|
|
|
|
f.WriteString("uploadMaxMB=" + strconv.FormatInt(int64(cfg.UploadMaxMB), 10) + "\n")
|
|
|
|
f.WriteString("port=" + strconv.FormatInt(int64(cfg.Port), 10) + "\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-12 05:57:06 +00:00
|
|
|
func setSecrets(secret []byte, secretFile string) error {
|
|
|
|
f, err := os.Create(secretFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
f.Write(secret)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSecrets(secretFile string) ([]byte, error) {
|
|
|
|
f, err := os.ReadFile(secretFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(f) != 48 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f[:], nil
|
|
|
|
}
|
|
|
|
|
2023-02-26 21:22:18 +00:00
|
|
|
func parseConfig(configFile string) *Config {
|
|
|
|
f, err := os.ReadFile(configFile)
|
|
|
|
cfg := &Config{}
|
|
|
|
if err != nil {
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
fileData := string(f[:])
|
|
|
|
|
|
|
|
lines := strings.Split(fileData, "\n")
|
|
|
|
|
|
|
|
for _, l := range lines {
|
|
|
|
if len(l) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !strings.Contains(l, "=") {
|
|
|
|
panic("Malformed config not in INI format")
|
|
|
|
}
|
|
|
|
|
|
|
|
kvp := strings.Split(l, "=")
|
|
|
|
k := strings.TrimSpace(kvp[0])
|
|
|
|
v := strings.TrimSpace(kvp[1])
|
|
|
|
switch k {
|
2023-07-10 06:38:08 +00:00
|
|
|
case "registrationSecret":
|
|
|
|
cfg.RegistrationSecret = v
|
2023-02-26 21:22:18 +00:00
|
|
|
case "mongoURI":
|
|
|
|
cfg.MongoURI = v
|
|
|
|
case "uploads":
|
|
|
|
cfg.Uploads = v
|
|
|
|
case "port":
|
|
|
|
port, _ := strconv.ParseInt(v, 10, 32)
|
|
|
|
cfg.Port = int(port)
|
|
|
|
case "uploadMaxMB":
|
|
|
|
maxUpload, _ := strconv.ParseInt(v, 10, 32)
|
|
|
|
cfg.UploadMaxMB = int(maxUpload)
|
|
|
|
default:
|
|
|
|
panic("Unrecognized config option: " + k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|