From ee9ddf52cdbe350f3c08b60ede2faf6548ba2fac Mon Sep 17 00:00:00 2001 From: fatedier Date: Thu, 13 Jul 2017 02:30:25 +0800 Subject: [PATCH] frpc: support --reload command --- cmd/frpc/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/cmd/frpc/main.go b/cmd/frpc/main.go index 986592ff..5aa6bec8 100644 --- a/cmd/frpc/main.go +++ b/cmd/frpc/main.go @@ -15,7 +15,11 @@ package main import ( + "encoding/base64" + "encoding/json" "fmt" + "io/ioutil" + "net/http" "os" "os/signal" "strconv" @@ -40,6 +44,7 @@ var usage string = `frpc is the client of frp Usage: frpc [-c config_file] [-L log_file] [--log-level=] [--server-addr=] + frpc [-c config_file] --reload frpc -h | --help frpc -v | --version @@ -48,6 +53,7 @@ Options: -L log_file set output log file, including console --log-level= set log level: debug, info, warn, error --server-addr= addr which frps is listening for, example: 0.0.0.0:7000 + --reload reload configure file without program exit -h --help show this screen -v --version show version ` @@ -75,6 +81,46 @@ func main() { } config.ClientCommonCfg.ConfigFile = confFile + // check if reload command + if args["--reload"] != nil { + if args["--reload"].(bool) { + req, err := http.NewRequest("GET", "http://"+ + config.ClientCommonCfg.AdminAddr+":"+fmt.Sprintf("%d", config.ClientCommonCfg.AdminPort)+"/api/reload", nil) + if err != nil { + fmt.Printf("frps reload error: %v\n", err) + os.Exit(1) + } + + authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(config.ClientCommonCfg.AdminUser+":"+ + config.ClientCommonCfg.AdminPwd)) + + req.Header.Add("Authorization", authStr) + resp, err := http.DefaultClient.Do(req) + if err != nil { + fmt.Printf("frpc reload error: %v\n", err) + os.Exit(1) + } else { + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Printf("frpc reload error: %v\n", err) + os.Exit(1) + } + res := &client.GeneralResponse{} + err = json.Unmarshal(body, &res) + if err != nil { + fmt.Printf("http response error: %s\n", strings.TrimSpace(string(body))) + os.Exit(1) + } else if res.Code != 0 { + fmt.Printf("reload error: %s\n", res.Msg) + os.Exit(1) + } + fmt.Printf("reload success\n") + os.Exit(0) + } + } + } + if args["-L"] != nil { if args["-L"].(string) == "console" { config.ClientCommonCfg.LogWay = "console"