1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-09 09:20:45 +00:00

conf loader as component

This commit is contained in:
Darien Raymond 2018-04-09 11:43:13 +02:00
parent b7f2f30244
commit 286e9a3835
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
6 changed files with 94 additions and 19 deletions

View File

@ -0,0 +1,21 @@
package confloader
import (
"io"
"os"
)
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg confloader -path Main,ConfLoader
type configFileLoader func(string) (io.ReadCloser, error)
var (
EffectiveConfigFileLoader configFileLoader
)
func LoadConfig(file string) (io.ReadCloser, error) {
if EffectiveConfigFileLoader == nil {
return os.Stdin, nil
}
return EffectiveConfigFileLoader(file)
}

View File

@ -0,0 +1,5 @@
package confloader
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Main", "ConfLoader") }

View File

@ -0,0 +1,5 @@
package external
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Main", "ConfLoader", "External") }

54
main/confloader/external/external.go vendored Normal file
View File

@ -0,0 +1,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 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
}

View File

@ -48,4 +48,7 @@ import (
// JSON config format
_ "v2ray.com/core/main/json"
// Load config from file or http(s)
_ "v2ray.com/core/main/confloader/external"
)

View File

@ -5,7 +5,6 @@ package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
@ -14,7 +13,7 @@ import (
"v2ray.com/core"
"v2ray.com/core/common/platform"
"v2ray.com/core/common/platform/ctlcmd"
"v2ray.com/core/main/confloader"
_ "v2ray.com/core/main/distro/all"
)
@ -61,24 +60,12 @@ func GetConfigFormat() string {
func startV2Ray() (core.Server, error) {
configFile := getConfigFilePath()
var configInput io.Reader
if configFile == "stdin:" {
configInput = os.Stdin
} else if strings.HasPrefix(configFile, "http://") || strings.HasPrefix(configFile, "https://") {
content, err := ctlcmd.Run([]string{"fetch", configFile}, nil)
if err != nil {
return nil, err
}
configInput = &content
} else {
fixedFile := os.ExpandEnv(configFile)
file, err := os.Open(fixedFile)
if err != nil {
return nil, newError("config file not readable").Base(err)
}
defer file.Close()
configInput = file
configInput, err := confloader.LoadConfig(configFile)
if err != nil {
return nil, newError("failed to load config: ", configFile).Base(err)
}
defer configInput.Close()
config, err := core.LoadConfig(GetConfigFormat(), configFile, configInput)
if err != nil {
return nil, newError("failed to read config file: ", configFile).Base(err)