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 (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
"github.com/fatedier/frp/utils/log"
|
2017-03-09 02:03:47 +08:00
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
2018-05-08 02:13:30 +08:00
|
|
|
|
|
|
|
"github.com/fatedier/golib/errors"
|
2016-04-18 15:16:40 +08:00
|
|
|
)
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
type muxFunc func(frpNet.Conn) (frpNet.Conn, map[string]string, error)
|
|
|
|
type httpAuthFunc func(frpNet.Conn, string, string, string) (bool, error)
|
|
|
|
type hostRewriteFunc func(frpNet.Conn, string) (frpNet.Conn, error)
|
2016-04-18 15:16:40 +08:00
|
|
|
|
|
|
|
type VhostMuxer struct {
|
2017-03-09 02:03:47 +08:00
|
|
|
listener frpNet.Listener
|
2016-12-18 22:40:58 +08:00
|
|
|
timeout time.Duration
|
|
|
|
vhostFunc muxFunc
|
|
|
|
authFunc httpAuthFunc
|
|
|
|
rewriteFunc hostRewriteFunc
|
|
|
|
registryRouter *VhostRouters
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
func NewVhostMuxer(listener frpNet.Listener, vhostFunc muxFunc, authFunc httpAuthFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *VhostMuxer, err error) {
|
2016-04-18 15:16:40 +08:00
|
|
|
mux = &VhostMuxer{
|
2016-12-18 22:40:58 +08:00
|
|
|
listener: listener,
|
|
|
|
timeout: timeout,
|
|
|
|
vhostFunc: vhostFunc,
|
|
|
|
authFunc: authFunc,
|
|
|
|
rewriteFunc: rewriteFunc,
|
|
|
|
registryRouter: NewVhostRouters(),
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
go mux.run()
|
|
|
|
return mux, nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 10:51:01 +08:00
|
|
|
type CreateConnFunc func(remoteAddr string) (frpNet.Conn, error)
|
2017-12-13 23:44:27 +08:00
|
|
|
|
2019-07-31 00:41:58 +08:00
|
|
|
// VhostRouteConfig is the params used to match HTTP requests
|
2017-03-10 02:01:17 +08:00
|
|
|
type VhostRouteConfig struct {
|
|
|
|
Domain string
|
|
|
|
Location string
|
|
|
|
RewriteHost string
|
|
|
|
Username string
|
|
|
|
Password string
|
2018-05-20 23:22:07 +08:00
|
|
|
Headers map[string]string
|
2017-12-13 23:44:27 +08:00
|
|
|
|
|
|
|
CreateConnFn CreateConnFunc
|
2017-03-10 02:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// listen for a new domain name, if rewriteHost is not empty and rewriteFunc is not nil
|
|
|
|
// then rewrite the host header to rewriteHost
|
|
|
|
func (v *VhostMuxer) Listen(cfg *VhostRouteConfig) (l *Listener, err error) {
|
2016-12-25 01:53:23 +08:00
|
|
|
l = &Listener{
|
2017-03-10 02:01:17 +08:00
|
|
|
name: cfg.Domain,
|
|
|
|
location: cfg.Location,
|
|
|
|
rewriteHost: cfg.RewriteHost,
|
|
|
|
userName: cfg.Username,
|
|
|
|
passWord: cfg.Password,
|
2016-12-25 01:53:23 +08:00
|
|
|
mux: v,
|
2017-03-09 02:03:47 +08:00
|
|
|
accept: make(chan frpNet.Conn),
|
2017-03-12 02:03:24 +08:00
|
|
|
Logger: log.NewPrefixLogger(""),
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
2019-07-31 00:41:58 +08:00
|
|
|
err = v.registryRouter.Add(cfg.Domain, cfg.Location, l)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-12-25 01:53:23 +08:00
|
|
|
return l, nil
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-25 01:53:23 +08:00
|
|
|
func (v *VhostMuxer) getListener(name, path string) (l *Listener, exist bool) {
|
|
|
|
// first we check the full hostname
|
|
|
|
// if not exist, then check the wildcard_domain such as *.example.com
|
|
|
|
vr, found := v.registryRouter.Get(name, path)
|
2016-12-18 22:40:58 +08:00
|
|
|
if found {
|
2017-12-13 03:27:43 +08:00
|
|
|
return vr.payload.(*Listener), 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, ".")
|
|
|
|
if len(domainSplit) < 3 {
|
2016-12-18 22:40:58 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:22:41 +08:00
|
|
|
for {
|
|
|
|
if len(domainSplit) < 3 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
domainSplit[0] = "*"
|
|
|
|
name = strings.Join(domainSplit, ".")
|
|
|
|
|
|
|
|
vr, found = v.registryRouter.Get(name, path)
|
|
|
|
if found {
|
|
|
|
return vr.payload.(*Listener), true
|
|
|
|
}
|
|
|
|
domainSplit = domainSplit[1:]
|
|
|
|
}
|
|
|
|
return
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *VhostMuxer) run() {
|
|
|
|
for {
|
|
|
|
conn, err := v.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go v.handle(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
func (v *VhostMuxer) handle(c frpNet.Conn) {
|
2016-04-18 15:16:40 +08:00
|
|
|
if err := c.SetDeadline(time.Now().Add(v.timeout)); err != nil {
|
2016-08-23 00:58:51 +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 {
|
2017-06-11 16:23:00 +08:00
|
|
|
log.Warn("get hostname from http/https request error: %v", err)
|
2016-08-23 00:58:51 +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"])
|
|
|
|
l, ok := v.getListener(name, path)
|
2016-04-18 15:16:40 +08:00
|
|
|
if !ok {
|
2017-06-11 16:23:00 +08:00
|
|
|
res := notFoundResponse()
|
|
|
|
res.Write(c)
|
2017-03-12 02:03:24 +08:00
|
|
|
log.Debug("http request for host [%s] path [%s] not found", name, path)
|
2016-08-23 00:58:51 +08:00
|
|
|
c.Close()
|
2016-04-18 15:16:40 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-29 10:53:02 +08:00
|
|
|
// if authFunc is exist and userName/password is set
|
2017-06-11 16:23:00 +08:00
|
|
|
// then verify user access
|
2017-03-12 02:03:24 +08:00
|
|
|
if l.mux.authFunc != nil && l.userName != "" && l.passWord != "" {
|
2016-08-29 10:53:02 +08:00
|
|
|
bAccess, err := l.mux.authFunc(c, l.userName, l.passWord, reqInfoMap["Authorization"])
|
|
|
|
if bAccess == false || err != nil {
|
2017-06-11 16:23:00 +08:00
|
|
|
l.Debug("check http Authorization failed")
|
2016-08-29 10:53:02 +08:00
|
|
|
res := noAuthResponse()
|
2017-03-09 02:03:47 +08:00
|
|
|
res.Write(c)
|
2016-08-29 10:53:02 +08:00
|
|
|
c.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 15:16:40 +08:00
|
|
|
if err = sConn.SetDeadline(time.Time{}); err != nil {
|
2016-08-23 00:58:51 +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
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
l.Debug("get new http request host [%s] path [%s]", name, path)
|
2017-11-01 10:51:30 +08:00
|
|
|
err = errors.PanicToError(func() {
|
|
|
|
l.accept <- c
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
l.Warn("listener is already closed, ignore this request")
|
|
|
|
}
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Listener struct {
|
2016-07-26 00:18:19 +08:00
|
|
|
name string
|
2016-12-27 02:52:32 +08:00
|
|
|
location string
|
2016-07-26 00:18:19 +08:00
|
|
|
rewriteHost string
|
2016-08-29 10:53:02 +08:00
|
|
|
userName string
|
|
|
|
passWord string
|
2016-07-26 00:18:19 +08:00
|
|
|
mux *VhostMuxer // for closing VhostMuxer
|
2017-03-09 02:03:47 +08:00
|
|
|
accept chan frpNet.Conn
|
2017-03-12 02:03:24 +08:00
|
|
|
log.Logger
|
2016-04-18 15:16:40 +08:00
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
func (l *Listener) Accept() (frpNet.Conn, error) {
|
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
|
|
|
|
2017-07-01 01:54:37 +08:00
|
|
|
// if rewriteFunc 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
|
|
|
|
if l.mux.rewriteFunc != nil {
|
2016-07-26 00:18:19 +08:00
|
|
|
sConn, err := l.mux.rewriteFunc(conn, l.rewriteHost)
|
|
|
|
if err != nil {
|
2017-03-12 02:03:24 +08:00
|
|
|
l.Warn("host header rewrite failed: %v", err)
|
|
|
|
return nil, fmt.Errorf("host header rewrite failed")
|
2016-07-24 15:00:18 +08:00
|
|
|
}
|
2017-03-12 02:03:24 +08:00
|
|
|
l.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
|
|
|
}
|
2016-08-29 10:53:02 +08:00
|
|
|
|
2017-03-12 02:03:24 +08:00
|
|
|
for _, prefix := range l.GetAllPrefix() {
|
|
|
|
conn.AddLogPrefix(prefix)
|
|
|
|
}
|
2016-04-18 15:16:40 +08:00
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) Close() error {
|
2016-12-27 02:52:32 +08:00
|
|
|
l.mux.registryRouter.Del(l.name, l.location)
|
2016-04-18 15:16:40 +08:00
|
|
|
close(l.accept)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) Name() string {
|
|
|
|
return l.name
|
|
|
|
}
|