mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-07-14 18:27:25 +08:00
Allow to override target host header using subdomains
This commit is contained in:
parent
f816be6f69
commit
9e57f3b61c
6
api.go
6
api.go
@ -61,8 +61,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Specific routes
|
// Specific routes
|
||||||
r.Host(`{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-{port:[0-9]*}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
|
r.Host(`{host:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-{port:[0-9]*}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
|
||||||
r.Host(`{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
|
r.Host(`{host:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
|
||||||
r.HandleFunc("/ping", handlers.Ping).Methods("GET")
|
r.HandleFunc("/ping", handlers.Ping).Methods("GET")
|
||||||
r.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET")
|
r.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET")
|
||||||
r.Handle("/sessions/{sessionId}/instances", http.HandlerFunc(handlers.NewInstance)).Methods("POST")
|
r.Handle("/sessions/{sessionId}/instances", http.HandlerFunc(handlers.NewInstance)).Methods("POST")
|
||||||
@ -110,7 +110,7 @@ func main() {
|
|||||||
|
|
||||||
ssl := mux.NewRouter()
|
ssl := mux.NewRouter()
|
||||||
sslProxyHandler := handlers.NewSSLDaemonHandler()
|
sslProxyHandler := handlers.NewSSLDaemonHandler()
|
||||||
ssl.Host(`{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-2375.{tld:.*}`).Handler(sslProxyHandler)
|
ssl.Host(`{host:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-2375.{tld:.*}`).Handler(sslProxyHandler)
|
||||||
log.Println("Listening TLS on port " + config.SSLPortNumber)
|
log.Println("Listening TLS on port " + config.SSLPortNumber)
|
||||||
|
|
||||||
s := &http.Server{Addr: "0.0.0.0:" + config.SSLPortNumber, Handler: ssl}
|
s := &http.Server{Addr: "0.0.0.0:" + config.SSLPortNumber, Handler: ssl}
|
||||||
|
@ -13,24 +13,10 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewMultipleHostReverseProxy() *httputil.ReverseProxy {
|
func getTargetInfo(vars map[string]string, req *http.Request) (string, string, string) {
|
||||||
var transport http.RoundTripper = &http.Transport{
|
node := vars["node"]
|
||||||
Proxy: http.ProxyFromEnvironment,
|
port := vars["port"]
|
||||||
DialContext: (&net.Dialer{
|
host := vars["host"]
|
||||||
Timeout: 10 * time.Second,
|
|
||||||
KeepAlive: 0,
|
|
||||||
}).DialContext,
|
|
||||||
DisableKeepAlives: true,
|
|
||||||
MaxIdleConns: 1,
|
|
||||||
IdleConnTimeout: 100 * time.Millisecond,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
}
|
|
||||||
director := func(req *http.Request) {
|
|
||||||
v := mux.Vars(req)
|
|
||||||
node := v["node"]
|
|
||||||
port := v["port"]
|
|
||||||
hostPort := strings.Split(req.Host, ":")
|
hostPort := strings.Split(req.Host, ":")
|
||||||
|
|
||||||
// give priority to the URL host port
|
// give priority to the URL host port
|
||||||
@ -51,6 +37,35 @@ func NewMultipleHostReverseProxy() *httputil.ReverseProxy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
DialContext: (&net.Dialer{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
KeepAlive: 0,
|
||||||
|
}).DialContext,
|
||||||
|
DisableKeepAlives: true,
|
||||||
|
MaxIdleConns: 1,
|
||||||
|
IdleConnTimeout: 100 * time.Millisecond,
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
director := func(req *http.Request) {
|
||||||
|
v := mux.Vars(req)
|
||||||
|
node, port, host := getTargetInfo(v, req)
|
||||||
|
|
||||||
if port == "443" {
|
if port == "443" {
|
||||||
// Only proxy http for now
|
// Only proxy http for now
|
||||||
req.URL.Scheme = "https"
|
req.URL.Scheme = "https"
|
||||||
@ -59,6 +74,7 @@ func NewMultipleHostReverseProxy() *httputil.ReverseProxy {
|
|||||||
req.URL.Scheme = "http"
|
req.URL.Scheme = "http"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.Host = host
|
||||||
req.URL.Host = fmt.Sprintf("%s:%s", node, port)
|
req.URL.Host = fmt.Sprintf("%s:%s", node, port)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,11 +3,8 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/franela/play-with-docker/config"
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/yhat/wsutil"
|
"github.com/yhat/wsutil"
|
||||||
)
|
)
|
||||||
@ -18,27 +15,8 @@ func NewMultipleHostWebsocketReverseProxy() *wsutil.ReverseProxy {
|
|||||||
}
|
}
|
||||||
director := func(req *http.Request) {
|
director := func(req *http.Request) {
|
||||||
v := mux.Vars(req)
|
v := mux.Vars(req)
|
||||||
node := v["node"]
|
|
||||||
port := v["port"]
|
|
||||||
hostPort := strings.Split(req.Host, ":")
|
|
||||||
|
|
||||||
// give priority to the URL host port
|
node, port, host := getTargetInfo(v, req)
|
||||||
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 port == "443" {
|
if port == "443" {
|
||||||
// Only proxy http for now
|
// Only proxy http for now
|
||||||
@ -47,6 +25,7 @@ func NewMultipleHostWebsocketReverseProxy() *wsutil.ReverseProxy {
|
|||||||
// Only proxy http for now
|
// Only proxy http for now
|
||||||
req.URL.Scheme = "ws"
|
req.URL.Scheme = "ws"
|
||||||
}
|
}
|
||||||
|
req.Host = host
|
||||||
req.URL.Host = fmt.Sprintf("%s:%s", node, port)
|
req.URL.Host = fmt.Sprintf("%s:%s", node, port)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user