1
0

status.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2018 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 sub
  15. import (
  16. "encoding/base64"
  17. "encoding/json"
  18. "fmt"
  19. "io"
  20. "net/http"
  21. "os"
  22. "strings"
  23. "github.com/rodaine/table"
  24. "github.com/spf13/cobra"
  25. "github.com/fatedier/frp/client"
  26. "github.com/fatedier/frp/pkg/config"
  27. )
  28. func init() {
  29. rootCmd.AddCommand(statusCmd)
  30. }
  31. var statusCmd = &cobra.Command{
  32. Use: "status",
  33. Short: "Overview of all proxies status",
  34. RunE: func(cmd *cobra.Command, args []string) error {
  35. cfg, _, _, err := config.ParseClientConfig(cfgFile)
  36. if err != nil {
  37. fmt.Println(err)
  38. os.Exit(1)
  39. }
  40. if err = status(cfg); err != nil {
  41. fmt.Printf("frpc get status error: %v\n", err)
  42. os.Exit(1)
  43. }
  44. return nil
  45. },
  46. }
  47. func status(clientCfg config.ClientCommonConf) error {
  48. if clientCfg.AdminPort == 0 {
  49. return fmt.Errorf("admin_port shoud be set if you want to get proxy status")
  50. }
  51. req, err := http.NewRequest("GET", "http://"+
  52. clientCfg.AdminAddr+":"+fmt.Sprintf("%d", clientCfg.AdminPort)+"/api/status", nil)
  53. if err != nil {
  54. return err
  55. }
  56. authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(clientCfg.AdminUser+":"+
  57. clientCfg.AdminPwd))
  58. req.Header.Add("Authorization", authStr)
  59. resp, err := http.DefaultClient.Do(req)
  60. if err != nil {
  61. return err
  62. }
  63. defer resp.Body.Close()
  64. if resp.StatusCode != 200 {
  65. return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
  66. }
  67. body, err := io.ReadAll(resp.Body)
  68. if err != nil {
  69. return err
  70. }
  71. res := make(client.StatusResp)
  72. err = json.Unmarshal(body, &res)
  73. if err != nil {
  74. return fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body)))
  75. }
  76. fmt.Println("Proxy Status...")
  77. types := []string{"tcp", "udp", "tcpmux", "http", "https", "stcp", "sudp", "xtcp"}
  78. for _, pxyType := range types {
  79. arrs := res[pxyType]
  80. if len(arrs) == 0 {
  81. continue
  82. }
  83. fmt.Println(strings.ToUpper(pxyType))
  84. tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
  85. for _, ps := range arrs {
  86. tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
  87. }
  88. tbl.Print()
  89. fmt.Println("")
  90. }
  91. return nil
  92. }