admin.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "fmt"
  17. "net"
  18. "net/http"
  19. "time"
  20. "github.com/fatedier/frp/g"
  21. frpNet "github.com/fatedier/frp/utils/net"
  22. "github.com/gorilla/mux"
  23. )
  24. var (
  25. httpServerReadTimeout = 10 * time.Second
  26. httpServerWriteTimeout = 10 * time.Second
  27. )
  28. func (svr *Service) RunAdminServer(addr string, port int) (err error) {
  29. // url router
  30. router := mux.NewRouter()
  31. user, passwd := g.GlbClientCfg.AdminUser, g.GlbClientCfg.AdminPwd
  32. router.Use(frpNet.NewHttpAuthMiddleware(user, passwd).Middleware)
  33. // api, see dashboard_api.go
  34. router.HandleFunc("/api/reload", svr.apiReload).Methods("GET")
  35. router.HandleFunc("/api/status", svr.apiStatus).Methods("GET")
  36. address := fmt.Sprintf("%s:%d", addr, port)
  37. server := &http.Server{
  38. Addr: address,
  39. Handler: router,
  40. ReadTimeout: httpServerReadTimeout,
  41. WriteTimeout: httpServerWriteTimeout,
  42. }
  43. if address == "" {
  44. address = ":http"
  45. }
  46. ln, err := net.Listen("tcp", address)
  47. if err != nil {
  48. return err
  49. }
  50. go server.Serve(ln)
  51. return
  52. }