reload.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/ioutil"
  20. "net/http"
  21. "os"
  22. "strings"
  23. "github.com/spf13/cobra"
  24. "github.com/fatedier/frp/client"
  25. "github.com/fatedier/frp/g"
  26. "github.com/fatedier/frp/models/config"
  27. )
  28. func init() {
  29. rootCmd.AddCommand(reloadCmd)
  30. }
  31. var reloadCmd = &cobra.Command{
  32. Use: "reload",
  33. Short: "Hot-Reload frpc configuration",
  34. RunE: func(cmd *cobra.Command, args []string) error {
  35. iniContent, err := config.GetRenderedConfFromFile(cfgFile)
  36. if err != nil {
  37. fmt.Println(err)
  38. os.Exit(1)
  39. }
  40. err = parseClientCommonCfg(CfgFileTypeIni, iniContent)
  41. if err != nil {
  42. fmt.Println(err)
  43. os.Exit(1)
  44. }
  45. err = reload()
  46. if err != nil {
  47. fmt.Printf("frpc reload error: %v\n", err)
  48. os.Exit(1)
  49. }
  50. fmt.Printf("reload success\n")
  51. return nil
  52. },
  53. }
  54. func reload() error {
  55. if g.GlbClientCfg.AdminPort == 0 {
  56. return fmt.Errorf("admin_port shoud be set if you want to use reload feature")
  57. }
  58. req, err := http.NewRequest("GET", "http://"+
  59. g.GlbClientCfg.AdminAddr+":"+fmt.Sprintf("%d", g.GlbClientCfg.AdminPort)+"/api/reload", nil)
  60. if err != nil {
  61. return err
  62. }
  63. authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(g.GlbClientCfg.AdminUser+":"+
  64. g.GlbClientCfg.AdminPwd))
  65. req.Header.Add("Authorization", authStr)
  66. resp, err := http.DefaultClient.Do(req)
  67. if err != nil {
  68. return err
  69. } else {
  70. if resp.StatusCode != 200 {
  71. return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
  72. }
  73. defer resp.Body.Close()
  74. body, err := ioutil.ReadAll(resp.Body)
  75. if err != nil {
  76. return err
  77. }
  78. res := &client.GeneralResponse{}
  79. err = json.Unmarshal(body, &res)
  80. if err != nil {
  81. return fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body)))
  82. } else if res.Code != 0 {
  83. return fmt.Errorf(res.Msg)
  84. }
  85. }
  86. return nil
  87. }