2018-04-10 17:46:49 +08:00
|
|
|
// Copyright 2018 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package sub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2021-09-29 10:33:57 +08:00
|
|
|
"io"
|
2018-04-10 17:46:49 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/spf13/cobra"
|
2022-08-29 01:02:53 +08:00
|
|
|
|
|
|
|
"github.com/fatedier/frp/pkg/config"
|
2023-09-06 10:18:02 +08:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2018-04-10 17:46:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(reloadCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var reloadCmd = &cobra.Command{
|
|
|
|
Use: "reload",
|
|
|
|
Short: "Hot-Reload frpc configuration",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-06 10:18:02 +08:00
|
|
|
cfg, _, _, _, err := config.LoadClientConfig(cfgFile)
|
2019-01-15 23:41:06 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-06-02 23:54:22 +08:00
|
|
|
err = reload(cfg)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("frpc reload error: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("reload success\n")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func reload(clientCfg *v1.ClientCommonConfig) error {
|
|
|
|
if clientCfg.WebServer.Port == 0 {
|
|
|
|
return fmt.Errorf("the port of web server shoud be set if you want to use reload feature")
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "http://"+
|
2023-09-06 10:18:02 +08:00
|
|
|
clientCfg.WebServer.Addr+":"+
|
|
|
|
fmt.Sprintf("%d", clientCfg.WebServer.Port)+"/api/reload", nil)
|
2018-04-10 17:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
authStr := "Basic " + base64.StdEncoding.EncodeToString(
|
|
|
|
[]byte(clientCfg.WebServer.User+":"+clientCfg.WebServer.Password))
|
2018-04-10 17:46:49 +08:00
|
|
|
|
|
|
|
req.Header.Add("Authorization", authStr)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-30 12:11:59 +08:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-02-11 12:15:31 +08:00
|
|
|
|
2019-05-30 12:11:59 +08:00
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-29 10:33:57 +08:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2019-05-30 12:11:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
return fmt.Errorf("code [%d], %s", resp.StatusCode, strings.TrimSpace(string(body)))
|
2018-04-10 17:46:49 +08:00
|
|
|
}
|