| // |
| 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 | "errors" |
| 20 | "fmt" |
| 21 | "net" |
| 22 | "net/http" |
| 23 | "slices" |
| 24 | "strconv" |
| 25 | "time" |
| 26 |
| 27 | "github.com/fatedier/frp/client/configmgmt" |
| 28 | "github.com/fatedier/frp/client/http/model" |
| 29 | "github.com/fatedier/frp/client/proxy" |
| 30 | httppkg "github.com/fatedier/frp/pkg/util/http" |
| 31 | "github.com/fatedier/frp/pkg/util/jsonx" |
| 32 | ) |
| 33 |
| 34 | // Controller handles HTTP API requests for frpc. |
| 35 | type Controller struct { |
| 36 | serverAddr string |
| 37 | manager configmgmt.ConfigManager |
| 38 | } |
| 39 |
| 40 | // ControllerParams contains parameters for creating an APIController. |
| 41 | type ControllerParams struct { |
| 42 | ServerAddr string |
| 43 | Manager configmgmt.ConfigManager |
| 44 | } |
| 45 |
| 46 | func NewController(params ControllerParams) *Controller { |
| 47 | return &Controller{ |
| 48 | serverAddr: params.ServerAddr, |
| 49 | manager: params.Manager, |
| 50 | } |
| 51 | } |
| 52 |
| 53 | func (c *Controller) toHTTPError(err error) error { |
| 54 | if err == nil { |
| 55 | return nil |
| 56 | } |
| 57 |
| 58 | code := http.StatusInternalServerError |
| 59 | switch { |
| 60 | case errors.Is(err, configmgmt.ErrInvalidArgument): |
| 61 | code = http.StatusBadRequest |
| 62 | case errors.Is(err, configmgmt.ErrNotFound), errors.Is(err, configmgmt.ErrStoreDisabled): |
| 63 | code = http.StatusNotFound |
| 64 | case errors.Is(err, configmgmt.ErrConflict): |
| 65 | code = http.StatusConflict |
| 66 | } |
| 67 | return httppkg.NewError(code, err.Error()) |
| 68 | } |
| 69 |
| 70 | // Reload handles GET /api/reload |
| 71 | func (c *Controller) Reload(ctx *httppkg.Context) (any, error) { |
| 72 | strictConfigMode := false |
| 73 | strictStr := ctx.Query("strictConfig") |
| 74 | if strictStr != "" { |
| 75 | strictConfigMode, _ = strconv.ParseBool(strictStr) |
| 76 | } |
| 77 |
| 78 | if err := c.manager.ReloadFromFile(strictConfigMode); err != nil { |
| 79 | return nil, c.toHTTPError(err) |
| 80 | } |
| 81 | return nil, nil |
| 82 | } |
| 83 |
| 84 | // Stop handles POST /api/stop |
| 85 | func (c *Controller) Stop(ctx *httppkg.Context) (any, error) { |
| 86 | go c.manager.GracefulClose(100 * time.Millisecond) |
| 87 | return nil, nil |
| 88 | } |
| 89 |
| 90 | // Status handles GET /api/status |
| 91 | func (c *Controller) Status(ctx *httppkg.Context) (any, error) { |
| 92 | res := make(model.StatusResp) |
| 93 | ps := c.manager.GetProxyStatus() |
| 94 | if ps == nil { |
| 95 | return res, nil |
| 96 | } |
| 97 |
| 98 | for _, status := range ps { |
| 99 | res[status.Type] = append(res[status.Type], c.buildProxyStatusResp(status)) |
| 100 | } |
| 101 |
| 102 | for _, arrs := range res { |
| 103 | if len(arrs) <= 1 { |
| 104 | continue |
| 105 | } |
| 106 | slices.SortFunc(arrs, func(a, b model.ProxyStatusResp) int { |
| 107 | return cmp.Compare(a.Name, b.Name) |
| 108 | }) |
| 109 | } |
| 110 | return res, nil |
| 111 | } |
| 112 |
| 113 | // GetConfig handles GET /api/config |
| 114 | func (c *Controller) GetConfig(ctx *httppkg.Context) (any, error) { |
| 115 | content, err := c.manager.ReadConfigFile() |
| 116 | if err != nil { |
| 117 | return nil, c.toHTTPError(err) |
| 118 | } |
| 119 | return content, nil |
| 120 | } |
| 121 |
| 122 | // PutConfig handles PUT /api/config |
| 123 | func (c *Controller) PutConfig(ctx *httppkg.Context) (any, error) { |
| 124 | body, err := ctx.Body() |
| 125 | if err != nil { |
| 126 | return nil, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("read request body error: %v", err)) |
| 127 | } |
| 128 |
| 129 | if len(body) == 0 { |
| 130 | return nil, httppkg.NewError(http.StatusBadRequest, "body can't be empty") |
| 131 | } |
| 132 |
| 133 | if err := c.manager.WriteConfigFile(body); err != nil { |
| 134 | return nil, c.toHTTPError(err) |
| 135 | } |
| 136 | return nil, nil |
| 137 | } |
| 138 |
| 139 | func (c *Controller) buildProxyStatusResp(status *proxy.WorkingStatus) model.ProxyStatusResp { |
| 140 | psr := model.ProxyStatusResp{ |
| 141 | Name: status.Name, |
| 142 | Type: status.Type, |
| 143 | Status: status.Phase, |
| 144 | Err: status.Err, |
| 145 | } |
| 146 | baseCfg := status.Cfg.GetBaseConfig() |
| 147 | if baseCfg.LocalPort != 0 { |
| 148 | psr.LocalAddr = net.JoinHostPort(baseCfg.LocalIP, strconv.Itoa(baseCfg.LocalPort)) |
| 149 | } |
| 150 | psr.Plugin = baseCfg.Plugin.Type |
| 151 |
| 152 | if status.Err == "" { |
| 153 | psr.RemoteAddr = status.RemoteAddr |
| 154 | if slices.Contains([]string{"tcp", "udp"}, status.Type) { |
| 155 | psr.RemoteAddr = c.serverAddr + psr.RemoteAddr |
| 156 | } |
| 157 | } |
| 158 |
| 159 | if c.manager.IsStoreProxyEnabled(status.Name) { |
| 160 | psr.Source = model.SourceStore |
| 161 | } |
| 162 | return psr |
| 163 | } |
| 164 |
| 165 | // GetProxyConfig handles GET /api/proxy/{name}/config |
| 166 | func (c *Controller) GetProxyConfig(ctx *httppkg.Context) (any, error) { |
| 167 | name := ctx.Param("name") |
| 168 | if name == "" { |
| 169 | return nil, httppkg.NewError(http.StatusBadRequest, "proxy name is required") |
| 170 | } |
| 171 |
| 172 | cfg, ok := c.manager.GetProxyConfig(name) |
| 173 | if !ok { |
| 174 | return nil, httppkg.NewError(http.StatusNotFound, fmt.Sprintf("proxy %q not found", name)) |
| 175 | } |
| 176 |
| 177 | payload, err := model.ProxyDefinitionFromConfigurer(cfg) |
| 178 | if err != nil { |
| 179 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 180 | } |
| 181 | return payload, nil |
| 182 | } |
| 183 |
| 184 | // GetVisitorConfig handles GET /api/visitor/{name}/config |
| 185 | func (c *Controller) GetVisitorConfig(ctx *httppkg.Context) (any, error) { |
| 186 | name := ctx.Param("name") |
| 187 | if name == "" { |
| 188 | return nil, httppkg.NewError(http.StatusBadRequest, "visitor name is required") |
| 189 | } |
| 190 |
| 191 | cfg, ok := c.manager.GetVisitorConfig(name) |
| 192 | if !ok { |
| 193 | return nil, httppkg.NewError(http.StatusNotFound, fmt.Sprintf("visitor %q not found", name)) |
| 194 | } |
| 195 |
| 196 | payload, err := model.VisitorDefinitionFromConfigurer(cfg) |
| 197 | if err != nil { |
| 198 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 199 | } |
| 200 | return payload, nil |
| 201 | } |
| 202 |
| 203 | func (c *Controller) ListStoreProxies(ctx *httppkg.Context) (any, error) { |
| 204 | proxies, err := c.manager.ListStoreProxies() |
| 205 | if err != nil { |
| 206 | return nil, c.toHTTPError(err) |
| 207 | } |
| 208 |
| 209 | resp := model.ProxyListResp{Proxies: make([]model.ProxyDefinition, 0, len(proxies))} |
| 210 | for _, p := range proxies { |
| 211 | payload, err := model.ProxyDefinitionFromConfigurer(p) |
| 212 | if err != nil { |
| 213 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 214 | } |
| 215 | resp.Proxies = append(resp.Proxies, payload) |
| 216 | } |
| 217 | slices.SortFunc(resp.Proxies, func(a, b model.ProxyDefinition) int { |
| 218 | return cmp.Compare(a.Name, b.Name) |
| 219 | }) |
| 220 | return resp, nil |
| 221 | } |
| 222 |
| 223 | func (c *Controller) GetStoreProxy(ctx *httppkg.Context) (any, error) { |
| 224 | name := ctx.Param("name") |
| 225 | if name == "" { |
| 226 | return nil, httppkg.NewError(http.StatusBadRequest, "proxy name is required") |
| 227 | } |
| 228 |
| 229 | p, err := c.manager.GetStoreProxy(name) |
| 230 | if err != nil { |
| 231 | return nil, c.toHTTPError(err) |
| 232 | } |
| 233 |
| 234 | payload, err := model.ProxyDefinitionFromConfigurer(p) |
| 235 | if err != nil { |
| 236 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 237 | } |
| 238 |
| 239 | return payload, nil |
| 240 | } |
| 241 |
| 242 | func (c *Controller) CreateStoreProxy(ctx *httppkg.Context) (any, error) { |
| 243 | body, err := ctx.Body() |
| 244 | if err != nil { |
| 245 | return nil, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("read body error: %v", err)) |
| 246 | } |
| 247 |
| 248 | var payload model.ProxyDefinition |
| 249 | if err := jsonx.Unmarshal(body, &payload); err != nil { |
| 250 | return nil, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("parse JSON error: %v", err)) |
| 251 | } |
| 252 |
| 253 | if err := payload.Validate("", false); err != nil { |
| 254 | return nil, httppkg.NewError(http.StatusBadRequest, err.Error()) |
| 255 | } |
| 256 | cfg, err := payload.ToConfigurer() |
| 257 | if err != nil { |
| 258 | return nil, httppkg.NewError(http.StatusBadRequest, err.Error()) |
| 259 | } |
| 260 | created, err := c.manager.CreateStoreProxy(cfg) |
| 261 | if err != nil { |
| 262 | return nil, c.toHTTPError(err) |
| 263 | } |
| 264 |
| 265 | resp, err := model.ProxyDefinitionFromConfigurer(created) |
| 266 | if err != nil { |
| 267 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 268 | } |
| 269 | return resp, nil |
| 270 | } |
| 271 |
| 272 | func (c *Controller) UpdateStoreProxy(ctx *httppkg.Context) (any, error) { |
| 273 | name := ctx.Param("name") |
| 274 | if name == "" { |
| 275 | return nil, httppkg.NewError(http.StatusBadRequest, "proxy name is required") |
| 276 | } |
| 277 |
| 278 | body, err := ctx.Body() |
| 279 | if err != nil { |
| 280 | return nil, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("read body error: %v", err)) |
| 281 | } |
| 282 |
| 283 | var payload model.ProxyDefinition |
| 284 | if err := jsonx.Unmarshal(body, &payload); err != nil { |
| 285 | return nil, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("parse JSON error: %v", err)) |
| 286 | } |
| 287 |
| 288 | if err := payload.Validate(name, true); err != nil { |
| 289 | return nil, httppkg.NewError(http.StatusBadRequest, err.Error()) |
| 290 | } |
| 291 | cfg, err := payload.ToConfigurer() |
| 292 | if err != nil { |
| 293 | return nil, httppkg.NewError(http.StatusBadRequest, err.Error()) |
| 294 | } |
| 295 | updated, err := c.manager.UpdateStoreProxy(name, cfg) |
| 296 | if err != nil { |
| 297 | return nil, c.toHTTPError(err) |
| 298 | } |
| 299 |
| 300 | resp, err := model.ProxyDefinitionFromConfigurer(updated) |
| 301 | if err != nil { |
| 302 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 303 | } |
| 304 | return resp, nil |
| 305 | } |
| 306 |
| 307 | func (c *Controller) DeleteStoreProxy(ctx *httppkg.Context) (any, error) { |
| 308 | name := ctx.Param("name") |
| 309 | if name == "" { |
| 310 | return nil, httppkg.NewError(http.StatusBadRequest, "proxy name is required") |
| 311 | } |
| 312 |
| 313 | if err := c.manager.DeleteStoreProxy(name); err != nil { |
| 314 | return nil, c.toHTTPError(err) |
| 315 | } |
| 316 | return nil, nil |
| 317 | } |
| 318 |
| 319 | func (c *Controller) ListStoreVisitors(ctx *httppkg.Context) (any, error) { |
| 320 | visitors, err := c.manager.ListStoreVisitors() |
| 321 | if err != nil { |
| 322 | return nil, c.toHTTPError(err) |
| 323 | } |
| 324 |
| 325 | resp := model.VisitorListResp{Visitors: make([]model.VisitorDefinition, 0, len(visitors))} |
| 326 | for _, v := range visitors { |
| 327 | payload, err := model.VisitorDefinitionFromConfigurer(v) |
| 328 | if err != nil { |
| 329 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 330 | } |
| 331 | resp.Visitors = append(resp.Visitors, payload) |
| 332 | } |
| 333 | slices.SortFunc(resp.Visitors, func(a, b model.VisitorDefinition) int { |
| 334 | return cmp.Compare(a.Name, b.Name) |
| 335 | }) |
| 336 | return resp, nil |
| 337 | } |
| 338 |
| 339 | func (c *Controller) GetStoreVisitor(ctx *httppkg.Context) (any, error) { |
| 340 | name := ctx.Param("name") |
| 341 | if name == "" { |
| 342 | return nil, httppkg.NewError(http.StatusBadRequest, "visitor name is required") |
| 343 | } |
| 344 |
| 345 | v, err := c.manager.GetStoreVisitor(name) |
| 346 | if err != nil { |
| 347 | return nil, c.toHTTPError(err) |
| 348 | } |
| 349 |
| 350 | payload, err := model.VisitorDefinitionFromConfigurer(v) |
| 351 | if err != nil { |
| 352 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 353 | } |
| 354 |
| 355 | return payload, nil |
| 356 | } |
| 357 |
| 358 | func (c *Controller) CreateStoreVisitor(ctx *httppkg.Context) (any, error) { |
| 359 | body, err := ctx.Body() |
| 360 | if err != nil { |
| 361 | return nil, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("read body error: %v", err)) |
| 362 | } |
| 363 |
| 364 | var payload model.VisitorDefinition |
| 365 | if err := jsonx.Unmarshal(body, &payload); err != nil { |
| 366 | return nil, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("parse JSON error: %v", err)) |
| 367 | } |
| 368 |
| 369 | if err := payload.Validate("", false); err != nil { |
| 370 | return nil, httppkg.NewError(http.StatusBadRequest, err.Error()) |
| 371 | } |
| 372 | cfg, err := payload.ToConfigurer() |
| 373 | if err != nil { |
| 374 | return nil, httppkg.NewError(http.StatusBadRequest, err.Error()) |
| 375 | } |
| 376 | created, err := c.manager.CreateStoreVisitor(cfg) |
| 377 | if err != nil { |
| 378 | return nil, c.toHTTPError(err) |
| 379 | } |
| 380 |
| 381 | resp, err := model.VisitorDefinitionFromConfigurer(created) |
| 382 | if err != nil { |
| 383 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 384 | } |
| 385 | return resp, nil |
| 386 | } |
| 387 |
| 388 | func (c *Controller) UpdateStoreVisitor(ctx *httppkg.Context) (any, error) { |
| 389 | name := ctx.Param("name") |
| 390 | if name == "" { |
| 391 | return nil, httppkg.NewError(http.StatusBadRequest, "visitor name is required") |
| 392 | } |
| 393 |
| 394 | body, err := ctx.Body() |
| 395 | if err != nil { |
| 396 | return nil, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("read body error: %v", err)) |
| 397 | } |
| 398 |
| 399 | var payload model.VisitorDefinition |
| 400 | if err := jsonx.Unmarshal(body, &payload); err != nil { |
| 401 | return nil, httppkg.NewError(http.StatusBadRequest, fmt.Sprintf("parse JSON error: %v", err)) |
| 402 | } |
| 403 |
| 404 | if err := payload.Validate(name, true); err != nil { |
| 405 | return nil, httppkg.NewError(http.StatusBadRequest, err.Error()) |
| 406 | } |
| 407 | cfg, err := payload.ToConfigurer() |
| 408 | if err != nil { |
| 409 | return nil, httppkg.NewError(http.StatusBadRequest, err.Error()) |
| 410 | } |
| 411 | updated, err := c.manager.UpdateStoreVisitor(name, cfg) |
| 412 | if err != nil { |
| 413 | return nil, c.toHTTPError(err) |
| 414 | } |
| 415 |
| 416 | resp, err := model.VisitorDefinitionFromConfigurer(updated) |
| 417 | if err != nil { |
| 418 | return nil, httppkg.NewError(http.StatusInternalServerError, err.Error()) |
| 419 | } |
| 420 | return resp, nil |
| 421 | } |
| 422 |
| 423 | func (c *Controller) DeleteStoreVisitor(ctx *httppkg.Context) (any, error) { |
| 424 | name := ctx.Param("name") |
| 425 | if name == "" { |
| 426 | return nil, httppkg.NewError(http.StatusBadRequest, "visitor name is required") |
| 427 | } |
| 428 |
| 429 | if err := c.manager.DeleteStoreVisitor(name); err != nil { |
| 430 | return nil, c.toHTTPError(err) |
| 431 | } |
| 432 | return nil, nil |
| 433 | } |