| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package http |
| 16 |
| 17 | import ( |
| 18 | "cmp" |
| 19 | "fmt" |
| 20 | "net/http" |
| 21 | "slices" |
| 22 | "strings" |
| 23 | "time" |
| 24 |
| 25 | "github.com/fatedier/frp/pkg/config/types" |
| 26 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 27 | "github.com/fatedier/frp/pkg/metrics/mem" |
| 28 | httppkg "github.com/fatedier/frp/pkg/util/http" |
| 29 | "github.com/fatedier/frp/pkg/util/log" |
| 30 | "github.com/fatedier/frp/pkg/util/version" |
| 31 | "github.com/fatedier/frp/server/http/model" |
| 32 | "github.com/fatedier/frp/server/proxy" |
| 33 | "github.com/fatedier/frp/server/registry" |
| 34 | ) |
| 35 |
| 36 | type Controller struct { |
| 37 | // dependencies |
| 38 | serverCfg *v1.ServerConfig |
| 39 | clientRegistry *registry.ClientRegistry |
| 40 | pxyManager ProxyManager |
| 41 | } |
| 42 |
| 43 | type ProxyManager interface { |
| 44 | GetByName(name string) (proxy.Proxy, bool) |
| 45 | } |
| 46 |
| 47 | func NewController( |
| 48 | serverCfg *v1.ServerConfig, |
| 49 | clientRegistry *registry.ClientRegistry, |
| 50 | pxyManager ProxyManager, |
| 51 | ) *Controller { |
| 52 | return &Controller{ |
| 53 | serverCfg: serverCfg, |
| 54 | clientRegistry: clientRegistry, |
| 55 | pxyManager: pxyManager, |
| 56 | } |
| 57 | } |
| 58 |
| 59 | // /api/serverinfo |
| 60 | func (c *Controller) APIServerInfo(ctx *httppkg.Context) (any, error) { |
| 61 | return c.buildServerInfoResp(), nil |
| 62 | } |
| 63 |
| 64 | func (c *Controller) buildServerInfoResp() model.ServerInfoResp { |
| 65 | serverStats := mem.StatsCollector.GetServer() |
| 66 | return model.ServerInfoResp{ |
| 67 | Version: version.Full(), |
| 68 | BindPort: c.serverCfg.BindPort, |
| 69 | VhostHTTPPort: c.serverCfg.VhostHTTPPort, |
| 70 | VhostHTTPSPort: c.serverCfg.VhostHTTPSPort, |
| 71 | TCPMuxHTTPConnectPort: c.serverCfg.TCPMuxHTTPConnectPort, |
| 72 | KCPBindPort: c.serverCfg.KCPBindPort, |
| 73 | QUICBindPort: c.serverCfg.QUICBindPort, |
| 74 | SubdomainHost: c.serverCfg.SubDomainHost, |
| 75 | MaxPoolCount: c.serverCfg.Transport.MaxPoolCount, |
| 76 | MaxPortsPerClient: c.serverCfg.MaxPortsPerClient, |
| 77 | HeartBeatTimeout: c.serverCfg.Transport.HeartbeatTimeout, |
| 78 | AllowPortsStr: types.PortsRangeSlice(c.serverCfg.AllowPorts).String(), |
| 79 | TLSForce: c.serverCfg.Transport.TLS.Force, |
| 80 |
| 81 | TotalTrafficIn: serverStats.TotalTrafficIn, |
| 82 | TotalTrafficOut: serverStats.TotalTrafficOut, |
| 83 | CurConns: serverStats.CurConns, |
| 84 | ClientCounts: serverStats.ClientCounts, |
| 85 | ProxyTypeCounts: serverStats.ProxyTypeCounts, |
| 86 | } |
| 87 | } |
| 88 |
| 89 | // /api/clients |
| 90 | func (c *Controller) APIClientList(ctx *httppkg.Context) (any, error) { |
| 91 | if c.clientRegistry == nil { |
| 92 | return nil, fmt.Errorf("client registry unavailable") |
| 93 | } |
| 94 |
| 95 | userFilter := ctx.Query("user") |
| 96 | clientIDFilter := ctx.Query("clientId") |
| 97 | runIDFilter := ctx.Query("runId") |
| 98 | statusFilter := strings.ToLower(ctx.Query("status")) |
| 99 |
| 100 | records := c.clientRegistry.List() |
| 101 | items := make([]model.ClientInfoResp, 0, len(records)) |
| 102 | for _, info := range records { |
| 103 | if userFilter != "" && info.User != userFilter { |
| 104 | continue |
| 105 | } |
| 106 | if clientIDFilter != "" && info.ClientID() != clientIDFilter { |
| 107 | continue |
| 108 | } |
| 109 | if runIDFilter != "" && info.RunID != runIDFilter { |
| 110 | continue |
| 111 | } |
| 112 | if !matchStatusFilter(info.Online, statusFilter) { |
| 113 | continue |
| 114 | } |
| 115 | items = append(items, buildClientInfoResp(info)) |
| 116 | } |
| 117 |
| 118 | slices.SortFunc(items, func(a, b model.ClientInfoResp) int { |
| 119 | if v := cmp.Compare(a.User, b.User); v != 0 { |
| 120 | return v |
| 121 | } |
| 122 | if v := cmp.Compare(a.ClientID, b.ClientID); v != 0 { |
| 123 | return v |
| 124 | } |
| 125 | return cmp.Compare(a.Key, b.Key) |
| 126 | }) |
| 127 |
| 128 | return items, nil |
| 129 | } |
| 130 |
| 131 | // /api/clients/{key} |
| 132 | func (c *Controller) APIClientDetail(ctx *httppkg.Context) (any, error) { |
| 133 | key := ctx.Param("key") |
| 134 | if key == "" { |
| 135 | return nil, fmt.Errorf("missing client key") |
| 136 | } |
| 137 |
| 138 | if c.clientRegistry == nil { |
| 139 | return nil, fmt.Errorf("client registry unavailable") |
| 140 | } |
| 141 |
| 142 | info, ok := c.clientRegistry.GetByKey(key) |
| 143 | if !ok { |
| 144 | return nil, httppkg.NewError(http.StatusNotFound, fmt.Sprintf("client %s not found", key)) |
| 145 | } |
| 146 |
| 147 | return buildClientInfoResp(info), nil |
| 148 | } |
| 149 |
| 150 | // /api/proxy/:type |
| 151 | func (c *Controller) APIProxyByType(ctx *httppkg.Context) (any, error) { |
| 152 | proxyType := ctx.Param("type") |
| 153 |
| 154 | proxyInfoResp := model.GetProxyInfoResp{} |
| 155 | proxyInfoResp.Proxies = c.getProxyStatsByType(proxyType) |
| 156 | slices.SortFunc(proxyInfoResp.Proxies, func(a, b *model.ProxyStatsInfo) int { |
| 157 | return cmp.Compare(a.Name, b.Name) |
| 158 | }) |
| 159 |
| 160 | return proxyInfoResp, nil |
| 161 | } |
| 162 |
| 163 | // /api/proxy/:type/:name |
| 164 | func (c *Controller) APIProxyByTypeAndName(ctx *httppkg.Context) (any, error) { |
| 165 | proxyType := ctx.Param("type") |
| 166 | name := ctx.Param("name") |
| 167 |
| 168 | proxyStatsResp, code, msg := c.getProxyStatsByTypeAndName(proxyType, name) |
| 169 | if code != 200 { |
| 170 | return nil, httppkg.NewError(code, msg) |
| 171 | } |
| 172 |
| 173 | return proxyStatsResp, nil |
| 174 | } |
| 175 |
| 176 | // /api/traffic/:name |
| 177 | func (c *Controller) APIProxyTraffic(ctx *httppkg.Context) (any, error) { |
| 178 | name := ctx.Param("name") |
| 179 |
| 180 | trafficResp := model.GetProxyTrafficResp{} |
| 181 | trafficResp.Name = name |
| 182 | proxyTrafficInfo := mem.StatsCollector.GetProxyTraffic(name) |
| 183 |
| 184 | if proxyTrafficInfo == nil { |
| 185 | return nil, httppkg.NewError(http.StatusNotFound, "no proxy info found") |
| 186 | } |
| 187 | trafficResp.TrafficIn = proxyTrafficInfo.TrafficIn |
| 188 | trafficResp.TrafficOut = proxyTrafficInfo.TrafficOut |
| 189 |
| 190 | return trafficResp, nil |
| 191 | } |
| 192 |
| 193 | // /api/proxies/:name |
| 194 | func (c *Controller) APIProxyByName(ctx *httppkg.Context) (any, error) { |
| 195 | name := ctx.Param("name") |
| 196 |
| 197 | ps := mem.StatsCollector.GetProxyByName(name) |
| 198 | if ps == nil { |
| 199 | return nil, httppkg.NewError(http.StatusNotFound, "no proxy info found") |
| 200 | } |
| 201 |
| 202 | proxyInfo := model.GetProxyStatsResp{ |
| 203 | Name: ps.Name, |
| 204 | User: ps.User, |
| 205 | ClientID: ps.ClientID, |
| 206 | TodayTrafficIn: ps.TodayTrafficIn, |
| 207 | TodayTrafficOut: ps.TodayTrafficOut, |
| 208 | CurConns: ps.CurConns, |
| 209 | LastStartTime: ps.LastStartTime, |
| 210 | LastCloseTime: ps.LastCloseTime, |
| 211 | } |
| 212 |
| 213 | if pxy, ok := c.pxyManager.GetByName(name); ok { |
| 214 | proxyInfo.Conf = getConfFromConfigurer(pxy.GetConfigurer()) |
| 215 | proxyInfo.Status = "online" |
| 216 | } else { |
| 217 | proxyInfo.Status = "offline" |
| 218 | } |
| 219 |
| 220 | return proxyInfo, nil |
| 221 | } |
| 222 |
| 223 | // DELETE /api/proxies?status=offline |
| 224 | func (c *Controller) DeleteProxies(ctx *httppkg.Context) (any, error) { |
| 225 | status := ctx.Query("status") |
| 226 | if status != "offline" { |
| 227 | return nil, httppkg.NewError(http.StatusBadRequest, "status only support offline") |
| 228 | } |
| 229 | cleared, total := mem.StatsCollector.ClearOfflineProxies() |
| 230 | log.Infof("cleared [%d] offline proxies, total [%d] proxies", cleared, total) |
| 231 | return httppkg.GeneralResponse{Code: 200, Msg: "success"}, nil |
| 232 | } |
| 233 |
| 234 | func (c *Controller) getProxyStatsByType(proxyType string) (proxyInfos []*model.ProxyStatsInfo) { |
| 235 | proxyStats := mem.StatsCollector.GetProxiesByType(proxyType) |
| 236 | proxyInfos = make([]*model.ProxyStatsInfo, 0, len(proxyStats)) |
| 237 | for _, ps := range proxyStats { |
| 238 | proxyInfo := &model.ProxyStatsInfo{ |
| 239 | User: ps.User, |
| 240 | ClientID: ps.ClientID, |
| 241 | } |
| 242 | if pxy, ok := c.pxyManager.GetByName(ps.Name); ok { |
| 243 | proxyInfo.Conf = getConfFromConfigurer(pxy.GetConfigurer()) |
| 244 | proxyInfo.Status = "online" |
| 245 | } else { |
| 246 | proxyInfo.Status = "offline" |
| 247 | } |
| 248 | proxyInfo.Name = ps.Name |
| 249 | proxyInfo.TodayTrafficIn = ps.TodayTrafficIn |
| 250 | proxyInfo.TodayTrafficOut = ps.TodayTrafficOut |
| 251 | proxyInfo.CurConns = ps.CurConns |
| 252 | proxyInfo.LastStartTime = ps.LastStartTime |
| 253 | proxyInfo.LastCloseTime = ps.LastCloseTime |
| 254 | proxyInfos = append(proxyInfos, proxyInfo) |
| 255 | } |
| 256 | return |
| 257 | } |
| 258 |
| 259 | func (c *Controller) getProxyStatsByTypeAndName(proxyType string, proxyName string) (proxyInfo model.GetProxyStatsResp, code int, msg string) { |
| 260 | proxyInfo.Name = proxyName |
| 261 | ps := mem.StatsCollector.GetProxiesByTypeAndName(proxyType, proxyName) |
| 262 | if ps == nil { |
| 263 | code = 404 |
| 264 | msg = "no proxy info found" |
| 265 | } else { |
| 266 | proxyInfo.User = ps.User |
| 267 | proxyInfo.ClientID = ps.ClientID |
| 268 | if pxy, ok := c.pxyManager.GetByName(proxyName); ok { |
| 269 | proxyInfo.Conf = getConfFromConfigurer(pxy.GetConfigurer()) |
| 270 | proxyInfo.Status = "online" |
| 271 | } else { |
| 272 | proxyInfo.Status = "offline" |
| 273 | } |
| 274 | proxyInfo.TodayTrafficIn = ps.TodayTrafficIn |
| 275 | proxyInfo.TodayTrafficOut = ps.TodayTrafficOut |
| 276 | proxyInfo.CurConns = ps.CurConns |
| 277 | proxyInfo.LastStartTime = ps.LastStartTime |
| 278 | proxyInfo.LastCloseTime = ps.LastCloseTime |
| 279 | code = 200 |
| 280 | } |
| 281 |
| 282 | return |
| 283 | } |
| 284 |
| 285 | func buildClientInfoResp(info registry.ClientInfo) model.ClientInfoResp { |
| 286 | resp := model.ClientInfoResp{ |
| 287 | Key: info.Key, |
| 288 | User: info.User, |
| 289 | ClientID: info.ClientID(), |
| 290 | RunID: info.RunID, |
| 291 | Version: info.Version, |
| 292 | WireProtocol: info.WireProtocol, |
| 293 | Hostname: info.Hostname, |
| 294 | ClientIP: info.IP, |
| 295 | FirstConnectedAt: toUnix(info.FirstConnectedAt), |
| 296 | LastConnectedAt: toUnix(info.LastConnectedAt), |
| 297 | Online: info.Online, |
| 298 | } |
| 299 | if !info.DisconnectedAt.IsZero() { |
| 300 | resp.DisconnectedAt = info.DisconnectedAt.Unix() |
| 301 | } |
| 302 | return resp |
| 303 | } |
| 304 |
| 305 | func toUnix(t time.Time) int64 { |
| 306 | if t.IsZero() { |
| 307 | return 0 |
| 308 | } |
| 309 | return t.Unix() |
| 310 | } |
| 311 |
| 312 | func matchStatusFilter(online bool, filter string) bool { |
| 313 | switch strings.ToLower(filter) { |
| 314 | case "", "all": |
| 315 | return true |
| 316 | case "online": |
| 317 | return online |
| 318 | case "offline": |
| 319 | return !online |
| 320 | default: |
| 321 | return true |
| 322 | } |
| 323 | } |
| 324 |
| 325 | func getConfFromConfigurer(cfg v1.ProxyConfigurer) any { |
| 326 | outBase := model.BaseOutConf{ProxyBaseConfig: *cfg.GetBaseConfig()} |
| 327 |
| 328 | switch c := cfg.(type) { |
| 329 | case *v1.TCPProxyConfig: |
| 330 | return &model.TCPOutConf{BaseOutConf: outBase, RemotePort: c.RemotePort} |
| 331 | case *v1.UDPProxyConfig: |
| 332 | return &model.UDPOutConf{BaseOutConf: outBase, RemotePort: c.RemotePort} |
| 333 | case *v1.HTTPProxyConfig: |
| 334 | return &model.HTTPOutConf{ |
| 335 | BaseOutConf: outBase, |
| 336 | DomainConfig: c.DomainConfig, |
| 337 | Locations: c.Locations, |
| 338 | HostHeaderRewrite: c.HostHeaderRewrite, |
| 339 | } |
| 340 | case *v1.HTTPSProxyConfig: |
| 341 | return &model.HTTPSOutConf{ |
| 342 | BaseOutConf: outBase, |
| 343 | DomainConfig: c.DomainConfig, |
| 344 | } |
| 345 | case *v1.TCPMuxProxyConfig: |
| 346 | return &model.TCPMuxOutConf{ |
| 347 | BaseOutConf: outBase, |
| 348 | DomainConfig: c.DomainConfig, |
| 349 | Multiplexer: c.Multiplexer, |
| 350 | RouteByHTTPUser: c.RouteByHTTPUser, |
| 351 | } |
| 352 | case *v1.STCPProxyConfig: |
| 353 | return &model.STCPOutConf{BaseOutConf: outBase} |
| 354 | case *v1.XTCPProxyConfig: |
| 355 | return &model.XTCPOutConf{BaseOutConf: outBase} |
| 356 | } |
| 357 | return outBase |
| 358 | } |