mirror of
https://github.com/ergochat/ergo.git
synced 2025-12-20 02:00:11 -08:00
new format for listener section
This commit is contained in:
parent
cfe991a335
commit
eee0747e5e
4 changed files with 157 additions and 86 deletions
118
irc/config.go
118
irc/config.go
|
|
@ -41,6 +41,12 @@ type TLSListenConfig struct {
|
|||
Key string
|
||||
}
|
||||
|
||||
// This is the YAML-deserializable type of the value of the `Server.Listeners` map
|
||||
type listenerConfigBlock struct {
|
||||
TLS TLSListenConfig
|
||||
Tor bool
|
||||
}
|
||||
|
||||
// listenerConfig is the config governing a particular listener (bound address),
|
||||
// in particular whether it has TLS or Tor (or both) enabled.
|
||||
type listenerConfig struct {
|
||||
|
|
@ -252,8 +258,8 @@ type FakelagConfig struct {
|
|||
}
|
||||
|
||||
type TorListenersConfig struct {
|
||||
Listeners []string
|
||||
RequireSasl bool `yaml:"require-sasl"`
|
||||
Listeners []string // legacy only
|
||||
RequireSasl bool `yaml:"require-sasl"`
|
||||
Vhost string
|
||||
MaxConnections int `yaml:"max-connections"`
|
||||
ThrottleDuration time.Duration `yaml:"throttle-duration"`
|
||||
|
|
@ -267,15 +273,19 @@ type Config struct {
|
|||
}
|
||||
|
||||
Server struct {
|
||||
Password string
|
||||
passwordBytes []byte
|
||||
Name string
|
||||
nameCasefolded string
|
||||
Listen []string
|
||||
UnixBindMode os.FileMode `yaml:"unix-bind-mode"`
|
||||
TLSListeners map[string]TLSListenConfig `yaml:"tls-listeners"`
|
||||
TorListeners TorListenersConfig `yaml:"tor-listeners"`
|
||||
listeners map[string]listenerConfig
|
||||
Password string
|
||||
passwordBytes []byte
|
||||
Name string
|
||||
nameCasefolded string
|
||||
// Listeners is the new style for configuring listeners:
|
||||
Listeners map[string]listenerConfigBlock
|
||||
UnixBindMode os.FileMode `yaml:"unix-bind-mode"`
|
||||
TorListeners TorListenersConfig `yaml:"tor-listeners"`
|
||||
// Listen and TLSListeners are the legacy style:
|
||||
Listen []string
|
||||
TLSListeners map[string]TLSListenConfig `yaml:"tls-listeners"`
|
||||
// either way, the result is this:
|
||||
trueListeners map[string]listenerConfig
|
||||
STS STSConfig
|
||||
CheckIdent bool `yaml:"check-ident"`
|
||||
MOTD string
|
||||
|
|
@ -481,37 +491,63 @@ func (conf *Config) Operators(oc map[string]*OperClass) (map[string]*Oper, error
|
|||
return operators, nil
|
||||
}
|
||||
|
||||
// prepareListeners populates Config.Server.listeners
|
||||
func loadTlsConfig(config TLSListenConfig) (tlsConfig *tls.Config, err error) {
|
||||
cert, err := tls.LoadX509KeyPair(config.Cert, config.Key)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidCertKeyPair
|
||||
}
|
||||
result := tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
ClientAuth: tls.RequestClientCert,
|
||||
}
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// prepareListeners populates Config.Server.trueListeners
|
||||
func (conf *Config) prepareListeners() (err error) {
|
||||
torListeners := make(map[string]bool, len(conf.Server.TorListeners.Listeners))
|
||||
for _, addr := range conf.Server.TorListeners.Listeners {
|
||||
torListeners[addr] = true
|
||||
}
|
||||
|
||||
conf.Server.listeners = make(map[string]listenerConfig, len(conf.Server.Listen))
|
||||
|
||||
for _, addr := range conf.Server.Listen {
|
||||
var lconf listenerConfig
|
||||
lconf.IsTor = torListeners[addr]
|
||||
tlsListenConf, ok := conf.Server.TLSListeners[addr]
|
||||
if ok {
|
||||
cert, err := tls.LoadX509KeyPair(tlsListenConf.Cert, tlsListenConf.Key)
|
||||
if err != nil {
|
||||
return ErrInvalidCertKeyPair
|
||||
listeners := make(map[string]listenerConfig)
|
||||
if 0 < len(conf.Server.Listeners) {
|
||||
for addr, block := range conf.Server.Listeners {
|
||||
var lconf listenerConfig
|
||||
lconf.IsTor = block.Tor
|
||||
if block.TLS.Cert != "" {
|
||||
tlsConfig, err := loadTlsConfig(block.TLS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lconf.TLSConfig = tlsConfig
|
||||
}
|
||||
tlsConfig := tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
ClientAuth: tls.RequestClientCert,
|
||||
}
|
||||
lconf.TLSConfig = &tlsConfig
|
||||
listeners[addr] = lconf
|
||||
}
|
||||
conf.Server.listeners[addr] = lconf
|
||||
} else if 0 < len(conf.Server.Listen) {
|
||||
log.Printf("WARNING: configuring listeners via the legacy `server.listen` config option")
|
||||
log.Printf("This will be removed in a later release: you should update to use `server.listeners`")
|
||||
torListeners := make(map[string]bool, len(conf.Server.TorListeners.Listeners))
|
||||
for _, addr := range conf.Server.TorListeners.Listeners {
|
||||
torListeners[addr] = true
|
||||
}
|
||||
for _, addr := range conf.Server.Listen {
|
||||
var lconf listenerConfig
|
||||
lconf.IsTor = torListeners[addr]
|
||||
tlsListenConf, ok := conf.Server.TLSListeners[addr]
|
||||
if ok {
|
||||
tlsConfig, err := loadTlsConfig(tlsListenConf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
lconf.TLSConfig = tlsConfig
|
||||
}
|
||||
listeners[addr] = lconf
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("No listeners were configured")
|
||||
}
|
||||
conf.Server.trueListeners = listeners
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadConfig loads the given YAML configuration file.
|
||||
func LoadConfig(filename string) (config *Config, err error) {
|
||||
// LoadRawConfig loads the config without doing any consistency checks or postprocessing
|
||||
func LoadRawConfig(filename string) (config *Config, err error) {
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -521,6 +557,15 @@ func LoadConfig(filename string) (config *Config, err error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// LoadConfig loads the given YAML configuration file.
|
||||
func LoadConfig(filename string) (config *Config, err error) {
|
||||
config, err = LoadRawConfig(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.Filename = filename
|
||||
|
||||
|
|
@ -536,9 +581,6 @@ func LoadConfig(filename string) (config *Config, err error) {
|
|||
if config.Datastore.Path == "" {
|
||||
return nil, ErrDatastorePathMissing
|
||||
}
|
||||
if len(config.Server.Listen) == 0 {
|
||||
return nil, ErrNoListenersDefined
|
||||
}
|
||||
//dan: automagically fix identlen until a few releases in the future (from now, 0.12.0), being a newly-introduced limit
|
||||
if config.Limits.IdentLen < 1 {
|
||||
config.Limits.IdentLen = 20
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue