Browse Source

feat: copy config file

v1.x.x
dnomd343 2 years ago
parent
commit
d4ee2faab3
  1. 46
      src/load.go

46
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)
}
}

Loading…
Cancel
Save