mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-17 18:06:15 -05:00
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package external
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
"v2ray.com/core/common/buf"
|
|
"v2ray.com/core/common/platform/ctlcmd"
|
|
"v2ray.com/core/main/confloader"
|
|
)
|
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg external -path Main,ConfLoader,External
|
|
|
|
type ClosableMultiBuffer struct {
|
|
buf.MultiBuffer
|
|
}
|
|
|
|
func (c *ClosableMultiBuffer) Close() error {
|
|
c.MultiBuffer.Release()
|
|
return nil
|
|
}
|
|
|
|
func loadConfigFile(configFile string) (io.ReadCloser, error) {
|
|
if configFile == "stdin:" {
|
|
return os.Stdin, nil
|
|
}
|
|
|
|
if strings.HasPrefix(configFile, "http://") || strings.HasPrefix(configFile, "https://") {
|
|
content, err := ctlcmd.Run([]string{"fetch", configFile}, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ClosableMultiBuffer{content}, nil
|
|
}
|
|
|
|
fixedFile := os.ExpandEnv(configFile)
|
|
file, err := os.Open(fixedFile)
|
|
if err != nil {
|
|
return nil, newError("config file not readable").Base(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
content, err := buf.ReadAllToMultiBuffer(file)
|
|
if err != nil {
|
|
return nil, newError("failed to load config file: ", fixedFile).Base(err).AtWarning()
|
|
}
|
|
return &ClosableMultiBuffer{content}, nil
|
|
|
|
}
|
|
|
|
func init() {
|
|
confloader.EffectiveConfigFileLoader = loadConfigFile
|
|
}
|