package main import ( "crypto/sha256" "net/url" "os" "github.com/goccy/go-yaml" ) type listenerDef struct { protocol string address string acceptProxy bool } var listenerDefs []listenerDef type credentials struct { username []byte password []byte } type pathDef struct { path string targetURL *url.URL credentials []credentials } var hostDefs map[string][]pathDef func parseConfig(configFile string) { configData, err := os.ReadFile(configFile) if err != nil { panic(err) } var config struct { Listeners []struct { Protocol string Address string AcceptProxy bool } Hosts []struct { Host string Paths []struct { Path string Address string Credentials []struct { Username string Password string } } } } err = yaml.Unmarshal(configData, &config) if err != nil { panic(err) } hostDefs = make(map[string][]pathDef, len(config.Hosts)) for _, host := range config.Hosts { hostDef := make([]pathDef, len(host.Paths)) hostDefs[host.Host] = hostDef for i, path := range host.Paths { targetURL, err := url.Parse(path.Address) if err != nil { panic(err) } var pdCredentials []credentials if path.Credentials != nil { pdCredentials = make([]credentials, len(path.Credentials)) for j, cfgCredentials := range path.Credentials { hash := sha256.New() hash.Write([]byte(cfgCredentials.Username)) usernameSum := hash.Sum(nil) hash = sha256.New() hash.Write([]byte(cfgCredentials.Password)) passwordSum := hash.Sum(nil) pdCredentials[j] = credentials{ username: usernameSum, password: passwordSum, } } } hostDef[i] = pathDef{ path: path.Path, targetURL: targetURL, credentials: pdCredentials, } } } listenerDefs = make([]listenerDef, len(config.Listeners)) for i, listener := range config.Listeners { listenerDef := listenerDef{ protocol: listener.Protocol, address: listener.Address, acceptProxy: listener.AcceptProxy, } listenerDefs[i] = listenerDef } }