1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-10-04 17:33: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

@@ -13,6 +13,41 @@ import (
"github.com/gorilla/mux"
)
func getTargetInfo(vars map[string]string, req *http.Request) (string, string, string) {
node := vars["node"]
port := vars["port"]
host := vars["host"]
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
}
}
if len(host) > 0 {
// Remove last "." from host
host = strings.TrimSuffix(host, ".")
} else {
host = req.Host
}
return node, port, host
}
func NewMultipleHostReverseProxy() *httputil.ReverseProxy {
var transport http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
@@ -29,27 +64,7 @@ func NewMultipleHostReverseProxy() *httputil.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
@@ -59,6 +74,7 @@ func NewMultipleHostReverseProxy() *httputil.ReverseProxy {
req.URL.Scheme = "http"
}
req.Host = host
req.URL.Host = fmt.Sprintf("%s:%s", node, port)
}

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)
}