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

proper handle stdin

This commit is contained in:
vcptr 2019-12-14 23:44:10 +08:00
parent 1bb34bfe17
commit 75f0879c12
4 changed files with 17 additions and 3 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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()
}

View File

@ -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)
}