status.go 2.7 KB

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