change log method

This commit is contained in:
fatedier
2019-10-12 20:13:12 +08:00
parent 5dc8175fc8
commit 649f47c345
44 changed files with 670 additions and 688 deletions

View File

@@ -17,11 +17,10 @@ package vhost
import (
"fmt"
"io"
"net"
"strings"
"time"
frpNet "github.com/fatedier/frp/utils/net"
gnet "github.com/fatedier/golib/net"
"github.com/fatedier/golib/pool"
)
@@ -48,7 +47,7 @@ type HttpsMuxer struct {
*VhostMuxer
}
func NewHttpsMuxer(listener frpNet.Listener, timeout time.Duration) (*HttpsMuxer, error) {
func NewHttpsMuxer(listener net.Listener, timeout time.Duration) (*HttpsMuxer, error) {
mux, err := NewVhostMuxer(listener, GetHttpsHostname, nil, nil, timeout)
return &HttpsMuxer{mux}, err
}
@@ -182,7 +181,7 @@ func readHandshake(rd io.Reader) (host string, err error) {
return
}
func GetHttpsHostname(c frpNet.Conn) (_ frpNet.Conn, _ map[string]string, err error) {
func GetHttpsHostname(c net.Conn) (_ net.Conn, _ map[string]string, err error) {
reqInfoMap := make(map[string]string, 0)
sc, rd := gnet.NewSharedConn(c)
host, err := readHandshake(rd)
@@ -191,5 +190,5 @@ func GetHttpsHostname(c frpNet.Conn) (_ frpNet.Conn, _ map[string]string, err er
}
reqInfoMap["Host"] = host
reqInfoMap["Scheme"] = "https"
return frpNet.WrapConn(sc), reqInfoMap, nil
return sc, reqInfoMap, nil
}

View File

@@ -13,22 +13,25 @@
package vhost
import (
"context"
"fmt"
"net"
"strings"
"time"
"github.com/fatedier/frp/utils/log"
frpNet "github.com/fatedier/frp/utils/net"
"github.com/fatedier/frp/utils/xlog"
"github.com/fatedier/golib/errors"
)
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)
type muxFunc func(net.Conn) (net.Conn, map[string]string, error)
type httpAuthFunc func(net.Conn, string, string, string) (bool, error)
type hostRewriteFunc func(net.Conn, string) (net.Conn, error)
type VhostMuxer struct {
listener frpNet.Listener
listener net.Listener
timeout time.Duration
vhostFunc muxFunc
authFunc httpAuthFunc
@@ -36,7 +39,7 @@ type VhostMuxer struct {
registryRouter *VhostRouters
}
func NewVhostMuxer(listener frpNet.Listener, vhostFunc muxFunc, authFunc httpAuthFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *VhostMuxer, err error) {
func NewVhostMuxer(listener net.Listener, vhostFunc muxFunc, authFunc httpAuthFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *VhostMuxer, err error) {
mux = &VhostMuxer{
listener: listener,
timeout: timeout,
@@ -49,7 +52,7 @@ func NewVhostMuxer(listener frpNet.Listener, vhostFunc muxFunc, authFunc httpAut
return mux, nil
}
type CreateConnFunc func(remoteAddr string) (frpNet.Conn, error)
type CreateConnFunc func(remoteAddr string) (net.Conn, error)
// VhostRouteConfig is the params used to match HTTP requests
type VhostRouteConfig struct {
@@ -65,7 +68,7 @@ type VhostRouteConfig struct {
// 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) {
func (v *VhostMuxer) Listen(ctx context.Context, cfg *VhostRouteConfig) (l *Listener, err error) {
l = &Listener{
name: cfg.Domain,
location: cfg.Location,
@@ -73,8 +76,8 @@ func (v *VhostMuxer) Listen(cfg *VhostRouteConfig) (l *Listener, err error) {
userName: cfg.Username,
passWord: cfg.Password,
mux: v,
accept: make(chan frpNet.Conn),
Logger: log.NewPrefixLogger(""),
accept: make(chan net.Conn),
ctx: ctx,
}
err = v.registryRouter.Add(cfg.Domain, cfg.Location, l)
if err != nil {
@@ -123,7 +126,7 @@ func (v *VhostMuxer) run() {
}
}
func (v *VhostMuxer) handle(c frpNet.Conn) {
func (v *VhostMuxer) handle(c net.Conn) {
if err := c.SetDeadline(time.Now().Add(v.timeout)); err != nil {
c.Close()
return
@@ -146,13 +149,14 @@ func (v *VhostMuxer) handle(c frpNet.Conn) {
c.Close()
return
}
xl := xlog.FromContextSafe(l.ctx)
// if authFunc is exist and userName/password is set
// then verify user access
if l.mux.authFunc != nil && l.userName != "" && l.passWord != "" {
bAccess, err := l.mux.authFunc(c, l.userName, l.passWord, reqInfoMap["Authorization"])
if bAccess == false || err != nil {
l.Debug("check http Authorization failed")
xl.Debug("check http Authorization failed")
res := noAuthResponse()
res.Write(c)
c.Close()
@@ -166,12 +170,12 @@ func (v *VhostMuxer) handle(c frpNet.Conn) {
}
c = sConn
l.Debug("get new http request host [%s] path [%s]", name, path)
xl.Debug("get new http request host [%s] path [%s]", name, path)
err = errors.PanicToError(func() {
l.accept <- c
})
if err != nil {
l.Warn("listener is already closed, ignore this request")
xl.Warn("listener is already closed, ignore this request")
}
}
@@ -182,11 +186,12 @@ type Listener struct {
userName string
passWord string
mux *VhostMuxer // for closing VhostMuxer
accept chan frpNet.Conn
log.Logger
accept chan net.Conn
ctx context.Context
}
func (l *Listener) Accept() (frpNet.Conn, error) {
func (l *Listener) Accept() (net.Conn, error) {
xl := xlog.FromContextSafe(l.ctx)
conn, ok := <-l.accept
if !ok {
return nil, fmt.Errorf("Listener closed")
@@ -198,17 +203,13 @@ func (l *Listener) Accept() (frpNet.Conn, error) {
if l.mux.rewriteFunc != nil {
sConn, err := l.mux.rewriteFunc(conn, l.rewriteHost)
if err != nil {
l.Warn("host header rewrite failed: %v", err)
xl.Warn("host header rewrite failed: %v", err)
return nil, fmt.Errorf("host header rewrite failed")
}
l.Debug("rewrite host to [%s] success", l.rewriteHost)
xl.Debug("rewrite host to [%s] success", l.rewriteHost)
conn = sConn
}
for _, prefix := range l.GetAllPrefix() {
conn.AddLogPrefix(prefix)
}
return conn, nil
return frpNet.NewContextConn(conn, l.ctx), nil
}
func (l *Listener) Close() error {
@@ -220,3 +221,7 @@ func (l *Listener) Close() error {
func (l *Listener) Name() string {
return l.name
}
func (l *Listener) Addr() net.Addr {
return (*net.TCPAddr)(nil)
}