From a06dc1eafaa571949e6f3d5fca1a00f0fc8960df Mon Sep 17 00:00:00 2001 From: XueBing Date: Sun, 18 Dec 2016 00:02:21 +0800 Subject: [PATCH 1/5] support the "custom_location" configure --- cross_compiles_package.sh | 8 ++++ src/cmd/frps/main.go | 7 +++- src/models/server/config.go | 12 ++++++ src/models/server/server.go | 9 +++++ src/utils/vhost/http.go | 9 ++++- src/utils/vhost/router.go | 79 +++++++++++++++++++++++++++++++++++++ src/utils/vhost/vhost.go | 11 ++++++ 7 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 src/utils/vhost/router.go diff --git a/cross_compiles_package.sh b/cross_compiles_package.sh index 5cbe6a67..3d1aa3d0 100755 --- a/cross_compiles_package.sh +++ b/cross_compiles_package.sh @@ -17,6 +17,14 @@ mkdir ./packages os_all='linux windows darwin' arch_all='386 amd64 arm' +if [ $1 ];then + os_all=$1 +fi + +if [ $2 ];then + arch_all=$2 +fi + for os in $os_all; do for arch in $arch_all; do frp_dir_name="frp_${frp_version}_${os}_${arch}" diff --git a/src/cmd/frps/main.go b/src/cmd/frps/main.go index 8e94d8d5..af6a7161 100644 --- a/src/cmd/frps/main.go +++ b/src/cmd/frps/main.go @@ -151,7 +151,12 @@ func main() { log.Error("Create vhost http listener error, %v", err) os.Exit(1) } - server.VhostHttpMuxer, err = vhost.NewHttpMuxer(vhostListener, 30*time.Second) + + if server.VhostHttpRouters == nil { + server.VhostHttpMuxer, err = vhost.NewHttpMuxer(vhostListener, 30*time.Second) + } else { + server.VhostHttpMuxer, err = vhost.NewHttpMuxerWithRouter(vhostListener, 30*time.Second, server.VhostHttpRouters) + } if err != nil { log.Error("Create vhost httpMuxer error, %v", err) } diff --git a/src/models/server/config.go b/src/models/server/config.go index e3296d05..7b0c8da1 100644 --- a/src/models/server/config.go +++ b/src/models/server/config.go @@ -54,6 +54,8 @@ var ( VhostHttpsMuxer *vhost.HttpsMuxer ProxyServers map[string]*ProxyServer = make(map[string]*ProxyServer) // all proxy servers info and resources ProxyServersMutex sync.RWMutex + + VhostHttpRouters *vhost.VhostRouters ) func LoadConf(confFile string) (err error) { @@ -272,6 +274,16 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e } else { return proxyServers, fmt.Errorf("Parse conf error: proxy [%s] custom_domains must be set when type equals http", proxyServer.Name) } + + //location + locStr, loc_ok := section["custom_location"] + if loc_ok { + if VhostHttpRouters == nil { + VhostHttpRouters = vhost.NewVhostRouters() + } + proxyServer.Locations = strings.Split(locStr, ",") + VhostHttpRouters.Add(proxyServer.Name, proxyServer.CustomDomains, proxyServer.Locations) + } } else if proxyServer.Type == "https" { // for https proxyServer.ListenPort = VhostHttpsPort diff --git a/src/models/server/server.go b/src/models/server/server.go index d1502116..a5e3aa70 100644 --- a/src/models/server/server.go +++ b/src/models/server/server.go @@ -37,6 +37,7 @@ type ProxyServer struct { BindAddr string ListenPort int64 CustomDomains []string + Locations []string Status int64 CtlConn *conn.Conn // control connection with frpc @@ -99,6 +100,14 @@ func (p *ProxyServer) Compare(p2 *ProxyServer) bool { return false } } + if len(p.Locations) != len(p2.Locations) { + return false + } + for i, _ := range p.Locations { + if p.Locations[i] != p2.Locations[i] { + return false + } + } return true } diff --git a/src/utils/vhost/http.go b/src/utils/vhost/http.go index 0d1d8e07..2ac5da1a 100644 --- a/src/utils/vhost/http.go +++ b/src/utils/vhost/http.go @@ -41,7 +41,8 @@ func GetHttpHostname(c *conn.Conn) (_ net.Conn, routerName string, err error) { return sc, "", err } tmpArr := strings.Split(request.Host, ":") - routerName = tmpArr[0] + //routerName = tmpArr[0] + routerName = tmpArr[0] + ":" + request.URL.Path request.Body.Close() return sc, routerName, nil } @@ -51,6 +52,12 @@ func NewHttpMuxer(listener *conn.Listener, timeout time.Duration) (*HttpMuxer, e return &HttpMuxer{mux}, err } +func NewHttpMuxerWithRouter(listener *conn.Listener, timeout time.Duration, r *VhostRouters) (*HttpMuxer, error) { + mux, err := NewVhostMuxer(listener, GetHttpHostname, HttpHostNameRewrite, timeout) + mux.routers = r + return &HttpMuxer{mux}, err +} + func HttpHostNameRewrite(c *conn.Conn, rewriteHost string) (_ net.Conn, err error) { sc, rd := newShareConn(c.TcpConn) var buff []byte diff --git a/src/utils/vhost/router.go b/src/utils/vhost/router.go new file mode 100644 index 00000000..a81d6eb8 --- /dev/null +++ b/src/utils/vhost/router.go @@ -0,0 +1,79 @@ +package vhost + +import ( + "sort" + "strings" + "sync" +) + +type VhostRouters struct { + RouterByDomain map[string][]*VhostRouter + mutex sync.RWMutex +} + +type VhostRouter struct { + Name string + Domain string + Location string +} + +func NewVhostRouters() *VhostRouters { + return &VhostRouters{ + RouterByDomain: make(map[string][]*VhostRouter), + } +} + +//sort by location +type ByLocation []*VhostRouter + +func (a ByLocation) Len() int { + return len(a) +} +func (a ByLocation) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} +func (a ByLocation) Less(i, j int) bool { + return strings.Compare(a[i].Location, a[j].Location) < 0 +} + +func (r *VhostRouters) Add(name string, domains, locations []string) { + r.mutex.Lock() + defer r.mutex.Unlock() + + for _, domain := range domains { + vrs, found := r.RouterByDomain[name] + if !found { + vrs = make([]*VhostRouter, 0) + } + + for _, loc := range locations { + vr := &VhostRouter{ + Name: name, + Domain: domain, + Location: loc, + } + vrs = append(vrs, vr) + } + + sort_vrs := sort.Reverse(ByLocation(vrs)) + r.RouterByDomain[name] = sort_vrs.(ByLocation) + } +} + +func (r *VhostRouters) getName(domain, url string) (name string, exist bool) { + r.mutex.RLock() + defer r.mutex.RUnlock() + + vrs, exist := r.RouterByDomain[domain] + if !exist { + return + } + + for _, vr := range vrs { + if strings.HasPrefix(url, vr.Location+"/") { + return vr.Name, true + } + } + + return +} diff --git a/src/utils/vhost/vhost.go b/src/utils/vhost/vhost.go index 93279b3b..f7849ac1 100644 --- a/src/utils/vhost/vhost.go +++ b/src/utils/vhost/vhost.go @@ -35,6 +35,7 @@ type VhostMuxer struct { vhostFunc muxFunc rewriteFunc hostRewriteFunc registryMap map[string]*Listener + routers *VhostRouters mutex sync.RWMutex } @@ -71,6 +72,16 @@ func (v *VhostMuxer) Listen(name string, rewriteHost string) (l *Listener, err e func (v *VhostMuxer) getListener(name string) (l *Listener, exist bool) { v.mutex.RLock() defer v.mutex.RUnlock() + + if v.routers != nil { + tmparray := strings.SplitN(name, ":", 2) + if len(tmparray) == 2 { + newname, found := v.routers.getName(tmparray[0], tmparray[1]) + if found { + name = newname + } + } + } l, exist = v.registryMap[name] return l, exist } From 745efb4991c7d6b4071847e82cfb3070cdc73bc0 Mon Sep 17 00:00:00 2001 From: XueBing Date: Sun, 18 Dec 2016 12:55:16 +0800 Subject: [PATCH 2/5] router by name --- cross_compiles_package.sh | 8 ---- src/cmd/frps/main.go | 6 +-- src/models/server/config.go | 6 --- src/models/server/server.go | 12 ++--- src/utils/vhost/http.go | 6 --- src/utils/vhost/router.go | 74 ++++++++++++++++++++---------- src/utils/vhost/vhost.go | 89 ++++++++++++++++++++++--------------- 7 files changed, 109 insertions(+), 92 deletions(-) diff --git a/cross_compiles_package.sh b/cross_compiles_package.sh index 3d1aa3d0..5cbe6a67 100755 --- a/cross_compiles_package.sh +++ b/cross_compiles_package.sh @@ -17,14 +17,6 @@ mkdir ./packages os_all='linux windows darwin' arch_all='386 amd64 arm' -if [ $1 ];then - os_all=$1 -fi - -if [ $2 ];then - arch_all=$2 -fi - for os in $os_all; do for arch in $arch_all; do frp_dir_name="frp_${frp_version}_${os}_${arch}" diff --git a/src/cmd/frps/main.go b/src/cmd/frps/main.go index af6a7161..a68947a3 100644 --- a/src/cmd/frps/main.go +++ b/src/cmd/frps/main.go @@ -152,11 +152,7 @@ func main() { os.Exit(1) } - if server.VhostHttpRouters == nil { - server.VhostHttpMuxer, err = vhost.NewHttpMuxer(vhostListener, 30*time.Second) - } else { - server.VhostHttpMuxer, err = vhost.NewHttpMuxerWithRouter(vhostListener, 30*time.Second, server.VhostHttpRouters) - } + server.VhostHttpMuxer, err = vhost.NewHttpMuxer(vhostListener, 30*time.Second) if err != nil { log.Error("Create vhost httpMuxer error, %v", err) } diff --git a/src/models/server/config.go b/src/models/server/config.go index 7b0c8da1..91d4009e 100644 --- a/src/models/server/config.go +++ b/src/models/server/config.go @@ -54,8 +54,6 @@ var ( VhostHttpsMuxer *vhost.HttpsMuxer ProxyServers map[string]*ProxyServer = make(map[string]*ProxyServer) // all proxy servers info and resources ProxyServersMutex sync.RWMutex - - VhostHttpRouters *vhost.VhostRouters ) func LoadConf(confFile string) (err error) { @@ -278,11 +276,7 @@ func loadProxyConf(confFile string) (proxyServers map[string]*ProxyServer, err e //location locStr, loc_ok := section["custom_location"] if loc_ok { - if VhostHttpRouters == nil { - VhostHttpRouters = vhost.NewVhostRouters() - } proxyServer.Locations = strings.Split(locStr, ",") - VhostHttpRouters.Add(proxyServer.Name, proxyServer.CustomDomains, proxyServer.Locations) } } else if proxyServer.Type == "https" { // for https diff --git a/src/models/server/server.go b/src/models/server/server.go index a5e3aa70..993bd816 100644 --- a/src/models/server/server.go +++ b/src/models/server/server.go @@ -130,19 +130,13 @@ func (p *ProxyServer) Start(c *conn.Conn) (err error) { } p.listeners = append(p.listeners, l) } else if p.Type == "http" { - for _, domain := range p.CustomDomains { - l, err := VhostHttpMuxer.Listen(domain, p.HostHeaderRewrite) - if err != nil { - return err - } + ls := VhostHttpMuxer.ListenByRouter(p.Name, p.CustomDomains, p.Locations, p.HostHeaderRewrite) + for _, l := range ls { p.listeners = append(p.listeners, l) } } else if p.Type == "https" { for _, domain := range p.CustomDomains { - l, err := VhostHttpsMuxer.Listen(domain, p.HostHeaderRewrite) - if err != nil { - return err - } + l := VhostHttpsMuxer.Listen(p.Name, domain, p.HostHeaderRewrite) p.listeners = append(p.listeners, l) } } diff --git a/src/utils/vhost/http.go b/src/utils/vhost/http.go index 2ac5da1a..2a282f06 100644 --- a/src/utils/vhost/http.go +++ b/src/utils/vhost/http.go @@ -52,12 +52,6 @@ func NewHttpMuxer(listener *conn.Listener, timeout time.Duration) (*HttpMuxer, e return &HttpMuxer{mux}, err } -func NewHttpMuxerWithRouter(listener *conn.Listener, timeout time.Duration, r *VhostRouters) (*HttpMuxer, error) { - mux, err := NewVhostMuxer(listener, GetHttpHostname, HttpHostNameRewrite, timeout) - mux.routers = r - return &HttpMuxer{mux}, err -} - func HttpHostNameRewrite(c *conn.Conn, rewriteHost string) (_ net.Conn, err error) { sc, rd := newShareConn(c.TcpConn) var buff []byte diff --git a/src/utils/vhost/router.go b/src/utils/vhost/router.go index a81d6eb8..d4735df1 100644 --- a/src/utils/vhost/router.go +++ b/src/utils/vhost/router.go @@ -12,9 +12,10 @@ type VhostRouters struct { } type VhostRouter struct { - Name string - Domain string - Location string + name string + domain string + location string + listener *Listener } func NewVhostRouters() *VhostRouters { @@ -33,45 +34,72 @@ func (a ByLocation) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByLocation) Less(i, j int) bool { - return strings.Compare(a[i].Location, a[j].Location) < 0 + return strings.Compare(a[i].location, a[j].location) < 0 } -func (r *VhostRouters) Add(name string, domains, locations []string) { +func (r *VhostRouters) add(name, domain string, locations []string, l *Listener) { r.mutex.Lock() defer r.mutex.Unlock() - for _, domain := range domains { - vrs, found := r.RouterByDomain[name] - if !found { - vrs = make([]*VhostRouter, 0) - } + vrs, found := r.RouterByDomain[domain] + if !found { + vrs = make([]*VhostRouter, 0) + } - for _, loc := range locations { - vr := &VhostRouter{ - Name: name, - Domain: domain, - Location: loc, + for _, loc := range locations { + vr := &VhostRouter{ + name: name, + domain: domain, + location: loc, + listener: l, + } + vrs = append(vrs, vr) + } + + sort.Reverse(ByLocation(vrs)) + r.RouterByDomain[domain] = vrs +} + +func (r *VhostRouters) del(l *Listener) { + r.mutex.Lock() + defer r.mutex.Unlock() + + vrs, found := r.RouterByDomain[l.domain] + if !found { + return + } + + for i, vr := range vrs { + if vr.listener == l { + if len(vrs) > i+1 { + r.RouterByDomain[l.domain] = append(vrs[:i], vrs[i+1:]...) + } else { + r.RouterByDomain[l.domain] = vrs[:i] } - vrs = append(vrs, vr) } - - sort_vrs := sort.Reverse(ByLocation(vrs)) - r.RouterByDomain[name] = sort_vrs.(ByLocation) } } -func (r *VhostRouters) getName(domain, url string) (name string, exist bool) { +func (r *VhostRouters) get(rname string) (vr *VhostRouter, exist bool) { r.mutex.RLock() defer r.mutex.RUnlock() + var domain, url string + tmparray := strings.SplitN(rname, ":", 2) + if len(tmparray) == 2 { + domain = tmparray[0] + url = tmparray[1] + } + vrs, exist := r.RouterByDomain[domain] if !exist { return } - for _, vr := range vrs { - if strings.HasPrefix(url, vr.Location+"/") { - return vr.Name, true + //can't support load balance,will to do + for _, vr = range vrs { + if strings.HasPrefix(url, vr.location) { + return vr, true } } diff --git a/src/utils/vhost/vhost.go b/src/utils/vhost/vhost.go index f7849ac1..ff253f16 100644 --- a/src/utils/vhost/vhost.go +++ b/src/utils/vhost/vhost.go @@ -24,72 +24,89 @@ import ( "time" "github.com/fatedier/frp/src/utils/conn" + "github.com/fatedier/frp/src/utils/log" ) type muxFunc func(*conn.Conn) (net.Conn, string, error) type hostRewriteFunc func(*conn.Conn, string) (net.Conn, error) type VhostMuxer struct { - listener *conn.Listener - timeout time.Duration - vhostFunc muxFunc - rewriteFunc hostRewriteFunc - registryMap map[string]*Listener - routers *VhostRouters - mutex sync.RWMutex + listener *conn.Listener + timeout time.Duration + vhostFunc muxFunc + rewriteFunc hostRewriteFunc + registryRouter *VhostRouters + mutex sync.RWMutex } func NewVhostMuxer(listener *conn.Listener, vhostFunc muxFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *VhostMuxer, err error) { mux = &VhostMuxer{ - listener: listener, - timeout: timeout, - vhostFunc: vhostFunc, - rewriteFunc: rewriteFunc, - registryMap: make(map[string]*Listener), + listener: listener, + timeout: timeout, + vhostFunc: vhostFunc, + rewriteFunc: rewriteFunc, + registryRouter: NewVhostRouters(), } go mux.run() return mux, nil } // 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(name string, rewriteHost string) (l *Listener, err error) { +func (v *VhostMuxer) Listen(name, domain string, rewriteHost string) (l *Listener) { v.mutex.Lock() defer v.mutex.Unlock() - if _, exist := v.registryMap[name]; exist { - return nil, fmt.Errorf("domain name %s is already bound", name) - } + + locations := []string{""} l = &Listener{ name: name, + domain: domain, + locations: locations, rewriteHost: rewriteHost, mux: v, accept: make(chan *conn.Conn), } - v.registryMap[name] = l - return l, nil + + v.registryRouter.add(name, domain, locations, l) + return l } -func (v *VhostMuxer) getListener(name string) (l *Listener, exist bool) { +func (v *VhostMuxer) ListenByRouter(name string, domains []string, locations []string, rewriteHost string) (ls []*Listener) { + v.mutex.Lock() + defer v.mutex.Unlock() + + ls = make([]*Listener, 0) + for _, domain := range domains { + l := &Listener{ + name: name, + domain: domain, + locations: locations, + rewriteHost: rewriteHost, + mux: v, + accept: make(chan *conn.Conn), + } + v.registryRouter.add(name, domain, locations, l) + ls = append(ls) + } + + return ls +} + +func (v *VhostMuxer) getListener(rname string) (l *Listener, exist bool) { v.mutex.RLock() defer v.mutex.RUnlock() - if v.routers != nil { - tmparray := strings.SplitN(name, ":", 2) - if len(tmparray) == 2 { - newname, found := v.routers.getName(tmparray[0], tmparray[1]) - if found { - name = newname - } - } + var frcname string + vr, found := v.registryRouter.get(rname) + if found { + frcname = vr.name + } else { + log.Warn("can't found the router for %s", rname) + return } - l, exist = v.registryMap[name] - return l, exist -} -func (v *VhostMuxer) unRegister(name string) { - v.mutex.Lock() - defer v.mutex.Unlock() - delete(v.registryMap, name) + log.Debug("get frcname %s for %s", frcname, rname) + return vr.listener, true } func (v *VhostMuxer) run() { @@ -128,6 +145,8 @@ func (v *VhostMuxer) handle(c *conn.Conn) { type Listener struct { name string + domain string + locations []string rewriteHost string mux *VhostMuxer // for closing VhostMuxer accept chan *conn.Conn @@ -152,7 +171,7 @@ func (l *Listener) Accept() (*conn.Conn, error) { } func (l *Listener) Close() error { - l.mux.unRegister(l.name) + l.mux.registryRouter.del(l) close(l.accept) return nil } From 1064f4881174aa09d6a9121b10083611df2a088c Mon Sep 17 00:00:00 2001 From: XueBing Date: Sun, 18 Dec 2016 16:34:38 +0800 Subject: [PATCH 3/5] 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 From 325bc86966828e5039c4c7309fc0745d1dc51dbd Mon Sep 17 00:00:00 2001 From: XueBing Date: Sun, 18 Dec 2016 16:50:45 +0800 Subject: [PATCH 4/5] modify the dashboard index.html --- src/assets/static/index.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/assets/static/index.html b/src/assets/static/index.html index 6ac0650d..f95cbb46 100644 --- a/src/assets/static/index.html +++ b/src/assets/static/index.html @@ -63,7 +63,8 @@ listen_port: "<<< .ListenPort >>>", current_conns: <<< .CurrentConns >>> , domains: [ <<< range.CustomDomains >>> "<<< . >>>", <<< end >>> ], - stat: "<<< .Status >>>", + locations: [ <<< range.Locations >>> "<<< . >>>", <<< end >>> ], + stat: "<<< .StatusDesc >>>", use_encryption: "<<< .UseEncryption >>>", use_gzip: "<<< .UseGzip >>>", privilege_mode: "<<< .PrivilegeMode >>>", From 228d1af8d41cdada3a2bded6f4931dbeeed83d4b Mon Sep 17 00:00:00 2001 From: XueBing Date: Sun, 18 Dec 2016 18:41:49 +0800 Subject: [PATCH 5/5] modify the index.html --- src/assets/static/index.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/assets/static/index.html b/src/assets/static/index.html index f95cbb46..dc7f3626 100644 --- a/src/assets/static/index.html +++ b/src/assets/static/index.html @@ -223,6 +223,10 @@ newrow += "Domains" + alldata[index].domains[domainindex] + ""; } + for (var locindex in alldata[index].locations) { + newrow += "Locations" + + alldata[index].locations[locindex] + ""; + } newrow += "Ip" + alldata[index].bind_addr + ""; newrow += "Status" + alldata[index].stat + ""; newrow += "Encryption" + alldata[index].use_encryption + "";