2019-07-31 00:41:58 +08:00
|
|
|
package group
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-10-12 20:13:12 +08:00
|
|
|
"net"
|
2019-07-31 00:41:58 +08:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/vhost"
|
2019-07-31 00:41:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type HTTPGroupController struct {
|
2022-05-26 23:57:30 +08:00
|
|
|
// groups by indexKey
|
2019-07-31 00:41:58 +08:00
|
|
|
groups map[string]*HTTPGroup
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
// register createConn for each group to vhostRouter.
|
|
|
|
// createConn will get a connection from one proxy of the group
|
2020-05-24 17:48:37 +08:00
|
|
|
vhostRouter *vhost.Routers
|
2019-07-31 00:41:58 +08:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func NewHTTPGroupController(vhostRouter *vhost.Routers) *HTTPGroupController {
|
2019-07-31 00:41:58 +08:00
|
|
|
return &HTTPGroupController{
|
|
|
|
groups: make(map[string]*HTTPGroup),
|
|
|
|
vhostRouter: vhostRouter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (ctl *HTTPGroupController) Register(
|
|
|
|
proxyName, group, groupKey string,
|
|
|
|
routeConfig vhost.RouteConfig,
|
|
|
|
) (err error) {
|
2019-07-31 00:41:58 +08:00
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
indexKey := group
|
2019-07-31 00:41:58 +08:00
|
|
|
ctl.mu.Lock()
|
|
|
|
g, ok := ctl.groups[indexKey]
|
|
|
|
if !ok {
|
|
|
|
g = NewHTTPGroup(ctl)
|
|
|
|
ctl.groups[indexKey] = g
|
|
|
|
}
|
|
|
|
ctl.mu.Unlock()
|
|
|
|
|
|
|
|
return g.Register(proxyName, group, groupKey, routeConfig)
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (ctl *HTTPGroupController) UnRegister(proxyName, group string, routeConfig vhost.RouteConfig) {
|
|
|
|
indexKey := group
|
2019-07-31 00:41:58 +08:00
|
|
|
ctl.mu.Lock()
|
|
|
|
defer ctl.mu.Unlock()
|
|
|
|
g, ok := ctl.groups[indexKey]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isEmpty := g.UnRegister(proxyName)
|
|
|
|
if isEmpty {
|
|
|
|
delete(ctl.groups, indexKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPGroup struct {
|
2022-05-26 23:57:30 +08:00
|
|
|
group string
|
|
|
|
groupKey string
|
|
|
|
domain string
|
|
|
|
location string
|
|
|
|
routeByHTTPUser string
|
2019-07-31 00:41:58 +08:00
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
// CreateConnFuncs indexed by echo proxy name
|
2019-07-31 00:41:58 +08:00
|
|
|
createFuncs map[string]vhost.CreateConnFunc
|
|
|
|
pxyNames []string
|
|
|
|
index uint64
|
|
|
|
ctl *HTTPGroupController
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTPGroup(ctl *HTTPGroupController) *HTTPGroup {
|
|
|
|
return &HTTPGroup{
|
|
|
|
createFuncs: make(map[string]vhost.CreateConnFunc),
|
|
|
|
pxyNames: make([]string, 0),
|
|
|
|
ctl: ctl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 23:57:30 +08:00
|
|
|
func (g *HTTPGroup) Register(
|
|
|
|
proxyName, group, groupKey string,
|
|
|
|
routeConfig vhost.RouteConfig,
|
|
|
|
) (err error) {
|
2019-07-31 00:41:58 +08:00
|
|
|
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
if len(g.createFuncs) == 0 {
|
|
|
|
// the first proxy in this group
|
|
|
|
tmp := routeConfig // copy object
|
|
|
|
tmp.CreateConnFn = g.createConn
|
2022-05-26 23:57:30 +08:00
|
|
|
err = g.ctl.vhostRouter.Add(routeConfig.Domain, routeConfig.Location, routeConfig.RouteByHTTPUser, &tmp)
|
2019-07-31 00:41:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
g.group = group
|
|
|
|
g.groupKey = groupKey
|
|
|
|
g.domain = routeConfig.Domain
|
|
|
|
g.location = routeConfig.Location
|
2022-05-26 23:57:30 +08:00
|
|
|
g.routeByHTTPUser = routeConfig.RouteByHTTPUser
|
2019-07-31 00:41:58 +08:00
|
|
|
} else {
|
2022-05-26 23:57:30 +08:00
|
|
|
if g.group != group || g.domain != routeConfig.Domain ||
|
|
|
|
g.location != routeConfig.Location || g.routeByHTTPUser != routeConfig.RouteByHTTPUser {
|
2019-07-31 00:41:58 +08:00
|
|
|
err = ErrGroupParamsInvalid
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if g.groupKey != groupKey {
|
|
|
|
err = ErrGroupAuthFailed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := g.createFuncs[proxyName]; ok {
|
|
|
|
err = ErrProxyRepeated
|
|
|
|
return
|
|
|
|
}
|
|
|
|
g.createFuncs[proxyName] = routeConfig.CreateConnFn
|
|
|
|
g.pxyNames = append(g.pxyNames, proxyName)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *HTTPGroup) UnRegister(proxyName string) (isEmpty bool) {
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
delete(g.createFuncs, proxyName)
|
|
|
|
for i, name := range g.pxyNames {
|
|
|
|
if name == proxyName {
|
|
|
|
g.pxyNames = append(g.pxyNames[:i], g.pxyNames[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(g.createFuncs) == 0 {
|
|
|
|
isEmpty = true
|
2022-05-26 23:57:30 +08:00
|
|
|
g.ctl.vhostRouter.Del(g.domain, g.location, g.routeByHTTPUser)
|
2019-07-31 00:41:58 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-12 20:13:12 +08:00
|
|
|
func (g *HTTPGroup) createConn(remoteAddr string) (net.Conn, error) {
|
2019-07-31 00:41:58 +08:00
|
|
|
var f vhost.CreateConnFunc
|
|
|
|
newIndex := atomic.AddUint64(&g.index, 1)
|
|
|
|
|
|
|
|
g.mu.RLock()
|
|
|
|
group := g.group
|
|
|
|
domain := g.domain
|
|
|
|
location := g.location
|
2022-05-26 23:57:30 +08:00
|
|
|
routeByHTTPUser := g.routeByHTTPUser
|
2019-07-31 00:41:58 +08:00
|
|
|
if len(g.pxyNames) > 0 {
|
|
|
|
name := g.pxyNames[int(newIndex)%len(g.pxyNames)]
|
|
|
|
f, _ = g.createFuncs[name]
|
|
|
|
}
|
|
|
|
g.mu.RUnlock()
|
|
|
|
|
|
|
|
if f == nil {
|
2022-05-26 23:57:30 +08:00
|
|
|
return nil, fmt.Errorf("no CreateConnFunc for http group [%s], domain [%s], location [%s], routeByHTTPUser [%s]",
|
|
|
|
group, domain, location, routeByHTTPUser)
|
2019-07-31 00:41:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return f(remoteAddr)
|
|
|
|
}
|