mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -05:00
conf loader as component
This commit is contained in:
parent
b7f2f30244
commit
286e9a3835
21
main/confloader/confloader.go
Normal file
21
main/confloader/confloader.go
Normal 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)
|
||||
}
|
5
main/confloader/errors.generated.go
Normal file
5
main/confloader/errors.generated.go
Normal 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") }
|
5
main/confloader/external/errors.generated.go
vendored
Normal file
5
main/confloader/external/errors.generated.go
vendored
Normal 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
54
main/confloader/external/external.go
vendored
Normal 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
|
||||
}
|
@ -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"
|
||||
)
|
||||
|
25
main/main.go
25
main/main.go
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user