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.

config.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2014-2015 Edmund Huber
  3. // Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
  4. // released under the MIT license
  5. package irc
  6. import (
  7. "crypto/tls"
  8. "errors"
  9. "io/ioutil"
  10. "log"
  11. "gopkg.in/yaml.v2"
  12. )
  13. type PassConfig struct {
  14. Password string
  15. }
  16. // TLSListenConfig defines configuration options for listening on TLS
  17. type TLSListenConfig struct {
  18. Cert string
  19. Key string
  20. }
  21. // Certificate returns the TLS certificate assicated with this TLSListenConfig
  22. func (conf *TLSListenConfig) Config() (*tls.Config, error) {
  23. cert, err := tls.LoadX509KeyPair(conf.Cert, conf.Key)
  24. if err != nil {
  25. return nil, errors.New("tls cert+key: invalid pair")
  26. }
  27. return &tls.Config{
  28. Certificates: []tls.Certificate{cert},
  29. }, err
  30. }
  31. func (conf *PassConfig) PasswordBytes() []byte {
  32. bytes, err := DecodePassword(conf.Password)
  33. if err != nil {
  34. log.Fatal("decode password error: ", err)
  35. }
  36. return bytes
  37. }
  38. type Config struct {
  39. Network struct {
  40. Name string
  41. }
  42. Server struct {
  43. PassConfig
  44. Password string
  45. Name string
  46. Database string
  47. Listen []string
  48. Wslisten string `yaml:"ws-listen"`
  49. TLSListeners map[string]*TLSListenConfig `yaml:"tls-listeners"`
  50. Log string
  51. MOTD string
  52. ProxyAllowedFrom []string `yaml:"proxy-allowed-from"`
  53. }
  54. Operator map[string]*PassConfig
  55. Theater map[string]*PassConfig
  56. }
  57. func (conf *Config) Operators() map[Name][]byte {
  58. operators := make(map[Name][]byte)
  59. for name, opConf := range conf.Operator {
  60. operators[NewName(name)] = opConf.PasswordBytes()
  61. }
  62. return operators
  63. }
  64. func (conf *Config) Theaters() map[Name][]byte {
  65. theaters := make(map[Name][]byte)
  66. for s, theaterConf := range conf.Theater {
  67. name := NewName(s)
  68. if !name.IsChannel() {
  69. log.Fatal("config uses a non-channel for a theater!")
  70. }
  71. theaters[name] = theaterConf.PasswordBytes()
  72. }
  73. return theaters
  74. }
  75. func (conf *Config) TLSListeners() map[Name]*tls.Config {
  76. tlsListeners := make(map[Name]*tls.Config)
  77. for s, tlsListenersConf := range conf.Server.TLSListeners {
  78. config, err := tlsListenersConf.Config()
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. tlsListeners[NewName(s)] = config
  83. }
  84. return tlsListeners
  85. }
  86. func LoadConfig(filename string) (config *Config, err error) {
  87. data, err := ioutil.ReadFile(filename)
  88. if err != nil {
  89. return nil, err
  90. }
  91. err = yaml.Unmarshal(data, &config)
  92. if err != nil {
  93. return nil, err
  94. }
  95. // we need this so PasswordBytes returns the correct info
  96. if config.Server.Password != "" {
  97. config.Server.PassConfig.Password = config.Server.Password
  98. }
  99. if config.Network.Name == "" {
  100. return nil, errors.New("Network name missing")
  101. }
  102. if config.Server.Name == "" {
  103. return nil, errors.New("Server name missing")
  104. }
  105. if !IsHostname(config.Server.Name) {
  106. return nil, errors.New("Server name must match the format of a hostname")
  107. }
  108. if config.Server.Database == "" {
  109. return nil, errors.New("Server database missing")
  110. }
  111. if len(config.Server.Listen) == 0 {
  112. return nil, errors.New("Server listening addresses missing")
  113. }
  114. return config, nil
  115. }