-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
355 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package proxy | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/cnrancher/autok3s/pkg/common" | ||
|
||
"github.com/sirupsen/logrus" | ||
utilnet "k8s.io/apimachinery/pkg/util/net" | ||
) | ||
|
||
const ( | ||
forwardProto = "X-Forwarded-Proto" | ||
whiteListDomainSettingName = "whitelist-domain" | ||
hostRegex = "[A-Za-z0-9-]+" | ||
) | ||
|
||
var ( | ||
httpStart = regexp.MustCompile("^http:/([^/])") | ||
httpsStart = regexp.MustCompile("^https:/([^/])") | ||
) | ||
|
||
type proxy struct { | ||
prefix string | ||
} | ||
|
||
// NewProxy return http proxy handler | ||
func NewProxy(prefix string) http.Handler { | ||
p := &proxy{ | ||
prefix: prefix, | ||
} | ||
return &httputil.ReverseProxy{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
InsecureSkipVerify: true, | ||
}, | ||
}, | ||
Director: func(req *http.Request) { | ||
if err := p.proxy(req); err != nil { | ||
logrus.Infof("Failed to proxy: %v", err) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
func (p *proxy) isAllowed(host string) bool { | ||
setting, err := common.DefaultDB.GetSetting(whiteListDomainSettingName) | ||
if err != nil || setting == nil { | ||
logrus.Errorf("failed to get whitelist-domain setting, err %v", err) | ||
return false | ||
} | ||
validHosts := strings.Split(setting.Value, ",") | ||
for _, valid := range validHosts { | ||
if valid == host { | ||
return true | ||
} | ||
|
||
if strings.HasPrefix(valid, "*") && strings.HasSuffix(host, valid[1:]) { | ||
return true | ||
} | ||
|
||
if strings.Contains(valid, ".%.") || strings.HasPrefix(valid, "%.") { | ||
r := constructRegex(valid) | ||
if match := r.MatchString(host); match { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (p *proxy) proxy(req *http.Request) error { | ||
path := req.URL.String() | ||
index := strings.Index(path, p.prefix) | ||
destPath := path[index+len(p.prefix):] | ||
destPath, err := unescapePath(destPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if httpsStart.MatchString(destPath) { | ||
destPath = httpsStart.ReplaceAllString(destPath, "https://$1") | ||
} else if httpStart.MatchString(destPath) { | ||
destPath = httpStart.ReplaceAllString(destPath, "http://$1") | ||
} else { | ||
destPath = "https://" + destPath | ||
} | ||
|
||
destURL, err := url.Parse(destPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
destURL.RawQuery = req.URL.RawQuery | ||
destURLHostname := destURL.Hostname() | ||
|
||
if !p.isAllowed(destURLHostname) { | ||
return fmt.Errorf("invalid host: %v", destURLHostname) | ||
} | ||
|
||
headerCopy := utilnet.CloneHeader(req.Header) | ||
if req.TLS != nil { | ||
headerCopy.Set(forwardProto, "https") | ||
} | ||
req.Host = destURLHostname | ||
req.URL = destURL | ||
req.Header = headerCopy | ||
|
||
return nil | ||
} | ||
|
||
func constructRegex(host string) *regexp.Regexp { | ||
// incoming host "ec2.%.amazonaws.com" | ||
// Converted to regex "^ec2\.[A-Za-z0-9-]+\.amazonaws\.com$" | ||
parts := strings.Split(host, ".") | ||
for i, part := range parts { | ||
if part == "%" { | ||
parts[i] = hostRegex | ||
} else { | ||
parts[i] = regexp.QuoteMeta(part) | ||
} | ||
} | ||
|
||
str := "^" + strings.Join(parts, "\\.") + "$" | ||
|
||
return regexp.MustCompile(str) | ||
} | ||
|
||
func unescapePath(destPath string) (string, error) { | ||
var err error | ||
if os.Getenv("AUTOK3S_DEV_MODE") != "" { | ||
destPath, err = url.QueryUnescape(destPath) | ||
logrus.Infof("******* proxy url: %v ********", destPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
return destPath, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package proxy | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/cnrancher/autok3s/pkg/utils" | ||
|
||
"github.com/sirupsen/logrus" | ||
k8sproxy "k8s.io/apimachinery/pkg/util/proxy" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
type K8sProxyHandler struct{} | ||
|
||
func NewK8sProxy() http.Handler { | ||
return &K8sProxyHandler{} | ||
} | ||
|
||
type errorResponder struct{} | ||
|
||
func (r *errorResponder) Error(w http.ResponseWriter, req *http.Request, err error) { | ||
logrus.Errorf("Error while proxying request: %v", err) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
|
||
func (kh *K8sProxyHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||
config := req.Header.Get("config") | ||
cfg, err := clientcmd.RESTConfigFromKubeConfig([]byte(utils.StringSupportBase64(config))) | ||
if err != nil { | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
rw.Write([]byte(err.Error())) | ||
return | ||
} | ||
host := cfg.Host | ||
if !strings.HasSuffix(host, "/") { | ||
host = host + "/" | ||
} | ||
u, err := url.Parse(host) | ||
if err != nil { | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
rw.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
u.Path = strings.TrimPrefix(req.URL.Path, "/k8s/proxy") | ||
u.RawQuery = req.URL.RawQuery | ||
req.URL.Host = req.Host | ||
responder := &errorResponder{} | ||
transport, err := rest.TransportFor(cfg) | ||
if err != nil { | ||
rw.WriteHeader(http.StatusInternalServerError) | ||
rw.Write([]byte(err.Error())) | ||
return | ||
} | ||
handler := k8sproxy.NewUpgradeAwareHandler(u, transport, true, false, responder) | ||
handler.ServeHTTP(rw, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.