123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package sub
- import (
- "encoding/base64"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "net/http"
- "os"
- "strings"
- "github.com/spf13/cobra"
- "github.com/fatedier/frp/client"
- "github.com/fatedier/frp/g"
- "github.com/fatedier/frp/models/config"
- )
- func init() {
- rootCmd.AddCommand(reloadCmd)
- }
- var reloadCmd = &cobra.Command{
- Use: "reload",
- Short: "Hot-Reload frpc configuration",
- RunE: func(cmd *cobra.Command, args []string) error {
- iniContent, err := config.GetRenderedConfFromFile(cfgFile)
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- err = parseClientCommonCfg(CfgFileTypeIni, iniContent)
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- err = reload()
- if err != nil {
- fmt.Printf("frpc reload error: %v\n", err)
- os.Exit(1)
- }
- fmt.Printf("reload success\n")
- return nil
- },
- }
- func reload() error {
- if g.GlbClientCfg.AdminPort == 0 {
- return fmt.Errorf("admin_port shoud be set if you want to use reload feature")
- }
- req, err := http.NewRequest("GET", "http://"+
- g.GlbClientCfg.AdminAddr+":"+fmt.Sprintf("%d", g.GlbClientCfg.AdminPort)+"/api/reload", nil)
- if err != nil {
- return err
- }
- authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(g.GlbClientCfg.AdminUser+":"+
- g.GlbClientCfg.AdminPwd))
- req.Header.Add("Authorization", authStr)
- resp, err := http.DefaultClient.Do(req)
- if err != nil {
- return err
- } else {
- if resp.StatusCode != 200 {
- return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return err
- }
- res := &client.GeneralResponse{}
- err = json.Unmarshal(body, &res)
- if err != nil {
- return fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body)))
- } else if res.Code != 0 {
- return fmt.Errorf(res.Msg)
- }
- }
- return nil
- }
|