dashboard_api.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/json"
  17. "fmt"
  18. "net/http"
  19. "github.com/fatedier/frp/src/models/metric"
  20. "github.com/fatedier/frp/src/utils/log"
  21. )
  22. type GeneralResponse struct {
  23. Code int64 `json:"code"`
  24. Msg string `json:"msg"`
  25. }
  26. func apiReload(w http.ResponseWriter, r *http.Request) {
  27. var buf []byte
  28. res := &GeneralResponse{}
  29. defer func() {
  30. log.Info("Http response [/api/reload]: %s", string(buf))
  31. }()
  32. log.Info("Http request: [/api/reload]")
  33. err := ReloadConf(ConfigFile)
  34. if err != nil {
  35. res.Code = 2
  36. res.Msg = fmt.Sprintf("%v", err)
  37. log.Error("frps reload error: %v", err)
  38. }
  39. buf, _ = json.Marshal(res)
  40. w.Write(buf)
  41. }
  42. type ProxiesResponse struct {
  43. Code int64 `json:"code"`
  44. Msg string `json:"msg"`
  45. Proxies []*metric.ServerMetric `json:"proxies"`
  46. }
  47. func apiProxies(w http.ResponseWriter, r *http.Request) {
  48. var buf []byte
  49. res := &ProxiesResponse{}
  50. defer func() {
  51. log.Info("Http response [/api/proxies]: code [%d]", res.Code)
  52. }()
  53. log.Info("Http request: [/api/proxies]")
  54. res.Proxies = metric.GetAllProxyMetrics()
  55. buf, _ = json.Marshal(res)
  56. w.Write(buf)
  57. }