mirror of https://github.com/v2ray/v2ray-core
proper handle stdin
parent
1bb34bfe17
commit
75f0879c12
11
config.go
11
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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue