cwtch/server/serverConfig.go

65 lines
2.0 KiB
Go
Raw Normal View History

package server
import (
2018-10-05 03:18:34 +00:00
"crypto/rand"
2018-06-19 22:38:22 +00:00
"encoding/json"
2018-10-05 03:18:34 +00:00
"git.openprivacy.ca/openprivacy/libricochet-go/identity"
"golang.org/x/crypto/ed25519"
"io/ioutil"
"log"
"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 {
ConfigDir string `json:"-"`
2018-10-05 03:18:34 +00:00
MaxBufferLines int `json:"maxBufferLines"`
PublicKey ed25519.PublicKey `json:"publicKey"`
PrivateKey ed25519.PrivateKey `json:"privateKey"`
ServerReporting Reporting `json:"serverReporting"`
}
2018-10-05 03:18:34 +00:00
// Identity returns an encapsulation of the servers keys for running ricochet
func (config *Config) Identity() identity.Identity {
return identity.InitializeV3("", &config.PrivateKey, &config.PublicKey)
}
// Save dumps the latest version of the config to a json file given by filename
func (config *Config) Save(dir, filename string) {
log.Printf("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-11-01 17:32:26 +00:00
log.Printf("Loading config from %s\n", filename)
config := Config{}
config.ConfigDir = configDir
config.MaxBufferLines = 100000
config.ServerReporting.LogMetricsToFile = false
raw, err := ioutil.ReadFile(path.Join(configDir, filename))
if err == nil {
err = json.Unmarshal(raw, &config)
if err != nil {
log.Println("Error reading config: ", err)
}
}
2018-10-05 03:18:34 +00:00
if config.PrivateKey == nil {
config.PublicKey, config.PrivateKey, _ = ed25519.GenerateKey(rand.Reader)
}
// Always save (first time generation, new version with new variables populated)
config.Save(configDir, filename)
return config
}