From 04a4591caa5a5ed0329b2e3d6b9593269091603c Mon Sep 17 00:00:00 2001 From: fatedier Date: Tue, 27 Dec 2016 01:01:39 +0800 Subject: [PATCH] vhost: check host and location for url router --- src/models/server/server.go | 3 ++- src/utils/vhost/router.go | 22 ++++++++++++++++++++-- src/utils/vhost/vhost.go | 5 +++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/models/server/server.go b/src/models/server/server.go index 374707cc..941f48f2 100644 --- a/src/models/server/server.go +++ b/src/models/server/server.go @@ -271,6 +271,8 @@ func (p *ProxyServer) Start(c *conn.Conn) (err error) { func (p *ProxyServer) Close() { p.Lock() + defer p.Unlock() + if p.Status != consts.Closed { p.Status = consts.Closed for _, l := range p.listeners { @@ -298,7 +300,6 @@ func (p *ProxyServer) Close() { if p.PrivilegeMode { DeleteProxy(p.Name) } - p.Unlock() } func (p *ProxyServer) WaitUserConn() (closeFlag bool) { diff --git a/src/utils/vhost/router.go b/src/utils/vhost/router.go index 803d0f83..df0f8329 100644 --- a/src/utils/vhost/router.go +++ b/src/utils/vhost/router.go @@ -72,7 +72,7 @@ func (r *VhostRouters) Get(host, path string) (vr *VhostRouter, exist bool) { return } - //can't support load balance,will to do + // can't support load balance, will to do for _, vr = range vrs { if strings.HasPrefix(path, vr.location) { return vr, true @@ -82,7 +82,25 @@ func (r *VhostRouters) Get(host, path string) (vr *VhostRouter, exist bool) { return } -//sort by location +func (r *VhostRouters) Exist(host, path string) (vr *VhostRouter, exist bool) { + r.mutex.RLock() + defer r.mutex.RUnlock() + + vrs, found := r.RouterByDomain[host] + if !found { + return + } + + for _, vr = range vrs { + if path == vr.location { + return vr, true + } + } + + return +} + +// sort by location type ByLocation []*VhostRouter func (a ByLocation) Len() int { diff --git a/src/utils/vhost/vhost.go b/src/utils/vhost/vhost.go index e118afbc..25b128a4 100644 --- a/src/utils/vhost/vhost.go +++ b/src/utils/vhost/vhost.go @@ -56,6 +56,11 @@ func (v *VhostMuxer) Listen(name, location, rewriteHost, userName, passWord stri v.mutex.Lock() defer v.mutex.Unlock() + _, ok := v.registryRouter.Exist(name, location) + if ok { + return nil, fmt.Errorf("hostname [%s] location [%s] is already registered", name, location) + } + l = &Listener{ name: name, rewriteHost: rewriteHost,