diff --git a/providers.example.yml b/providers.example.yml index 8cf6d75..13a125e 100644 --- a/providers.example.yml +++ b/providers.example.yml @@ -8,4 +8,4 @@ app: # alias # optional, defaults to empty path: # optional - path_mode: \ No newline at end of file + path_mode: diff --git a/src/go-proxy/constants.go b/src/go-proxy/constants.go index 0085f13..2b8fa9e 100644 --- a/src/go-proxy/constants.go +++ b/src/go-proxy/constants.go @@ -1,6 +1,10 @@ package main -import "time" +import ( + "net" + "net/http" + "time" +) var ( ImageNamePortMap = map[string]string{ @@ -60,6 +64,19 @@ const ( keyPath = "certs/priv.key" ) +// TODO: default + per proxy +var transport = &http.Transport{ + Proxy: http.ProxyFromEnvironment, + DialContext: (&net.Dialer{ + Timeout: 60 * time.Second, + KeepAlive: 60 * time.Second, + }).DialContext, + MaxIdleConns: 1000, + MaxIdleConnsPerHost: 1000, +} + +const clientUrlFromEnv = "FROM_ENV" + const configPath = "config.yml" const StreamStopListenTimeout = 1 * time.Second diff --git a/src/go-proxy/docker_provider.go b/src/go-proxy/docker_provider.go index c58ab65..c35fdad 100755 --- a/src/go-proxy/docker_provider.go +++ b/src/go-proxy/docker_provider.go @@ -63,14 +63,16 @@ func (p *Provider) getContainerProxyConfigs(container types.Container, clientHos } if config.Port == "" { // no ports exposed or specified + p.Logf("Build", "no ports exposed for %s, ignored", container_name) continue } if config.Scheme == "" { - if strings.HasSuffix(config.Port, "443") { + switch { + case strings.HasSuffix(config.Port, "443"): config.Scheme = "https" - } else if strings.HasPrefix(container.Image, "sha256:") { + case strings.HasPrefix(container.Image, "sha256:"): config.Scheme = "http" - } else { + default: imageSplit := strings.Split(container.Image, "/") imageSplit = strings.Split(imageSplit[len(imageSplit)-1], ":") imageName := imageSplit[0] @@ -92,8 +94,7 @@ func (p *Provider) getContainerProxyConfigs(container types.Container, clientHos config.Host = clientHost case container.HostConfig.NetworkMode == "host": config.Host = "host.docker.internal" - case config.LoadBalance == "true": - case config.LoadBalance == "1": + case config.LoadBalance == "true", config.LoadBalance == "1": for _, network := range container.NetworkSettings.Networks { config.Host = network.IPAddress break @@ -185,5 +186,4 @@ func (p *Provider) grWatchDockerChanges() { } } -// var dockerUrlRegex = regexp.MustCompile(`^(?P\w+)://(?P[^:]+)(?P:\d+)?(?P/.*)?$`) -const clientUrlFromEnv = "FROM_ENV" +// var dockerUrlRegex = regexp.MustCompile(`^(?P\w+)://(?P[^:]+)(?P:\d+)?(?P/.*)?$`) \ No newline at end of file diff --git a/src/go-proxy/file_provider.go b/src/go-proxy/file_provider.go index 7632598..84b636f 100644 --- a/src/go-proxy/file_provider.go +++ b/src/go-proxy/file_provider.go @@ -25,7 +25,7 @@ func (p *Provider) getFileProxyConfigs() ([]*ProxyConfig, error) { for alias, cfg := range configMap { cfg.Alias = alias - err = cfg.SetDefault() + err = cfg.SetDefaults() if err != nil { return nil, err } diff --git a/src/go-proxy/http_route.go b/src/go-proxy/http_route.go index 8d35b1f..0a6c345 100755 --- a/src/go-proxy/http_route.go +++ b/src/go-proxy/http_route.go @@ -2,12 +2,10 @@ package main import ( "fmt" - "net" "net/http" "net/http/httputil" "net/url" "strings" - "time" "github.com/golang/glog" ) @@ -20,15 +18,6 @@ type HTTPRoute struct { Proxy *httputil.ReverseProxy } -func isValidProxyPathMode(mode string) bool { - switch mode { - case ProxyPathMode_Forward, ProxyPathMode_Sub, ProxyPathMode_RemovedPath: - return true - default: - return false - } -} - func NewHTTPRoute(config *ProxyConfig) (*HTTPRoute, error) { url, err := url.Parse(fmt.Sprintf("%s://%s:%s", config.Scheme, config.Host, config.Port)) if err != nil { @@ -124,6 +113,24 @@ func (p *httpLoadBalancePool) Pick() *HTTPRoute { return p.pool[index%len(p.pool)] } +func (r *HTTPRoute) RemoveFromRoutes() { + routes.HTTPRoutes.Delete(r.Alias) +} + +// dummy implementation for Route interface +func (r *HTTPRoute) SetupListen() {} +func (r *HTTPRoute) Listen() {} +func (r *HTTPRoute) StopListening() {} + +func isValidProxyPathMode(mode string) bool { + switch mode { + case ProxyPathMode_Forward, ProxyPathMode_Sub, ProxyPathMode_RemovedPath: + return true + default: + return false + } +} + func redirectToTLS(w http.ResponseWriter, r *http.Request) { // Redirect to the same host but with HTTPS var redirectCode int @@ -158,23 +165,3 @@ func httpProxyHandler(w http.ResponseWriter, r *http.Request) { } route.Proxy.ServeHTTP(w, r) } - -func (r *HTTPRoute) RemoveFromRoutes() { - routes.HTTPRoutes.Delete(r.Alias) -} - -// dummy implementation for Route interface -func (r *HTTPRoute) SetupListen() {} -func (r *HTTPRoute) Listen() {} -func (r *HTTPRoute) StopListening() {} - -// TODO: default + per proxy -var transport = &http.Transport{ - Proxy: http.ProxyFromEnvironment, - DialContext: (&net.Dialer{ - Timeout: 60 * time.Second, - KeepAlive: 60 * time.Second, - }).DialContext, - MaxIdleConns: 1000, - MaxIdleConnsPerHost: 1000, -} diff --git a/src/go-proxy/proxy_config.go b/src/go-proxy/proxy_config.go index b98021c..52a04d0 100644 --- a/src/go-proxy/proxy_config.go +++ b/src/go-proxy/proxy_config.go @@ -11,7 +11,7 @@ type ProxyConfig struct { Path string // http proxy only PathMode string `yaml:"path_mode"` // http proxy only - provider *Provider + provider *Provider } func NewProxyConfig(provider *Provider) ProxyConfig { @@ -21,7 +21,7 @@ func NewProxyConfig(provider *Provider) ProxyConfig { } // used by `GetFileProxyConfigs` -func (cfg *ProxyConfig) SetDefault() error { +func (cfg *ProxyConfig) SetDefaults() error { if cfg.Alias == "" { return fmt.Errorf("alias is required") } diff --git a/src/go-proxy/route.go b/src/go-proxy/route.go index 2c5c6c0..a6a8124 100755 --- a/src/go-proxy/route.go +++ b/src/go-proxy/route.go @@ -18,7 +18,28 @@ type Route interface { RemoveFromRoutes() } -var routes = initRoutes() +func NewRoute(cfg *ProxyConfig) (Route, error) { + if isStreamScheme(cfg.Scheme) { + id := cfg.GetID() + if routes.StreamRoutes.Contains(id) { + return nil, fmt.Errorf("duplicated %s stream %s, ignoring", cfg.Scheme, id) + } + route, err := NewStreamRoute(cfg) + if err != nil { + return nil, err + } + routes.StreamRoutes.Set(id, route) + return route, nil + } else { + routes.HTTPRoutes.Ensure(cfg.Alias) + route, err := NewHTTPRoute(cfg) + if err != nil { + return nil, err + } + routes.HTTPRoutes.Get(cfg.Alias).Add(cfg.Path, route) + return route, nil + } +} func isValidScheme(s string) bool { for _, v := range ValidSchemes { @@ -45,25 +66,4 @@ func initRoutes() *Routes { return &r } -func NewRoute(cfg *ProxyConfig) (Route, error) { - if isStreamScheme(cfg.Scheme) { - id := cfg.GetID() - if routes.StreamRoutes.Contains(id) { - return nil, fmt.Errorf("duplicated %s stream %s, ignoring", cfg.Scheme, id) - } - route, err := NewStreamRoute(cfg) - if err != nil { - return nil, err - } - routes.StreamRoutes.Set(id, route) - return route, nil - } else { - routes.HTTPRoutes.Ensure(cfg.Alias) - route, err := NewHTTPRoute(cfg) - if err != nil { - return nil, err - } - routes.HTTPRoutes.Get(cfg.Alias).Add(cfg.Path, route) - return route, nil - } -} \ No newline at end of file +var routes = initRoutes() \ No newline at end of file