Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serverConfig.go 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package server
  2. import (
  3. "crypto/rand"
  4. "encoding/json"
  5. "git.openprivacy.ca/openprivacy/libricochet-go/identity"
  6. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  7. "golang.org/x/crypto/ed25519"
  8. "io/ioutil"
  9. "path"
  10. )
  11. // Reporting is a struct for storing a the config a server needs to be a peer, and connect to a group to report
  12. type Reporting struct {
  13. LogMetricsToFile bool `json:"logMetricsToFile"`
  14. ReportingGroupID string `json:"reportingGroupId"`
  15. ReportingServerAddr string `json:"reportingServerAddr"`
  16. }
  17. // Config is a struct for storing basic server configuration
  18. type Config struct {
  19. ConfigDir string `json:"-"`
  20. MaxBufferLines int `json:"maxBufferLines"`
  21. PublicKey ed25519.PublicKey `json:"publicKey"`
  22. PrivateKey ed25519.PrivateKey `json:"privateKey"`
  23. ServerReporting Reporting `json:"serverReporting"`
  24. }
  25. // Identity returns an encapsulation of the servers keys for running ricochet
  26. func (config *Config) Identity() identity.Identity {
  27. return identity.InitializeV3("", &config.PrivateKey, &config.PublicKey)
  28. }
  29. // Save dumps the latest version of the config to a json file given by filename
  30. func (config *Config) Save(dir, filename string) {
  31. log.Infof("Saving config to %s\n", path.Join(dir, filename))
  32. bytes, _ := json.MarshalIndent(config, "", "\t")
  33. ioutil.WriteFile(path.Join(dir, filename), bytes, 0600)
  34. }
  35. // LoadConfig loads a Config from a json file specified by filename
  36. func LoadConfig(configDir, filename string) Config {
  37. log.Infof("Loading config from %s\n", path.Join(configDir, filename))
  38. config := Config{}
  39. config.ConfigDir = configDir
  40. config.MaxBufferLines = 100000
  41. config.ServerReporting.LogMetricsToFile = false
  42. raw, err := ioutil.ReadFile(path.Join(configDir, filename))
  43. if err == nil {
  44. err = json.Unmarshal(raw, &config)
  45. if err != nil {
  46. log.Errorf("reading config: %v", err)
  47. }
  48. }
  49. if config.PrivateKey == nil {
  50. config.PublicKey, config.PrivateKey, _ = ed25519.GenerateKey(rand.Reader)
  51. }
  52. // Always save (first time generation, new version with new variables populated)
  53. config.Save(configDir, filename)
  54. return config
  55. }