Fix: frontend redirect
This commit is contained in:
parent
623a7dc7e6
commit
b0c12e2422
2 changed files with 223 additions and 65 deletions
|
@ -49,6 +49,10 @@ import (
|
|||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultRedirectRegex = `^(?:https?:\/\/)?([\w\._-]+)(?::\d+)?(.*)$`
|
||||
)
|
||||
|
||||
var (
|
||||
httpServerLogger = stdlog.New(log.WriterLevel(logrus.DebugLevel), "", 0)
|
||||
)
|
||||
|
@ -940,7 +944,7 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura
|
|||
if entryPoint.Redirect != nil {
|
||||
if redirectHandlers[entryPointName] != nil {
|
||||
n.Use(redirectHandlers[entryPointName])
|
||||
} else if handler, err := s.loadEntryPointConfig(entryPointName, entryPoint); err != nil {
|
||||
} else if handler, err := s.buildEntryPointRedirect(entryPointName, entryPoint); err != nil {
|
||||
log.Errorf("Error loading entrypoint configuration for frontend %s: %v", frontendName, err)
|
||||
log.Errorf("Skipping frontend %s...", frontendName)
|
||||
continue frontend
|
||||
|
@ -1121,25 +1125,19 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura
|
|||
|
||||
ipWhitelistMiddleware, err := configureIPWhitelistMiddleware(frontend.WhitelistSourceRange)
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating IP Whitelister: %s", err)
|
||||
log.Errorf("Error creating IP Whitelister: %s", err)
|
||||
} else if ipWhitelistMiddleware != nil {
|
||||
n.Use(ipWhitelistMiddleware)
|
||||
log.Infof("Configured IP Whitelists: %s", frontend.WhitelistSourceRange)
|
||||
}
|
||||
|
||||
if len(frontend.Redirect) > 0 {
|
||||
proto := "http"
|
||||
if s.globalConfiguration.EntryPoints[frontend.Redirect].TLS != nil {
|
||||
proto = "https"
|
||||
}
|
||||
|
||||
regex, replacement, err := s.buildRedirect(proto, entryPoint)
|
||||
rewrite, err := middlewares.NewRewrite(regex, replacement, true)
|
||||
rewrite, err := s.buildRedirectRewrite(entryPointName, frontend.Redirect)
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating Frontend Redirect: %v", err)
|
||||
log.Errorf("Error creating Frontend Redirect: %v", err)
|
||||
}
|
||||
n.Use(rewrite)
|
||||
log.Debugf("Creating frontend %s redirect to %s", frontendName, proto)
|
||||
log.Debugf("Frontend %s redirect created", frontendName)
|
||||
}
|
||||
|
||||
if len(frontend.BasicAuth) > 0 {
|
||||
|
@ -1289,38 +1287,57 @@ func (s *Server) wireFrontendBackend(serverRoute *serverRoute, handler http.Hand
|
|||
serverRoute.route.Handler(handler)
|
||||
}
|
||||
|
||||
func (s *Server) loadEntryPointConfig(entryPointName string, entryPoint *configuration.EntryPoint) (negroni.Handler, error) {
|
||||
func (s *Server) buildEntryPointRedirect(srcEntryPointName string, entryPoint *configuration.EntryPoint) (*middlewares.Rewrite, error) {
|
||||
if len(entryPoint.Redirect.EntryPoint) > 0 {
|
||||
return s.buildRedirectRewrite(srcEntryPointName, entryPoint.Redirect.EntryPoint)
|
||||
}
|
||||
|
||||
regex := entryPoint.Redirect.Regex
|
||||
replacement := entryPoint.Redirect.Replacement
|
||||
var err error
|
||||
if len(entryPoint.Redirect.EntryPoint) > 0 {
|
||||
var protocol = "http"
|
||||
if s.globalConfiguration.EntryPoints[entryPoint.Redirect.EntryPoint].TLS != nil {
|
||||
protocol = "https"
|
||||
}
|
||||
regex, replacement, err = s.buildRedirect(protocol, entryPoint)
|
||||
}
|
||||
rewrite, err := middlewares.NewRewrite(regex, replacement, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debugf("Creating entryPoint redirect %s -> %s : %s -> %s", entryPointName, entryPoint.Redirect.EntryPoint, regex, replacement)
|
||||
log.Debugf("Creating entryPoint redirect %s -> %s : %s -> %s", srcEntryPointName, entryPoint.Redirect.EntryPoint, regex, replacement)
|
||||
|
||||
return rewrite, nil
|
||||
}
|
||||
|
||||
func (s *Server) buildRedirect(protocol string, entryPoint *configuration.EntryPoint) (string, string, error) {
|
||||
regex := `^(?:https?:\/\/)?([\w\._-]+)(?::\d+)?(.*)$`
|
||||
if s.globalConfiguration.EntryPoints[entryPoint.Redirect.EntryPoint] == nil {
|
||||
return "", "", fmt.Errorf("unknown target entrypoint %q", entryPoint.Redirect.EntryPoint)
|
||||
func (s *Server) buildRedirectRewrite(srcEntryPointName string, redirectEntryPoint string) (*middlewares.Rewrite, error) {
|
||||
regex, replacement, err := s.buildRedirect(redirectEntryPoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, _ := regexp.Compile(`(:\d+)`)
|
||||
match := r.FindStringSubmatch(s.globalConfiguration.EntryPoints[entryPoint.Redirect.EntryPoint].Address)
|
||||
|
||||
rewrite, err := middlewares.NewRewrite(regex, replacement, true)
|
||||
if err != nil {
|
||||
// Impossible case because error is always nil
|
||||
return nil, err
|
||||
}
|
||||
log.Debugf("Creating entryPoint redirect %s -> %s : %s -> %s", srcEntryPointName, redirectEntryPoint, regex, replacement)
|
||||
|
||||
return rewrite, nil
|
||||
}
|
||||
|
||||
func (s *Server) buildRedirect(entryPointName string) (string, string, error) {
|
||||
entryPoint := s.globalConfiguration.EntryPoints[entryPointName]
|
||||
if entryPoint == nil {
|
||||
return "", "", fmt.Errorf("unknown target entrypoint %q", entryPointName)
|
||||
}
|
||||
|
||||
exp := regexp.MustCompile(`(:\d+)`)
|
||||
match := exp.FindStringSubmatch(entryPoint.Address)
|
||||
if len(match) == 0 {
|
||||
return "", "", fmt.Errorf("bad Address format %q", s.globalConfiguration.EntryPoints[entryPoint.Redirect.EntryPoint].Address)
|
||||
return "", "", fmt.Errorf("bad Address format %q", entryPoint.Address)
|
||||
}
|
||||
|
||||
var protocol = "http"
|
||||
if s.globalConfiguration.EntryPoints[entryPointName].TLS != nil {
|
||||
protocol = "https"
|
||||
}
|
||||
|
||||
replacement := protocol + "://$1" + match[0] + "$2"
|
||||
return regex, replacement, nil
|
||||
return defaultRedirectRegex, replacement, nil
|
||||
}
|
||||
|
||||
func (s *Server) buildDefaultHTTPRouter() *mux.Router {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue