admin.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/models/config"
  21. frpNet "github.com/fatedier/frp/utils/net"
  22. "github.com/julienschmidt/httprouter"
  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 := httprouter.New()
  31. user, passwd := config.ClientCommonCfg.AdminUser, config.ClientCommonCfg.AdminPwd
  32. // api, see dashboard_api.go
  33. router.GET("/api/reload", frpNet.HttprouterBasicAuth(svr.apiReload, user, passwd))
  34. router.GET("/api/status", frpNet.HttprouterBasicAuth(svr.apiStatus, user, passwd))
  35. address := fmt.Sprintf("%s:%d", addr, port)
  36. server := &http.Server{
  37. Addr: address,
  38. Handler: router,
  39. ReadTimeout: httpServerReadTimeout,
  40. WriteTimeout: httpServerWriteTimeout,
  41. }
  42. if address == "" {
  43. address = ":http"
  44. }
  45. ln, err := net.Listen("tcp", address)
  46. if err != nil {
  47. return err
  48. }
  49. go server.Serve(ln)
  50. return
  51. }