1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-10-05 18:03:21 +08:00

Allow to override target host header using subdomains

This commit is contained in:
Marcos Lilljedahl
2017-02-17 12:53:16 -03:00
parent f816be6f69
commit 9e57f3b61c
3 changed files with 42 additions and 47 deletions

View File

@@ -3,11 +3,8 @@ package handlers
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"strings"
"github.com/franela/play-with-docker/config"
"github.com/gorilla/mux"
"github.com/yhat/wsutil"
)
@@ -18,27 +15,8 @@ func NewMultipleHostWebsocketReverseProxy() *wsutil.ReverseProxy {
}
director := func(req *http.Request) {
v := mux.Vars(req)
node := v["node"]
port := v["port"]
hostPort := strings.Split(req.Host, ":")
// give priority to the URL host port
if len(hostPort) > 1 && hostPort[1] != config.PortNumber {
port = hostPort[1]
} else if port == "" {
port = "80"
}
if strings.HasPrefix(node, "pwd") {
// Node is actually an ip, need to convert underscores by dots.
ip := strings.Replace(strings.TrimPrefix(node, "pwd"), "_", ".", -1)
if net.ParseIP(ip) == nil {
// Not a valid IP, so treat this is a hostname.
} else {
node = ip
}
}
node, port, host := getTargetInfo(v, req)
if port == "443" {
// Only proxy http for now
@@ -47,6 +25,7 @@ func NewMultipleHostWebsocketReverseProxy() *wsutil.ReverseProxy {
// Only proxy http for now
req.URL.Scheme = "ws"
}
req.Host = host
req.URL.Host = fmt.Sprintf("%s:%s", node, port)
}