1
0

dashboard.go 2.5 KB

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