1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 10:17:26 +08:00
docker-labs/handlers/websocket_reverseproxy.go
Jonathan Leibiusky f816be6f69 Add DNS support for PWD instances (#94)
* Add DNS support for PWD instances

* Store IP address of PWD in all session networks and restore it with the
same IP address

* Remove unnecesary print

* Change url format to pwd<ip>-port for better DNS filtering

* Make PWD listen on 80 and 443 for DNS resolve to work
2017-02-17 11:10:01 -03:00

55 lines
1.2 KiB
Go

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"
)
func NewMultipleHostWebsocketReverseProxy() *wsutil.ReverseProxy {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
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
}
}
if port == "443" {
// Only proxy http for now
req.URL.Scheme = "wss"
} else {
// Only proxy http for now
req.URL.Scheme = "ws"
}
req.URL.Host = fmt.Sprintf("%s:%s", node, port)
}
return &wsutil.ReverseProxy{Director: director, TLSClientConfig: tlsConfig}
}