dashboard.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 server
  15. import (
  16. "net"
  17. "net/http"
  18. "net/http/pprof"
  19. "time"
  20. "github.com/fatedier/frp/assets"
  21. frpNet "github.com/fatedier/frp/pkg/util/net"
  22. "github.com/gorilla/mux"
  23. "github.com/prometheus/client_golang/prometheus/promhttp"
  24. )
  25. var (
  26. httpServerReadTimeout = 60 * time.Second
  27. httpServerWriteTimeout = 60 * time.Second
  28. )
  29. func (svr *Service) RunDashboardServer(address string) (err error) {
  30. // url router
  31. router := mux.NewRouter()
  32. router.HandleFunc("/healthz", svr.Healthz)
  33. // debug
  34. if svr.cfg.PprofEnable {
  35. router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
  36. router.HandleFunc("/debug/pprof/profile", pprof.Profile)
  37. router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
  38. router.HandleFunc("/debug/pprof/trace", pprof.Trace)
  39. router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
  40. }
  41. subRouter := router.NewRoute().Subrouter()
  42. user, passwd := svr.cfg.DashboardUser, svr.cfg.DashboardPwd
  43. subRouter.Use(frpNet.NewHTTPAuthMiddleware(user, passwd).Middleware)
  44. // metrics
  45. if svr.cfg.EnablePrometheus {
  46. subRouter.Handle("/metrics", promhttp.Handler())
  47. }
  48. // api, see dashboard_api.go
  49. subRouter.HandleFunc("/api/serverinfo", svr.APIServerInfo).Methods("GET")
  50. subRouter.HandleFunc("/api/proxy/{type}", svr.APIProxyByType).Methods("GET")
  51. subRouter.HandleFunc("/api/proxy/{type}/{name}", svr.APIProxyByTypeAndName).Methods("GET")
  52. subRouter.HandleFunc("/api/traffic/{name}", svr.APIProxyTraffic).Methods("GET")
  53. // view
  54. subRouter.Handle("/favicon.ico", http.FileServer(assets.FileSystem)).Methods("GET")
  55. subRouter.PathPrefix("/static/").Handler(frpNet.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))).Methods("GET")
  56. subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  57. http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
  58. })
  59. server := &http.Server{
  60. Addr: address,
  61. Handler: router,
  62. ReadTimeout: httpServerReadTimeout,
  63. WriteTimeout: httpServerWriteTimeout,
  64. }
  65. if address == "" || address == ":" {
  66. address = ":http"
  67. }
  68. ln, err := net.Listen("tcp", address)
  69. if err != nil {
  70. return err
  71. }
  72. go server.Serve(ln)
  73. return
  74. }