admin.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2023 The frp Authors
  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. "context"
  17. "fmt"
  18. "os"
  19. "strings"
  20. "time"
  21. "github.com/rodaine/table"
  22. "github.com/spf13/cobra"
  23. "github.com/fatedier/frp/pkg/config"
  24. v1 "github.com/fatedier/frp/pkg/config/v1"
  25. clientsdk "github.com/fatedier/frp/pkg/sdk/client"
  26. )
  27. var adminAPITimeout = 30 * time.Second
  28. func init() {
  29. commands := []struct {
  30. name string
  31. description string
  32. handler func(*v1.ClientCommonConfig) error
  33. }{
  34. {"reload", "Hot-Reload frpc configuration", ReloadHandler},
  35. {"status", "Overview of all proxies status", StatusHandler},
  36. {"stop", "Stop the running frpc", StopHandler},
  37. }
  38. for _, cmdConfig := range commands {
  39. cmd := NewAdminCommand(cmdConfig.name, cmdConfig.description, cmdConfig.handler)
  40. cmd.Flags().DurationVar(&adminAPITimeout, "api-timeout", adminAPITimeout, "Timeout for admin API calls")
  41. rootCmd.AddCommand(cmd)
  42. }
  43. }
  44. func NewAdminCommand(name, short string, handler func(*v1.ClientCommonConfig) error) *cobra.Command {
  45. return &cobra.Command{
  46. Use: name,
  47. Short: short,
  48. Run: func(cmd *cobra.Command, args []string) {
  49. cfg, _, _, _, err := config.LoadClientConfig(cfgFile, strictConfigMode)
  50. if err != nil {
  51. fmt.Println(err)
  52. os.Exit(1)
  53. }
  54. if cfg.WebServer.Port <= 0 {
  55. fmt.Println("web server port should be set if you want to use this feature")
  56. os.Exit(1)
  57. }
  58. if err := handler(cfg); err != nil {
  59. fmt.Println(err)
  60. os.Exit(1)
  61. }
  62. },
  63. }
  64. }
  65. func ReloadHandler(clientCfg *v1.ClientCommonConfig) error {
  66. client := clientsdk.New(clientCfg.WebServer.Addr, clientCfg.WebServer.Port)
  67. client.SetAuth(clientCfg.WebServer.User, clientCfg.WebServer.Password)
  68. ctx, cancel := context.WithTimeout(context.Background(), adminAPITimeout)
  69. defer cancel()
  70. if err := client.Reload(ctx, strictConfigMode); err != nil {
  71. return err
  72. }
  73. fmt.Println("reload success")
  74. return nil
  75. }
  76. func StatusHandler(clientCfg *v1.ClientCommonConfig) error {
  77. client := clientsdk.New(clientCfg.WebServer.Addr, clientCfg.WebServer.Port)
  78. client.SetAuth(clientCfg.WebServer.User, clientCfg.WebServer.Password)
  79. ctx, cancel := context.WithTimeout(context.Background(), adminAPITimeout)
  80. defer cancel()
  81. res, err := client.GetAllProxyStatus(ctx)
  82. if err != nil {
  83. return err
  84. }
  85. fmt.Printf("Proxy Status...\n\n")
  86. for _, typ := range proxyTypes {
  87. arrs := res[string(typ)]
  88. if len(arrs) == 0 {
  89. continue
  90. }
  91. fmt.Println(strings.ToUpper(string(typ)))
  92. tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error")
  93. for _, ps := range arrs {
  94. tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err)
  95. }
  96. tbl.Print()
  97. fmt.Println("")
  98. }
  99. return nil
  100. }
  101. func StopHandler(clientCfg *v1.ClientCommonConfig) error {
  102. client := clientsdk.New(clientCfg.WebServer.Addr, clientCfg.WebServer.Port)
  103. client.SetAuth(clientCfg.WebServer.User, clientCfg.WebServer.Password)
  104. ctx, cancel := context.WithTimeout(context.Background(), adminAPITimeout)
  105. defer cancel()
  106. if err := client.Stop(ctx); err != nil {
  107. return err
  108. }
  109. fmt.Println("stop success")
  110. return nil
  111. }