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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. }
  58. func (conf *Config) Operators() map[Name][]byte {
  59. operators := make(map[Name][]byte)
  60. for name, opConf := range conf.Operator {
  61. operators[NewName(name)] = opConf.PasswordBytes()
  62. }
  63. return operators
  64. }
  65. func (conf *Config) Theaters() map[Name][]byte {
  66. theaters := make(map[Name][]byte)
  67. for s, theaterConf := range conf.Theater {
  68. name := NewName(s)
  69. if !name.IsChannel() {
  70. log.Fatal("config uses a non-channel for a theater!")
  71. }
  72. theaters[name] = theaterConf.PasswordBytes()
  73. }
  74. return theaters
  75. }
  76. func (conf *Config) TLSListeners() map[Name]*tls.Config {
  77. tlsListeners := make(map[Name]*tls.Config)
  78. for s, tlsListenersConf := range conf.Server.TLSListeners {
  79. config, err := tlsListenersConf.Config()
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. tlsListeners[NewName(s)] = config
  84. }
  85. return tlsListeners
  86. }
  87. func LoadConfig(filename string) (config *Config, err error) {
  88. data, err := ioutil.ReadFile(filename)
  89. if err != nil {
  90. return nil, err
  91. }
  92. err = yaml.Unmarshal(data, &config)
  93. if err != nil {
  94. return nil, err
  95. }
  96. // we need this so PasswordBytes returns the correct info
  97. if config.Server.Password != "" {
  98. config.Server.PassConfig.Password = config.Server.Password
  99. }
  100. if config.Network.Name == "" {
  101. return nil, errors.New("Network name missing")
  102. }
  103. if config.Server.Name == "" {
  104. return nil, errors.New("Server name missing")
  105. }
  106. if !IsHostname(config.Server.Name) {
  107. return nil, errors.New("Server name must match the format of a hostname")
  108. }
  109. if config.Server.Database == "" {
  110. return nil, errors.New("Server database missing")
  111. }
  112. if len(config.Server.Listen) == 0 {
  113. return nil, errors.New("Server listening addresses missing")
  114. }
  115. return config, nil
  116. }