dashboard.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. router.GET("/api/serverinfo", httprouterBasicAuth(apiServerInfo))
  33. router.GET("/api/proxy/tcp", httprouterBasicAuth(apiProxyTcp))
  34. router.GET("/api/proxy/udp", httprouterBasicAuth(apiProxyUdp))
  35. router.GET("/api/proxy/http", httprouterBasicAuth(apiProxyHttp))
  36. router.GET("/api/proxy/https", httprouterBasicAuth(apiProxyHttps))
  37. router.GET("/api/proxy/traffic/:name", httprouterBasicAuth(apiProxyTraffic))
  38. // view
  39. router.Handler("GET", "/favicon.ico", http.FileServer(assets.FileSystem))
  40. router.Handler("GET", "/static/*filepath", basicAuthWraper(http.StripPrefix("/static/", http.FileServer(assets.FileSystem))))
  41. router.HandlerFunc("GET", "/", basicAuth(func(w http.ResponseWriter, r *http.Request) {
  42. http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
  43. }))
  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. type AuthWraper struct {
  68. h http.Handler
  69. user string
  70. passwd string
  71. }
  72. func (aw *AuthWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  73. user, passwd, hasAuth := r.BasicAuth()
  74. if hasAuth && user == aw.user || passwd == aw.passwd {
  75. aw.h.ServeHTTP(w, r)
  76. } else {
  77. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  78. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  79. }
  80. }
  81. func basicAuthWraper(h http.Handler) http.Handler {
  82. return &AuthWraper{
  83. h: h,
  84. user: config.ServerCommonCfg.DashboardUser,
  85. passwd: config.ServerCommonCfg.DashboardPwd,
  86. }
  87. }
  88. func basicAuth(h http.HandlerFunc) http.HandlerFunc {
  89. return func(w http.ResponseWriter, r *http.Request) {
  90. user, passwd, hasAuth := r.BasicAuth()
  91. if hasAuth && user == config.ServerCommonCfg.DashboardUser || passwd == config.ServerCommonCfg.DashboardPwd {
  92. h.ServeHTTP(w, r)
  93. } else {
  94. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  95. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  96. }
  97. }
  98. }
  99. func httprouterBasicAuth(h httprouter.Handle) httprouter.Handle {
  100. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  101. user, passwd, hasAuth := r.BasicAuth()
  102. if hasAuth && user == config.ServerCommonCfg.DashboardUser || passwd == config.ServerCommonCfg.DashboardPwd {
  103. h(w, r, ps)
  104. } else {
  105. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  106. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  107. }
  108. }
  109. }