1
0

admin.go 3.0 KB

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