frpc: support --reload command

This commit is contained in:
fatedier 2017-07-13 02:30:25 +08:00
parent d246400a71
commit ee9ddf52cd
1 changed files with 46 additions and 0 deletions

View File

@ -15,7 +15,11 @@
package main package main
import ( import (
"encoding/base64"
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http"
"os" "os"
"os/signal" "os/signal"
"strconv" "strconv"
@ -40,6 +44,7 @@ var usage string = `frpc is the client of frp
Usage: Usage:
frpc [-c config_file] [-L log_file] [--log-level=<log_level>] [--server-addr=<server_addr>] frpc [-c config_file] [-L log_file] [--log-level=<log_level>] [--server-addr=<server_addr>]
frpc [-c config_file] --reload
frpc -h | --help frpc -h | --help
frpc -v | --version frpc -v | --version
@ -48,6 +53,7 @@ Options:
-L log_file set output log file, including console -L log_file set output log file, including console
--log-level=<log_level> set log level: debug, info, warn, error --log-level=<log_level> set log level: debug, info, warn, error
--server-addr=<server_addr> addr which frps is listening for, example: 0.0.0.0:7000 --server-addr=<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 -h --help show this screen
-v --version show version -v --version show version
` `
@ -75,6 +81,46 @@ func main() {
} }
config.ClientCommonCfg.ConfigFile = confFile 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"] != nil {
if args["-L"].(string) == "console" { if args["-L"].(string) == "console" {
config.ClientCommonCfg.LogWay = "console" config.ClientCommonCfg.LogWay = "console"