1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-06-10 21:19:10 -04:00

revert back to multiconfig

This commit is contained in:
vcptr
2020-01-03 09:26:48 +08:00
committed by kslr
parent 5d13ec9196
commit 0d71d3dcf8
19 changed files with 557 additions and 83 deletions

View File

@@ -5,15 +5,30 @@ import (
"os"
)
type configFileLoader func(string) (io.ReadCloser, error)
type configFileLoader func(string) (io.Reader, error)
type extconfigLoader func([]string) (io.Reader, error)
var (
EffectiveConfigFileLoader configFileLoader
EffectiveExtConfigLoader extconfigLoader
)
func LoadConfig(file string) (io.ReadCloser, error) {
// LoadConfig reads from a path/url/stdin
// actual work is in external module
func LoadConfig(file string) (io.Reader, error) {
if EffectiveConfigFileLoader == nil {
newError("external config module not loaded, reading from stdin").AtInfo().WriteToLog()
return os.Stdin, nil
}
return EffectiveConfigFileLoader(file)
}
// LoadExtConfig calls v2ctl to handle multiple config
// the actual work also in external module
func LoadExtConfig(files []string) (io.Reader, error) {
if EffectiveExtConfigLoader == nil {
return nil, newError("external config module not loaded").AtError()
}
return EffectiveExtConfigLoader(files)
}

View File

@@ -0,0 +1,9 @@
package confloader
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@@ -1,48 +1,86 @@
package external
//go:generate errorgen
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/platform/ctlcmd"
"v2ray.com/core/main/confloader"
)
//go:generate errorgen
func ConfigLoader(arg string) (out io.Reader, err error) {
func loadConfigFile(configFile string) (io.ReadCloser, error) {
if configFile == "stdin:" {
return os.Stdin, nil
var data []byte
if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
data, err = FetchHTTPContent(arg)
} else if arg == "stdin:" {
data, err = ioutil.ReadAll(os.Stdin)
} else {
data, err = ioutil.ReadFile(arg)
}
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
}
fixedFile := os.ExpandEnv(configFile)
file, err := os.Open(fixedFile)
if err != nil {
return nil, newError("config file not readable").Base(err)
return
}
defer file.Close()
out = bytes.NewBuffer(data)
return
}
content, err := buf.ReadFrom(file)
func FetchHTTPContent(target string) ([]byte, error) {
parsedTarget, err := url.Parse(target)
if err != nil {
return nil, newError("failed to load config file: ", fixedFile).Base(err).AtWarning()
return nil, newError("invalid URL: ", target).Base(err)
}
return &buf.MultiBufferContainer{
MultiBuffer: content,
}, nil
if s := strings.ToLower(parsedTarget.Scheme); s != "http" && s != "https" {
return nil, newError("invalid scheme: ", parsedTarget.Scheme)
}
client := &http.Client{
Timeout: 30 * time.Second,
}
resp, err := client.Do(&http.Request{
Method: "GET",
URL: parsedTarget,
Close: true,
})
if err != nil {
return nil, newError("failed to dial to ", target).Base(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, newError("unexpected HTTP status code: ", resp.StatusCode)
}
content, err := buf.ReadAllToBytes(resp.Body)
if err != nil {
return nil, newError("failed to read HTTP response").Base(err)
}
return content, nil
}
func ExtConfigLoader(files []string) (io.Reader, error) {
buf, err := ctlcmd.Run(append([]string{"config"}, files...), os.Stdin)
if err != nil {
return nil, err
}
return strings.NewReader(buf.String()), nil
}
func init() {
confloader.EffectiveConfigFileLoader = loadConfigFile
confloader.EffectiveConfigFileLoader = ConfigLoader
confloader.EffectiveExtConfigLoader = ExtConfigLoader
}