status.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/fatedier/frp/client"
  24. "github.com/fatedier/frp/pkg/config"
  25. "github.com/rodaine/table"
  26. "github.com/spf13/cobra"
  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 := &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. if len(res.TCP) > 0 {
  78. fmt.Println("TCP")
  79. tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
  80. for _, ps := range res.TCP {
  81. tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
  82. }
  83. tbl.Print()
  84. fmt.Println("")
  85. }
  86. if len(res.UDP) > 0 {
  87. fmt.Println("UDP")
  88. tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
  89. for _, ps := range res.UDP {
  90. tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
  91. }
  92. tbl.Print()
  93. fmt.Println("")
  94. }
  95. if len(res.HTTP) > 0 {
  96. fmt.Println("HTTP")
  97. tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
  98. for _, ps := range res.HTTP {
  99. tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
  100. }
  101. tbl.Print()
  102. fmt.Println("")
  103. }
  104. if len(res.HTTPS) > 0 {
  105. fmt.Println("HTTPS")
  106. tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
  107. for _, ps := range res.HTTPS {
  108. tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
  109. }
  110. tbl.Print()
  111. fmt.Println("")
  112. }
  113. if len(res.STCP) > 0 {
  114. fmt.Println("STCP")
  115. tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
  116. for _, ps := range res.STCP {
  117. tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
  118. }
  119. tbl.Print()
  120. fmt.Println("")
  121. }
  122. if len(res.XTCP) > 0 {
  123. fmt.Println("XTCP")
  124. tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
  125. for _, ps := range res.XTCP {
  126. tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
  127. }
  128. tbl.Print()
  129. fmt.Println("")
  130. }
  131. return nil
  132. }