dashboard.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/traffic/:name", apiProxyTraffic)
  39. // view
  40. router.Handler("GET", "/favicon.ico", http.FileServer(assets.FileSystem))
  41. router.Handler("GET", "/static/*filepath", http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))
  42. router.HandlerFunc("GET", "/", func(w http.ResponseWriter, r *http.Request) {
  43. http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
  44. })
  45. address := fmt.Sprintf("%s:%d", addr, port)
  46. server := &http.Server{
  47. Addr: address,
  48. Handler: router,
  49. ReadTimeout: httpServerReadTimeout,
  50. WriteTimeout: httpServerWriteTimeout,
  51. }
  52. if address == "" {
  53. address = ":http"
  54. }
  55. ln, err := net.Listen("tcp", address)
  56. if err != nil {
  57. return err
  58. }
  59. go server.Serve(ln)
  60. return
  61. }
  62. func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
  63. for _, m := range middleware {
  64. h = m(h)
  65. }
  66. return h
  67. }
  68. func basicAuth(h http.HandlerFunc) http.HandlerFunc {
  69. return func(w http.ResponseWriter, r *http.Request) {
  70. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  71. username, passwd, ok := r.BasicAuth()
  72. if !ok {
  73. http.Error(w, "Not authorized", 401)
  74. return
  75. }
  76. if username != config.ServerCommonCfg.DashboardUser || passwd != config.ServerCommonCfg.DashboardPwd {
  77. http.Error(w, "Not authorized", 401)
  78. return
  79. }
  80. h.ServeHTTP(w, r)
  81. }
  82. }