2019-01-01 08:23:01 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2019-10-27 05:53:25 +00:00
|
|
|
"strings"
|
2019-01-01 08:23:01 +00:00
|
|
|
"time"
|
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
"github.com/rancher/remotedialer"
|
|
|
|
"github.com/rancher/wrangler/pkg/kv"
|
2019-01-01 08:23:01 +00:00
|
|
|
utilnet "k8s.io/apimachinery/pkg/util/net"
|
|
|
|
"k8s.io/apiserver/pkg/endpoints/request"
|
|
|
|
"k8s.io/kubernetes/cmd/kube-apiserver/app"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupTunnel() http.Handler {
|
|
|
|
tunnelServer := remotedialer.New(authorizer, remotedialer.DefaultErrorWriter)
|
|
|
|
setupProxyDialer(tunnelServer)
|
|
|
|
return tunnelServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupProxyDialer(tunnelServer *remotedialer.Server) {
|
2020-01-22 17:57:51 +00:00
|
|
|
app.DefaultProxyDialerFn = utilnet.DialFunc(func(ctx context.Context, network, address string) (net.Conn, error) {
|
2019-01-01 08:23:01 +00:00
|
|
|
_, port, _ := net.SplitHostPort(address)
|
|
|
|
addr := "127.0.0.1"
|
|
|
|
if port != "" {
|
|
|
|
addr += ":" + port
|
|
|
|
}
|
|
|
|
nodeName, _ := kv.Split(address, ":")
|
2020-01-22 17:57:51 +00:00
|
|
|
if tunnelServer.HasSession(nodeName) {
|
|
|
|
return tunnelServer.Dial(nodeName, 15*time.Second, "tcp", addr)
|
|
|
|
}
|
|
|
|
var d net.Dialer
|
|
|
|
return d.DialContext(ctx, network, address)
|
2019-01-01 08:23:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func authorizer(req *http.Request) (clientKey string, authed bool, err error) {
|
|
|
|
user, ok := request.UserFrom(req.Context())
|
|
|
|
if !ok {
|
|
|
|
return "", false, nil
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
if strings.HasPrefix(user.GetName(), "system:node:") {
|
|
|
|
return strings.TrimPrefix(user.GetName(), "system:node:"), true, nil
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
return "", false, nil
|
2019-01-01 08:23:01 +00:00
|
|
|
}
|