2019-12-15 20:33:41 -05:00
|
|
|
package external
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/platform/ctlcmd"
|
|
|
|
"v2ray.com/core/main/confloader"
|
|
|
|
)
|
|
|
|
|
2020-01-01 12:23:56 -05:00
|
|
|
//go:generate errorgen
|
2019-12-15 20:33:41 -05:00
|
|
|
|
2020-01-01 12:23:56 -05:00
|
|
|
func loadConfigFile(configFile string) (io.ReadCloser, error) {
|
|
|
|
if configFile == "stdin:" {
|
|
|
|
return os.Stdin, nil
|
2019-12-15 20:33:41 -05:00
|
|
|
}
|
|
|
|
|
2020-01-01 12:23:56 -05:00
|
|
|
if strings.HasPrefix(configFile, "http://") || strings.HasPrefix(configFile, "https://") {
|
|
|
|
content, err := ctlcmd.Run([]string{"fetch", configFile}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &buf.MultiBufferContainer{
|
|
|
|
MultiBuffer: content,
|
|
|
|
}, nil
|
2019-12-15 20:33:41 -05:00
|
|
|
}
|
|
|
|
|
2020-01-01 12:23:56 -05:00
|
|
|
fixedFile := os.ExpandEnv(configFile)
|
|
|
|
file, err := os.Open(fixedFile)
|
2019-12-15 20:33:41 -05:00
|
|
|
if err != nil {
|
2020-01-01 12:23:56 -05:00
|
|
|
return nil, newError("config file not readable").Base(err)
|
2019-12-15 20:33:41 -05:00
|
|
|
}
|
2020-01-01 12:23:56 -05:00
|
|
|
defer file.Close()
|
2019-12-15 20:33:41 -05:00
|
|
|
|
2020-01-01 12:23:56 -05:00
|
|
|
content, err := buf.ReadFrom(file)
|
2019-12-15 20:33:41 -05:00
|
|
|
if err != nil {
|
2020-01-01 12:23:56 -05:00
|
|
|
return nil, newError("failed to load config file: ", fixedFile).Base(err).AtWarning()
|
2019-12-15 20:33:41 -05:00
|
|
|
}
|
2020-01-01 12:23:56 -05:00
|
|
|
return &buf.MultiBufferContainer{
|
|
|
|
MultiBuffer: content,
|
|
|
|
}, nil
|
2019-12-15 20:33:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-01-01 12:23:56 -05:00
|
|
|
confloader.EffectiveConfigFileLoader = loadConfigFile
|
2019-12-15 20:33:41 -05:00
|
|
|
}
|