|
|
@ -60,19 +60,28 @@ func NewHTTPReverseProxy(option HTTPReverseProxyOptions, vhostRouter *Routers) *
|
|
|
|
// Modify incoming requests by route policies.
|
|
|
|
// Modify incoming requests by route policies.
|
|
|
|
Director: func(req *http.Request) {
|
|
|
|
Director: func(req *http.Request) {
|
|
|
|
req.URL.Scheme = "http"
|
|
|
|
req.URL.Scheme = "http"
|
|
|
|
url := req.Context().Value(RouteInfoURL).(string)
|
|
|
|
reqRouteInfo := req.Context().Value(RouteInfoKey).(*RequestRouteInfo)
|
|
|
|
routeByHTTPUser := req.Context().Value(RouteInfoHTTPUser).(string)
|
|
|
|
oldHost, _ := util.CanonicalHost(reqRouteInfo.Host)
|
|
|
|
oldHost, _ := util.CanonicalHost(req.Context().Value(RouteInfoHost).(string))
|
|
|
|
|
|
|
|
rc := rp.GetRouteConfig(oldHost, url, routeByHTTPUser)
|
|
|
|
rc := rp.GetRouteConfig(oldHost, reqRouteInfo.URL, reqRouteInfo.HTTPUser)
|
|
|
|
if rc != nil {
|
|
|
|
if rc != nil {
|
|
|
|
if rc.RewriteHost != "" {
|
|
|
|
if rc.RewriteHost != "" {
|
|
|
|
req.Host = rc.RewriteHost
|
|
|
|
req.Host = rc.RewriteHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set {domain}.{location}.{routeByHTTPUser} as URL host here to let http transport reuse connections.
|
|
|
|
|
|
|
|
// TODO(fatedier): use proxy name instead?
|
|
|
|
var endpoint string
|
|
|
|
|
|
|
|
if rc.ChooseEndpointFn != nil {
|
|
|
|
|
|
|
|
// ignore error here, it will use CreateConnFn instead later
|
|
|
|
|
|
|
|
endpoint, _ = rc.ChooseEndpointFn()
|
|
|
|
|
|
|
|
reqRouteInfo.Endpoint = endpoint
|
|
|
|
|
|
|
|
frpLog.Trace("choose endpoint name [%s] for http request host [%s] path [%s] httpuser [%s]",
|
|
|
|
|
|
|
|
endpoint, oldHost, reqRouteInfo.URL, reqRouteInfo.HTTPUser)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set {domain}.{location}.{routeByHTTPUser}.{endpoint} as URL host here to let http transport reuse connections.
|
|
|
|
req.URL.Host = rc.Domain + "." +
|
|
|
|
req.URL.Host = rc.Domain + "." +
|
|
|
|
base64.StdEncoding.EncodeToString([]byte(rc.Location)) + "." +
|
|
|
|
base64.StdEncoding.EncodeToString([]byte(rc.Location)) + "." +
|
|
|
|
base64.StdEncoding.EncodeToString([]byte(rc.RouteByHTTPUser))
|
|
|
|
base64.StdEncoding.EncodeToString([]byte(rc.RouteByHTTPUser)) + "." +
|
|
|
|
|
|
|
|
base64.StdEncoding.EncodeToString([]byte(endpoint))
|
|
|
|
|
|
|
|
|
|
|
|
for k, v := range rc.Headers {
|
|
|
|
for k, v := range rc.Headers {
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
req.Header.Set(k, v)
|
|
|
@ -85,12 +94,9 @@ func NewHTTPReverseProxy(option HTTPReverseProxyOptions, vhostRouter *Routers) *
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
ResponseHeaderTimeout: rp.responseHeaderTimeout,
|
|
|
|
ResponseHeaderTimeout: rp.responseHeaderTimeout,
|
|
|
|
IdleConnTimeout: 60 * time.Second,
|
|
|
|
IdleConnTimeout: 60 * time.Second,
|
|
|
|
|
|
|
|
MaxIdleConnsPerHost: 5,
|
|
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
url := ctx.Value(RouteInfoURL).(string)
|
|
|
|
return rp.CreateConnection(ctx.Value(RouteInfoKey).(*RequestRouteInfo), true)
|
|
|
|
host, _ := util.CanonicalHost(ctx.Value(RouteInfoHost).(string))
|
|
|
|
|
|
|
|
routerByHTTPUser := ctx.Value(RouteInfoHTTPUser).(string)
|
|
|
|
|
|
|
|
remote := ctx.Value(RouteInfoRemote).(string)
|
|
|
|
|
|
|
|
return rp.CreateConnection(host, url, routerByHTTPUser, remote)
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
// Use proxy mode if there is host in HTTP first request line.
|
|
|
|
// Use proxy mode if there is host in HTTP first request line.
|
|
|
@ -100,7 +106,7 @@ func NewHTTPReverseProxy(option HTTPReverseProxyOptions, vhostRouter *Routers) *
|
|
|
|
// Normal:
|
|
|
|
// Normal:
|
|
|
|
// GET / HTTP/1.1
|
|
|
|
// GET / HTTP/1.1
|
|
|
|
// Host: example.com
|
|
|
|
// Host: example.com
|
|
|
|
urlHost := req.Context().Value(RouteInfoURLHost).(string)
|
|
|
|
urlHost := req.Context().Value(RouteInfoKey).(*RequestRouteInfo).URLHost
|
|
|
|
if urlHost != "" {
|
|
|
|
if urlHost != "" {
|
|
|
|
return req.URL, nil
|
|
|
|
return req.URL, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -143,14 +149,6 @@ func (rp *HTTPReverseProxy) GetRouteConfig(domain, location, routeByHTTPUser str
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (rp *HTTPReverseProxy) GetRealHost(domain, location, routeByHTTPUser string) (host string) {
|
|
|
|
|
|
|
|
vr, ok := rp.getVhost(domain, location, routeByHTTPUser)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
|
|
host = vr.payload.(*RouteConfig).RewriteHost
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (rp *HTTPReverseProxy) GetHeaders(domain, location, routeByHTTPUser string) (headers map[string]string) {
|
|
|
|
func (rp *HTTPReverseProxy) GetHeaders(domain, location, routeByHTTPUser string) (headers map[string]string) {
|
|
|
|
vr, ok := rp.getVhost(domain, location, routeByHTTPUser)
|
|
|
|
vr, ok := rp.getVhost(domain, location, routeByHTTPUser)
|
|
|
|
if ok {
|
|
|
|
if ok {
|
|
|
@ -160,15 +158,22 @@ func (rp *HTTPReverseProxy) GetHeaders(domain, location, routeByHTTPUser string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CreateConnection create a new connection by route config
|
|
|
|
// CreateConnection create a new connection by route config
|
|
|
|
func (rp *HTTPReverseProxy) CreateConnection(domain, location, routeByHTTPUser string, remoteAddr string) (net.Conn, error) {
|
|
|
|
func (rp *HTTPReverseProxy) CreateConnection(reqRouteInfo *RequestRouteInfo, byEndpoint bool) (net.Conn, error) {
|
|
|
|
vr, ok := rp.getVhost(domain, location, routeByHTTPUser)
|
|
|
|
host, _ := util.CanonicalHost(reqRouteInfo.Host)
|
|
|
|
|
|
|
|
vr, ok := rp.getVhost(host, reqRouteInfo.URL, reqRouteInfo.HTTPUser)
|
|
|
|
if ok {
|
|
|
|
if ok {
|
|
|
|
|
|
|
|
if byEndpoint {
|
|
|
|
|
|
|
|
fn := vr.payload.(*RouteConfig).CreateConnByEndpointFn
|
|
|
|
|
|
|
|
if fn != nil {
|
|
|
|
|
|
|
|
return fn(reqRouteInfo.Endpoint, reqRouteInfo.RemoteAddr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
fn := vr.payload.(*RouteConfig).CreateConnFn
|
|
|
|
fn := vr.payload.(*RouteConfig).CreateConnFn
|
|
|
|
if fn != nil {
|
|
|
|
if fn != nil {
|
|
|
|
return fn(remoteAddr)
|
|
|
|
return fn(reqRouteInfo.RemoteAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("%v: %s %s %s", ErrNoRouteFound, domain, location, routeByHTTPUser)
|
|
|
|
return nil, fmt.Errorf("%v: %s %s %s", ErrNoRouteFound, host, reqRouteInfo.URL, reqRouteInfo.HTTPUser)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (rp *HTTPReverseProxy) CheckAuth(domain, location, routeByHTTPUser, user, passwd string) bool {
|
|
|
|
func (rp *HTTPReverseProxy) CheckAuth(domain, location, routeByHTTPUser, user, passwd string) bool {
|
|
|
@ -244,12 +249,7 @@ func (rp *HTTPReverseProxy) connectHandler(rw http.ResponseWriter, req *http.Req
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
url := req.Context().Value(RouteInfoURL).(string)
|
|
|
|
remote, err := rp.CreateConnection(req.Context().Value(RouteInfoKey).(*RequestRouteInfo), false)
|
|
|
|
routeByHTTPUser := req.Context().Value(RouteInfoHTTPUser).(string)
|
|
|
|
|
|
|
|
domain, _ := util.CanonicalHost(req.Context().Value(RouteInfoHost).(string))
|
|
|
|
|
|
|
|
remoteAddr := req.Context().Value(RouteInfoRemote).(string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
remote, err := rp.CreateConnection(domain, url, routeByHTTPUser, remoteAddr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
_ = notFoundResponse().Write(client)
|
|
|
|
_ = notFoundResponse().Write(client)
|
|
|
|
client.Close()
|
|
|
|
client.Close()
|
|
|
@ -278,11 +278,6 @@ func parseBasicAuth(auth string) (username, password string, ok bool) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (rp *HTTPReverseProxy) injectRequestInfoToCtx(req *http.Request) *http.Request {
|
|
|
|
func (rp *HTTPReverseProxy) injectRequestInfoToCtx(req *http.Request) *http.Request {
|
|
|
|
newctx := req.Context()
|
|
|
|
|
|
|
|
newctx = context.WithValue(newctx, RouteInfoURL, req.URL.Path)
|
|
|
|
|
|
|
|
newctx = context.WithValue(newctx, RouteInfoHost, req.Host)
|
|
|
|
|
|
|
|
newctx = context.WithValue(newctx, RouteInfoURLHost, req.URL.Host)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user := ""
|
|
|
|
user := ""
|
|
|
|
// If url host isn't empty, it's a proxy request. Get http user from Proxy-Authorization header.
|
|
|
|
// If url host isn't empty, it's a proxy request. Get http user from Proxy-Authorization header.
|
|
|
|
if req.URL.Host != "" {
|
|
|
|
if req.URL.Host != "" {
|
|
|
@ -294,8 +289,16 @@ func (rp *HTTPReverseProxy) injectRequestInfoToCtx(req *http.Request) *http.Requ
|
|
|
|
if user == "" {
|
|
|
|
if user == "" {
|
|
|
|
user, _, _ = req.BasicAuth()
|
|
|
|
user, _, _ = req.BasicAuth()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newctx = context.WithValue(newctx, RouteInfoHTTPUser, user)
|
|
|
|
|
|
|
|
newctx = context.WithValue(newctx, RouteInfoRemote, req.RemoteAddr)
|
|
|
|
reqRouteInfo := &RequestRouteInfo{
|
|
|
|
|
|
|
|
URL: req.URL.Path,
|
|
|
|
|
|
|
|
Host: req.Host,
|
|
|
|
|
|
|
|
HTTPUser: user,
|
|
|
|
|
|
|
|
RemoteAddr: req.RemoteAddr,
|
|
|
|
|
|
|
|
URLHost: req.URL.Host,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
newctx := req.Context()
|
|
|
|
|
|
|
|
newctx = context.WithValue(newctx, RouteInfoKey, reqRouteInfo)
|
|
|
|
return req.Clone(newctx)
|
|
|
|
return req.Clone(newctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|