add more log

This commit is contained in:
fatedier 2017-03-12 02:03:24 +08:00
parent b02e1007fb
commit a1023fdfc2
9 changed files with 115 additions and 61 deletions

View File

@ -281,18 +281,21 @@ func (ctl *Control) manager() {
ctl.Warn("[%s] start error: %s", m.ProxyName, m.Error) ctl.Warn("[%s] start error: %s", m.ProxyName, m.Error)
continue continue
} }
oldPxy, ok := ctl.proxies[m.ProxyName]
if ok {
oldPxy.Close()
}
cfg, ok := ctl.pxyCfgs[m.ProxyName] cfg, ok := ctl.pxyCfgs[m.ProxyName]
if !ok { if !ok {
// it will never go to this branch // it will never go to this branch
ctl.Warn("[%s] no proxy conf found", m.ProxyName) ctl.Warn("[%s] no proxy conf found", m.ProxyName)
continue continue
} }
oldPxy, ok := ctl.proxies[m.ProxyName]
if ok {
oldPxy.Close()
}
pxy := NewProxy(ctl, cfg) pxy := NewProxy(ctl, cfg)
pxy.Run() if err := pxy.Run(); err != nil {
ctl.Warn("[%s] proxy start running error: %v", m.ProxyName, err)
continue
}
ctl.proxies[m.ProxyName] = pxy ctl.proxies[m.ProxyName] = pxy
ctl.Info("[%s] start proxy success", m.ProxyName) ctl.Info("[%s] start proxy success", m.ProxyName)
case *msg.Pong: case *msg.Pong:
@ -307,46 +310,64 @@ func (ctl *Control) controler() {
var err error var err error
maxDelayTime := 30 * time.Second maxDelayTime := 30 * time.Second
delayTime := time.Second delayTime := time.Second
checkInterval := 60 * time.Second
checkProxyTicker := time.NewTicker(checkInterval)
for { for {
// we won't get any variable from this channel select {
_, ok := <-ctl.closedCh case <-checkProxyTicker.C:
if !ok { // Every 60 seconds, check which proxy registered failed and reregister it to server.
// close related channels
close(ctl.readCh)
close(ctl.sendCh)
time.Sleep(time.Second)
// loop util reconnect to server success
for {
ctl.Info("try to reconnect to server...")
err = ctl.login()
if err != nil {
ctl.Warn("reconnect to server error: %v", err)
time.Sleep(delayTime)
delayTime = delayTime * 2
if delayTime > maxDelayTime {
delayTime = maxDelayTime
}
continue
}
// reconnect success, init the delayTime
delayTime = time.Second
break
}
// init related channels and variables
ctl.init()
// previous work goroutines should be closed and start them here
go ctl.manager()
go ctl.writer()
go ctl.reader()
// send NewProxy message for all configured proxies
for _, cfg := range ctl.pxyCfgs { for _, cfg := range ctl.pxyCfgs {
var newProxyMsg msg.NewProxy if _, exist := ctl.proxies[cfg.GetName()]; !exist {
cfg.UnMarshalToMsg(&newProxyMsg) ctl.Info("try to reregister proxy [%s]", cfg.GetName())
ctl.sendCh <- &newProxyMsg var newProxyMsg msg.NewProxy
cfg.UnMarshalToMsg(&newProxyMsg)
ctl.sendCh <- &newProxyMsg
}
}
case _, ok := <-ctl.closedCh:
// we won't get any variable from this channel
if !ok {
// close related channels
close(ctl.readCh)
close(ctl.sendCh)
time.Sleep(time.Second)
// loop util reconnect to server success
for {
ctl.Info("try to reconnect to server...")
err = ctl.login()
if err != nil {
ctl.Warn("reconnect to server error: %v", err)
time.Sleep(delayTime)
delayTime = delayTime * 2
if delayTime > maxDelayTime {
delayTime = maxDelayTime
}
continue
}
// reconnect success, init the delayTime
delayTime = time.Second
break
}
// init related channels and variables
ctl.init()
// previous work goroutines should be closed and start them here
go ctl.manager()
go ctl.writer()
go ctl.reader()
// send NewProxy message for all configured proxies
for _, cfg := range ctl.pxyCfgs {
var newProxyMsg msg.NewProxy
cfg.UnMarshalToMsg(&newProxyMsg)
ctl.sendCh <- &newProxyMsg
}
checkProxyTicker.Stop()
checkProxyTicker = time.NewTicker(checkInterval)
} }
} }
} }

