From 1064f4881174aa09d6a9121b10083611df2a088c Mon Sep 17 00:00:00 2001 From: XueBing Date: Sun, 18 Dec 2016 16:34:38 +0800 Subject: [PATCH] modify the metric --- src/cmd/frps/control.go | 2 +- src/models/config/config.go | 28 +++++++++++++++++++--------- src/models/metric/server.go | 32 +++++++++----------------------- src/models/server/config.go | 8 +++----- src/models/server/server.go | 12 ++++-------- src/utils/vhost/vhost.go | 10 +++++++--- 6 files changed, 43 insertions(+), 49 deletions(-) diff --git a/src/cmd/frps/control.go b/src/cmd/frps/control.go index 15620ff6..f120d77b 100644 --- a/src/cmd/frps/control.go +++ b/src/cmd/frps/control.go @@ -302,7 +302,7 @@ func doLogin(req *msg.ControlReq, c *conn.Conn) (ret int64, info string) { } // update metric's proxy status - metric.SetProxyInfo(s.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip, s.PrivilegeMode, s.CustomDomains, s.ListenPort) + metric.SetProxyInfo(*s.ProxyServerConf) // start proxy and listen for user connections, no block err := s.Start(c) diff --git a/src/models/config/config.go b/src/models/config/config.go index 325dcb9b..b86491a3 100644 --- a/src/models/config/config.go +++ b/src/models/config/config.go @@ -15,13 +15,23 @@ package config type BaseConf struct { - Name string - AuthToken string - Type string - UseEncryption bool - UseGzip bool - PrivilegeMode bool - PrivilegeToken string - PoolCount int64 - HostHeaderRewrite string + Name string `json:"name"` + AuthToken string `json:"-"` + Type string `json:"type"` + UseEncryption bool `json:"use_encryption"` + UseGzip bool `json:"use_gzip"` + PrivilegeMode bool `json:"privilege_mode"` + PrivilegeToken string `json:"-"` + PoolCount int64 `json:"pool_count"` + HostHeaderRewrite string `json:"host_header_rewrite"` +} + +type ProxyServerConf struct { + BaseConf + BindAddr string `json:"bind_addr"` + ListenPort int64 `json:"bind_port"` + CustomDomains []string `json:"custom_domains"` + Locations []string `json:"custom_locations"` + + Status int64 `json:"status"` } diff --git a/src/models/metric/server.go b/src/models/metric/server.go index fce7811a..d7775df9 100644 --- a/src/models/metric/server.go +++ b/src/models/metric/server.go @@ -19,6 +19,7 @@ import ( "sync" "time" + "github.com/fatedier/frp/src/models/config" "github.com/fatedier/frp/src/models/consts" ) @@ -29,15 +30,8 @@ var ( ) type ServerMetric struct { - Name string `json:"name"` - Type string `json:"type"` - BindAddr string `json:"bind_addr"` - ListenPort int64 `json:"listen_port"` - CustomDomains []string `json:"custom_domains"` - Status string `json:"status"` - UseEncryption bool `json:"use_encryption"` - UseGzip bool `json:"use_gzip"` - PrivilegeMode bool `json:"privilege_mode"` + config.ProxyServerConf + StatusDesc string `json:"status_desc"` // statistics CurrentConns int64 `json:"current_conns"` @@ -110,24 +104,16 @@ func GetProxyMetrics(proxyName string) *ServerMetric { } } -func SetProxyInfo(proxyName string, proxyType, bindAddr string, - useEncryption, useGzip, privilegeMode bool, customDomains []string, - listenPort int64) { +func SetProxyInfo(p config.ProxyServerConf) { smMutex.Lock() - info, ok := ServerMetricInfoMap[proxyName] + info, ok := ServerMetricInfoMap[p.Name] if !ok { info = &ServerMetric{} info.Daily = make([]*DailyServerStats, 0) } - info.Name = proxyName - info.Type = proxyType - info.UseEncryption = useEncryption - info.UseGzip = useGzip - info.PrivilegeMode = privilegeMode - info.BindAddr = bindAddr - info.ListenPort = listenPort - info.CustomDomains = customDomains - ServerMetricInfoMap[proxyName] = info + + info.ProxyServerConf = p + ServerMetricInfoMap[p.Name] = info smMutex.Unlock() } @@ -137,7 +123,7 @@ func SetStatus(proxyName string, status int64) { smMutex.RUnlock() if ok { metric.mutex.Lock() - metric.Status = consts.StatusStr[status] + metric.StatusDesc = consts.StatusStr[status] metric.mutex.Unlock() } } diff --git a/src/models/server/config.go b/src/models/server/config.go index 91d4009e..66698be5 100644 --- a/src/models/server/config.go +++ b/src/models/server/config.go @@ -300,9 +300,8 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e } // set metric statistics of all proxies - for name, p := range proxyServers { - metric.SetProxyInfo(name, p.Type, p.BindAddr, p.UseEncryption, p.UseGzip, - p.PrivilegeMode, p.CustomDomains, p.ListenPort) + for _, p := range proxyServers { + metric.SetProxyInfo(*p.ProxyServerConf) } return proxyServers, nil } @@ -363,8 +362,7 @@ func CreateProxy(s *ProxyServer) error { } } ProxyServers[s.Name] = s - metric.SetProxyInfo(s.Name, s.Type, s.BindAddr, s.UseEncryption, s.UseGzip, - s.PrivilegeMode, s.CustomDomains, s.ListenPort) + metric.SetProxyInfo(*s.ProxyServerConf) s.Init() return nil } diff --git a/src/models/server/server.go b/src/models/server/server.go index 993bd816..5a1c0902 100644 --- a/src/models/server/server.go +++ b/src/models/server/server.go @@ -33,14 +33,9 @@ type Listener interface { } type ProxyServer struct { - config.BaseConf - BindAddr string - ListenPort int64 - CustomDomains []string - Locations []string + *config.ProxyServerConf - Status int64 - CtlConn *conn.Conn // control connection with frpc + CtlConn *conn.Conn `json:"-"` // control connection with frpc listeners []Listener // accept new connection from remote users ctlMsgChan chan int64 // every time accept a new user conn, put "1" to the channel workConnChan chan *conn.Conn // get new work conns from control goroutine @@ -49,8 +44,9 @@ type ProxyServer struct { } func NewProxyServer() (p *ProxyServer) { + psc := &config.ProxyServerConf{CustomDomains: make([]string, 0)} p = &ProxyServer{ - CustomDomains: make([]string, 0), + ProxyServerConf: psc, } return p } diff --git a/src/utils/vhost/vhost.go b/src/utils/vhost/vhost.go index ff253f16..73b49cde 100644 --- a/src/utils/vhost/vhost.go +++ b/src/utils/vhost/vhost.go @@ -19,7 +19,7 @@ import ( "fmt" "io" "net" - "strings" + //"strings" "sync" "time" @@ -86,7 +86,7 @@ func (v *VhostMuxer) ListenByRouter(name string, domains []string, locations []s accept: make(chan *conn.Conn), } v.registryRouter.add(name, domain, locations, l) - ls = append(ls) + ls = append(ls, l) } return ls @@ -129,17 +129,19 @@ func (v *VhostMuxer) handle(c *conn.Conn) { return } - name = strings.ToLower(name) + //name = strings.ToLower(name) l, ok := v.getListener(name) if !ok { return } if err = sConn.SetDeadline(time.Time{}); err != nil { + log.Error("set dead line err: %v", err) return } c.SetTcpConn(sConn) + log.Debug("handle request: %s", c.GetRemoteAddr()) l.accept <- c } @@ -153,10 +155,12 @@ type Listener struct { } func (l *Listener) Accept() (*conn.Conn, error) { + log.Debug("[%s][%s] now to accept ...", l.name, l.domain) conn, ok := <-l.accept if !ok { return nil, fmt.Errorf("Listener closed") } + log.Debug("[%s][%s] accept something ...", l.name, l.domain) // if rewriteFunc is exist and rewriteHost is set // rewrite http requests with a modified host header