1
0

dashboard.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. "compress/gzip"
  17. "fmt"
  18. "io"
  19. "net"
  20. "net/http"
  21. "strings"
  22. "time"
  23. "github.com/fatedier/frp/assets"
  24. "github.com/fatedier/frp/models/config"
  25. "github.com/julienschmidt/httprouter"
  26. )
  27. var (
  28. httpServerReadTimeout = 10 * time.Second
  29. httpServerWriteTimeout = 10 * time.Second
  30. )
  31. func RunDashboardServer(addr string, port int64) (err error) {
  32. // url router
  33. router := httprouter.New()
  34. // api, see dashboard_api.go
  35. router.GET("/api/serverinfo", httprouterBasicAuth(apiServerInfo))
  36. router.GET("/api/proxy/tcp", httprouterBasicAuth(apiProxyTcp))
  37. router.GET("/api/proxy/udp", httprouterBasicAuth(apiProxyUdp))
  38. router.GET("/api/proxy/http", httprouterBasicAuth(apiProxyHttp))
  39. router.GET("/api/proxy/https", httprouterBasicAuth(apiProxyHttps))
  40. router.GET("/api/proxy/traffic/:name", httprouterBasicAuth(apiProxyTraffic))
  41. // view
  42. router.Handler("GET", "/favicon.ico", http.FileServer(assets.FileSystem))
  43. router.Handler("GET", "/static/*filepath", MakeGzipHandler(basicAuthWraper(http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))))
  44. router.HandlerFunc("GET", "/", basicAuth(func(w http.ResponseWriter, r *http.Request) {
  45. http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
  46. }))
  47. address := fmt.Sprintf("%s:%d", addr, port)
  48. server := &http.Server{
  49. Addr: address,
  50. Handler: router,
  51. ReadTimeout: httpServerReadTimeout,
  52. WriteTimeout: httpServerWriteTimeout,
  53. }
  54. if address == "" {
  55. address = ":http"
  56. }
  57. ln, err := net.Listen("tcp", address)
  58. if err != nil {
  59. return err
  60. }
  61. go server.Serve(ln)
  62. return
  63. }
  64. func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
  65. for _, m := range middleware {
  66. h = m(h)
  67. }
  68. return h
  69. }
  70. type AuthWraper struct {
  71. h http.Handler
  72. user string
  73. passwd string
  74. }
  75. func (aw *AuthWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  76. user, passwd, hasAuth := r.BasicAuth()
  77. if (aw.user == "" && aw.passwd == "") || (hasAuth && user == aw.user && passwd == aw.passwd) {
  78. aw.h.ServeHTTP(w, r)
  79. } else {
  80. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  81. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  82. }
  83. }
  84. func basicAuthWraper(h http.Handler) http.Handler {
  85. return &AuthWraper{
  86. h: h,
  87. user: config.ServerCommonCfg.DashboardUser,
  88. passwd: config.ServerCommonCfg.DashboardPwd,
  89. }
  90. }
  91. func basicAuth(h http.HandlerFunc) http.HandlerFunc {
  92. return func(w http.ResponseWriter, r *http.Request) {
  93. user, passwd, hasAuth := r.BasicAuth()
  94. if (config.ServerCommonCfg.DashboardUser == "" && config.ServerCommonCfg.DashboardPwd == "") ||
  95. (hasAuth && user == config.ServerCommonCfg.DashboardUser && passwd == config.ServerCommonCfg.DashboardPwd) {
  96. h.ServeHTTP(w, r)
  97. } else {
  98. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  99. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  100. }
  101. }
  102. }
  103. func httprouterBasicAuth(h httprouter.Handle) httprouter.Handle {
  104. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  105. user, passwd, hasAuth := r.BasicAuth()
  106. if (config.ServerCommonCfg.DashboardUser == "" && config.ServerCommonCfg.DashboardPwd == "") ||
  107. (hasAuth && user == config.ServerCommonCfg.DashboardUser && passwd == config.ServerCommonCfg.DashboardPwd) {
  108. h(w, r, ps)
  109. } else {
  110. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  111. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  112. }
  113. }
  114. }
  115. type GzipWraper struct {
  116. h http.Handler
  117. }
  118. func (gw *GzipWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  119. if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  120. gw.h.ServeHTTP(w, r)
  121. return
  122. }
  123. w.Header().Set("Content-Encoding", "gzip")
  124. gz := gzip.NewWriter(w)
  125. defer gz.Close()
  126. gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
  127. gw.h.ServeHTTP(gzr, r)
  128. }
  129. func MakeGzipHandler(h http.Handler) http.Handler {
  130. return &GzipWraper{
  131. h: h,
  132. }
  133. }
  134. type gzipResponseWriter struct {
  135. io.Writer
  136. http.ResponseWriter
  137. }
  138. func (w gzipResponseWriter) Write(b []byte) (int, error) {
  139. return w.Writer.Write(b)
  140. }