| // |
| 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 client |
| 16 |
| 17 | import ( |
| 18 | "net/http" |
| 19 |
| 20 | adminapi "github.com/fatedier/frp/client/http" |
| 21 | "github.com/fatedier/frp/client/proxy" |
| 22 | httppkg "github.com/fatedier/frp/pkg/util/http" |
| 23 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 24 | ) |
| 25 |
| 26 | func (svr *Service) registerRouteHandlers(helper *httppkg.RouterRegisterHelper) { |
| 27 | apiController := newAPIController(svr) |
| 28 |
| 29 | // Healthz endpoint without auth |
| 30 | helper.Router.HandleFunc("/healthz", healthz) |
| 31 |
| 32 | // API routes and static files with auth |
| 33 | subRouter := helper.Router.NewRoute().Subrouter() |
| 34 | subRouter.Use(helper.AuthMiddleware) |
| 35 | subRouter.Use(httppkg.NewRequestLogger) |
| 36 | subRouter.HandleFunc("/api/reload", httppkg.MakeHTTPHandlerFunc(apiController.Reload)).Methods(http.MethodGet) |
| 37 | subRouter.HandleFunc("/api/stop", httppkg.MakeHTTPHandlerFunc(apiController.Stop)).Methods(http.MethodPost) |
| 38 | subRouter.HandleFunc("/api/status", httppkg.MakeHTTPHandlerFunc(apiController.Status)).Methods(http.MethodGet) |
| 39 | subRouter.HandleFunc("/api/config", httppkg.MakeHTTPHandlerFunc(apiController.GetConfig)).Methods(http.MethodGet) |
| 40 | subRouter.HandleFunc("/api/config", httppkg.MakeHTTPHandlerFunc(apiController.PutConfig)).Methods(http.MethodPut) |
| 41 | subRouter.HandleFunc("/api/proxy/{name}/config", httppkg.MakeHTTPHandlerFunc(apiController.GetProxyConfig)).Methods(http.MethodGet) |
| 42 | subRouter.HandleFunc("/api/visitor/{name}/config", httppkg.MakeHTTPHandlerFunc(apiController.GetVisitorConfig)).Methods(http.MethodGet) |
| 43 |
| 44 | if svr.storeSource != nil { |
| 45 | subRouter.HandleFunc("/api/store/proxies", httppkg.MakeHTTPHandlerFunc(apiController.ListStoreProxies)).Methods(http.MethodGet) |
| 46 | subRouter.HandleFunc("/api/store/proxies", httppkg.MakeHTTPHandlerFunc(apiController.CreateStoreProxy)).Methods(http.MethodPost) |
| 47 | subRouter.HandleFunc("/api/store/proxies/{name}", httppkg.MakeHTTPHandlerFunc(apiController.GetStoreProxy)).Methods(http.MethodGet) |
| 48 | subRouter.HandleFunc("/api/store/proxies/{name}", httppkg.MakeHTTPHandlerFunc(apiController.UpdateStoreProxy)).Methods(http.MethodPut) |
| 49 | subRouter.HandleFunc("/api/store/proxies/{name}", httppkg.MakeHTTPHandlerFunc(apiController.DeleteStoreProxy)).Methods(http.MethodDelete) |
| 50 | subRouter.HandleFunc("/api/store/visitors", httppkg.MakeHTTPHandlerFunc(apiController.ListStoreVisitors)).Methods(http.MethodGet) |
| 51 | subRouter.HandleFunc("/api/store/visitors", httppkg.MakeHTTPHandlerFunc(apiController.CreateStoreVisitor)).Methods(http.MethodPost) |
| 52 | subRouter.HandleFunc("/api/store/visitors/{name}", httppkg.MakeHTTPHandlerFunc(apiController.GetStoreVisitor)).Methods(http.MethodGet) |
| 53 | subRouter.HandleFunc("/api/store/visitors/{name}", httppkg.MakeHTTPHandlerFunc(apiController.UpdateStoreVisitor)).Methods(http.MethodPut) |
| 54 | subRouter.HandleFunc("/api/store/visitors/{name}", httppkg.MakeHTTPHandlerFunc(apiController.DeleteStoreVisitor)).Methods(http.MethodDelete) |
| 55 | } |
| 56 |
| 57 | subRouter.Handle("/favicon.ico", http.FileServer(helper.AssetsFS)).Methods("GET") |
| 58 | subRouter.PathPrefix("/static/").Handler( |
| 59 | netpkg.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(helper.AssetsFS))), |
| 60 | ).Methods("GET") |
| 61 | subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 62 | http.Redirect(w, r, "/static/", http.StatusMovedPermanently) |
| 63 | }) |
| 64 | } |
| 65 |
| 66 | func healthz(w http.ResponseWriter, _ *http.Request) { |
| 67 | w.WriteHeader(http.StatusOK) |
| 68 | } |
| 69 |
| 70 | func newAPIController(svr *Service) *adminapi.Controller { |
| 71 | manager := newServiceConfigManager(svr) |
| 72 | return adminapi.NewController(adminapi.ControllerParams{ |
| 73 | ServerAddr: svr.common.ServerAddr, |
| 74 | Manager: manager, |
| 75 | }) |
| 76 | } |
| 77 |
| 78 | // getAllProxyStatus returns all proxy statuses. |
| 79 | func (svr *Service) getAllProxyStatus() []*proxy.WorkingStatus { |
| 80 | svr.ctlMu.RLock() |
| 81 | ctl := svr.ctl |
| 82 | svr.ctlMu.RUnlock() |
| 83 | if ctl == nil { |
| 84 | return nil |
| 85 | } |
| 86 | return ctl.pm.GetAllProxyStatus() |
| 87 | } |