forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 30
/
external.go
54 lines (43 loc) · 1.11 KB
/
external.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package external
import (
"io"
"os"
"strings"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/platform/ctlcmd"
"v2ray.com/core/main/confloader"
)
//go:generate errorgen
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
}