| 9 | "github.com/fatedier/frp/pkg/util/vhost" |
| 10 | ) |
| 11 |
| 12 | // HTTPGroupController manages HTTP groups that use round-robin |
| 13 | // callback routing (fundamentally different from listener-based groups). |
| 14 | type HTTPGroupController struct { |
| 15 | groupRegistry[*HTTPGroup] |
| 16 | vhostRouter *vhost.Routers |
| 17 | } |
| 18 |
| 19 | func NewHTTPGroupController(vhostRouter *vhost.Routers) *HTTPGroupController { |
| 20 | return &HTTPGroupController{ |
| 21 | groupRegistry: newGroupRegistry[*HTTPGroup](), |
| 22 | vhostRouter: vhostRouter, |
| 23 | } |
| 24 | } |
| 25 |
| 26 | func (ctl *HTTPGroupController) Register( |
| 27 | proxyName, group, groupKey string, |
| 28 | routeConfig vhost.RouteConfig, |
| 29 | ) error { |
| 30 | for { |
| 31 | g := ctl.getOrCreate(group, func() *HTTPGroup { |
| 32 | return NewHTTPGroup(ctl) |
| 33 | }) |
| 34 | err := g.Register(proxyName, group, groupKey, routeConfig) |
| 35 | if err == errGroupStale { |
| 36 | continue |
| 37 | } |
| 38 | return err |
| 39 | } |
| 40 | } |
| 41 |
| 42 | func (ctl *HTTPGroupController) UnRegister(proxyName, group string, _ vhost.RouteConfig) { |
| 43 | g, ok := ctl.get(group) |
| 44 | if !ok { |
| 45 | return |
| 46 | } |
| 47 | g.UnRegister(proxyName) |
| 48 | } |
| 49 |
| 50 | type HTTPGroup struct { |
| 51 | group string |
| 52 | groupKey string |
| 53 | domain string |
| 54 | location string |
| 55 | routeByHTTPUser string |
| 56 |
| 57 | // CreateConnFuncs indexed by proxy name |
| 58 | createFuncs map[string]vhost.CreateConnFunc |
| 59 | pxyNames []string |
| 60 | index atomic.Uint64 |
| 61 | ctl *HTTPGroupController |
| 62 | mu sync.RWMutex |
| 63 | } |
| 64 |
| 65 | func NewHTTPGroup(ctl *HTTPGroupController) *HTTPGroup { |
| 66 | return &HTTPGroup{ |
| 67 | createFuncs: make(map[string]vhost.CreateConnFunc), |
| 68 | pxyNames: make([]string, 0), |
| 69 | ctl: ctl, |
| 70 | } |
| 71 | } |
| 72 |
| 73 | func (g *HTTPGroup) Register( |
| 74 | proxyName, group, groupKey string, |
| 75 | routeConfig vhost.RouteConfig, |
| 76 | ) (err error) { |
| 77 | g.mu.Lock() |
| 78 | defer g.mu.Unlock() |
| 79 | if !g.ctl.isCurrent(group, func(cur *HTTPGroup) bool { return cur == g }) { |
| 80 | return errGroupStale |
| 81 | } |
| 82 | if len(g.createFuncs) == 0 { |
| 83 | // the first proxy in this group |
| 84 | tmp := routeConfig // copy object |
| 85 | tmp.CreateConnFn = g.createConn |
| 86 | tmp.ChooseEndpointFn = g.chooseEndpoint |
| 87 | tmp.CreateConnByEndpointFn = g.createConnByEndpoint |
| 88 | err = g.ctl.vhostRouter.Add(routeConfig.Domain, routeConfig.Location, routeConfig.RouteByHTTPUser, &tmp) |
| 89 | if err != nil { |
| 90 | return |
| 91 | } |
| 92 |
| 93 | g.group = group |
| 94 | g.groupKey = groupKey |
| 95 | g.domain = routeConfig.Domain |
| 96 | g.location = routeConfig.Location |
| 97 | g.routeByHTTPUser = routeConfig.RouteByHTTPUser |
| 98 | } else { |
| 99 | if g.group != group || g.domain != routeConfig.Domain || |
| 100 | g.location != routeConfig.Location || g.routeByHTTPUser != routeConfig.RouteByHTTPUser { |
| 101 | err = ErrGroupParamsInvalid |
| 102 | return |
| 103 | } |
| 104 | if g.groupKey != groupKey { |
| 105 | err = ErrGroupAuthFailed |
| 106 | return |
| 107 | } |
| 108 | } |
| 109 | if _, ok := g.createFuncs[proxyName]; ok { |
| 110 | err = ErrProxyRepeated |
| 111 | return |
| 112 | } |
| 113 | g.createFuncs[proxyName] = routeConfig.CreateConnFn |
| 114 | g.pxyNames = append(g.pxyNames, proxyName) |
| 115 | return nil |
| 116 | } |
| 117 |
| 118 | func (g *HTTPGroup) UnRegister(proxyName string) { |
| 119 | g.mu.Lock() |
| 120 | defer g.mu.Unlock() |
| 121 | delete(g.createFuncs, proxyName) |
| 122 | for i, name := range g.pxyNames { |
| 123 | if name == proxyName { |
| 124 | g.pxyNames = append(g.pxyNames[:i], g.pxyNames[i+1:]...) |
| 125 | break |
| 126 | } |
| 127 | } |
| 128 |
| 129 | if len(g.createFuncs) == 0 { |
| 130 | g.ctl.vhostRouter.Del(g.domain, g.location, g.routeByHTTPUser) |
| 131 | g.ctl.removeIf(g.group, func(cur *HTTPGroup) bool { |
| 132 | return cur == g |
| 133 | }) |
| 134 | } |
| 135 | } |
| 136 |
| 137 | func (g *HTTPGroup) createConn(remoteAddr string) (net.Conn, error) { |
| 138 | var f vhost.CreateConnFunc |
| 139 | newIndex := g.index.Add(1) |
| 140 |
| 141 | g.mu.RLock() |
| 142 | group := g.group |
| 143 | domain := g.domain |
| 144 | location := g.location |
| 145 | routeByHTTPUser := g.routeByHTTPUser |
| 146 | if len(g.pxyNames) > 0 { |
| 147 | name := g.pxyNames[newIndex%uint64(len(g.pxyNames))] |
| 148 | f = g.createFuncs[name] |
| 149 | } |
| 150 | g.mu.RUnlock() |
| 151 |
| 152 | if f == nil { |
| 153 | return nil, fmt.Errorf("no CreateConnFunc for http group [%s], domain [%s], location [%s], routeByHTTPUser [%s]", |
| 154 | group, domain, location, routeByHTTPUser) |
| 155 | } |
| 156 |
| 157 | return f(remoteAddr) |
| 158 | } |
| 159 |
| 160 | func (g *HTTPGroup) chooseEndpoint() (string, error) { |
| 161 | newIndex := g.index.Add(1) |
| 162 | name := "" |
| 163 |
| 164 | g.mu.RLock() |
| 165 | group := g.group |
| 166 | domain := g.domain |
| 167 | location := g.location |
| 168 | routeByHTTPUser := g.routeByHTTPUser |
| 169 | if len(g.pxyNames) > 0 { |
| 170 | name = g.pxyNames[newIndex%uint64(len(g.pxyNames))] |
| 171 | } |
| 172 | g.mu.RUnlock() |
| 173 |
| 174 | if name == "" { |
| 175 | return "", fmt.Errorf("no healthy endpoint for http group [%s], domain [%s], location [%s], routeByHTTPUser [%s]", |
| 176 | group, domain, location, routeByHTTPUser) |
| 177 | } |
| 178 | return name, nil |
| 179 | } |
| 180 |
| 181 | func (g *HTTPGroup) createConnByEndpoint(endpoint, remoteAddr string) (net.Conn, error) { |
| 182 | var f vhost.CreateConnFunc |
| 183 | g.mu.RLock() |
| 184 | f = g.createFuncs[endpoint] |
| 185 | g.mu.RUnlock() |
| 186 |
| 187 | if f == nil { |
| 188 | return nil, fmt.Errorf("no CreateConnFunc for endpoint [%s] in group [%s]", endpoint, g.group) |
| 189 | } |
| 190 | return f(remoteAddr) |
| 191 | } |