1
0
mirror of https://github.com/nxshock/gonx.git synced 2024-11-27 17:11:01 +05:00
gonx/config.go

77 lines
1.5 KiB
Go
Raw Permalink Normal View History

2023-12-28 16:00:04 +05:00
package main
import (
"crypto/tls"
"fmt"
"log/slog"
"path/filepath"
"github.com/BurntSushi/toml"
)
type Config struct {
// Log level
LogLevel slog.Level
// Path to TLS-certificates generated by Certbot
TlsKeysDir string
// TLS listen address
TlsListenAddr string
// HTTP listen address
HttpListenAddr string
// Map of hostname -> redirect URL
TLS map[string]string
// Acme path
AcmeChallengePath string
// Parsed list of servers
proxyRules HostMapping
// loaded TLS keys
tlsConfig *tls.Config
}
func LoadConfig(configFilePath string) (*Config, error) {
config := new(Config)
_, err := toml.DecodeFile(configFilePath, &config)
if err != nil {
return nil, err
}
config.proxyRules = make(HostMapping)
for inputUrlStr, outputUrlStr := range config.TLS {
err = config.proxyRules.Add(inputUrlStr, outputUrlStr)
if err != nil {
return nil, err
}
}
return config, nil
}
func (c *Config) initTls() error {
2023-12-29 12:16:35 +05:00
slog.Debug("Loading TLS keys")
2023-12-28 16:00:04 +05:00
c.tlsConfig = new(tls.Config)
for hostName := range c.proxyRules {
slog.Debug("reading tls key", slog.String("host", hostName))
certFilePath := filepath.Join(c.TlsKeysDir, hostName, defaultCertFileName)
keyFilePath := filepath.Join(c.TlsKeysDir, hostName, defaultKeyFileName)
cert, err := tls.LoadX509KeyPair(certFilePath, keyFilePath)
if err != nil {
return fmt.Errorf("read tls files error: %v", err)
}
c.tlsConfig.Certificates = append(c.tlsConfig.Certificates, cert)
}
return nil
}