| // |
| 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 server |
| 16 |
| 17 | import ( |
| 18 | "net/http" |
| 19 |
| 20 | "github.com/prometheus/client_golang/prometheus/promhttp" |
| 21 |
| 22 | httppkg "github.com/fatedier/frp/pkg/util/http" |
| 23 | netpkg "github.com/fatedier/frp/pkg/util/net" |
| 24 | adminapi "github.com/fatedier/frp/server/http" |
| 25 | ) |
| 26 |
| 27 | func (svr *Service) registerRouteHandlers(helper *httppkg.RouterRegisterHelper) { |
| 28 | helper.Router.HandleFunc("/healthz", healthz) |
| 29 | subRouter := helper.Router.NewRoute().Subrouter() |
| 30 |
| 31 | subRouter.Use(helper.AuthMiddleware) |
| 32 | subRouter.Use(httppkg.NewRequestLogger) |
| 33 |
| 34 | // metrics |
| 35 | if svr.cfg.EnablePrometheus { |
| 36 | subRouter.Handle("/metrics", promhttp.Handler()) |
| 37 | } |
| 38 |
| 39 | apiController := adminapi.NewController(svr.cfg, svr.clientRegistry, svr.pxyManager) |
| 40 |
| 41 | // apis |
| 42 | subRouter.HandleFunc("/api/serverinfo", httppkg.MakeHTTPHandlerFunc(apiController.APIServerInfo)).Methods("GET") |
| 43 | subRouter.HandleFunc("/api/proxy/{type}", httppkg.MakeHTTPHandlerFunc(apiController.APIProxyByType)).Methods("GET") |
| 44 | subRouter.HandleFunc("/api/proxy/{type}/{name}", httppkg.MakeHTTPHandlerFunc(apiController.APIProxyByTypeAndName)).Methods("GET") |
| 45 | subRouter.HandleFunc("/api/proxies/{name}", httppkg.MakeHTTPHandlerFunc(apiController.APIProxyByName)).Methods("GET") |
| 46 | subRouter.HandleFunc("/api/traffic/{name}", httppkg.MakeHTTPHandlerFunc(apiController.APIProxyTraffic)).Methods("GET") |
| 47 | subRouter.HandleFunc("/api/clients", httppkg.MakeHTTPHandlerFunc(apiController.APIClientList)).Methods("GET") |
| 48 | subRouter.HandleFunc("/api/clients/{key}", httppkg.MakeHTTPHandlerFunc(apiController.APIClientDetail)).Methods("GET") |
| 49 | subRouter.HandleFunc("/api/proxies", httppkg.MakeHTTPHandlerFunc(apiController.DeleteProxies)).Methods("DELETE") |
| 50 |
| 51 | subRouter.HandleFunc("/api/v2/users", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2UserList)).Methods("GET") |
| 52 | subRouter.HandleFunc("/api/v2/system/info", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2SystemInfo)).Methods("GET") |
| 53 | subRouter.HandleFunc("/api/v2/system/prune", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2SystemPrune)).Methods("POST") |
| 54 | subRouter.HandleFunc("/api/v2/clients", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2ClientList)).Methods("GET") |
| 55 | v2EncodedPathRouter := subRouter.NewRoute().Subrouter() |
| 56 | v2EncodedPathRouter.UseEncodedPath() |
| 57 | v2EncodedPathRouter.HandleFunc("/api/v2/clients/{key}", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2ClientDetail)).Methods("GET") |
| 58 | subRouter.HandleFunc("/api/v2/proxies", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2ProxyList)).Methods("GET") |
| 59 | v2EncodedPathRouter.HandleFunc("/api/v2/proxies/{name}/traffic", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2ProxyTraffic)).Methods("GET") |
| 60 | v2EncodedPathRouter.HandleFunc("/api/v2/proxies/{name}", httppkg.MakeHTTPHandlerFuncV2(apiController.APIV2ProxyDetail)).Methods("GET") |
| 61 |
| 62 | // view |
| 63 | subRouter.Handle("/favicon.ico", http.FileServer(helper.AssetsFS)).Methods("GET") |
| 64 | subRouter.PathPrefix("/static/").Handler( |
| 65 | netpkg.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(helper.AssetsFS))), |
| 66 | ).Methods("GET") |
| 67 |
| 68 | subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 69 | http.Redirect(w, r, "/static/", http.StatusMovedPermanently) |
| 70 | }) |
| 71 | } |
| 72 |
| 73 | func healthz(w http.ResponseWriter, _ *http.Request) { |
| 74 | w.WriteHeader(200) |
| 75 | } |