dynamic conf
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
@@ -30,8 +31,8 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func NewConfig() (Config, error) {
|
||||
var c Config
|
||||
func NewConfig() (*Config, error) {
|
||||
c := &Config{}
|
||||
|
||||
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
||||
fs.StringVar(&c.Cert.CRT, "crt", "", "path to .crt")
|
||||
@@ -60,3 +61,25 @@ func NewConfig() (Config, error) {
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *Config) Start() {
|
||||
go c.start()
|
||||
}
|
||||
|
||||
func (c *Config) start() {
|
||||
ch := time.NewTicker(30 * time.Second)
|
||||
defer ch.Stop()
|
||||
|
||||
for range ch.C {
|
||||
func() {
|
||||
c2, err := NewConfig()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if fmt.Sprintf("%+v", c.Domains) != fmt.Sprintf("%+v", c2.Domains) {
|
||||
c.Domains = c2.Domains
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Start()
|
||||
|
||||
s := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", c.Port),
|
||||
@@ -40,7 +41,7 @@ func main() {
|
||||
|
||||
var resolver = dnscache.New(time.Minute * 500)
|
||||
|
||||
func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (c *Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
endpoint := c.endpoint(r)
|
||||
if endpoint.To == "" {
|
||||
http.NotFound(w, r)
|
||||
@@ -54,7 +55,7 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c Config) serveHTTPRedir(w http.ResponseWriter, r *http.Request, endpoint Endpoint) {
|
||||
func (c *Config) serveHTTPRedir(w http.ResponseWriter, r *http.Request, endpoint Endpoint) {
|
||||
if !c.basicAuth(w, r) {
|
||||
return
|
||||
}
|
||||
@@ -68,7 +69,7 @@ func (c Config) serveHTTPRedir(w http.ResponseWriter, r *http.Request, endpoint
|
||||
|
||||
var someDialer = &net.Dialer{}
|
||||
|
||||
func (c Config) serveHTTPProxy(w http.ResponseWriter, r *http.Request, endpoint Endpoint) {
|
||||
func (c *Config) serveHTTPProxy(w http.ResponseWriter, r *http.Request, endpoint Endpoint) {
|
||||
cors(w)
|
||||
if r.Method == http.MethodOptions {
|
||||
w.Header().Set("Content-Length", "0")
|
||||
@@ -146,11 +147,11 @@ func cors(w http.ResponseWriter) {
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, TRACE, PATCH, HEAD, DELETE")
|
||||
}
|
||||
|
||||
func (c Config) key(r *http.Request) string {
|
||||
func (c *Config) key(r *http.Request) string {
|
||||
return strings.Split(r.Host, ".")[0]
|
||||
}
|
||||
|
||||
func (c Config) handleAdmin(w http.ResponseWriter, r *http.Request) bool {
|
||||
func (c *Config) handleAdmin(w http.ResponseWriter, r *http.Request) bool {
|
||||
switch c.key(r) {
|
||||
case "_":
|
||||
panic("not impl: list")
|
||||
@@ -160,7 +161,7 @@ func (c Config) handleAdmin(w http.ResponseWriter, r *http.Request) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c Config) basicAuth(w http.ResponseWriter, r *http.Request) bool {
|
||||
func (c *Config) basicAuth(w http.ResponseWriter, r *http.Request) bool {
|
||||
basicAuth := c.endpoint(r).BasicAuth
|
||||
if noAuth := basicAuth == ""; noAuth {
|
||||
return true
|
||||
@@ -176,7 +177,7 @@ func (c Config) basicAuth(w http.ResponseWriter, r *http.Request) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c Config) endpoint(r *http.Request) Endpoint {
|
||||
func (c *Config) endpoint(r *http.Request) Endpoint {
|
||||
key := c.key(r)
|
||||
domain := strings.Split(strings.TrimPrefix(r.Host, key), ":")[0]
|
||||
m, ok := c.Domains[domain]
|
||||
|
||||
Reference in New Issue
Block a user