mirror of https://github.com/fatedier/frp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
1.9 KiB
115 lines
1.9 KiB
8 years ago
|
package vhost
|
||
|
|
||
|
import (
|
||
|
"sort"
|
||
|
"strings"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type VhostRouters struct {
|
||
|
RouterByDomain map[string][]*VhostRouter
|
||
|
mutex sync.RWMutex
|
||
|
}
|
||
|
|
||
|
type VhostRouter struct {
|
||
|
domain string
|
||
|
location string
|
||
|
listener *Listener
|
||
|
}
|
||
|
|
||
|
func NewVhostRouters() *VhostRouters {
|
||
|
return &VhostRouters{
|
||
|
RouterByDomain: make(map[string][]*VhostRouter),
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func (r *VhostRouters) Add(domain, location string, l *Listener) {
|
||
8 years ago
|
r.mutex.Lock()
|
||
|
defer r.mutex.Unlock()
|
||
|
|
||
|
vrs, found := r.RouterByDomain[domain]
|
||
|
if !found {
|
||
8 years ago
|
vrs = make([]*VhostRouter, 0, 1)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
vr := &VhostRouter{
|
||
|
domain: domain,
|
||
|
location: location,
|
||
|
listener: l,
|
||
8 years ago
|
}
|
||
8 years ago
|
vrs = append(vrs, vr)
|
||
8 years ago
|
|
||
8 years ago
|
sort.Sort(sort.Reverse(ByLocation(vrs)))
|
||
8 years ago
|
r.RouterByDomain[domain] = vrs
|
||
|
}
|
||
|
|
||
8 years ago
|
func (r *VhostRouters) Del(domain, location string) {
|
||
8 years ago
|
r.mutex.Lock()
|
||
|
defer r.mutex.Unlock()
|
||
|
|
||
8 years ago
|
vrs, found := r.RouterByDomain[domain]
|
||
8 years ago
|
if !found {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
for i, vr := range vrs {
|
||
8 years ago
|
if vr.location == location {
|
||
8 years ago
|
if len(vrs) > i+1 {
|
||
8 years ago
|
r.RouterByDomain[domain] = append(vrs[:i], vrs[i+1:]...)
|
||
8 years ago
|
} else {
|
||
8 years ago
|
r.RouterByDomain[domain] = vrs[:i]
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func (r *VhostRouters) Get(host, path string) (vr *VhostRouter, exist bool) {
|
||
8 years ago
|
r.mutex.RLock()
|
||
|
defer r.mutex.RUnlock()
|
||
|
|
||
8 years ago
|
vrs, found := r.RouterByDomain[host]
|
||
8 years ago
|
if !found {
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
// can't support load balance, will to do
|
||
8 years ago
|
for _, vr = range vrs {
|
||
8 years ago
|
if strings.HasPrefix(path, vr.location) {
|
||
8 years ago
|
return vr, true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
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
|
||
8 years ago
|
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
|
||
|
}
|