2019-01-22 07:30:04 +00:00
|
|
|
package redirect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-04-01 13:30:07 +00:00
|
|
|
"errors"
|
2021-08-11 15:10:12 +00:00
|
|
|
"net"
|
2019-01-22 07:30:04 +00:00
|
|
|
"net/http"
|
2021-08-11 15:10:12 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-01-22 07:30:04 +00:00
|
|
|
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares"
|
2019-01-22 07:30:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeSchemeName = "RedirectScheme"
|
2020-06-16 23:10:04 +00:00
|
|
|
schemeRedirectRegex = `^(https?:\/\/)?(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$`
|
2019-01-22 07:30:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewRedirectScheme creates a new RedirectScheme middleware.
|
2019-07-10 07:26:04 +00:00
|
|
|
func NewRedirectScheme(ctx context.Context, next http.Handler, conf dynamic.RedirectScheme, name string) (http.Handler, error) {
|
2019-09-13 17:28:04 +00:00
|
|
|
logger := log.FromContext(middlewares.GetLoggerCtx(ctx, name, typeSchemeName))
|
2019-01-22 07:30:04 +00:00
|
|
|
logger.Debug("Creating middleware")
|
|
|
|
logger.Debugf("Setting up redirection to %s %s", conf.Scheme, conf.Port)
|
|
|
|
|
|
|
|
if len(conf.Scheme) == 0 {
|
|
|
|
return nil, errors.New("you must provide a target scheme")
|
|
|
|
}
|
|
|
|
|
|
|
|
port := ""
|
2021-08-11 15:10:12 +00:00
|
|
|
if len(conf.Port) > 0 && !(conf.Scheme == schemeHTTP && conf.Port == "80" || conf.Scheme == schemeHTTPS && conf.Port == "443") {
|
2019-01-22 07:30:04 +00:00
|
|
|
port = ":" + conf.Port
|
|
|
|
}
|
|
|
|
|
2021-08-11 15:10:12 +00:00
|
|
|
return newRedirect(next, schemeRedirectRegex, conf.Scheme+"://${2}"+port+"${4}", conf.Permanent, rawURLScheme, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rawURLScheme(req *http.Request) string {
|
|
|
|
scheme := schemeHTTP
|
|
|
|
host, port, err := net.SplitHostPort(req.Host)
|
|
|
|
if err != nil {
|
|
|
|
host = req.Host
|
|
|
|
} else {
|
|
|
|
port = ":" + port
|
|
|
|
}
|
|
|
|
uri := req.RequestURI
|
|
|
|
|
|
|
|
schemeRegex := `^(https?):\/\/(\[[\w:.]+\]|[\w\._-]+)?(:\d+)?(.*)$`
|
|
|
|
re, _ := regexp.Compile(schemeRegex)
|
|
|
|
if re.Match([]byte(req.RequestURI)) {
|
|
|
|
match := re.FindStringSubmatch(req.RequestURI)
|
|
|
|
scheme = match[1]
|
|
|
|
|
|
|
|
if len(match[2]) > 0 {
|
|
|
|
host = match[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(match[3]) > 0 {
|
|
|
|
port = match[3]
|
|
|
|
}
|
|
|
|
|
|
|
|
uri = match[4]
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.TLS != nil {
|
|
|
|
scheme = schemeHTTPS
|
|
|
|
}
|
|
|
|
|
|
|
|
if scheme == schemeHTTP && port == ":80" || scheme == schemeHTTPS && port == ":443" || port == "" {
|
|
|
|
port = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join([]string{scheme, "://", host, port, uri}, "")
|
2019-01-22 07:30:04 +00:00
|
|
|
}
|