diff --git a/config.go b/config.go index 4ac4474..25f0ef6 100644 --- a/config.go +++ b/config.go @@ -10,9 +10,9 @@ import ( type ( Config struct { - Cert Cert - Port int - Endpoints map[string]Endpoint + Cert Cert + Port int + Domains map[string]map[string]Endpoint } Cert struct { @@ -21,7 +21,6 @@ type ( } Endpoint struct { - From string To string BasicAuth string } @@ -34,7 +33,7 @@ func NewConfig() (Config, error) { fs.StringVar(&c.Cert.CRT, "crt", "", "path to .crt") fs.StringVar(&c.Cert.Key, "key", "", "path to .key") fs.IntVar(&c.Port, "p", 56112, "port to listen on") - f := fs.String("f", "/dev/null", `file of {endpoints:{"": {from:"", to:"", basicAuth:""}}}`) + f := fs.String("f", "/dev/null", `file of {domain:{"":{"": {to:"", basicAuth:""}}}}`) if err := fs.Parse(os.Args[1:]); err != nil { return c, err } @@ -43,8 +42,8 @@ func NewConfig() (Config, error) { return c, err } else if err := yaml.Unmarshal(b, &c); err != nil { return c, err - } else if len(c.Endpoints) == 0 { - return c, fmt.Errorf("no endpoints configured") + } else if len(c.Domains) == 0 { + return c, fmt.Errorf("no domains configured") } return c, nil diff --git a/main.go b/main.go index f7df054..577ac75 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,8 @@ func main() { } func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if c.endpoint(r).To == "" { + endpoint := c.endpoint(r) + if endpoint.To == "" { http.NotFound(w, r) return } @@ -55,7 +56,6 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - endpoint := c.endpoint(r) u, err := url.Parse(endpoint.To) if err != nil { log.Printf("[%s] %v", c.key(r), err) @@ -111,7 +111,13 @@ func (c Config) basicAuth(w http.ResponseWriter, r *http.Request) bool { } func (c Config) endpoint(r *http.Request) Endpoint { - return c.Endpoints[c.key(r)] + key := c.key(r) + domain := strings.TrimPrefix(r.Host, key) + m, ok := c.Domains[domain] + if !ok { + return Endpoint{} + } + return m[c.key(r)] } type redirPurge struct { diff --git a/testdata/gitea.yaml b/testdata/gitea.yaml new file mode 100644 index 0000000..9773a36 --- /dev/null +++ b/testdata/gitea.yaml @@ -0,0 +1,6 @@ +port: 443 +domains: + .bel.blue: + gitea: + to: http://192.168.0.86:59526 + basicAuth: "" diff --git a/testdata/test.yaml b/testdata/test.yaml index d76d26f..411f202 100644 --- a/testdata/test.yaml +++ b/testdata/test.yaml @@ -1,6 +1,6 @@ #port: 51552 # priority over cli -endpoints: +domains: foo: - from: foo.blapointe.com - to: http://192.168.0.86:51552 - basicAuth: u:p + bar: + to: http://192.168.0.86:51552 + basicAuth: u:p