View File

@ -25,7 +25,7 @@ import (
// Proxy defines how to work for different proxy type. // Proxy defines how to work for different proxy type.
type Proxy interface { type Proxy interface {
Run() Run() error
// InWorkConn accept work connections registered to server. // InWorkConn accept work connections registered to server.
InWorkConn(conn net.Conn) InWorkConn(conn net.Conn)
@ -64,7 +64,8 @@ type TcpProxy struct {
ctl *Control ctl *Control
} }
func (pxy *TcpProxy) Run() { func (pxy *TcpProxy) Run() (err error) {
return
} }
func (pxy *TcpProxy) Close() { func (pxy *TcpProxy) Close() {
@ -81,7 +82,8 @@ type HttpProxy struct {
ctl *Control ctl *Control
} }
func (pxy *HttpProxy) Run() { func (pxy *HttpProxy) Run() (err error) {
return
} }
func (pxy *HttpProxy) Close() { func (pxy *HttpProxy) Close() {
@ -98,7 +100,8 @@ type HttpsProxy struct {
ctl *Control ctl *Control
} }
func (pxy *HttpsProxy) Run() { func (pxy *HttpsProxy) Run() (err error) {
return
} }
func (pxy *HttpsProxy) Close() { func (pxy *HttpsProxy) Close() {
@ -115,7 +118,8 @@ type UdpProxy struct {
ctl *Control ctl *Control
} }
func (pxy *UdpProxy) Run() { func (pxy *UdpProxy) Run() (err error) {
return
} }
func (pxy *UdpProxy) Close() { func (pxy *UdpProxy) Close() {

View File

@ -108,9 +108,9 @@ func (ctl *Control) RegisterWorkConn(conn net.Conn) {
select { select {
case ctl.workConnCh <- conn: case ctl.workConnCh <- conn:
ctl.conn.Debug("new work connection registered.") ctl.conn.Debug("new work connection registered")
default: default:
ctl.conn.Debug("work connection pool is full, discarding.") ctl.conn.Debug("work connection pool is full, discarding")
conn.Close() conn.Close()
} }
} }

View File

@ -109,6 +109,7 @@ func (pxy *TcpProxy) Run() error {
if err != nil { if err != nil {
return err return err
} }
listener.AddLogPrefix(pxy.name)
pxy.listeners = append(pxy.listeners, listener) pxy.listeners = append(pxy.listeners, listener)
pxy.Info("tcp proxy listen port [%d]", pxy.cfg.RemotePort) pxy.Info("tcp proxy listen port [%d]", pxy.cfg.RemotePort)
@ -148,6 +149,7 @@ func (pxy *HttpProxy) Run() (err error) {
if err != nil { if err != nil {
return err return err
} }
l.AddLogPrefix(pxy.name)
pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location) pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
pxy.listeners = append(pxy.listeners, l) pxy.listeners = append(pxy.listeners, l)
} }
@ -161,6 +163,7 @@ func (pxy *HttpProxy) Run() (err error) {
if err != nil { if err != nil {
return err return err
} }
l.AddLogPrefix(pxy.name)
pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location) pxy.Info("http proxy listen for host [%s] location [%s]", routeConfig.Domain, routeConfig.Location)
pxy.listeners = append(pxy.listeners, l) pxy.listeners = append(pxy.listeners, l)
} }
@ -192,6 +195,7 @@ func (pxy *HttpsProxy) Run() (err error) {
if err != nil { if err != nil {
return err return err
} }
l.AddLogPrefix(pxy.name)
pxy.Info("https proxy listen for host [%s]", routeConfig.Domain) pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
pxy.listeners = append(pxy.listeners, l) pxy.listeners = append(pxy.listeners, l)
} }
@ -202,6 +206,7 @@ func (pxy *HttpsProxy) Run() (err error) {
if err != nil { if err != nil {
return err return err
} }
l.AddLogPrefix(pxy.name)
pxy.Info("https proxy listen for host [%s]", routeConfig.Domain) pxy.Info("https proxy listen for host [%s]", routeConfig.Domain)
pxy.listeners = append(pxy.listeners, l) pxy.listeners = append(pxy.listeners, l)
} }

View File

@ -82,6 +82,7 @@ func Debug(format string, v ...interface{}) {
// Logger // Logger
type Logger interface { type Logger interface {
AddLogPrefix(string) AddLogPrefix(string)
GetAllPrefix() []string
ClearLogPrefix() ClearLogPrefix()
Error(string, ...interface{}) Error(string, ...interface{})
Warn(string, ...interface{}) Warn(string, ...interface{})
@ -90,11 +91,14 @@ type Logger interface {
} }
type PrefixLogger struct { type PrefixLogger struct {
prefix string prefix string
allPrefix []string
} }
func NewPrefixLogger(prefix string) *PrefixLogger { func NewPrefixLogger(prefix string) *PrefixLogger {
logger := &PrefixLogger{} logger := &PrefixLogger{
allPrefix: make([]string, 0),
}
logger.AddLogPrefix(prefix) logger.AddLogPrefix(prefix)
return logger return logger
} }
@ -104,14 +108,17 @@ func (pl *PrefixLogger) AddLogPrefix(prefix string) {
return return
} }
if len(pl.prefix) > 0 {
pl.prefix += " "
}
pl.prefix += "[" + prefix + "] " pl.prefix += "[" + prefix + "] "
pl.allPrefix = append(pl.allPrefix, prefix)
}
func (pl *PrefixLogger) GetAllPrefix() []string {
return pl.allPrefix
} }
func (pl *PrefixLogger) ClearLogPrefix() { func (pl *PrefixLogger) ClearLogPrefix() {
pl.prefix = "" pl.prefix = ""
pl.allPrefix = make([]string, 0)
} }
func (pl *PrefixLogger) Error(format string, v ...interface{}) { func (pl *PrefixLogger) Error(format string, v ...interface{}) {

View File

@ -29,4 +29,5 @@ type Conn interface {
type Listener interface { type Listener interface {
Accept() (Conn, error) Accept() (Conn, error)
Close() error Close() error
log.Logger
} }

View File

@ -30,6 +30,7 @@ type TcpListener struct {
listener net.Listener listener net.Listener
accept chan Conn accept chan Conn
closeFlag bool closeFlag bool
log.Logger
} }
func ListenTcp(bindAddr string, bindPort int64) (l *TcpListener, err error) { func ListenTcp(bindAddr string, bindPort int64) (l *TcpListener, err error) {
@ -47,6 +48,7 @@ func ListenTcp(bindAddr string, bindPort int64) (l *TcpListener, err error) {
listener: listener, listener: listener,
accept: make(chan Conn), accept: make(chan Conn),
closeFlag: false, closeFlag: false,
Logger: log.NewPrefixLogger(""),
} }
go func() { go func() {

View File

@ -21,7 +21,7 @@ import (
"sync" "sync"
"time" "time"
flog "github.com/fatedier/frp/utils/log" "github.com/fatedier/frp/utils/log"
"github.com/fatedier/frp/utils/pool" "github.com/fatedier/frp/utils/pool"
) )
@ -32,7 +32,7 @@ type UdpPacket struct {
} }
type FakeUdpConn struct { type FakeUdpConn struct {
flog.Logger log.Logger
l *UdpListener l *UdpListener
localAddr net.Addr localAddr net.Addr
@ -46,7 +46,7 @@ type FakeUdpConn struct {
func NewFakeUdpConn(l *UdpListener, laddr, raddr net.Addr) *FakeUdpConn { func NewFakeUdpConn(l *UdpListener, laddr, raddr net.Addr) *FakeUdpConn {
fc := &FakeUdpConn{ fc := &FakeUdpConn{
Logger: flog.NewPrefixLogger(""), Logger: log.NewPrefixLogger(""),
l: l, l: l,
localAddr: laddr, localAddr: laddr,
remoteAddr: raddr, remoteAddr: raddr,
@ -163,6 +163,8 @@ type UdpListener struct {
closeFlag bool closeFlag bool
fakeConns map[string]*FakeUdpConn fakeConns map[string]*FakeUdpConn
log.Logger
} }
func ListenUDP(bindAddr string, bindPort int64) (l *UdpListener, err error) { func ListenUDP(bindAddr string, bindPort int64) (l *UdpListener, err error) {
@ -177,6 +179,7 @@ func ListenUDP(bindAddr string, bindPort int64) (l *UdpListener, err error) {
accept: make(chan Conn), accept: make(chan Conn),
writeCh: make(chan *UdpPacket, 1000), writeCh: make(chan *UdpPacket, 1000),
fakeConns: make(map[string]*FakeUdpConn), fakeConns: make(map[string]*FakeUdpConn),
Logger: log.NewPrefixLogger(""),
} }
// for reading // for reading

View File

@ -20,6 +20,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/fatedier/frp/utils/log"
frpNet "github.com/fatedier/frp/utils/net" frpNet "github.com/fatedier/frp/utils/net"
) )
@ -77,6 +78,7 @@ func (v *VhostMuxer) Listen(cfg *VhostRouteConfig) (l *Listener, err error) {
passWord: cfg.Password, passWord: cfg.Password,
mux: v, mux: v,
accept: make(chan frpNet.Conn), accept: make(chan frpNet.Conn),
Logger: log.NewPrefixLogger(""),
} }
v.registryRouter.Add(cfg.Domain, cfg.Location, l) v.registryRouter.Add(cfg.Domain, cfg.Location, l)
return l, nil return l, nil
@ -126,6 +128,7 @@ func (v *VhostMuxer) handle(c frpNet.Conn) {
sConn, reqInfoMap, err := v.vhostFunc(c) sConn, reqInfoMap, err := v.vhostFunc(c)
if err != nil { if err != nil {
log.Error("get hostname from http/https request error: %v", err)
c.Close() c.Close()
return return
} }
@ -134,16 +137,17 @@ func (v *VhostMuxer) handle(c frpNet.Conn) {
path := strings.ToLower(reqInfoMap["Path"]) path := strings.ToLower(reqInfoMap["Path"])
l, ok := v.getListener(name, path) l, ok := v.getListener(name, path)
if !ok { if !ok {
log.Debug("http request for host [%s] path [%s] not found", name, path)
c.Close() c.Close()
return return
} }
// if authFunc is exist and userName/password is set // if authFunc is exist and userName/password is set
// verify user access // verify user access
if l.mux.authFunc != nil && if l.mux.authFunc != nil && l.userName != "" && l.passWord != "" {
l.userName != "" && l.passWord != "" {
bAccess, err := l.mux.authFunc(c, l.userName, l.passWord, reqInfoMap["Authorization"]) bAccess, err := l.mux.authFunc(c, l.userName, l.passWord, reqInfoMap["Authorization"])
if bAccess == false || err != nil { if bAccess == false || err != nil {
l.Debug("check Authorization failed")
res := noAuthResponse() res := noAuthResponse()
res.Write(c) res.Write(c)
c.Close() c.Close()
@ -157,6 +161,7 @@ func (v *VhostMuxer) handle(c frpNet.Conn) {
} }
c = sConn c = sConn
l.Debug("get new http request host [%s] path [%s]", name, path)
l.accept <- c l.accept <- c
} }
@ -168,6 +173,7 @@ type Listener struct {
passWord string passWord string
mux *VhostMuxer // for closing VhostMuxer mux *VhostMuxer // for closing VhostMuxer
accept chan frpNet.Conn accept chan frpNet.Conn
log.Logger
} }
func (l *Listener) Accept() (frpNet.Conn, error) { func (l *Listener) Accept() (frpNet.Conn, error) {
@ -181,11 +187,16 @@ func (l *Listener) Accept() (frpNet.Conn, error) {
if l.mux.rewriteFunc != nil && l.rewriteHost != "" { if l.mux.rewriteFunc != nil && l.rewriteHost != "" {
sConn, err := l.mux.rewriteFunc(conn, l.rewriteHost) sConn, err := l.mux.rewriteFunc(conn, l.rewriteHost)
if err != nil { if err != nil {
return nil, fmt.Errorf("http host header rewrite failed") l.Warn("host header rewrite failed: %v", err)
return nil, fmt.Errorf("host header rewrite failed")
} }
l.Debug("rewrite host to [%s] success", l.rewriteHost)
conn = sConn conn = sConn
} }
for _, prefix := range l.GetAllPrefix() {
conn.AddLogPrefix(prefix)
}
return conn, nil return conn, nil
} }