|
@@ -29,35 +29,32 @@ type httpAuthFunc func(*conn.Conn, string, string, string) (bool, error)
|
|
type hostRewriteFunc func(*conn.Conn, string) (net.Conn, error)
|
|
type hostRewriteFunc func(*conn.Conn, string) (net.Conn, error)
|
|
|
|
|
|
type VhostMuxer struct {
|
|
type VhostMuxer struct {
|
|
- listener *conn.Listener
|
|
|
|
- timeout time.Duration
|
|
|
|
- vhostFunc muxFunc
|
|
|
|
- authFunc httpAuthFunc
|
|
|
|
- rewriteFunc hostRewriteFunc
|
|
|
|
- registryMap map[string]*Listener
|
|
|
|
- mutex sync.RWMutex
|
|
|
|
|
|
+ listener *conn.Listener
|
|
|
|
+ timeout time.Duration
|
|
|
|
+ vhostFunc muxFunc
|
|
|
|
+ authFunc httpAuthFunc
|
|
|
|
+ rewriteFunc hostRewriteFunc
|
|
|
|
+ registryRouter *VhostRouters
|
|
|
|
+ mutex sync.RWMutex
|
|
}
|
|
}
|
|
|
|
|
|
func NewVhostMuxer(listener *conn.Listener, vhostFunc muxFunc, authFunc httpAuthFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *VhostMuxer, err error) {
|
|
func NewVhostMuxer(listener *conn.Listener, vhostFunc muxFunc, authFunc httpAuthFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *VhostMuxer, err error) {
|
|
mux = &VhostMuxer{
|
|
mux = &VhostMuxer{
|
|
- listener: listener,
|
|
|
|
- timeout: timeout,
|
|
|
|
- vhostFunc: vhostFunc,
|
|
|
|
- authFunc: authFunc,
|
|
|
|
- rewriteFunc: rewriteFunc,
|
|
|
|
- registryMap: make(map[string]*Listener),
|
|
|
|
|
|
+ listener: listener,
|
|
|
|
+ timeout: timeout,
|
|
|
|
+ vhostFunc: vhostFunc,
|
|
|
|
+ authFunc: authFunc,
|
|
|
|
+ rewriteFunc: rewriteFunc,
|
|
|
|
+ registryRouter: NewVhostRouters(),
|
|
}
|
|
}
|
|
go mux.run()
|
|
go mux.run()
|
|
return mux, nil
|
|
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
|
|
// 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, userName, passWord string) (l *Listener, err error) {
|
|
|
|
|
|
+func (v *VhostMuxer) Listen(name, location, rewriteHost, userName, passWord string) (l *Listener, err error) {
|
|
v.mutex.Lock()
|
|
v.mutex.Lock()
|
|
defer v.mutex.Unlock()
|
|
defer v.mutex.Unlock()
|
|
- if _, exist := v.registryMap[name]; exist {
|
|
|
|
- return nil, fmt.Errorf("domain name %s is already bound", name)
|
|
|
|
- }
|
|
|
|
|
|
|
|
l = &Listener{
|
|
l = &Listener{
|
|
name: name,
|
|
name: name,
|
|
@@ -67,33 +64,34 @@ func (v *VhostMuxer) Listen(name string, rewriteHost, userName, passWord string)
|
|
mux: v,
|
|
mux: v,
|
|
accept: make(chan *conn.Conn),
|
|
accept: make(chan *conn.Conn),
|
|
}
|
|
}
|
|
- v.registryMap[name] = l
|
|
|
|
|
|
+ v.registryRouter.Add(name, location, l)
|
|
return l, nil
|
|
return l, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (v *VhostMuxer) getListener(name string) (l *Listener, exist bool) {
|
|
|
|
|
|
+func (v *VhostMuxer) getListener(name, path string) (l *Listener, exist bool) {
|
|
v.mutex.RLock()
|
|
v.mutex.RLock()
|
|
defer v.mutex.RUnlock()
|
|
defer v.mutex.RUnlock()
|
|
|
|
+
|
|
// first we check the full hostname
|
|
// first we check the full hostname
|
|
// if not exist, then check the wildcard_domain such as *.example.com
|
|
// if not exist, then check the wildcard_domain such as *.example.com
|
|
- l, exist = v.registryMap[name]
|
|
|
|
- if exist {
|
|
|
|
- return l, exist
|
|
|
|
|
|
+ vr, found := v.registryRouter.Get(name, path)
|
|
|
|
+ if found {
|
|
|
|
+ return vr.listener, true
|
|
}
|
|
}
|
|
|
|
+
|
|
domainSplit := strings.Split(name, ".")
|
|
domainSplit := strings.Split(name, ".")
|
|
if len(domainSplit) < 3 {
|
|
if len(domainSplit) < 3 {
|
|
return l, false
|
|
return l, false
|
|
}
|
|
}
|
|
domainSplit[0] = "*"
|
|
domainSplit[0] = "*"
|
|
name = strings.Join(domainSplit, ".")
|
|
name = strings.Join(domainSplit, ".")
|
|
- 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)
|
|
|
|
|
|
+ vr, found = v.registryRouter.Get(name, path)
|
|
|
|
+ if !found {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return vr.listener, true
|
|
}
|
|
}
|
|
|
|
|
|
func (v *VhostMuxer) run() {
|
|
func (v *VhostMuxer) run() {
|
|
@@ -119,8 +117,8 @@ func (v *VhostMuxer) handle(c *conn.Conn) {
|
|
}
|
|
}
|
|
|
|
|
|
name := strings.ToLower(reqInfoMap["Host"])
|
|
name := strings.ToLower(reqInfoMap["Host"])
|
|
- // get listener by hostname
|
|
|
|
- l, ok := v.getListener(name)
|
|
|
|
|
|
+ path := strings.ToLower(reqInfoMap["Path"])
|
|
|
|
+ l, ok := v.getListener(name, path)
|
|
if !ok {
|
|
if !ok {
|
|
c.Close()
|
|
c.Close()
|
|
return
|
|
return
|
|
@@ -177,7 +175,7 @@ func (l *Listener) Accept() (*conn.Conn, error) {
|
|
}
|
|
}
|
|
|
|
|
|
func (l *Listener) Close() error {
|
|
func (l *Listener) Close() error {
|
|
- l.mux.unRegister(l.name)
|
|
|
|
|
|
+ l.mux.registryRouter.Del(l)
|
|
close(l.accept)
|
|
close(l.accept)
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|