mirror of
https://github.com/nxshock/gonx.git
synced 2025-07-01 23:53:35 +05:00
Initial commit
This commit is contained in:
commit
0b430973c9
14 changed files with 460 additions and 0 deletions
74
config.go
Normal file
74
config.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
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 {
|
||||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue