1
0

dashboard.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2016 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. "encoding/base64"
  17. "fmt"
  18. "net"
  19. "net/http"
  20. "strings"
  21. "time"
  22. "github.com/fatedier/frp/src/assets"
  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. mux := http.NewServeMux()
  31. // api, see dashboard_api.go
  32. mux.HandleFunc("/api/reload", apiReload)
  33. mux.HandleFunc("/api/proxies", apiProxies)
  34. // view, see dashboard_view.go
  35. mux.Handle("/favicon.ico", http.FileServer(assets.FileSystem))
  36. mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))
  37. mux.HandleFunc("/", use(viewDashboard, basicAuth))
  38. address := fmt.Sprintf("%s:%d", addr, port)
  39. server := &http.Server{
  40. Addr: address,
  41. Handler: mux,
  42. ReadTimeout: httpServerReadTimeout,
  43. WriteTimeout: httpServerWriteTimeout,
  44. }
  45. if address == "" {
  46. address = ":http"
  47. }
  48. ln, err := net.Listen("tcp", address)
  49. if err != nil {
  50. return err
  51. }
  52. go server.Serve(ln)
  53. return
  54. }
  55. func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
  56. for _, m := range middleware {
  57. h = m(h)
  58. }
  59. return h
  60. }
  61. func basicAuth(h http.HandlerFunc) http.HandlerFunc {
  62. return func(w http.ResponseWriter, r *http.Request) {
  63. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  64. s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
  65. if len(s) != 2 {
  66. http.Error(w, "Not authorized", 401)
  67. return
  68. }
  69. b, err := base64.StdEncoding.DecodeString(s[1])
  70. if err != nil {
  71. http.Error(w, err.Error(), 401)
  72. return
  73. }
  74. pair := strings.SplitN(string(b), ":", 2)
  75. if len(pair) != 2 {
  76. http.Error(w, "Not authorized", 401)
  77. return
  78. }
  79. if pair[0] != DashboardUsername || pair[1] != DashboardPassword {
  80. http.Error(w, "Not authorized", 401)
  81. return
  82. }
  83. h.ServeHTTP(w, r)
  84. }
  85. }