dashboard.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "fmt"
  17. "net"
  18. "net/http"
  19. "time"
  20. "github.com/fatedier/frp/assets"
  21. "github.com/fatedier/frp/models/config"
  22. "github.com/julienschmidt/httprouter"
  23. )
  24. var (
  25. httpServerReadTimeout = 10 * time.Second
  26. httpServerWriteTimeout = 10 * time.Second
  27. )
  28. func RunDashboardServer(addr string, port int64) (err error) {
  29. // url router
  30. router := httprouter.New()
  31. // api, see dashboard_api.go
  32. //mux.HandleFunc("/api/reload", use(apiReload, basicAuth))
  33. router.GET("/api/serverinfo", apiServerInfo)
  34. router.GET("/api/proxy/tcp", apiProxyTcp)
  35. router.GET("/api/proxy/udp", apiProxyUdp)
  36. router.GET("/api/proxy/http", apiProxyHttp)
  37. router.GET("/api/proxy/https", apiProxyHttps)
  38. router.GET("/api/proxy/flow/:name", apiProxyFlow)
  39. // view, see dashboard_view.go
  40. //router.GET("/favicon.ico", http.FileServer(assets.FileSystem))
  41. router.Handler("GET", "/favicon.ico", http.FileServer(assets.FileSystem))
  42. router.Handler("GET", "/static", http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))
  43. //router.GET("/", use(viewDashboard, basicAuth))
  44. address := fmt.Sprintf("%s:%d", addr, port)
  45. server := &http.Server{
  46. Addr: address,
  47. Handler: router,
  48. ReadTimeout: httpServerReadTimeout,
  49. WriteTimeout: httpServerWriteTimeout,
  50. }
  51. if address == "" {
  52. address = ":http"
  53. }
  54. ln, err := net.Listen("tcp", address)
  55. if err != nil {
  56. return err
  57. }
  58. go server.Serve(ln)
  59. return
  60. }
  61. func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
  62. for _, m := range middleware {
  63. h = m(h)
  64. }
  65. return h
  66. }
  67. func basicAuth(h http.HandlerFunc) http.HandlerFunc {
  68. return func(w http.ResponseWriter, r *http.Request) {
  69. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  70. username, passwd, ok := r.BasicAuth()
  71. if !ok {
  72. http.Error(w, "Not authorized", 401)
  73. return
  74. }
  75. if username != config.ServerCommonCfg.DashboardUser || passwd != config.ServerCommonCfg.DashboardPwd {
  76. http.Error(w, "Not authorized", 401)
  77. return
  78. }
  79. h.ServeHTTP(w, r)
  80. }
  81. }