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.
 
 
 

82 lines
2.8 KiB

  1. package server
  2. import (
  3. "cwtch.im/tapir/primitives"
  4. "encoding/json"
  5. "git.openprivacy.ca/openprivacy/log"
  6. "golang.org/x/crypto/ed25519"
  7. "io/ioutil"
  8. "path"
  9. )
  10. // Reporting is a struct for storing a the config a server needs to be a peer, and connect to a group to report
  11. type Reporting struct {
  12. LogMetricsToFile bool `json:"logMetricsToFile"`
  13. ReportingGroupID string `json:"reportingGroupId"`
  14. ReportingServerAddr string `json:"reportingServerAddr"`
  15. }
  16. // Config is a struct for storing basic server configuration
  17. type Config struct {
  18. ConfigDir string `json:"-"`
  19. FilePath string `json:"-"`
  20. MaxBufferLines int `json:"maxBufferLines"`
  21. PublicKey ed25519.PublicKey `json:"publicKey"`
  22. PrivateKey ed25519.PrivateKey `json:"privateKey"`
  23. TokenServerPublicKey ed25519.PublicKey `json:"tokenServerPublicKey"`
  24. TokenServerPrivateKey ed25519.PrivateKey `json:"tokenServerPrivateKey"`
  25. ServerReporting Reporting `json:"serverReporting"`
  26. AutoStart bool `json:"autostart"`
  27. }
  28. // Identity returns an encapsulation of the servers keys
  29. func (config *Config) Identity() primitives.Identity {
  30. return primitives.InitializeIdentity("", &config.PrivateKey, &config.PublicKey)
  31. }
  32. // TokenServiceIdentity returns an encapsulation of the servers token server (experimental)
  33. func (config *Config) TokenServiceIdentity() primitives.Identity {
  34. return primitives.InitializeIdentity("", &config.TokenServerPrivateKey, &config.TokenServerPublicKey)
  35. }
  36. // Save dumps the latest version of the config to a json file given by filename
  37. func (config *Config) Save(dir, filename string) {
  38. log.Infof("Saving config to %s\n", path.Join(dir, filename))
  39. bytes, _ := json.MarshalIndent(config, "", "\t")
  40. ioutil.WriteFile(path.Join(dir, filename), bytes, 0600)
  41. }
  42. // LoadConfig loads a Config from a json file specified by filename
  43. func LoadConfig(configDir, filename string) Config {
  44. log.Infof("Loading config from %s\n", path.Join(configDir, filename))
  45. config := Config{}
  46. id, pk := primitives.InitializeEphemeralIdentity()
  47. tid, tpk := primitives.InitializeEphemeralIdentity()
  48. config.PrivateKey = pk
  49. config.PublicKey = id.PublicKey()
  50. config.TokenServerPrivateKey = tpk
  51. config.TokenServerPublicKey = tid.PublicKey()
  52. config.MaxBufferLines = 100000
  53. config.ServerReporting = Reporting{
  54. LogMetricsToFile: true,
  55. ReportingGroupID: "",
  56. ReportingServerAddr: "",
  57. }
  58. config.AutoStart = false
  59. config.ConfigDir = configDir
  60. config.FilePath = filename
  61. raw, err := ioutil.ReadFile(path.Join(configDir, filename))
  62. if err == nil {
  63. err = json.Unmarshal(raw, &config)
  64. if err != nil {
  65. log.Errorf("reading config: %v", err)
  66. }
  67. }
  68. // Always save (first time generation, new version with new variables populated)
  69. config.Save(configDir, filename)
  70. return config
  71. }