2016-04-18 15:16:40 +08:00
|
|
|
// 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 (
|
2019-10-12 20:13:12 +08:00
|
|
|
"context"
|
2016-04-18 15:16:40 +08:00
|
|
|
"fmt"
|
2019-10-12 20:13:12 +08:00
|
|
|
"net"
|
2016-04-18 15:16:40 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
"github.com/fatedier/golib/errors"
|
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/log"
|
2023-05-29 14:10:34 +08:00
|
|
|
utilnet "github.com/fatedier/frp/pkg/util/net"
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/xlog"
|
2016-04-18 15:16:40 +08:00
|
|
|
)
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
type RouteInfo string
|
|
|
|
|
|
|
|
const (
|
2022-10-19 12:14:35 +08:00
|
|
|
RouteInfoKey RouteInfo = "routeInfo"
|
2020-05-24 17:48:37 +08:00
|
|
|
)
|
|
|
|
|
2022-10-19 12:14:35 +08:00
|
|
|
type RequestRouteInfo struct {
|
|
|
|
URL string
|
|
|
|
Host string
|
|
|
|
HTTPUser string
|
|
|
|
RemoteAddr string
|
|
|
|
URLHost string
|
|
|
|
Endpoint string
|
|
|
|
}
|
|
|
|
|
2022-08-29 01:02:53 +08:00
|
|
|
type (
|
|
|
|
muxFunc func(net.Conn) (net.Conn, map[string]string, error)
|
2023-03-07 19:53:32 +08:00
|
|
|
authFunc func(conn net.Conn, username, password string, reqInfoMap map[string]string) (bool, error)
|
2022-08-29 01:02:53 +08:00
|
|
|
hostRewriteFunc func(net.Conn, string) (net.Conn, error)
|
2023-03-07 19:53:32 +08:00
|
|
|
successHookFunc func(net.Conn, map[string]string) error
|
2023-09-18 14:28:05 +08:00
|
|
|
failHookFunc func(net.Conn)
|
2022-08-29 01:02:53 +08:00
|
|
|
)
|
2016-04-18 15:16:40 +08:00
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
// Muxer is a functional component used for https and tcpmux proxies.
|
|
|
|
// It accepts connections and extracts vhost information from the beginning of the connection data.
|
|
|
|
// It then routes the connection to its appropriate listener.
|
2020-05-24 17:48:37 +08:00
|
|
|
type Muxer struct {
|
2023-03-07 19:53:32 +08:00
|
|
|
listener net.Listener
|
|
|
|
timeout time.Duration
|
|
|
|
|
2016-12-18 22:40:58 +08:00
|
|
|
vhostFunc muxFunc
|
2023-03-07 19:53:32 +08:00
|
|
|
checkAuth authFunc
|
|
|
|
successHook successHookFunc
|
2023-09-18 14:28:05 +08:00
|
|
|
failHook failHookFunc
|
2023-03-07 19:53:32 +08:00
|
|
|
rewriteHost hostRewriteFunc
|
2020-05-24 17:48:37 +08:00
|
|
|
registryRouter *Routers
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func NewMuxer(
|
|
|
|
listener net.Listener,
|
|
|
|
vhostFunc muxFunc,
|
|
|
|
timeout time.Duration,
|
|
|
|
) (mux *Muxer, err error) {
|
2020-05-24 17:48:37 +08:00
|
|
|
mux = &Muxer{
|
2016-12-18 22:40:58 +08:00
|
|
|
listener: listener,
|
|
|
|
timeout: timeout,
|
|
|
|
vhostFunc: vhostFunc,
|
2020-05-24 17:48:37 +08:00
|
|
|
registryRouter: NewRouters(),
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
go mux.run()
|
|
|
|
return mux, nil
|
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
func (v *Muxer) SetCheckAuthFunc(f authFunc) *Muxer {
|
|
|
|
v.checkAuth = f
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Muxer) SetSuccessHookFunc(f successHookFunc) *Muxer {
|
|
|
|
v.successHook = f
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2023-09-18 14:28:05 +08:00
|
|
|
func (v *Muxer) SetFailHookFunc(f failHookFunc) *Muxer {
|
|
|
|
v.failHook = f
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
func (v *Muxer) SetRewriteHostFunc(f hostRewriteFunc) *Muxer {
|
|
|
|
v.rewriteHost = f
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:14:35 +08:00
|
|
|
type ChooseEndpointFunc func() (string, error)
|
|
|
|
|
2019-10-12 20:13:12 +08:00
|
|
|
type CreateConnFunc func(remoteAddr string) (net.Conn, error)
|
2017-12-13 23:44:27 +08:00
|
|
|
|
2022-10-19 12:14:35 +08:00
|
|
|
type CreateConnByEndpointFunc func(endpoint, remoteAddr string) (net.Conn, error)
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
// RouteConfig is the params used to match HTTP requests
|
|
|
|
type RouteConfig struct {
|
2022-05-26 23:57:30 +08:00
|
|
|
Domain string
|
|
|
|
Location string
|
|
|
|
RewriteHost string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Headers map[string]string
|
|
|
|
RouteByHTTPUser string
|
2017-12-13 23:44:27 +08:00
|
|
|
|
2022-10-19 12:14:35 +08:00
|
|
|
CreateConnFn CreateConnFunc
|
|
|
|
ChooseEndpointFn ChooseEndpointFunc
|
|
|
|
CreateConnByEndpointFn CreateConnByEndpointFunc
|
2017-03-10 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
// listen for a new domain name, if rewriteHost is not empty and rewriteHost func is not nil,
|
2017-03-10 02:01:17 +08:00
|
|
|
// then rewrite the host header to rewriteHost
|
2020-05-24 17:48:37 +08:00
|
|
|
func (v *Muxer) Listen(ctx context.Context, cfg *RouteConfig) (l *Listener, err error) {
|
2016-12-25 01:53:23 +08:00
|
|
|
l = &Listener{
|
2022-05-26 23:57:30 +08:00
|
|
|
name: cfg.Domain,
|
|
|
|
location: cfg.Location,
|
|
|
|
routeByHTTPUser: cfg.RouteByHTTPUser,
|
|
|
|
rewriteHost: cfg.RewriteHost,
|
2023-03-07 19:53:32 +08:00
|
|
|
username: cfg.Username,
|
|
|
|
password: cfg.Password,
|
2022-05-26 23:57:30 +08:00
|
|
|
mux: v,
|
|
|
|
accept: make(chan net.Conn),
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
err = v.registryRouter.Add(cfg.Domain, cfg.Location, cfg.RouteByHTTPUser, l)
|
2019-07-31 00:41:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-12-25 01:53:23 +08:00
|
|
|
return l, nil
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (v *Muxer) getListener(name, path, httpUser string) (*Listener, bool) {
|
|
|
|
findRouter := func(inName, inPath, inHTTPUser string) (*Listener, bool) {
|
2022-08-29 01:02:53 +08:00
|
|
|
vr, ok := v.registryRouter.Get(inName, inPath, inHTTPUser)
|
2022-05-26 23:57:30 +08:00
|
|
|
if ok {
|
|
|
|
return vr.payload.(*Listener), true
|
|
|
|
}
|
|
|
|
// Try to check if there is one proxy that doesn't specify routerByHTTPUser, it means match all.
|
|
|
|
vr, ok = v.registryRouter.Get(inName, inPath, "")
|
|
|
|
if ok {
|
|
|
|
return vr.payload.(*Listener), true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2016-12-25 01:53:23 +08:00
|
|
|
// first we check the full hostname
|
|
|
|
// if not exist, then check the wildcard_domain such as *.example.com
|
2022-05-26 23:57:30 +08:00
|
|
|
l, ok := findRouter(name, path, httpUser)
|
|
|
|
if ok {
|
|
|
|
return l, true
|
2016-08-23 00:58:51 +08:00
|
|
|
}
|
2016-12-18 22:40:58 +08:00
|
|
|
|
2016-08-23 00:58:51 +08:00
|
|
|
domainSplit := strings.Split(name, ".")
|
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] = "*"
|
|
|
|
name = strings.Join(domainSplit, ".")
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
l, ok = findRouter(name, path, httpUser)
|
|
|
|
if ok {
|
|
|
|
return l, true
|
2019-03-15 16:22:41 +08:00
|
|
|
}
|
|
|
|
domainSplit = domainSplit[1:]
|
|
|
|
}
|
2022-05-26 23:57:30 +08:00
|
|
|
// Finally, try to check if there is one proxy that domain is "*" means match all domains.
|
|
|
|
l, ok = findRouter("*", path, httpUser)
|
|
|
|
if ok {
|
|
|
|
return l, true
|
|
|
|
}
|
|
|
|
return nil, false
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (v *Muxer) run() {
|
2016-04-18 15:16:40 +08:00
|
|
|
for {
|
|
|
|
conn, err := v.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go v.handle(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func (v *Muxer) handle(c net.Conn) {
|
2016-04-18 15:16:40 +08:00
|
|
|
if err := c.SetDeadline(time.Now().Add(v.timeout)); err != nil {
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = c.Close()
|
2016-04-18 15:16:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-29 10:53:02 +08:00
|
|
|
sConn, reqInfoMap, err := v.vhostFunc(c)
|
2016-04-18 15:16:40 +08:00
|
|
|
if err != nil {
|
2020-12-25 13:39:26 +08:00
|
|
|
log.Debug("get hostname from http/https request error: %v", err)
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = c.Close()
|
2016-04-18 15:16:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-25 01:53:23 +08:00
|
|
|
name := strings.ToLower(reqInfoMap["Host"])
|
|
|
|
path := strings.ToLower(reqInfoMap["Path"])
|
2022-05-26 23:57:30 +08:00
|
|
|
httpUser := reqInfoMap["HTTPUser"]
|
|
|
|
l, ok := v.getListener(name, path, httpUser)
|
2016-04-18 15:16:40 +08:00
|
|
|
if !ok {
|
2022-05-26 23:57:30 +08:00
|
|
|
log.Debug("http request for host [%s] path [%s] httpUser [%s] not found", name, path, httpUser)
|
2023-09-18 14:28:05 +08:00
|
|
|
v.failHook(sConn)
|
2016-04-18 15:16:40 +08:00
|
|
|
return
|
|
|
|
}
|
2020-03-05 21:47:49 +08:00
|
|
|
|
2019-10-12 20:13:12 +08:00
|
|
|
xl := xlog.FromContextSafe(l.ctx)
|
2023-03-07 19:53:32 +08:00
|
|
|
if v.successHook != nil {
|
|
|
|
if err := v.successHook(c, reqInfoMap); err != nil {
|
2020-03-05 21:47:49 +08:00
|
|
|
xl.Info("success func failure on vhost connection: %v", err)
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = c.Close()
|
2020-03-05 21:47:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 15:16:40 +08:00
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
// if checkAuth func is exist and username/password is set
|
2017-06-11 16:23:00 +08:00
|
|
|
// then verify user access
|
2023-03-07 19:53:32 +08:00
|
|
|
if l.mux.checkAuth != nil && l.username != "" {
|
|
|
|
ok, err := l.mux.checkAuth(c, l.username, l.password, reqInfoMap)
|
|
|
|
if !ok || err != nil {
|
|
|
|
xl.Debug("auth failed for user: %s", l.username)
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = c.Close()
|
2016-08-29 10:53:02 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 15:16:40 +08:00
|
|
|
if err = sConn.SetDeadline(time.Time{}); err != nil {
|
2022-08-29 01:02:53 +08:00
|
|
|
_ = c.Close()
|
2016-04-18 15:16:40 +08:00
|
|
|
return
|
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
c = sConn
|
2016-04-18 15:16:40 +08:00
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
xl.Debug("new request host [%s] path [%s] httpUser [%s]", name, path, httpUser)
|
2017-11-01 10:51:30 +08:00
|
|
|
err = errors.PanicToError(func() {
|
|
|
|
l.accept <- c
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-10-12 20:13:12 +08:00
|
|
|
xl.Warn("listener is already closed, ignore this request")
|
2017-11-01 10:51:30 +08:00
|
|
|
}
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Listener struct {
|
2022-05-26 23:57:30 +08:00
|
|
|
name string
|
|
|
|
location string
|
|
|
|
routeByHTTPUser string
|
|
|
|
rewriteHost string
|
2023-03-07 19:53:32 +08:00
|
|
|
username string
|
|
|
|
password string
|
2022-05-26 23:57:30 +08:00
|
|
|
mux *Muxer // for closing Muxer
|
|
|
|
accept chan net.Conn
|
|
|
|
ctx context.Context
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 20:13:12 +08:00
|
|
|
func (l *Listener) Accept() (net.Conn, error) {
|
|
|
|
xl := xlog.FromContextSafe(l.ctx)
|
2016-04-18 15:16:40 +08:00
|
|
|
conn, ok := <-l.accept
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Listener closed")
|
|
|
|
}
|
2016-07-26 00:18:19 +08:00
|
|
|
|
2023-03-07 19:53:32 +08:00
|
|
|
// if rewriteHost func is exist
|
2016-07-26 00:18:19 +08:00
|
|
|
// rewrite http requests with a modified host header
|
2017-07-01 01:54:37 +08:00
|
|
|
// if l.rewriteHost is empty, nothing to do
|
2023-03-07 19:53:32 +08:00
|
|
|
if l.mux.rewriteHost != nil {
|
|
|
|
sConn, err := l.mux.rewriteHost(conn, l.rewriteHost)
|
2016-07-26 00:18:19 +08:00
|
|
|
if err != nil {
|
2019-10-12 20:13:12 +08:00
|
|
|
xl.Warn("host header rewrite failed: %v", err)
|
2017-03-12 02:03:24 +08:00
|
|
|
return nil, fmt.Errorf("host header rewrite failed")
|
2016-07-24 15:00:18 +08:00
|
|
|
}
|
2019-10-12 20:13:12 +08:00
|
|
|
xl.Debug("rewrite host to [%s] success", l.rewriteHost)
|
2017-03-09 02:03:47 +08:00
|
|
|
conn = sConn
|
2016-07-24 15:00:18 +08:00
|
|
|
}
|
2023-05-29 14:10:34 +08:00
|
|
|
return utilnet.NewContextConn(l.ctx, conn), nil
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) Close() error {
|
2022-05-26 23:57:30 +08:00
|
|
|
l.mux.registryRouter.Del(l.name, l.location, l.routeByHTTPUser)
|
2016-04-18 15:16:40 +08:00
|
|
|
close(l.accept)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) Name() string {
|
|
|
|
return l.name
|
|
|
|
}
|
2019-10-12 20:13:12 +08:00
|
|
|
|
|
|
|
func (l *Listener) Addr() net.Addr {
|
|
|
|
return (*net.TCPAddr)(nil)
|
|
|
|
}
|