admin_api.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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. package client
  15. import (
  16. "net/http"
  17. "github.com/fatedier/frp/client/api"
  18. "github.com/fatedier/frp/client/proxy"
  19. httppkg "github.com/fatedier/frp/pkg/util/http"
  20. netpkg "github.com/fatedier/frp/pkg/util/net"
  21. )
  22. func (svr *Service) registerRouteHandlers(helper *httppkg.RouterRegisterHelper) {
  23. apiController := newAPIController(svr)
  24. // Healthz endpoint without auth
  25. helper.Router.HandleFunc("/healthz", healthz)
  26. // API routes and static files with auth
  27. subRouter := helper.Router.NewRoute().Subrouter()
  28. subRouter.Use(helper.AuthMiddleware)
  29. subRouter.Use(httppkg.NewRequestLogger)
  30. subRouter.HandleFunc("/api/reload", httppkg.MakeHTTPHandlerFunc(apiController.Reload)).Methods(http.MethodGet)
  31. subRouter.HandleFunc("/api/stop", httppkg.MakeHTTPHandlerFunc(apiController.Stop)).Methods(http.MethodPost)
  32. subRouter.HandleFunc("/api/status", httppkg.MakeHTTPHandlerFunc(apiController.Status)).Methods(http.MethodGet)
  33. subRouter.HandleFunc("/api/config", httppkg.MakeHTTPHandlerFunc(apiController.GetConfig)).Methods(http.MethodGet)
  34. subRouter.HandleFunc("/api/config", httppkg.MakeHTTPHandlerFunc(apiController.PutConfig)).Methods(http.MethodPut)
  35. subRouter.Handle("/favicon.ico", http.FileServer(helper.AssetsFS)).Methods("GET")
  36. subRouter.PathPrefix("/static/").Handler(
  37. netpkg.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(helper.AssetsFS))),
  38. ).Methods("GET")
  39. subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  40. http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
  41. })
  42. }
  43. func healthz(w http.ResponseWriter, _ *http.Request) {
  44. w.WriteHeader(http.StatusOK)
  45. }
  46. func newAPIController(svr *Service) *api.Controller {
  47. return api.NewController(api.ControllerParams{
  48. GetProxyStatus: svr.getAllProxyStatus,
  49. ServerAddr: svr.common.ServerAddr,
  50. ConfigFilePath: svr.configFilePath,
  51. UnsafeFeatures: svr.unsafeFeatures,
  52. UpdateConfig: svr.UpdateAllConfigurer,
  53. GracefulClose: svr.GracefulClose,
  54. })
  55. }
  56. // getAllProxyStatus returns all proxy statuses.
  57. func (svr *Service) getAllProxyStatus() []*proxy.WorkingStatus {
  58. svr.ctlMu.RLock()
  59. ctl := svr.ctl
  60. svr.ctlMu.RUnlock()
  61. if ctl == nil {
  62. return nil
  63. }
  64. return ctl.pm.GetAllProxyStatus()
  65. }