1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 10:17:26 +08:00
docker-labs/handlers/reverseproxy.go
Jonathan Leibiusky @xetorthio ec9d34ffda Add reverse proxy endpoint.
It works by using the Host of the request. When it receives something in
the form of: `<node>.<session>.play-with-docker.com` it does a reverse
proxy http request to `node`, validating that the `node` actually belongs
to the `session`.
If the node has a prefix `ip` and continues with a valid IP address
where the dots where replaces by underscores (like `ip10_0_0_1`) then it
will remove the `ip` prefix and and replace the underscores by dots, and
assume it is an ip address.
2016-11-23 11:52:59 -03:00

38 lines
749 B
Go

package handlers
import (
"net"
"net/http"
"net/http/httputil"
"strings"
"github.com/gorilla/mux"
)
func NewMultipleHostReverseProxy() *httputil.ReverseProxy {
director := func(req *http.Request) {
v := mux.Vars(req)
node := v["node"]
if strings.HasPrefix(node, "ip") {
// Node is actually an ip, need to convert underscores by dots.
ip := strings.Replace(strings.TrimPrefix(node, "ip"), "_", ".", -1)
if net.ParseIP(ip) == nil {
// Not a valid IP, so treat this is a hostname.
} else {
node = ip
}
}
// Validate that the node actually exists in the network
// TODO:
// Only proxy http for now
req.URL.Scheme = "http"
req.URL.Host = node
}
return &httputil.ReverseProxy{Director: director}
}