1
0

dashboard.go 3.0 KB

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