admin.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. frpNet "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.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.AdminUser, svr.cfg.AdminPwd
  42. subRouter.Use(frpNet.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/status", svr.apiStatus).Methods("GET")
  46. subRouter.HandleFunc("/api/config", svr.apiGetConfig).Methods("GET")
  47. subRouter.HandleFunc("/api/config", svr.apiPutConfig).Methods("PUT")
  48. // view
  49. subRouter.Handle("/favicon.ico", http.FileServer(assets.FileSystem)).Methods("GET")
  50. subRouter.PathPrefix("/static/").Handler(frpNet.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))).Methods("GET")
  51. subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  52. http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
  53. })
  54. server := &http.Server{
  55. Addr: address,
  56. Handler: router,
  57. ReadTimeout: httpServerReadTimeout,
  58. WriteTimeout: httpServerWriteTimeout,
  59. }
  60. if address == "" {
  61. address = ":http"
  62. }
  63. ln, err := net.Listen("tcp", address)
  64. if err != nil {
  65. return err
  66. }
  67. go func() {
  68. _ = server.Serve(ln)
  69. }()
  70. return
  71. }