dashboard.go 2.4 KB

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