mirror of
https://github.com/fatedier/frp.git
synced 2025-08-04 12:43:09 +00:00
type http/tcpmux proxy support route_by_http_user, tcpmux support passthourgh mode (#2932)
This commit is contained in:
@@ -11,33 +11,42 @@ var (
|
||||
ErrRouterConfigConflict = errors.New("router config conflict")
|
||||
)
|
||||
|
||||
type routerByHTTPUser map[string][]*Router
|
||||
|
||||
type Routers struct {
|
||||
RouterByDomain map[string][]*Router
|
||||
mutex sync.RWMutex
|
||||
indexByDomain map[string]routerByHTTPUser
|
||||
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
domain string
|
||||
location string
|
||||
httpUser string
|
||||
|
||||
// store any object here
|
||||
payload interface{}
|
||||
}
|
||||
|
||||
func NewRouters() *Routers {
|
||||
return &Routers{
|
||||
RouterByDomain: make(map[string][]*Router),
|
||||
indexByDomain: make(map[string]routerByHTTPUser),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Routers) Add(domain, location string, payload interface{}) error {
|
||||
func (r *Routers) Add(domain, location, httpUser string, payload interface{}) error {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
if _, exist := r.exist(domain, location); exist {
|
||||
if _, exist := r.exist(domain, location, httpUser); exist {
|
||||
return ErrRouterConfigConflict
|
||||
}
|
||||
|
||||
vrs, found := r.RouterByDomain[domain]
|
||||
routersByHTTPUser, found := r.indexByDomain[domain]
|
||||
if !found {
|
||||
routersByHTTPUser = make(map[string][]*Router)
|
||||
}
|
||||
vrs, found := routersByHTTPUser[httpUser]
|
||||
if !found {
|
||||
vrs = make([]*Router, 0, 1)
|
||||
}
|
||||
@@ -45,20 +54,27 @@ func (r *Routers) Add(domain, location string, payload interface{}) error {
|
||||
vr := &Router{
|
||||
domain: domain,
|
||||
location: location,
|
||||
httpUser: httpUser,
|
||||
payload: payload,
|
||||
}
|
||||
vrs = append(vrs, vr)
|
||||
|
||||
sort.Sort(sort.Reverse(ByLocation(vrs)))
|
||||
r.RouterByDomain[domain] = vrs
|
||||
|
||||
routersByHTTPUser[httpUser] = vrs
|
||||
r.indexByDomain[domain] = routersByHTTPUser
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Routers) Del(domain, location string) {
|
||||
func (r *Routers) Del(domain, location, httpUser string) {
|
||||
r.mutex.Lock()
|
||||
defer r.mutex.Unlock()
|
||||
|
||||
vrs, found := r.RouterByDomain[domain]
|
||||
routersByHTTPUser, found := r.indexByDomain[domain]
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
|
||||
vrs, found := routersByHTTPUser[httpUser]
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
@@ -68,40 +84,46 @@ func (r *Routers) Del(domain, location string) {
|
||||
newVrs = append(newVrs, vr)
|
||||
}
|
||||
}
|
||||
r.RouterByDomain[domain] = newVrs
|
||||
routersByHTTPUser[httpUser] = newVrs
|
||||
}
|
||||
|
||||
func (r *Routers) Get(host, path string) (vr *Router, exist bool) {
|
||||
func (r *Routers) Get(host, path, httpUser string) (vr *Router, exist bool) {
|
||||
r.mutex.RLock()
|
||||
defer r.mutex.RUnlock()
|
||||
|
||||
vrs, found := r.RouterByDomain[host]
|
||||
routersByHTTPUser, found := r.indexByDomain[host]
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
|
||||
vrs, found := routersByHTTPUser[httpUser]
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
|
||||
// can't support load balance, will to do
|
||||
for _, vr = range vrs {
|
||||
if strings.HasPrefix(path, vr.location) {
|
||||
return vr, true
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Routers) exist(host, path string) (vr *Router, exist bool) {
|
||||
vrs, found := r.RouterByDomain[host]
|
||||
func (r *Routers) exist(host, path, httpUser string) (route *Router, exist bool) {
|
||||
routersByHTTPUser, found := r.indexByDomain[host]
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
routers, found := routersByHTTPUser[httpUser]
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
|
||||
for _, vr = range vrs {
|
||||
if path == vr.location {
|
||||
return vr, true
|
||||
for _, route = range routers {
|
||||
if path == route.location {
|
||||
return route, true
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user