cwtch/server/serverConfig.go

99 lines
3.1 KiB
Go
Raw Permalink Normal View History

package server
import (
"crypto/rand"
2018-06-19 22:38:22 +00:00
"encoding/json"
2021-04-09 01:22:08 +00:00
"git.openprivacy.ca/cwtch.im/tapir/primitives"
"git.openprivacy.ca/openprivacy/log"
"github.com/gtank/ristretto255"
2018-10-05 03:18:34 +00:00
"golang.org/x/crypto/ed25519"
"io/ioutil"
"path"
)
// Reporting is a struct for storing a the config a server needs to be a peer, and connect to a group to report
type Reporting struct {
LogMetricsToFile bool `json:"logMetricsToFile"`
2018-06-19 22:38:22 +00:00
ReportingGroupID string `json:"reportingGroupId"`
ReportingServerAddr string `json:"reportingServerAddr"`
}
// Config is a struct for storing basic server configuration
type Config struct {
2021-05-05 19:49:24 +00:00
ConfigDir string `json:"-"`
FilePath string `json:"-"`
MaxBufferLines int `json:"maxBufferLines"`
2021-05-05 19:49:24 +00:00
PublicKey ed25519.PublicKey `json:"publicKey"`
PrivateKey ed25519.PrivateKey `json:"privateKey"`
2020-09-21 21:26:28 +00:00
TokenServerPublicKey ed25519.PublicKey `json:"tokenServerPublicKey"`
TokenServerPrivateKey ed25519.PrivateKey `json:"tokenServerPrivateKey"`
2021-05-05 19:49:24 +00:00
TokenServiceK ristretto255.Scalar `json:"tokenServiceK"`
2021-05-05 19:49:24 +00:00
ServerReporting Reporting `json:"serverReporting"`
AutoStart bool `json:"autostart"`
}
2020-09-21 21:26:28 +00:00
// Identity returns an encapsulation of the servers keys
2020-07-14 00:46:05 +00:00
func (config *Config) Identity() primitives.Identity {
return primitives.InitializeIdentity("", &config.PrivateKey, &config.PublicKey)
}
2020-09-21 21:26:28 +00:00
// TokenServiceIdentity returns an encapsulation of the servers token server (experimental)
func (config *Config) TokenServiceIdentity() primitives.Identity {
return primitives.InitializeIdentity("", &config.TokenServerPrivateKey, &config.TokenServerPublicKey)
}
// Save dumps the latest version of the config to a json file given by filename
func (config *Config) Save(dir, filename string) {
2018-12-04 02:52:11 +00:00
log.Infof("Saving config to %s\n", path.Join(dir, filename))
bytes, _ := json.MarshalIndent(config, "", "\t")
ioutil.WriteFile(path.Join(dir, filename), bytes, 0600)
}
2018-10-05 03:18:34 +00:00
// LoadConfig loads a Config from a json file specified by filename
func LoadConfig(configDir, filename string) Config {
2018-12-04 02:52:11 +00:00
log.Infof("Loading config from %s\n", path.Join(configDir, filename))
config := Config{}
2020-09-21 22:39:15 +00:00
id, pk := primitives.InitializeEphemeralIdentity()
tid, tpk := primitives.InitializeEphemeralIdentity()
config.PrivateKey = pk
config.PublicKey = id.PublicKey()
config.TokenServerPrivateKey = tpk
config.TokenServerPublicKey = tid.PublicKey()
config.MaxBufferLines = 100000
2020-09-21 22:39:15 +00:00
config.ServerReporting = Reporting{
LogMetricsToFile: true,
ReportingGroupID: "",
ReportingServerAddr: "",
}
2020-11-02 23:53:13 +00:00
config.AutoStart = false
config.ConfigDir = configDir
config.FilePath = filename
2020-09-21 22:39:15 +00:00
k := new(ristretto255.Scalar)
b := make([]byte, 64)
_, err := rand.Read(b)
if err != nil {
// unable to generate secure random numbers
panic("unable to generate secure random numbers")
}
k.FromUniformBytes(b)
config.TokenServiceK = *k
raw, err := ioutil.ReadFile(path.Join(configDir, filename))
if err == nil {
err = json.Unmarshal(raw, &config)
if err != nil {
log.Errorf("reading config: %v", err)
}
}
// Always save (first time generation, new version with new variables populated)
config.Save(configDir, filename)
return config
}