2017-12-13 03:27:43 +08:00
|
|
|
// Copyright 2017 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package vhost
|
|
|
|
|
|
|
|
import (
|
2017-12-13 04:28:58 +08:00
|
|
|
"bytes"
|
2017-12-13 03:27:43 +08:00
|
|
|
"context"
|
2021-01-18 21:49:44 +08:00
|
|
|
"encoding/base64"
|
2017-12-13 03:27:43 +08:00
|
|
|
"errors"
|
2019-03-15 16:22:41 +08:00
|
|
|
"fmt"
|
2017-12-13 03:27:43 +08:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-08-29 01:02:53 +08:00
|
|
|
"net/http/httputil"
|
2022-05-26 23:57:30 +08:00
|
|
|
"net/url"
|
2017-12-13 03:27:43 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-05-29 14:10:34 +08:00
|
|
|
libio "github.com/fatedier/golib/io"
|
2018-05-08 02:13:30 +08:00
|
|
|
"github.com/fatedier/golib/pool"
|
2017-12-13 03:27:43 +08:00
|
|
|
|
2023-11-27 15:47:49 +08:00
|
|
|
httppkg "github.com/fatedier/frp/pkg/util/http"
|
|
|
|
logpkg "github.com/fatedier/frp/pkg/util/log"
|
2017-12-13 03:27:43 +08:00
|
|
|
)
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
var ErrNoRouteFound = errors.New("no route found")
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
type HTTPReverseProxyOptions struct {
|
2018-08-08 11:18:38 +08:00
|
|
|
ResponseHeaderTimeoutS int64
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
type HTTPReverseProxy struct {
|
2022-08-29 01:02:53 +08:00
|
|
|
proxy *httputil.ReverseProxy
|
2020-05-24 17:48:37 +08:00
|
|
|
vhostRouter *Routers
|
2017-12-13 03:27:43 +08:00
|
|
|
|
2018-08-08 11:18:38 +08:00
|
|
|
responseHeaderTimeout time.Duration
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func NewHTTPReverseProxy(option HTTPReverseProxyOptions, vhostRouter *Routers) *HTTPReverseProxy {
|
2018-08-08 11:18:38 +08:00
|
|
|
if option.ResponseHeaderTimeoutS <= 0 {
|
|
|
|
option.ResponseHeaderTimeoutS = 60
|
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
rp := &HTTPReverseProxy{
|
2018-08-08 11:18:38 +08:00
|
|
|
responseHeaderTimeout: time.Duration(option.ResponseHeaderTimeoutS) * time.Second,
|
2019-07-31 00:41:58 +08:00
|
|
|
vhostRouter: vhostRouter,
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
2022-08-29 01:02:53 +08:00
|
|
|
proxy := &httputil.ReverseProxy{
|
2022-05-26 23:57:30 +08:00
|
|
|
// Modify incoming requests by route policies.
|
2024-02-19 21:04:29 +08:00
|
|
|
Rewrite: func(r *httputil.ProxyRequest) {
|
|
|
|
r.SetXForwarded()
|
|
|
|
req := r.Out
|
2017-12-13 03:27:43 +08:00
|
|
|
req.URL.Scheme = "http"
|
2022-10-19 12:14:35 +08:00
|
|
|
reqRouteInfo := req.Context().Value(RouteInfoKey).(*RequestRouteInfo)
|
2023-11-27 15:47:49 +08:00
|
|
|
oldHost, _ := httppkg.CanonicalHost(reqRouteInfo.Host)
|
2022-10-19 12:14:35 +08:00
|
|
|
|
|
|
|
rc := rp.GetRouteConfig(oldHost, reqRouteInfo.URL, reqRouteInfo.HTTPUser)
|
2021-01-18 21:49:44 +08:00
|
|
|
if rc != nil {
|
|
|
|
if rc.RewriteHost != "" {
|
|
|
|
req.Host = rc.RewriteHost
|
|
|
|
}
|
2022-10-19 12:14:35 +08:00
|
|
|
|
|
|
|
var endpoint string
|
|
|
|
if rc.ChooseEndpointFn != nil {
|
|
|
|
// ignore error here, it will use CreateConnFn instead later
|
|
|
|
endpoint, _ = rc.ChooseEndpointFn()
|
|
|
|
reqRouteInfo.Endpoint = endpoint
|
2023-11-27 15:47:49 +08:00
|
|
|
logpkg.Trace("choose endpoint name [%s] for http request host [%s] path [%s] httpuser [%s]",
|
2022-10-19 12:14:35 +08:00
|
|
|
endpoint, oldHost, reqRouteInfo.URL, reqRouteInfo.HTTPUser)
|
|
|
|
}
|
|
|
|
// Set {domain}.{location}.{routeByHTTPUser}.{endpoint} as URL host here to let http transport reuse connections.
|
2022-05-26 23:57:30 +08:00
|
|
|
req.URL.Host = rc.Domain + "." +
|
|
|
|
base64.StdEncoding.EncodeToString([]byte(rc.Location)) + "." +
|
2022-10-19 12:14:35 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte(rc.RouteByHTTPUser)) + "." +
|
|
|
|
base64.StdEncoding.EncodeToString([]byte(endpoint))
|
2018-05-20 23:22:07 +08:00
|
|
|
|
2021-01-18 21:49:44 +08:00
|
|
|
for k, v := range rc.Headers {
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
req.URL.Host = req.Host
|
2018-05-20 23:22:07 +08:00
|
|
|
}
|
2017-12-13 03:27:43 +08:00
|
|
|
},
|
2022-05-26 23:57:30 +08:00
|
|
|
// Create a connection to one proxy routed by route policy.
|
2017-12-13 03:27:43 +08:00
|
|
|
Transport: &http.Transport{
|
2018-08-08 11:18:38 +08:00
|
|
|
ResponseHeaderTimeout: rp.responseHeaderTimeout,
|
2021-01-18 21:49:44 +08:00
|
|
|
IdleConnTimeout: 60 * time.Second,
|
2022-10-19 12:14:35 +08:00
|
|
|
MaxIdleConnsPerHost: 5,
|
2017-12-13 03:27:43 +08:00
|
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2022-10-19 12:14:35 +08:00
|
|
|
return rp.CreateConnection(ctx.Value(RouteInfoKey).(*RequestRouteInfo), true)
|
2022-05-26 23:57:30 +08:00
|
|
|
},
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
// Use proxy mode if there is host in HTTP first request line.
|
|
|
|
// GET http://example.com/ HTTP/1.1
|
|
|
|
// Host: example.com
|
|
|
|
//
|
|
|
|
// Normal:
|
|
|
|
// GET / HTTP/1.1
|
|
|
|
// Host: example.com
|
2022-10-19 12:14:35 +08:00
|
|
|
urlHost := req.Context().Value(RouteInfoKey).(*RequestRouteInfo).URLHost
|
2022-05-26 23:57:30 +08:00
|
|
|
if urlHost != "" {
|
|
|
|
return req.URL, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
2017-12-13 03:27:43 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
BufferPool: newWrapPool(),
|
|
|
|
ErrorLog: log.New(newWrapLogger(), "", 0),
|
2019-08-06 16:49:22 +08:00
|
|
|
ErrorHandler: func(rw http.ResponseWriter, req *http.Request, err error) {
|
2023-11-27 15:47:49 +08:00
|
|
|
logpkg.Warn("do http proxy request [host: %s] error: %v", req.Host, err)
|
2019-08-06 16:49:22 +08:00
|
|
|
rw.WriteHeader(http.StatusNotFound)
|
2022-08-29 01:02:53 +08:00
|
|
|
_, _ = rw.Write(getNotFoundPageContent())
|
2019-08-06 16:49:22 +08:00
|
|
|
},
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
|
|
|
rp.proxy = proxy
|
|
|
|
return rp
|
|
|
|
}
|
|
|
|
|
2019-07-31 00:41:58 +08:00
|
|
|
// Register register the route config to reverse proxy
|
|
|
|
// reverse proxy will use CreateConnFn from routeCfg to create a connection to the remote service
|
2020-05-24 17:48:37 +08:00
|
|
|
func (rp *HTTPReverseProxy) Register(routeCfg RouteConfig) error {
|
2022-05-26 23:57:30 +08:00
|
|
|
err := rp.vhostRouter.Add(routeCfg.Domain, routeCfg.Location, routeCfg.RouteByHTTPUser, &routeCfg)
|
2019-07-31 00:41:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 00:41:58 +08:00
|
|
|
// UnRegister unregister route config by domain and location
|
2022-05-26 23:57:30 +08:00
|
|
|
func (rp *HTTPReverseProxy) UnRegister(routeCfg RouteConfig) {
|
|
|
|
rp.vhostRouter.Del(routeCfg.Domain, routeCfg.Location, routeCfg.RouteByHTTPUser)
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (rp *HTTPReverseProxy) GetRouteConfig(domain, location, routeByHTTPUser string) *RouteConfig {
|
|
|
|
vr, ok := rp.getVhost(domain, location, routeByHTTPUser)
|
2021-01-18 21:49:44 +08:00
|
|
|
if ok {
|
2023-11-27 15:47:49 +08:00
|
|
|
logpkg.Debug("get new HTTP request host [%s] path [%s] httpuser [%s]", domain, location, routeByHTTPUser)
|
2021-01-18 21:49:44 +08:00
|
|
|
return vr.payload.(*RouteConfig)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (rp *HTTPReverseProxy) GetHeaders(domain, location, routeByHTTPUser string) (headers map[string]string) {
|
|
|
|
vr, ok := rp.getVhost(domain, location, routeByHTTPUser)
|
2018-05-20 23:22:07 +08:00
|
|
|
if ok {
|
2020-05-24 17:48:37 +08:00
|
|
|
headers = vr.payload.(*RouteConfig).Headers
|
2018-05-20 23:22:07 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 00:41:58 +08:00
|
|
|
// CreateConnection create a new connection by route config
|
2022-10-19 12:14:35 +08:00
|
|
|
func (rp *HTTPReverseProxy) CreateConnection(reqRouteInfo *RequestRouteInfo, byEndpoint bool) (net.Conn, error) {
|
2023-11-27 15:47:49 +08:00
|
|
|
host, _ := httppkg.CanonicalHost(reqRouteInfo.Host)
|
2022-10-19 12:14:35 +08:00
|
|
|
vr, ok := rp.getVhost(host, reqRouteInfo.URL, reqRouteInfo.HTTPUser)
|
2017-12-13 03:27:43 +08:00
|
|
|
if ok {
|
2022-10-19 12:14:35 +08:00
|
|
|
if byEndpoint {
|
|
|
|
fn := vr.payload.(*RouteConfig).CreateConnByEndpointFn
|
|
|
|
if fn != nil {
|
|
|
|
return fn(reqRouteInfo.Endpoint, reqRouteInfo.RemoteAddr)
|
|
|
|
}
|
|
|
|
}
|
2020-05-24 17:48:37 +08:00
|
|
|
fn := vr.payload.(*RouteConfig).CreateConnFn
|
2017-12-13 03:27:43 +08:00
|
|
|
if fn != nil {
|
2022-10-19 12:14:35 +08:00
|
|
|
return fn(reqRouteInfo.RemoteAddr)
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-19 12:14:35 +08:00
|
|
|
return nil, fmt.Errorf("%v: %s %s %s", ErrNoRouteFound, host, reqRouteInfo.URL, reqRouteInfo.HTTPUser)
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (rp *HTTPReverseProxy) CheckAuth(domain, location, routeByHTTPUser, user, passwd string) bool {
|
|
|
|
vr, ok := rp.getVhost(domain, location, routeByHTTPUser)
|
2017-12-13 23:44:27 +08:00
|
|
|
if ok {
|
2020-05-24 17:48:37 +08:00
|
|
|
checkUser := vr.payload.(*RouteConfig).Username
|
|
|
|
checkPasswd := vr.payload.(*RouteConfig).Password
|
2017-12-13 23:44:27 +08:00
|
|
|
if (checkUser != "" || checkPasswd != "") && (checkUser != user || checkPasswd != passwd) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-11-22 14:30:22 +08:00
|
|
|
// getVhost tries to get vhost router by route policy.
|
2022-05-26 23:57:30 +08:00
|
|
|
func (rp *HTTPReverseProxy) getVhost(domain, location, routeByHTTPUser string) (*Router, bool) {
|
|
|
|
findRouter := func(inDomain, inLocation, inRouteByHTTPUser string) (*Router, bool) {
|
|
|
|
vr, ok := rp.vhostRouter.Get(inDomain, inLocation, inRouteByHTTPUser)
|
|
|
|
if ok {
|
|
|
|
return vr, ok
|
|
|
|
}
|
|
|
|
// Try to check if there is one proxy that doesn't specify routerByHTTPUser, it means match all.
|
|
|
|
vr, ok = rp.vhostRouter.Get(inDomain, inLocation, "")
|
|
|
|
if ok {
|
|
|
|
return vr, ok
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// First we check the full hostname
|
2017-12-13 03:27:43 +08:00
|
|
|
// if not exist, then check the wildcard_domain such as *.example.com
|
2022-05-26 23:57:30 +08:00
|
|
|
vr, ok := findRouter(domain, location, routeByHTTPUser)
|
2017-12-13 03:27:43 +08:00
|
|
|
if ok {
|
2022-05-26 23:57:30 +08:00
|
|
|
return vr, ok
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
// e.g. domain = test.example.com, try to match wildcard domains.
|
|
|
|
// *.example.com
|
|
|
|
// *.com
|
2017-12-13 03:27:43 +08:00
|
|
|
domainSplit := strings.Split(domain, ".")
|
2019-03-15 16:22:41 +08:00
|
|
|
for {
|
|
|
|
if len(domainSplit) < 3 {
|
2022-05-26 23:57:30 +08:00
|
|
|
break
|
2019-03-15 16:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
domainSplit[0] = "*"
|
|
|
|
domain = strings.Join(domainSplit, ".")
|
2022-05-26 23:57:30 +08:00
|
|
|
vr, ok = findRouter(domain, location, routeByHTTPUser)
|
2019-03-15 16:22:41 +08:00
|
|
|
if ok {
|
|
|
|
return vr, true
|
|
|
|
}
|
|
|
|
domainSplit = domainSplit[1:]
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
2022-05-26 23:57:30 +08:00
|
|
|
|
|
|
|
// Finally, try to check if there is one proxy that domain is "*" means match all domains.
|
|
|
|
vr, ok = findRouter("*", location, routeByHTTPUser)
|
|
|
|
if ok {
|
|
|
|
return vr, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *HTTPReverseProxy) connectHandler(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
hj, ok := rw.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client, _, err := hj.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:14:35 +08:00
|
|
|
remote, err := rp.CreateConnection(req.Context().Value(RouteInfoKey).(*RequestRouteInfo), false)
|
2022-05-26 23:57:30 +08:00
|
|
|
if err != nil {
|
2023-09-18 14:28:05 +08:00
|
|
|
_ = NotFoundResponse().Write(client)
|
2022-05-26 23:57:30 +08:00
|
|
|
client.Close()
|
|
|
|
return
|
|
|
|
}
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = req.Write(remote)
|
2023-05-29 14:10:34 +08:00
|
|
|
go libio.Join(remote, client)
|
2022-05-26 23:57:30 +08:00
|
|
|
}
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
func parseBasicAuth(auth string) (username, password string, ok bool) {
|
|
|
|
const prefix = "Basic "
|
|
|
|
// Case insensitive prefix match. See Issue 22736.
|
|
|
|
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cs := string(c)
|
|
|
|
s := strings.IndexByte(cs, ':')
|
|
|
|
if s < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return cs[:s], cs[s+1:], true
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (rp *HTTPReverseProxy) injectRequestInfoToCtx(req *http.Request) *http.Request {
|
|
|
|
user := ""
|
|
|
|
// If url host isn't empty, it's a proxy request. Get http user from Proxy-Authorization header.
|
|
|
|
if req.URL.Host != "" {
|
|
|
|
proxyAuth := req.Header.Get("Proxy-Authorization")
|
|
|
|
if proxyAuth != "" {
|
|
|
|
user, _, _ = parseBasicAuth(proxyAuth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if user == "" {
|
|
|
|
user, _, _ = req.BasicAuth()
|
|
|
|
}
|
2022-10-19 12:14:35 +08:00
|
|
|
|
|
|
|
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)
|
2022-05-26 23:57:30 +08:00
|
|
|
return req.Clone(newctx)
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (rp *HTTPReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2023-11-27 15:47:49 +08:00
|
|
|
domain, _ := httppkg.CanonicalHost(req.Host)
|
2017-12-13 23:44:27 +08:00
|
|
|
location := req.URL.Path
|
|
|
|
user, passwd, _ := req.BasicAuth()
|
2022-05-26 23:57:30 +08:00
|
|
|
if !rp.CheckAuth(domain, location, user, user, passwd) {
|
2017-12-13 23:44:27 +08:00
|
|
|
rw.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2022-05-26 23:57:30 +08:00
|
|
|
|
|
|
|
newreq := rp.injectRequestInfoToCtx(req)
|
|
|
|
if req.Method == http.MethodConnect {
|
|
|
|
rp.connectHandler(rw, newreq)
|
|
|
|
} else {
|
|
|
|
rp.proxy.ServeHTTP(rw, newreq)
|
|
|
|
}
|
2017-12-13 03:27:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type wrapPool struct{}
|
|
|
|
|
|
|
|
func newWrapPool() *wrapPool { return &wrapPool{} }
|
|
|
|
|
|
|
|
func (p *wrapPool) Get() []byte { return pool.GetBuf(32 * 1024) }
|
|
|
|
|
|
|
|
func (p *wrapPool) Put(buf []byte) { pool.PutBuf(buf) }
|
|
|
|
|
|
|
|
type wrapLogger struct{}
|
|
|
|
|
|
|
|
func newWrapLogger() *wrapLogger { return &wrapLogger{} }
|
|
|
|
|
|
|
|
func (l *wrapLogger) Write(p []byte) (n int, err error) {
|
2023-11-27 15:47:49 +08:00
|
|
|
logpkg.Warn("%s", string(bytes.TrimRight(p, "\n")))
|
2017-12-13 03:27:43 +08:00
|
|
|
return len(p), nil
|
|
|
|
}
|