1
0

admin.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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"
  17. "net/http"
  18. "net/http/pprof"
  19. "time"
  20. "github.com/gorilla/mux"
  21. "github.com/fatedier/frp/assets"
  22. utilnet "github.com/fatedier/frp/pkg/util/net"
  23. )
  24. var (
  25. httpServerReadTimeout = 60 * time.Second
  26. httpServerWriteTimeout = 60 * time.Second
  27. )
  28. func (svr *Service) RunAdminServer(address string) (err error) {
  29. // url router
  30. router := mux.NewRouter()
  31. router.HandleFunc("/healthz", svr.healthz)
  32. // debug
  33. if svr.cfg.WebServer.PprofEnable {
  34. router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
  35. router.HandleFunc("/debug/pprof/profile", pprof.Profile)
  36. router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
  37. router.HandleFunc("/debug/pprof/trace", pprof.Trace)
  38. router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
  39. }
  40. subRouter := router.NewRoute().Subrouter()
  41. user, passwd := svr.cfg.WebServer.User, svr.cfg.WebServer.Password
  42. subRouter.Use(utilnet.NewHTTPAuthMiddleware(user, passwd).SetAuthFailDelay(200 * time.Millisecond).Middleware)
  43. // api, see admin_api.go
  44. subRouter.HandleFunc("/api/reload", svr.apiReload).Methods("GET")
  45. subRouter.HandleFunc("/api/stop", svr.apiStop).Methods("POST")
  46. subRouter.HandleFunc("/api/status", svr.apiStatus).Methods("GET")
  47. subRouter.HandleFunc("/api/config", svr.apiGetConfig).Methods("GET")
  48. subRouter.HandleFunc("/api/config", svr.apiPutConfig).Methods("PUT")
  49. // view
  50. subRouter.Handle("/favicon.ico", http.FileServer(assets.FileSystem)).Methods("GET")
  51. subRouter.PathPrefix("/static/").Handler(utilnet.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))).Methods("GET")
  52. subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  53. http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
  54. })
  55. server := &http.Server{
  56. Addr: address,
  57. Handler: router,
  58. ReadTimeout: httpServerReadTimeout,
  59. WriteTimeout: httpServerWriteTimeout,
  60. }
  61. if address == "" {
  62. address = ":http"
  63. }
  64. ln, err := net.Listen("tcp", address)
  65. if err != nil {
  66. return err
  67. }
  68. go func() {
  69. _ = server.Serve(ln)
  70. }()
  71. return
  72. }