From d4ee2faab3dfa8c5d16b713f4aa7e29b7d0ba5ed Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 15 Aug 2022 17:43:23 +0800 Subject: [PATCH] feat: copy config file --- src/load.go | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/load.go b/src/load.go index 47e08f6..be25c39 100644 --- a/src/load.go +++ b/src/load.go @@ -2,6 +2,8 @@ package main import ( log "github.com/sirupsen/logrus" + "io" + "io/ioutil" "os" "strings" ) @@ -57,11 +59,45 @@ func createFolder(folderPath string) { log.Debugf("Loading folder -> %s", folderPath) err := os.MkdirAll(folderPath, 0755) if err != nil { - log.Errorf("Create folder `%s` failed", folderPath) + log.Errorf("Failed to create folder -> %s", folderPath) panic("Create folder failed") } } +func listFolder(folderPath string, suffix string) []string { + var fileList []string + files, err := ioutil.ReadDir(folderPath) + if err != nil { + log.Errorf("Failed to list folder -> %s", folderPath) + panic("List folder failed") + } + for _, file := range files { + if strings.HasSuffix(file.Name(), suffix) { + fileList = append(fileList, file.Name()) + } + } + return fileList +} + +func copyFile(source string, target string) { + log.Infof("Copy file `%s` => `%s`", source, target) + srcFile, err := os.Open(source) + if err != nil { + log.Errorf("Failed to open file -> %s", source) + panic("Open file failed") + } + dstFile, err := os.OpenFile(target, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) + if err != nil { + log.Errorf("Failed to open file -> %s", target) + panic("Open file failed") + } + _, err = io.Copy(dstFile, srcFile) + if err != nil { + log.Errorf("Failed to copy from `%s` to `%s`", source, target) + panic("Copy file failed") + } +} + func saveConfig(configDir string, caption string, content string, overwrite bool) { filePath := configDir + "/" + caption + ".json" if !overwrite && isFileExist(filePath) { // file exist and don't overwrite @@ -90,6 +126,10 @@ func loadProxy(configDir string, exposeDir string) { // TODO: load inbounds config - // TODO: copy exposeDir/config/*.json -> configDir (exclude log and inbounds) - + for _, configFile := range listFolder(exposeDir+"/config", ".json") { + if configFile == "log.json" || configFile == "inbounds.json" { + log.Warningf("Config file `%s` will be overrided", configFile) + } + copyFile(exposeDir+"/config/"+configFile, configDir+"/"+configFile) + } }