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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. CheckIdent bool `yaml:"check-ident"`
  51. Log string
  52. MOTD string
  53. ProxyAllowedFrom []string `yaml:"proxy-allowed-from"`
  54. }
  55. Operator map[string]*PassConfig
  56. Theater map[string]*PassConfig
  57. Limits struct {
  58. NickLen int `yaml:"nicklen"`
  59. ChannelLen int `yaml:"channellen"`
  60. }
  61. }
  62. func (conf *Config) Operators() map[Name][]byte {
  63. operators := make(map[Name][]byte)
  64. for name, opConf := range conf.Operator {
  65. operators[NewName(name)] = opConf.PasswordBytes()
  66. }
  67. return operators
  68. }
  69. func (conf *Config) Theaters() map[Name][]byte {
  70. theaters := make(map[Name][]byte)
  71. for s, theaterConf := range conf.Theater {
  72. name := NewName(s)
  73. if !name.IsChannel() {
  74. log.Fatal("config uses a non-channel for a theater!")
  75. }
  76. theaters[name] = theaterConf.PasswordBytes()
  77. }
  78. return theaters
  79. }
  80. func (conf *Config) TLSListeners() map[Name]*tls.Config {
  81. tlsListeners := make(map[Name]*tls.Config)
  82. for s, tlsListenersConf := range conf.Server.TLSListeners {
  83. config, err := tlsListenersConf.Config()
  84. if err != nil {
  85. log.Fatal(err)
  86. }
  87. tlsListeners[NewName(s)] = config
  88. }
  89. return tlsListeners
  90. }
  91. func LoadConfig(filename string) (config *Config, err error) {
  92. data, err := ioutil.ReadFile(filename)
  93. if err != nil {
  94. return nil, err
  95. }
  96. err = yaml.Unmarshal(data, &config)
  97. if err != nil {
  98. return nil, err
  99. }
  100. // we need this so PasswordBytes returns the correct info
  101. if config.Server.Password != "" {
  102. config.Server.PassConfig.Password = config.Server.Password
  103. }
  104. if config.Network.Name == "" {
  105. return nil, errors.New("Network name missing")
  106. }
  107. if config.Server.Name == "" {
  108. return nil, errors.New("Server name missing")
  109. }
  110. if !IsHostname(config.Server.Name) {
  111. return nil, errors.New("Server name must match the format of a hostname")
  112. }
  113. if config.Server.Database == "" {
  114. return nil, errors.New("Server database missing")
  115. }
  116. if len(config.Server.Listen) == 0 {
  117. return nil, errors.New("Server listening addresses missing")
  118. }
  119. if config.Limits.NickLen < 1 || config.Limits.ChannelLen < 2 {
  120. return nil, errors.New("Limits aren't setup properly, check them and make them sane")
  121. }
  122. return config, nil
  123. }