admin.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 client
  15. import (
  16. "net"
  17. "net/http"
  18. "time"
  19. "github.com/fatedier/frp/assets"
  20. frpNet "github.com/fatedier/frp/pkg/util/net"
  21. "github.com/gorilla/mux"
  22. )
  23. var (
  24. httpServerReadTimeout = 10 * time.Second
  25. httpServerWriteTimeout = 10 * time.Second
  26. )
  27. func (svr *Service) RunAdminServer(address string) (err error) {
  28. // url router
  29. router := mux.NewRouter()
  30. user, passwd := svr.cfg.AdminUser, svr.cfg.AdminPwd
  31. router.Use(frpNet.NewHTTPAuthMiddleware(user, passwd).Middleware)
  32. // api, see dashboard_api.go
  33. router.HandleFunc("/api/reload", svr.apiReload).Methods("GET")
  34. router.HandleFunc("/api/status", svr.apiStatus).Methods("GET")
  35. router.HandleFunc("/api/config", svr.apiGetConfig).Methods("GET")
  36. router.HandleFunc("/api/config", svr.apiPutConfig).Methods("PUT")
  37. // view
  38. router.Handle("/favicon.ico", http.FileServer(assets.FileSystem)).Methods("GET")
  39. router.PathPrefix("/static/").Handler(frpNet.MakeHTTPGzipHandler(http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))).Methods("GET")
  40. router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  41. http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
  42. })
  43. server := &http.Server{
  44. Addr: address,
  45. Handler: router,
  46. ReadTimeout: httpServerReadTimeout,
  47. WriteTimeout: httpServerWriteTimeout,
  48. }
  49. if address == "" {
  50. address = ":http"
  51. }
  52. ln, err := net.Listen("tcp", address)
  53. if err != nil {
  54. return err
  55. }
  56. go server.Serve(ln)
  57. return
  58. }