From 7b289d16cf2e00d07f40a759bcfb3d9726e7749c Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Sat, 14 Dec 2019 20:47:47 +0800 Subject: [PATCH 01/11] conf obj override method --- infra/conf/v2ray.go | 80 ++++++++++++++++++++++++++++++++++++++++ infra/conf/v2ray_test.go | 70 +++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/infra/conf/v2ray.go b/infra/conf/v2ray.go index 58c9c6dad..afe1fee6d 100644 --- a/infra/conf/v2ray.go +++ b/infra/conf/v2ray.go @@ -304,6 +304,86 @@ type Config struct { Reverse *ReverseConfig `json:"reverse"` } +func (c *Config) findInboundTag(tag string) int { + found := -1 + for idx, ib := range c.InboundConfigs { + if ib.Tag == tag { + found = idx + break + } + } + return found +} + +func (c *Config) findOutboundTag(tag string) int { + found := -1 + for idx, ob := range c.OutboundConfigs { + if ob.Tag == tag { + found = idx + break + } + } + return found +} + +// Override method accepts another Config overrides the current attribute +func (c *Config) Override(o *Config) { + + // only process the non-deprecated members + + if o.LogConfig != nil { + c.LogConfig = o.LogConfig + } + if o.RouterConfig != nil { + c.RouterConfig = o.RouterConfig + } + if o.DNSConfig != nil { + c.DNSConfig = o.DNSConfig + } + if o.Transport != nil { + c.Transport = o.Transport + } + if o.Policy != nil { + c.Policy = o.Policy + } + if o.Api != nil { + c.Api = o.Api + } + if o.Stats != nil { + c.Stats = o.Stats + } + if o.Reverse != nil { + c.Reverse = o.Reverse + } + + // update the Inbound in slice if the only one in overide config has same tag + if len(o.InboundConfigs) > 0 { + if len(c.InboundConfigs) > 0 && len(o.InboundConfigs) == 1 { + if idx := c.findInboundTag(o.InboundConfigs[0].Tag); idx > -1 { + c.InboundConfigs[idx] = o.InboundConfigs[0] + newError("updated inbound with tag: ", o.InboundConfigs[0].Tag).AtInfo().WriteToLog() + } else { + c.InboundConfigs = append(c.InboundConfigs, o.InboundConfigs[0]) + } + } else { + c.InboundConfigs = o.InboundConfigs + } + } + + // update the Outbound in slice if the only one in overide config has same tag + if len(o.OutboundConfigs) > 0 { + if len(c.OutboundConfigs) > 0 && len(o.OutboundConfigs) == 1 { + if idx := c.findOutboundTag(o.OutboundConfigs[0].Tag); idx > -1 { + c.OutboundConfigs[idx] = o.OutboundConfigs[0] + } else { + c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[0]) + } + } else { + c.OutboundConfigs = o.OutboundConfigs + } + } +} + func applyTransportConfig(s *StreamConfig, t *TransportConfig) { if s.TCPSettings == nil { s.TCPSettings = t.TCPConfig diff --git a/infra/conf/v2ray_test.go b/infra/conf/v2ray_test.go index d324d3ea6..9381af62e 100644 --- a/infra/conf/v2ray_test.go +++ b/infra/conf/v2ray_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/golang/protobuf/proto" + "github.com/google/go-cmp/cmp" "v2ray.com/core" "v2ray.com/core/app/dispatcher" "v2ray.com/core/app/log" @@ -369,3 +370,72 @@ func TestMuxConfig_Build(t *testing.T) { }) } } + +func TestConfig_Override(t *testing.T) { + tests := []struct { + name string + orig *Config + over *Config + want *Config + }{ + {"combine/empty", + &Config{}, + &Config{ + LogConfig: &LogConfig{}, + RouterConfig: &RouterConfig{}, + DNSConfig: &DnsConfig{}, + Transport: &TransportConfig{}, + Policy: &PolicyConfig{}, + Api: &ApiConfig{}, + Stats: &StatsConfig{}, + Reverse: &ReverseConfig{}, + }, + &Config{ + LogConfig: &LogConfig{}, + RouterConfig: &RouterConfig{}, + DNSConfig: &DnsConfig{}, + Transport: &TransportConfig{}, + Policy: &PolicyConfig{}, + Api: &ApiConfig{}, + Stats: &StatsConfig{}, + Reverse: &ReverseConfig{}, + }, + }, + {"combine/newattr", + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "old"}}}, + &Config{LogConfig: &LogConfig{}}, + &Config{LogConfig: &LogConfig{}, InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "old"}}}}, + {"replace/inbounds", + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos0"}, InboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos0"}, InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, + {"replace/inbounds-all", + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos0"}, InboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, InboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}, + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, InboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}}, + {"replace/notag", + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{}, InboundDetourConfig{Protocol: "vmess"}}}, + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, + &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{}, InboundDetourConfig{Protocol: "vmess"}, InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, + {"replace/outbounds", + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, + {"replace/outbounds-all", + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}}, + {"replace/outbound-notag", + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{}, OutboundDetourConfig{Protocol: "vmess"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{}, OutboundDetourConfig{Protocol: "vmess"}, OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.orig.Override(tt.over) + if r := cmp.Diff(tt.orig, tt.want); r != "" { + t.Error(r) + } + }) + } +} From 904db6bd61785ecab7ffed017c66704739281444 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Sat, 14 Dec 2019 21:43:47 +0800 Subject: [PATCH 02/11] mconfig subcommand ready --- common/log/logger.go | 9 ++++ common/platform/ctlcmd/ctlcmd.go | 3 ++ infra/conf/serial/loader.go | 11 ++++- infra/conf/v2ray.go | 7 ++- infra/control/fetch.go | 44 ++++++++++-------- infra/control/main/main.go | 5 +- infra/control/mconfig.go | 78 ++++++++++++++++++++++++++++++++ 7 files changed, 135 insertions(+), 22 deletions(-) create mode 100644 infra/control/mconfig.go diff --git a/common/log/logger.go b/common/log/logger.go index 9a2591cb9..a6b8316b1 100644 --- a/common/log/logger.go +++ b/common/log/logger.go @@ -119,6 +119,15 @@ func CreateStdoutLogWriter() WriterCreator { } } +// CreateStderrLogWriter returns a LogWriterCreator that creates LogWriter for stderr. +func CreateStderrLogWriter() WriterCreator { + return func() Writer { + return &consoleLogWriter{ + logger: log.New(os.Stderr, "", log.Ldate|log.Ltime), + } + } +} + // CreateFileLogWriter returns a LogWriterCreator that creates LogWriter for the given file. func CreateFileLogWriter(path string) (WriterCreator, error) { file, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) diff --git a/common/platform/ctlcmd/ctlcmd.go b/common/platform/ctlcmd/ctlcmd.go index ed770b555..91c22e504 100644 --- a/common/platform/ctlcmd/ctlcmd.go +++ b/common/platform/ctlcmd/ctlcmd.go @@ -39,6 +39,9 @@ func Run(args []string, input io.Reader) (buf.MultiBuffer, error) { } return nil, newError(msg).Base(err) } + if !errBuffer.IsEmpty() { + newError("v2ctl > ", errBuffer.String()).AtInfo().WriteToLog() + } return outBuffer.MultiBuffer, nil } diff --git a/infra/conf/serial/loader.go b/infra/conf/serial/loader.go index 1d084778d..cdff522a4 100644 --- a/infra/conf/serial/loader.go +++ b/infra/conf/serial/loader.go @@ -38,7 +38,7 @@ func findOffset(b []byte, o int) *offset { return &offset{line: line, char: char} } -func LoadJSONConfig(reader io.Reader) (*core.Config, error) { +func DecodeJSONConfig(reader io.Reader) (*conf.Config, error) { jsonConfig := &conf.Config{} jsonContent := bytes.NewBuffer(make([]byte, 0, 10240)) @@ -62,6 +62,15 @@ func LoadJSONConfig(reader io.Reader) (*core.Config, error) { return nil, newError("failed to read config file").Base(err) } + return jsonConfig, nil +} + +func LoadJSONConfig(reader io.Reader) (*core.Config, error) { + jsonConfig, err := DecodeJSONConfig(reader) + if err != nil { + return nil, err + } + pbConfig, err := jsonConfig.Build() if err != nil { return nil, newError("failed to parse json config").Base(err) diff --git a/infra/conf/v2ray.go b/infra/conf/v2ray.go index afe1fee6d..1a666f134 100644 --- a/infra/conf/v2ray.go +++ b/infra/conf/v2ray.go @@ -327,7 +327,7 @@ func (c *Config) findOutboundTag(tag string) int { } // Override method accepts another Config overrides the current attribute -func (c *Config) Override(o *Config) { +func (c *Config) Override(o *Config, fn string) { // only process the non-deprecated members @@ -361,9 +361,10 @@ func (c *Config) Override(o *Config) { if len(c.InboundConfigs) > 0 && len(o.InboundConfigs) == 1 { if idx := c.findInboundTag(o.InboundConfigs[0].Tag); idx > -1 { c.InboundConfigs[idx] = o.InboundConfigs[0] - newError("updated inbound with tag: ", o.InboundConfigs[0].Tag).AtInfo().WriteToLog() + newError("<", fn, "> updated inbound with tag: ", o.InboundConfigs[0].Tag).AtInfo().WriteToLog() } else { c.InboundConfigs = append(c.InboundConfigs, o.InboundConfigs[0]) + newError("<", fn, "> appended inbound with tag: ", o.InboundConfigs[0].Tag).AtInfo().WriteToLog() } } else { c.InboundConfigs = o.InboundConfigs @@ -375,8 +376,10 @@ func (c *Config) Override(o *Config) { if len(c.OutboundConfigs) > 0 && len(o.OutboundConfigs) == 1 { if idx := c.findOutboundTag(o.OutboundConfigs[0].Tag); idx > -1 { c.OutboundConfigs[idx] = o.OutboundConfigs[0] + newError("<", fn, "> updated outbound with tag: ", o.OutboundConfigs[0].Tag).AtInfo().WriteToLog() } else { c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[0]) + newError("<", fn, "> updated outbound with tag: ", o.OutboundConfigs[0].Tag).AtInfo().WriteToLog() } } else { c.OutboundConfigs = o.OutboundConfigs diff --git a/infra/control/fetch.go b/infra/control/fetch.go index 167c7483c..81e600954 100644 --- a/infra/control/fetch.go +++ b/infra/control/fetch.go @@ -5,6 +5,7 @@ import ( "net/url" "os" "strings" + "time" "v2ray.com/core/common" "v2ray.com/core/common/buf" @@ -23,46 +24,53 @@ func (c *FetchCommand) Description() Description { } } -func (c *FetchCommand) isValidScheme(scheme string) bool { - scheme = strings.ToLower(scheme) - return scheme == "http" || scheme == "https" -} - func (c *FetchCommand) Execute(args []string) error { if len(args) < 1 { return newError("empty url") } - target := args[0] - parsedTarget, err := url.Parse(target) + content, err := FetchHTTPContent(args[0]) if err != nil { - return newError("invalid URL: ", target).Base(err) - } - if !c.isValidScheme(parsedTarget.Scheme) { - return newError("invalid scheme: ", parsedTarget.Scheme) + return newError("failed to read HTTP response").Base(err) } - client := &http.Client{} + os.Stdout.Write(content) + return nil +} + +func FetchHTTPContent(target string) ([]byte, error) { + + parsedTarget, err := url.Parse(target) + if err != nil { + return nil, newError("invalid URL: ", target).Base(err) + } + + 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 newError("failed to dial to ", target).Base(err) + return nil, newError("failed to dial to ", target).Base(err) } + defer resp.Body.Close() if resp.StatusCode != 200 { - return newError("unexpected HTTP status code: ", resp.StatusCode) + return nil, newError("unexpected HTTP status code: ", resp.StatusCode) } content, err := buf.ReadAllToBytes(resp.Body) if err != nil { - return newError("failed to read HTTP response").Base(err) + return nil, newError("failed to read HTTP response").Base(err) } - os.Stdout.Write(content) - - return nil + return content, nil } func init() { diff --git a/infra/control/main/main.go b/infra/control/main/main.go index 5c41aeb0e..e78fa5d12 100644 --- a/infra/control/main/main.go +++ b/infra/control/main/main.go @@ -5,7 +5,8 @@ import ( "fmt" "os" - _ "v2ray.com/core/infra/conf/command" + commlog "v2ray.com/core/common/log" + // _ "v2ray.com/core/infra/conf/command" "v2ray.com/core/infra/control" ) @@ -17,6 +18,8 @@ func getCommandName() string { } func main() { + // let the v2ctl prints log at stderr + commlog.RegisterHandler(commlog.NewLogger(commlog.CreateStderrLogWriter())) name := getCommandName() cmd := control.GetCommand(name) if cmd == nil { diff --git a/infra/control/mconfig.go b/infra/control/mconfig.go new file mode 100644 index 000000000..5222df41b --- /dev/null +++ b/infra/control/mconfig.go @@ -0,0 +1,78 @@ +package control + +import ( + "bytes" + "io" + "io/ioutil" + "os" + "strings" + + "github.com/golang/protobuf/proto" + "v2ray.com/core/common" + "v2ray.com/core/infra/conf" + "v2ray.com/core/infra/conf/serial" +) + +type MconfigCommand struct{} + +func (c *MconfigCommand) Name() string { + return "mconfig" +} + +func (c *MconfigCommand) Description() Description { + return Description{ + Short: "merge multiple json config", + Usage: []string{"v2ctl mconfig 1.json 2.json .json"}, + } +} + +func (c *MconfigCommand) Execute(args []string) error { + if len(args) < 1 { + return newError("empty config list") + } + + conf := &conf.Config{} + for _, arg := range args { + r, err := c.LoadArg(arg) + common.Must(err) + c, err := serial.DecodeJSONConfig(r) + common.Must(err) + conf.Override(c, arg) + } + + pbConfig, err := conf.Build() + if err != nil { + return err + } + + bytesConfig, err := proto.Marshal(pbConfig) + if err != nil { + return newError("failed to marshal proto config").Base(err) + } + + if _, err := os.Stdout.Write(bytesConfig); err != nil { + return newError("failed to write proto config").Base(err) + } + + return nil +} + +func (c *MconfigCommand) LoadArg(arg string) (out io.Reader, err error) { + + var data []byte + if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") { + data, err = FetchHTTPContent(arg) + } else { + data, err = ioutil.ReadFile(arg) + } + + if err != nil { + return + } + out = bytes.NewBuffer(data) + return +} + +func init() { + common.Must(RegisterCommand(&MconfigCommand{})) +} From 1e76123a4c3ad12e004363aaba2c57638516ce82 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Sat, 14 Dec 2019 22:24:32 +0800 Subject: [PATCH 03/11] main loads multiple --- common/platform/ctlcmd/ctlcmd.go | 4 +- infra/control/{mconfig.go => config.go} | 2 +- main/confloader/confloader.go | 19 ----- main/confloader/external/external.go | 48 ----------- main/distro/all/all.go | 3 - main/json/config_json.go | 7 +- .../external => jsonem}/errors.generated.go | 2 +- main/jsonem/jsonem.go | 80 ++++++++++++++++++- main/main.go | 49 +++++++----- 9 files changed, 118 insertions(+), 96 deletions(-) rename infra/control/{mconfig.go => config.go} (98%) delete mode 100644 main/confloader/confloader.go delete mode 100644 main/confloader/external/external.go rename main/{confloader/external => jsonem}/errors.generated.go (91%) diff --git a/common/platform/ctlcmd/ctlcmd.go b/common/platform/ctlcmd/ctlcmd.go index 91c22e504..adebbf7a0 100644 --- a/common/platform/ctlcmd/ctlcmd.go +++ b/common/platform/ctlcmd/ctlcmd.go @@ -39,8 +39,10 @@ func Run(args []string, input io.Reader) (buf.MultiBuffer, error) { } return nil, newError(msg).Base(err) } + + // log stderr, info message if !errBuffer.IsEmpty() { - newError("v2ctl > ", errBuffer.String()).AtInfo().WriteToLog() + newError("v2ctl > \n", errBuffer.MultiBuffer.String()).AtInfo().WriteToLog() } return outBuffer.MultiBuffer, nil diff --git a/infra/control/mconfig.go b/infra/control/config.go similarity index 98% rename from infra/control/mconfig.go rename to infra/control/config.go index 5222df41b..aa6d142ab 100644 --- a/infra/control/mconfig.go +++ b/infra/control/config.go @@ -16,7 +16,7 @@ import ( type MconfigCommand struct{} func (c *MconfigCommand) Name() string { - return "mconfig" + return "config" } func (c *MconfigCommand) Description() Description { diff --git a/main/confloader/confloader.go b/main/confloader/confloader.go deleted file mode 100644 index 9edf6909e..000000000 --- a/main/confloader/confloader.go +++ /dev/null @@ -1,19 +0,0 @@ -package confloader - -import ( - "io" - "os" -) - -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) -} diff --git a/main/confloader/external/external.go b/main/confloader/external/external.go deleted file mode 100644 index 75dcdea37..000000000 --- a/main/confloader/external/external.go +++ /dev/null @@ -1,48 +0,0 @@ -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 - -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 &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) - } - defer file.Close() - - content, err := buf.ReadFrom(file) - if err != nil { - return nil, newError("failed to load config file: ", fixedFile).Base(err).AtWarning() - } - return &buf.MultiBufferContainer{ - MultiBuffer: content, - }, nil -} - -func init() { - confloader.EffectiveConfigFileLoader = loadConfigFile -} diff --git a/main/distro/all/all.go b/main/distro/all/all.go index 71144c087..f7986889e 100644 --- a/main/distro/all/all.go +++ b/main/distro/all/all.go @@ -58,7 +58,4 @@ import ( _ "v2ray.com/core/main/json" // The following line loads JSON internally // _ "v2ray.com/core/main/jsonem" - - // Load config from file or http(s) - _ "v2ray.com/core/main/confloader/external" ) diff --git a/main/json/config_json.go b/main/json/config_json.go index 45c6374d9..274f0aac0 100644 --- a/main/json/config_json.go +++ b/main/json/config_json.go @@ -3,7 +3,9 @@ package json //go:generate errorgen import ( + "encoding/json" "io" + "io/ioutil" "v2ray.com/core" "v2ray.com/core/common" @@ -16,7 +18,10 @@ func init() { Name: "JSON", Extension: []string{"json"}, Loader: func(input io.Reader) (*core.Config, error) { - jsonContent, err := ctlcmd.Run([]string{"config"}, input) + fns := []string{} + data, _ := ioutil.ReadAll(input) + json.Unmarshal(data, &fns) + jsonContent, err := ctlcmd.Run(append([]string{"config"}, fns...), nil) if err != nil { return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning() } diff --git a/main/confloader/external/errors.generated.go b/main/jsonem/errors.generated.go similarity index 91% rename from main/confloader/external/errors.generated.go rename to main/jsonem/errors.generated.go index 919f10d00..48961bcea 100644 --- a/main/confloader/external/errors.generated.go +++ b/main/jsonem/errors.generated.go @@ -1,4 +1,4 @@ -package external +package jsonem import "v2ray.com/core/common/errors" diff --git a/main/jsonem/jsonem.go b/main/jsonem/jsonem.go index 7b58bb9b5..d8bf1f7be 100644 --- a/main/jsonem/jsonem.go +++ b/main/jsonem/jsonem.go @@ -1,8 +1,19 @@ package jsonem import ( + "bytes" + "encoding/json" + "io" + "io/ioutil" + "net/http" + "net/url" + "strings" + "time" + "v2ray.com/core" "v2ray.com/core/common" + "v2ray.com/core/common/buf" + "v2ray.com/core/infra/conf" "v2ray.com/core/infra/conf/serial" ) @@ -10,6 +21,71 @@ func init() { common.Must(core.RegisterConfigLoader(&core.ConfigFormat{ Name: "JSON", Extension: []string{"json"}, - Loader: serial.LoadJSONConfig, - })) + Loader: func(input io.Reader) (*core.Config, error) { + fns := []string{} + data, _ := ioutil.ReadAll(input) + json.Unmarshal(data, &fns) + + cf := &conf.Config{} + for _, arg := range fns { + r, err := LoadArg(arg) + common.Must(err) + c, err := serial.DecodeJSONConfig(r) + common.Must(err) + cf.Override(c, arg) + } + return cf.Build() + }})) +} + +func LoadArg(arg string) (out io.Reader, err error) { + + var data []byte + if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") { + data, err = FetchHTTPContent(arg) + } else { + data, err = ioutil.ReadFile(arg) + } + + if err != nil { + return + } + out = bytes.NewBuffer(data) + return +} + +func FetchHTTPContent(target string) ([]byte, error) { + + parsedTarget, err := url.Parse(target) + if err != nil { + return nil, newError("invalid URL: ", target).Base(err) + } + + 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 } diff --git a/main/main.go b/main/main.go index a5d3a447d..1b8adca27 100644 --- a/main/main.go +++ b/main/main.go @@ -3,6 +3,8 @@ package main //go:generate errorgen import ( + "bytes" + "encoding/json" "flag" "fmt" "os" @@ -14,15 +16,25 @@ import ( "v2ray.com/core" "v2ray.com/core/common/platform" - "v2ray.com/core/main/confloader" _ "v2ray.com/core/main/distro/all" ) +type CmdConfig []string + +func (c *CmdConfig) String() string { + return strings.Join([]string(*c), ",") +} + +func (c *CmdConfig) Set(value string) error { + *c = append(*c, value) + return nil +} + var ( - configFile = flag.String("config", "", "Config file for V2Ray.") - version = flag.Bool("version", false, "Show current version of V2Ray.") - test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.") - format = flag.String("format", "json", "Format of input file.") + configFiles CmdConfig // "Config file for V2Ray.", the option is customed type, parse in main + version = flag.Bool("version", false, "Show current version of V2Ray.") + test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.") + format = flag.String("format", "json", "Format of input file.") ) func fileExists(file string) bool { @@ -30,23 +42,24 @@ func fileExists(file string) bool { return err == nil && !info.IsDir() } -func getConfigFilePath() string { - if len(*configFile) > 0 { - return *configFile +func getConfigFilePath() CmdConfig { + + if len(configFiles) > 0 { + return configFiles } if workingDir, err := os.Getwd(); err == nil { configFile := filepath.Join(workingDir, "config.json") if fileExists(configFile) { - return configFile + return []string{configFile} } } if configFile := platform.GetConfigurationPath(); fileExists(configFile) { - return configFile + return []string{configFile} } - return "" + return []string{} } func GetConfigFormat() string { @@ -59,16 +72,11 @@ func GetConfigFormat() string { } func startV2Ray() (core.Server, error) { - configFile := getConfigFilePath() - configInput, err := confloader.LoadConfig(configFile) + configFiles := getConfigFilePath() + fs, _ := json.Marshal(configFiles) + config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], bytes.NewBuffer(fs)) 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) + return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err) } server, err := core.New(config) @@ -87,6 +95,7 @@ func printVersion() { } func main() { + flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.") flag.Parse() printVersion() From 61e95e06c0cfd89eda1756f34e3b90fc327b65e7 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Sat, 14 Dec 2019 22:25:52 +0800 Subject: [PATCH 04/11] remove unused pkg --- infra/conf/command/command.go | 48 -------------------------- infra/conf/command/errors.generated.go | 9 ----- infra/control/config.go | 15 ++++---- 3 files changed, 8 insertions(+), 64 deletions(-) delete mode 100644 infra/conf/command/command.go delete mode 100644 infra/conf/command/errors.generated.go diff --git a/infra/conf/command/command.go b/infra/conf/command/command.go deleted file mode 100644 index 9f8c12d56..000000000 --- a/infra/conf/command/command.go +++ /dev/null @@ -1,48 +0,0 @@ -package command - -//go:generate errorgen - -import ( - "os" - - "github.com/golang/protobuf/proto" - "v2ray.com/core/common" - "v2ray.com/core/infra/conf/serial" - "v2ray.com/core/infra/control" -) - -type ConfigCommand struct{} - -func (c *ConfigCommand) Name() string { - return "config" -} - -func (c *ConfigCommand) Description() control.Description { - return control.Description{ - Short: "Convert config among different formats.", - Usage: []string{ - "v2ctl config", - }, - } -} - -func (c *ConfigCommand) Execute(args []string) error { - pbConfig, err := serial.LoadJSONConfig(os.Stdin) - if err != nil { - return newError("failed to parse json config").Base(err) - } - - bytesConfig, err := proto.Marshal(pbConfig) - if err != nil { - return newError("failed to marshal proto config").Base(err) - } - - if _, err := os.Stdout.Write(bytesConfig); err != nil { - return newError("failed to write proto config").Base(err) - } - return nil -} - -func init() { - common.Must(control.RegisterCommand(&ConfigCommand{})) -} diff --git a/infra/conf/command/errors.generated.go b/infra/conf/command/errors.generated.go deleted file mode 100644 index 66f780510..000000000 --- a/infra/conf/command/errors.generated.go +++ /dev/null @@ -1,9 +0,0 @@ -package command - -import "v2ray.com/core/common/errors" - -type errPathObjHolder struct{} - -func newError(values ...interface{}) *errors.Error { - return errors.New(values...).WithPathObj(errPathObjHolder{}) -} diff --git a/infra/control/config.go b/infra/control/config.go index aa6d142ab..cc2c98601 100644 --- a/infra/control/config.go +++ b/infra/control/config.go @@ -13,26 +13,27 @@ import ( "v2ray.com/core/infra/conf/serial" ) -type MconfigCommand struct{} +type ConfigCommand struct{} -func (c *MconfigCommand) Name() string { +func (c *ConfigCommand) Name() string { return "config" } -func (c *MconfigCommand) Description() Description { +func (c *ConfigCommand) Description() Description { return Description{ Short: "merge multiple json config", - Usage: []string{"v2ctl mconfig 1.json 2.json .json"}, + Usage: []string{"v2ctl config config.json c1.json c2.json .json"}, } } -func (c *MconfigCommand) Execute(args []string) error { +func (c *ConfigCommand) Execute(args []string) error { if len(args) < 1 { return newError("empty config list") } conf := &conf.Config{} for _, arg := range args { + newError("Reading config: ", arg).AtInfo().WriteToLog() r, err := c.LoadArg(arg) common.Must(err) c, err := serial.DecodeJSONConfig(r) @@ -57,7 +58,7 @@ func (c *MconfigCommand) Execute(args []string) error { return nil } -func (c *MconfigCommand) LoadArg(arg string) (out io.Reader, err error) { +func (c *ConfigCommand) LoadArg(arg string) (out io.Reader, err error) { var data []byte if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") { @@ -74,5 +75,5 @@ func (c *MconfigCommand) LoadArg(arg string) (out io.Reader, err error) { } func init() { - common.Must(RegisterCommand(&MconfigCommand{})) + common.Must(RegisterCommand(&ConfigCommand{})) } From 1bb34bfe17a7bfb31c5390b40485edbad12cdbeb Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Sat, 14 Dec 2019 23:29:54 +0800 Subject: [PATCH 05/11] abstract type for conf load func --- common/cmdarg/cmdarg.go | 14 ++++++++++++++ config.go | 34 ++++++++++++++++++++++++-------- main/json/config_json.go | 28 +++++++++++++++------------ main/jsonem/jsonem.go | 34 ++++++++++++++++++-------------- main/main.go | 42 +++++++++++++++++++--------------------- 5 files changed, 95 insertions(+), 57 deletions(-) create mode 100644 common/cmdarg/cmdarg.go diff --git a/common/cmdarg/cmdarg.go b/common/cmdarg/cmdarg.go new file mode 100644 index 000000000..de5ed9498 --- /dev/null +++ b/common/cmdarg/cmdarg.go @@ -0,0 +1,14 @@ +package cmdarg + +import "strings" + +type Arg []string + +func (c *Arg) String() string { + return strings.Join([]string(*c), " ") +} + +func (c *Arg) Set(value string) error { + *c = append(*c, value) + return nil +} diff --git a/config.go b/config.go index d328a87bd..73eea2ca1 100755 --- a/config.go +++ b/config.go @@ -4,11 +4,13 @@ package core import ( "io" + "io/ioutil" "strings" "github.com/golang/protobuf/proto" "v2ray.com/core/common" "v2ray.com/core/common/buf" + "v2ray.com/core/common/cmdarg" ) // ConfigFormat is a configurable format of V2Ray config file. @@ -19,7 +21,7 @@ type ConfigFormat struct { } // ConfigLoader is a utility to load V2Ray config from external source. -type ConfigLoader func(input io.Reader) (*Config, error) +type ConfigLoader func(input interface{}) (*Config, error) var ( configLoaderByName = make(map[string]*ConfigFormat) @@ -54,7 +56,10 @@ func getExtension(filename string) string { } // LoadConfig loads config with given format from given source. -func LoadConfig(formatName string, filename string, input io.Reader) (*Config, error) { +// input accepts 2 different types: +// * []string slice of multiple filename/url(s) to open to read +// * io.Reader that reads a config content (the original way) +func LoadConfig(formatName string, filename string, input interface{}) (*Config, error) { ext := getExtension(filename) if len(ext) > 0 { if f, found := configLoaderByExt[ext]; found { @@ -69,12 +74,8 @@ func LoadConfig(formatName string, filename string, input io.Reader) (*Config, e return nil, newError("Unable to load config in ", formatName).AtWarning() } -func loadProtobufConfig(input io.Reader) (*Config, error) { +func loadProtobufConfig(data []byte) (*Config, error) { config := new(Config) - data, err := buf.ReadAllToBytes(input) - if err != nil { - return nil, err - } if err := proto.Unmarshal(data, config); err != nil { return nil, err } @@ -85,6 +86,23 @@ func init() { common.Must(RegisterConfigLoader(&ConfigFormat{ Name: "Protobuf", Extension: []string{"pb"}, - Loader: loadProtobufConfig, + Loader: func(input interface{}) (*Config, error) { + switch v := input.(type) { + case cmdarg.Arg: + if len(v) == 0 { + return nil, newError("input has no element") + } + // pb type can only handle the first config + data, err := ioutil.ReadFile(v[0]) + common.Must(err) + return loadProtobufConfig(data) + case io.Reader: + data, err := buf.ReadAllToBytes(v) + common.Must(err) + return loadProtobufConfig(data) + default: + return nil, newError("unknow type") + } + }, })) } diff --git a/main/json/config_json.go b/main/json/config_json.go index 274f0aac0..84f405bd2 100644 --- a/main/json/config_json.go +++ b/main/json/config_json.go @@ -3,31 +3,35 @@ package json //go:generate errorgen import ( - "encoding/json" "io" - "io/ioutil" "v2ray.com/core" "v2ray.com/core/common" "v2ray.com/core/common/buf" + "v2ray.com/core/common/cmdarg" "v2ray.com/core/common/platform/ctlcmd" + "v2ray.com/core/infra/conf/serial" ) func init() { common.Must(core.RegisterConfigLoader(&core.ConfigFormat{ Name: "JSON", Extension: []string{"json"}, - Loader: func(input io.Reader) (*core.Config, error) { - fns := []string{} - data, _ := ioutil.ReadAll(input) - json.Unmarshal(data, &fns) - jsonContent, err := ctlcmd.Run(append([]string{"config"}, fns...), nil) - if err != nil { - return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning() + Loader: func(input interface{}) (*core.Config, error) { + switch v := input.(type) { + case cmdarg.Arg: + jsonContent, err := ctlcmd.Run(append([]string{"config"}, v...), nil) + if err != nil { + return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning() + } + return core.LoadConfig("protobuf", "", &buf.MultiBufferContainer{ + MultiBuffer: jsonContent, + }) + case io.Reader: + return serial.LoadJSONConfig(v) + default: + return nil, newError("unknow type") } - return core.LoadConfig("protobuf", "", &buf.MultiBufferContainer{ - MultiBuffer: jsonContent, - }) }, })) } diff --git a/main/jsonem/jsonem.go b/main/jsonem/jsonem.go index d8bf1f7be..95e0bb139 100644 --- a/main/jsonem/jsonem.go +++ b/main/jsonem/jsonem.go @@ -2,7 +2,6 @@ package jsonem import ( "bytes" - "encoding/json" "io" "io/ioutil" "net/http" @@ -13,6 +12,7 @@ import ( "v2ray.com/core" "v2ray.com/core/common" "v2ray.com/core/common/buf" + "v2ray.com/core/common/cmdarg" "v2ray.com/core/infra/conf" "v2ray.com/core/infra/conf/serial" ) @@ -21,21 +21,25 @@ func init() { common.Must(core.RegisterConfigLoader(&core.ConfigFormat{ Name: "JSON", Extension: []string{"json"}, - Loader: func(input io.Reader) (*core.Config, error) { - fns := []string{} - data, _ := ioutil.ReadAll(input) - json.Unmarshal(data, &fns) - - cf := &conf.Config{} - for _, arg := range fns { - r, err := LoadArg(arg) - common.Must(err) - c, err := serial.DecodeJSONConfig(r) - common.Must(err) - cf.Override(c, arg) + Loader: func(input interface{}) (*core.Config, error) { + switch v := input.(type) { + case cmdarg.Arg: + cf := &conf.Config{} + for _, arg := range v { + r, err := LoadArg(arg) + common.Must(err) + c, err := serial.DecodeJSONConfig(r) + common.Must(err) + cf.Override(c, arg) + } + return cf.Build() + case io.Reader: + return serial.LoadJSONConfig(v) + default: + return nil, newError("unknow type") } - return cf.Build() - }})) + }, + })) } func LoadArg(arg string) (out io.Reader, err error) { diff --git a/main/main.go b/main/main.go index 1b8adca27..90d6e3de4 100644 --- a/main/main.go +++ b/main/main.go @@ -3,8 +3,6 @@ package main //go:generate errorgen import ( - "bytes" - "encoding/json" "flag" "fmt" "os" @@ -15,26 +13,17 @@ import ( "syscall" "v2ray.com/core" + "v2ray.com/core/common/cmdarg" "v2ray.com/core/common/platform" _ "v2ray.com/core/main/distro/all" ) -type CmdConfig []string - -func (c *CmdConfig) String() string { - return strings.Join([]string(*c), ",") -} - -func (c *CmdConfig) Set(value string) error { - *c = append(*c, value) - return nil -} - var ( - configFiles CmdConfig // "Config file for V2Ray.", the option is customed type, parse in main + configFiles cmdarg.Arg // "Config file for V2Ray.", the option is customed type, parse in main version = flag.Bool("version", false, "Show current version of V2Ray.") test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.") - format = flag.String("format", "json", "Format of input file.") + format = flag.String("format", "", "Format of input file.") + errNoConfig = newError("no valid config") ) func fileExists(file string) bool { @@ -42,8 +31,7 @@ func fileExists(file string) bool { return err == nil && !info.IsDir() } -func getConfigFilePath() CmdConfig { - +func getConfigFilePath() cmdarg.Arg { if len(configFiles) > 0 { return configFiles } @@ -51,15 +39,15 @@ func getConfigFilePath() CmdConfig { if workingDir, err := os.Getwd(); err == nil { configFile := filepath.Join(workingDir, "config.json") if fileExists(configFile) { - return []string{configFile} + return cmdarg.Arg{configFile} } } if configFile := platform.GetConfigurationPath(); fileExists(configFile) { - return []string{configFile} + return cmdarg.Arg{configFile} } - return []string{} + return configFiles } func GetConfigFormat() string { @@ -73,8 +61,14 @@ func GetConfigFormat() string { func startV2Ray() (core.Server, error) { configFiles := getConfigFilePath() - fs, _ := json.Marshal(configFiles) - config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], bytes.NewBuffer(fs)) + if len(configFiles) == 0 { + if *format == "" { + return nil, errNoConfig + } + configFiles = []string{"stdin:"} + } + + config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], configFiles) if err != nil { return nil, newError("failed to read config files: [", configFiles.String(), "]").Base(err) } @@ -96,6 +90,7 @@ func printVersion() { func main() { flag.Var(&configFiles, "config", "Config file for V2Ray. Multiple assign is accepted (only json). Latter ones overrides the former ones.") + flag.Var(&configFiles, "c", "short alias of -config") flag.Parse() printVersion() @@ -108,6 +103,9 @@ func main() { if err != nil { fmt.Println(err.Error()) // Configuration error. Exit with a special value to prevent systemd from restarting. + if err == errNoConfig { + flag.PrintDefaults() + } os.Exit(23) } From 75f0879c1297220bfa0828ae14468bfd5f0a6041 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Sat, 14 Dec 2019 23:44:10 +0800 Subject: [PATCH 06/11] proper handle stdin --- config.go | 11 +++++++++-- infra/control/config.go | 2 ++ main/json/config_json.go | 3 ++- main/jsonem/jsonem.go | 4 ++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 73eea2ca1..727329fa9 100755 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ package core import ( "io" "io/ioutil" + "os" "strings" "github.com/golang/protobuf/proto" @@ -92,9 +93,15 @@ func init() { if len(v) == 0 { return nil, newError("input has no element") } + var data []byte + var rerr error // pb type can only handle the first config - data, err := ioutil.ReadFile(v[0]) - common.Must(err) + if v[0] == "stdin:" { + data, rerr = buf.ReadAllToBytes(os.Stdin) + } else { + data, rerr = ioutil.ReadFile(v[0]) + } + common.Must(rerr) return loadProtobufConfig(data) case io.Reader: data, err := buf.ReadAllToBytes(v) diff --git a/infra/control/config.go b/infra/control/config.go index cc2c98601..a0f456dd5 100644 --- a/infra/control/config.go +++ b/infra/control/config.go @@ -63,6 +63,8 @@ func (c *ConfigCommand) LoadArg(arg string) (out io.Reader, err error) { 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) } diff --git a/main/json/config_json.go b/main/json/config_json.go index 84f405bd2..683341041 100644 --- a/main/json/config_json.go +++ b/main/json/config_json.go @@ -4,6 +4,7 @@ package json import ( "io" + "os" "v2ray.com/core" "v2ray.com/core/common" @@ -20,7 +21,7 @@ func init() { Loader: func(input interface{}) (*core.Config, error) { switch v := input.(type) { case cmdarg.Arg: - jsonContent, err := ctlcmd.Run(append([]string{"config"}, v...), nil) + jsonContent, err := ctlcmd.Run(append([]string{"config"}, v...), os.Stdin) if err != nil { return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning() } diff --git a/main/jsonem/jsonem.go b/main/jsonem/jsonem.go index 95e0bb139..dd4471872 100644 --- a/main/jsonem/jsonem.go +++ b/main/jsonem/jsonem.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/http" "net/url" + "os" "strings" "time" @@ -26,6 +27,7 @@ func init() { case cmdarg.Arg: cf := &conf.Config{} for _, arg := range v { + newError("Reading config: ", arg).AtInfo().WriteToLog() r, err := LoadArg(arg) common.Must(err) c, err := serial.DecodeJSONConfig(r) @@ -47,6 +49,8 @@ func LoadArg(arg string) (out io.Reader, err error) { 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) } From 3b4a4607e5418a5a30eff0e6987c6a622c18285f Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Sun, 15 Dec 2019 00:14:46 +0800 Subject: [PATCH 07/11] fix test case; outbound with tail appends --- infra/conf/v2ray.go | 9 +++++++-- infra/conf/v2ray_test.go | 23 ++++++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/infra/conf/v2ray.go b/infra/conf/v2ray.go index 1a666f134..b87447231 100644 --- a/infra/conf/v2ray.go +++ b/infra/conf/v2ray.go @@ -378,8 +378,13 @@ func (c *Config) Override(o *Config, fn string) { c.OutboundConfigs[idx] = o.OutboundConfigs[0] newError("<", fn, "> updated outbound with tag: ", o.OutboundConfigs[0].Tag).AtInfo().WriteToLog() } else { - c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[0]) - newError("<", fn, "> updated outbound with tag: ", o.OutboundConfigs[0].Tag).AtInfo().WriteToLog() + if strings.Contains(strings.ToLower(fn), "tail") { + c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[0]) + newError("<", fn, "> appended outbound with tag: ", o.OutboundConfigs[0].Tag).AtInfo().WriteToLog() + } else { + c.OutboundConfigs = append(o.OutboundConfigs, c.OutboundConfigs...) + newError("<", fn, "> prepended outbound with tag: ", o.OutboundConfigs[0].Tag).AtInfo().WriteToLog() + } } } else { c.OutboundConfigs = o.OutboundConfigs diff --git a/infra/conf/v2ray_test.go b/infra/conf/v2ray_test.go index 9381af62e..742896e04 100644 --- a/infra/conf/v2ray_test.go +++ b/infra/conf/v2ray_test.go @@ -376,6 +376,7 @@ func TestConfig_Override(t *testing.T) { name string orig *Config over *Config + fn string want *Config }{ {"combine/empty", @@ -390,6 +391,7 @@ func TestConfig_Override(t *testing.T) { Stats: &StatsConfig{}, Reverse: &ReverseConfig{}, }, + "", &Config{ LogConfig: &LogConfig{}, RouterConfig: &RouterConfig{}, @@ -403,36 +405,47 @@ func TestConfig_Override(t *testing.T) { }, {"combine/newattr", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "old"}}}, - &Config{LogConfig: &LogConfig{}}, + &Config{LogConfig: &LogConfig{}}, "", &Config{LogConfig: &LogConfig{}, InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "old"}}}}, {"replace/inbounds", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos0"}, InboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, + "", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos0"}, InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, {"replace/inbounds-all", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos0"}, InboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, InboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}, + "", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, InboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}}, {"replace/notag", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{}, InboundDetourConfig{Protocol: "vmess"}}}, &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, + "", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{}, InboundDetourConfig{Protocol: "vmess"}, InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, {"replace/outbounds", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, + "", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, - {"replace/outbounds-all", + {"replace/outbounds-append", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}, + "", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}}, - {"replace/outbound-notag", + {"replace/outbounds-append", + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}, + "config_tail.json", + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Protocol: "vmess", Tag: "pos1"}, OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}}, + {"replace/outbound-prepend", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{}, OutboundDetourConfig{Protocol: "vmess"}}}, &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, - &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{}, OutboundDetourConfig{Protocol: "vmess"}, OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, + "config.json", + &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, OutboundDetourConfig{}, OutboundDetourConfig{Protocol: "vmess"}}}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.orig.Override(tt.over) + tt.orig.Override(tt.over, tt.fn) if r := cmp.Diff(tt.orig, tt.want); r != "" { t.Error(r) } From f14eb97e3cbac48ff5a4f4994be331b51852d894 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Mon, 16 Dec 2019 09:33:41 +0800 Subject: [PATCH 08/11] rearrange loader --- config.go | 19 ++--- main/confloader/confloader.go | 30 +++++++ main/confloader/errors.generated.go | 9 ++ main/confloader/external/errors.generated.go | 9 ++ main/confloader/external/external.go | 86 ++++++++++++++++++++ main/distro/all/all.go | 3 + main/json/config_json.go | 10 +-- main/jsonem/jsonem.go | 65 +-------------- main/main.go | 21 ++--- 9 files changed, 156 insertions(+), 96 deletions(-) create mode 100644 main/confloader/confloader.go create mode 100644 main/confloader/errors.generated.go create mode 100644 main/confloader/external/errors.generated.go create mode 100644 main/confloader/external/external.go diff --git a/config.go b/config.go index 727329fa9..bcadbb18b 100755 --- a/config.go +++ b/config.go @@ -4,14 +4,13 @@ package core import ( "io" - "io/ioutil" - "os" "strings" "github.com/golang/protobuf/proto" "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/cmdarg" + "v2ray.com/core/main/confloader" ) // ConfigFormat is a configurable format of V2Ray config file. @@ -90,18 +89,10 @@ func init() { Loader: func(input interface{}) (*Config, error) { switch v := input.(type) { case cmdarg.Arg: - if len(v) == 0 { - return nil, newError("input has no element") - } - var data []byte - var rerr error - // pb type can only handle the first config - if v[0] == "stdin:" { - data, rerr = buf.ReadAllToBytes(os.Stdin) - } else { - data, rerr = ioutil.ReadFile(v[0]) - } - common.Must(rerr) + r, err := confloader.LoadConfig(v[0]) + common.Must(err) + data, err := buf.ReadAllToBytes(r) + common.Must(err) return loadProtobufConfig(data) case io.Reader: data, err := buf.ReadAllToBytes(v) diff --git a/main/confloader/confloader.go b/main/confloader/confloader.go new file mode 100644 index 000000000..f987ddc76 --- /dev/null +++ b/main/confloader/confloader.go @@ -0,0 +1,30 @@ +package confloader + +import ( + "io" + "os" +) + +type configFileLoader func(string) (io.Reader, error) +type extconfigLoader func([]string) (io.Reader, error) + +var ( + EffectiveConfigFileLoader configFileLoader + EffectiveExtConfigLoader extconfigLoader +) + +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) +} + +func LoadExtConfig(files []string) (io.Reader, error) { + if EffectiveExtConfigLoader == nil { + return nil, newError("external config module not loaded").AtError() + } + + return EffectiveExtConfigLoader(files) +} diff --git a/main/confloader/errors.generated.go b/main/confloader/errors.generated.go new file mode 100644 index 000000000..deda6e515 --- /dev/null +++ b/main/confloader/errors.generated.go @@ -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{}) +} diff --git a/main/confloader/external/errors.generated.go b/main/confloader/external/errors.generated.go new file mode 100644 index 000000000..919f10d00 --- /dev/null +++ b/main/confloader/external/errors.generated.go @@ -0,0 +1,9 @@ +package external + +import "v2ray.com/core/common/errors" + +type errPathObjHolder struct{} + +func newError(values ...interface{}) *errors.Error { + return errors.New(values...).WithPathObj(errPathObjHolder{}) +} diff --git a/main/confloader/external/external.go b/main/confloader/external/external.go new file mode 100644 index 000000000..a16f0c7a5 --- /dev/null +++ b/main/confloader/external/external.go @@ -0,0 +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" +) + +func ConfigLoader(arg string) (out io.Reader, err error) { + + 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 err != nil { + return + } + out = bytes.NewBuffer(data) + return +} + +func FetchHTTPContent(target string) ([]byte, error) { + + parsedTarget, err := url.Parse(target) + if err != nil { + return nil, newError("invalid URL: ", target).Base(err) + } + + 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 = ConfigLoader + confloader.EffectiveExtConfigLoader = ExtConfigLoader +} diff --git a/main/distro/all/all.go b/main/distro/all/all.go index f7986889e..71144c087 100644 --- a/main/distro/all/all.go +++ b/main/distro/all/all.go @@ -58,4 +58,7 @@ import ( _ "v2ray.com/core/main/json" // The following line loads JSON internally // _ "v2ray.com/core/main/jsonem" + + // Load config from file or http(s) + _ "v2ray.com/core/main/confloader/external" ) diff --git a/main/json/config_json.go b/main/json/config_json.go index 683341041..59bcc63d0 100644 --- a/main/json/config_json.go +++ b/main/json/config_json.go @@ -4,14 +4,12 @@ package json import ( "io" - "os" "v2ray.com/core" "v2ray.com/core/common" - "v2ray.com/core/common/buf" "v2ray.com/core/common/cmdarg" - "v2ray.com/core/common/platform/ctlcmd" "v2ray.com/core/infra/conf/serial" + "v2ray.com/core/main/confloader" ) func init() { @@ -21,13 +19,11 @@ func init() { Loader: func(input interface{}) (*core.Config, error) { switch v := input.(type) { case cmdarg.Arg: - jsonContent, err := ctlcmd.Run(append([]string{"config"}, v...), os.Stdin) + r, err := confloader.LoadExtConfig(v) if err != nil { return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning() } - return core.LoadConfig("protobuf", "", &buf.MultiBufferContainer{ - MultiBuffer: jsonContent, - }) + return core.LoadConfig("protobuf", "", r) case io.Reader: return serial.LoadJSONConfig(v) default: diff --git a/main/jsonem/jsonem.go b/main/jsonem/jsonem.go index dd4471872..6000e2853 100644 --- a/main/jsonem/jsonem.go +++ b/main/jsonem/jsonem.go @@ -1,21 +1,14 @@ package jsonem import ( - "bytes" "io" - "io/ioutil" - "net/http" - "net/url" - "os" - "strings" - "time" "v2ray.com/core" "v2ray.com/core/common" - "v2ray.com/core/common/buf" "v2ray.com/core/common/cmdarg" "v2ray.com/core/infra/conf" "v2ray.com/core/infra/conf/serial" + "v2ray.com/core/main/confloader" ) func init() { @@ -28,7 +21,7 @@ func init() { cf := &conf.Config{} for _, arg := range v { newError("Reading config: ", arg).AtInfo().WriteToLog() - r, err := LoadArg(arg) + r, err := confloader.LoadConfig(arg) common.Must(err) c, err := serial.DecodeJSONConfig(r) common.Must(err) @@ -43,57 +36,3 @@ func init() { }, })) } - -func LoadArg(arg string) (out io.Reader, err error) { - - 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 err != nil { - return - } - out = bytes.NewBuffer(data) - return -} - -func FetchHTTPContent(target string) ([]byte, error) { - - parsedTarget, err := url.Parse(target) - if err != nil { - return nil, newError("invalid URL: ", target).Base(err) - } - - 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 -} diff --git a/main/main.go b/main/main.go index 90d6e3de4..94ba7088d 100644 --- a/main/main.go +++ b/main/main.go @@ -22,7 +22,7 @@ var ( configFiles cmdarg.Arg // "Config file for V2Ray.", the option is customed type, parse in main version = flag.Bool("version", false, "Show current version of V2Ray.") test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.") - format = flag.String("format", "", "Format of input file.") + format = flag.String("format", "json", "Format of input file.") errNoConfig = newError("no valid config") ) @@ -31,23 +31,23 @@ func fileExists(file string) bool { return err == nil && !info.IsDir() } -func getConfigFilePath() cmdarg.Arg { +func getConfigFilePath() (cmdarg.Arg, error) { if len(configFiles) > 0 { - return configFiles + return configFiles, nil } if workingDir, err := os.Getwd(); err == nil { configFile := filepath.Join(workingDir, "config.json") if fileExists(configFile) { - return cmdarg.Arg{configFile} + return cmdarg.Arg{configFile}, nil } } if configFile := platform.GetConfigurationPath(); fileExists(configFile) { - return cmdarg.Arg{configFile} + return cmdarg.Arg{configFile}, nil } - return configFiles + return cmdarg.Arg{"stdin:"}, nil } func GetConfigFormat() string { @@ -60,12 +60,9 @@ func GetConfigFormat() string { } func startV2Ray() (core.Server, error) { - configFiles := getConfigFilePath() - if len(configFiles) == 0 { - if *format == "" { - return nil, errNoConfig - } - configFiles = []string{"stdin:"} + configFiles, err := getConfigFilePath() + if err != nil { + return nil, err } config, err := core.LoadConfig(GetConfigFormat(), configFiles[0], configFiles) From f54bbb903d2dad5e3f0b2c8d776e8a7569234679 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Mon, 16 Dec 2019 10:29:18 +0800 Subject: [PATCH 09/11] fix testcase --- infra/conf/v2ray_test.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/infra/conf/v2ray_test.go b/infra/conf/v2ray_test.go index 742896e04..8c59a7ae8 100644 --- a/infra/conf/v2ray_test.go +++ b/infra/conf/v2ray_test.go @@ -412,12 +412,12 @@ func TestConfig_Override(t *testing.T) { &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, "", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos0"}, InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, - {"replace/inbounds-all", + {"replace/inbounds-replaceall", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos0"}, InboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, InboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}, "", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, InboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}}, - {"replace/notag", + {"replace/notag-append", &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{}, InboundDetourConfig{Protocol: "vmess"}}}, &Config{InboundConfigs: []InboundDetourConfig{InboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, "", @@ -427,21 +427,16 @@ func TestConfig_Override(t *testing.T) { &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, "", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}}, - {"replace/outbounds-append", + {"replace/outbounds-prepend", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}, - "", + "config.json", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}}, {"replace/outbounds-append", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Protocol: "vmess", Tag: "pos1"}}}, &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}, "config_tail.json", &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos0"}, OutboundDetourConfig{Protocol: "vmess", Tag: "pos1"}, OutboundDetourConfig{Tag: "pos2", Protocol: "kcp"}}}}, - {"replace/outbound-prepend", - &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{}, OutboundDetourConfig{Protocol: "vmess"}}}, - &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}}}, - "config.json", - &Config{OutboundConfigs: []OutboundDetourConfig{OutboundDetourConfig{Tag: "pos1", Protocol: "kcp"}, OutboundDetourConfig{}, OutboundDetourConfig{Protocol: "vmess"}}}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From a36631357c47a0d5aa16a99f595cfbb9bc333a23 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Tue, 24 Dec 2019 01:06:01 +0800 Subject: [PATCH 10/11] add comments to new added code --- common/cmdarg/cmdarg.go | 2 ++ infra/conf/serial/loader.go | 2 ++ infra/control/config.go | 5 +++++ infra/control/fetch.go | 1 + 4 files changed, 10 insertions(+) diff --git a/common/cmdarg/cmdarg.go b/common/cmdarg/cmdarg.go index de5ed9498..f524eb37d 100644 --- a/common/cmdarg/cmdarg.go +++ b/common/cmdarg/cmdarg.go @@ -2,12 +2,14 @@ package cmdarg import "strings" +// Arg is used by flag to accept multiple argument. type Arg []string func (c *Arg) String() string { return strings.Join([]string(*c), " ") } +// Set is the method flag package calls func (c *Arg) Set(value string) error { *c = append(*c, value) return nil diff --git a/infra/conf/serial/loader.go b/infra/conf/serial/loader.go index cdff522a4..f7ecf2db1 100644 --- a/infra/conf/serial/loader.go +++ b/infra/conf/serial/loader.go @@ -38,6 +38,8 @@ func findOffset(b []byte, o int) *offset { return &offset{line: line, char: char} } +// DecodeJSONConfig reads from reader and decode the config into *conf.Config +// syntax error could be detected. func DecodeJSONConfig(reader io.Reader) (*conf.Config, error) { jsonConfig := &conf.Config{} diff --git a/infra/control/config.go b/infra/control/config.go index a0f456dd5..02d7cd5dd 100644 --- a/infra/control/config.go +++ b/infra/control/config.go @@ -13,12 +13,15 @@ import ( "v2ray.com/core/infra/conf/serial" ) +// ConfigCommand is the json to pb convert struct type ConfigCommand struct{} +// Name for cmd usage func (c *ConfigCommand) Name() string { return "config" } +// Description for help usage func (c *ConfigCommand) Description() Description { return Description{ Short: "merge multiple json config", @@ -26,6 +29,7 @@ func (c *ConfigCommand) Description() Description { } } +// Execute real work here. func (c *ConfigCommand) Execute(args []string) error { if len(args) < 1 { return newError("empty config list") @@ -58,6 +62,7 @@ func (c *ConfigCommand) Execute(args []string) error { return nil } +// LoadArg loads one arg, maybe an remote url, or local file path func (c *ConfigCommand) LoadArg(arg string) (out io.Reader, err error) { var data []byte diff --git a/infra/control/fetch.go b/infra/control/fetch.go index 81e600954..a9deef25f 100644 --- a/infra/control/fetch.go +++ b/infra/control/fetch.go @@ -37,6 +37,7 @@ func (c *FetchCommand) Execute(args []string) error { return nil } +// FetchHTTPContent dials https for remote content func FetchHTTPContent(target string) ([]byte, error) { parsedTarget, err := url.Parse(target) From f578f0d617f4511e8e70990e3dab681dbe39543f Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Tue, 24 Dec 2019 01:13:10 +0800 Subject: [PATCH 11/11] add comment to please codacy --- main/confloader/confloader.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main/confloader/confloader.go b/main/confloader/confloader.go index f987ddc76..d52b5b89e 100644 --- a/main/confloader/confloader.go +++ b/main/confloader/confloader.go @@ -13,6 +13,8 @@ var ( EffectiveExtConfigLoader extconfigLoader ) +// 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() @@ -21,6 +23,8 @@ func LoadConfig(file string) (io.Reader, error) { 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()