frpc: support 'includes' in common section to include proxy configs in other files(#2421)

This commit is contained in:
fatedier
2021-06-02 00:39:05 +08:00
committed by GitHub
parent 9ae322cccf
commit c32a2ed140
4 changed files with 116 additions and 54 deletions

View File

@@ -15,11 +15,14 @@
package sub
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
@@ -179,24 +182,84 @@ func parseClientCommonCfgFromCmd() (cfg config.ClientCommonConf, err error) {
return
}
func runClient(cfgFilePath string) (err error) {
func runClient(cfgFilePath string) error {
cfg, pxyCfgs, visitorCfgs, err := parseConfig(cfgFilePath)
if err != nil {
return err
}
return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
}
func parseConfig(cfgFilePath string) (
cfg config.ClientCommonConf,
pxyCfgs map[string]config.ProxyConf,
visitorCfgs map[string]config.VisitorConf,
err error,
) {
var content []byte
content, err = config.GetRenderedConfFromFile(cfgFilePath)
if err != nil {
return err
return
}
configBuffer := bytes.NewBuffer(nil)
configBuffer.Write(content)
cfg, err := parseClientCommonCfg(CfgFileTypeIni, content)
// Parse common section.
cfg, err = parseClientCommonCfg(CfgFileTypeIni, content)
if err != nil {
return err
return
}
pxyCfgs, visitorCfgs, err := config.LoadAllProxyConfsFromIni(cfg.User, content, cfg.Start)
// Aggregate proxy configs from include files.
var buf []byte
buf, err = getIncludeContents(cfg.IncludeConfigFiles)
if err != nil {
return err
err = fmt.Errorf("getIncludeContents error: %v", err)
return
}
configBuffer.WriteString("\n")
configBuffer.Write(buf)
return startService(cfg, pxyCfgs, visitorCfgs, cfgFilePath)
// Parse all proxy and visitor configs.
pxyCfgs, visitorCfgs, err = config.LoadAllProxyConfsFromIni(cfg.User, configBuffer.Bytes(), cfg.Start)
if err != nil {
return
}
return
}
// getIncludeContents renders all configs from paths.
// files format can be a single file path or directory or regex path.
func getIncludeContents(paths []string) ([]byte, error) {
out := bytes.NewBuffer(nil)
for _, path := range paths {
absDir, err := filepath.Abs(filepath.Dir(path))
if err != nil {
return nil, err
}
if _, err := os.Stat(absDir); os.IsNotExist(err) {
return nil, err
}
files, err := ioutil.ReadDir(absDir)
if err != nil {
return nil, err
}
for _, fi := range files {
if fi.IsDir() {
continue
}
absFile := filepath.Join(absDir, fi.Name())
if matched, _ := filepath.Match(filepath.Join(absDir, filepath.Base(path)), absFile); matched {
tmpContent, err := config.GetRenderedConfFromFile(absFile)
if err != nil {
return nil, fmt.Errorf("render extra config %s error: %v", absFile, err)
}
out.Write(tmpContent)
out.WriteString("\n")
}
}
}
return out.Bytes(), nil
}
func startService(

View File

@@ -18,8 +18,6 @@ import (
"fmt"
"os"
"github.com/fatedier/frp/pkg/config"
"github.com/spf13/cobra"
)
@@ -31,18 +29,7 @@ var verifyCmd = &cobra.Command{
Use: "verify",
Short: "Verify that the configures is valid",
RunE: func(cmd *cobra.Command, args []string) error {
iniContent, err := config.GetRenderedConfFromFile(cfgFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cfg, err := parseClientCommonCfg(CfgFileTypeIni, iniContent)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
_, _, err = config.LoadAllProxyConfsFromIni(cfg.User, iniContent, cfg.Start)
_, _, _, err := parseConfig(cfgFile)
if err != nil {
fmt.Println(err)
os.Exit(1)