mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-22 01:06:30 -05:00
proper handle stdin
This commit is contained in:
parent
1bb34bfe17
commit
75f0879c12
11
config.go
11
config.go
@ -5,6 +5,7 @@ package core
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
@ -92,9 +93,15 @@ func init() {
|
|||||||
if len(v) == 0 {
|
if len(v) == 0 {
|
||||||
return nil, newError("input has no element")
|
return nil, newError("input has no element")
|
||||||
}
|
}
|
||||||
|
var data []byte
|
||||||
|
var rerr error
|
||||||
// pb type can only handle the first config
|
// pb type can only handle the first config
|
||||||
data, err := ioutil.ReadFile(v[0])
|
if v[0] == "stdin:" {
|
||||||
common.Must(err)
|
data, rerr = buf.ReadAllToBytes(os.Stdin)
|
||||||
|
} else {
|
||||||
|
data, rerr = ioutil.ReadFile(v[0])
|
||||||
|
}
|
||||||
|
common.Must(rerr)
|
||||||
return loadProtobufConfig(data)
|
return loadProtobufConfig(data)
|
||||||
case io.Reader:
|
case io.Reader:
|
||||||
data, err := buf.ReadAllToBytes(v)
|
data, err := buf.ReadAllToBytes(v)
|
||||||
|
@ -63,6 +63,8 @@ func (c *ConfigCommand) LoadArg(arg string) (out io.Reader, err error) {
|
|||||||
var data []byte
|
var data []byte
|
||||||
if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
|
if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
|
||||||
data, err = FetchHTTPContent(arg)
|
data, err = FetchHTTPContent(arg)
|
||||||
|
} else if arg == "stdin:" {
|
||||||
|
data, err = ioutil.ReadAll(os.Stdin)
|
||||||
} else {
|
} else {
|
||||||
data, err = ioutil.ReadFile(arg)
|
data, err = ioutil.ReadFile(arg)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ package json
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
"v2ray.com/core"
|
"v2ray.com/core"
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
@ -20,7 +21,7 @@ func init() {
|
|||||||
Loader: func(input interface{}) (*core.Config, error) {
|
Loader: func(input interface{}) (*core.Config, error) {
|
||||||
switch v := input.(type) {
|
switch v := input.(type) {
|
||||||
case cmdarg.Arg:
|
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 {
|
if err != nil {
|
||||||
return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning()
|
return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning()
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -26,6 +27,7 @@ func init() {
|
|||||||
case cmdarg.Arg:
|
case cmdarg.Arg:
|
||||||
cf := &conf.Config{}
|
cf := &conf.Config{}
|
||||||
for _, arg := range v {
|
for _, arg := range v {
|
||||||
|
newError("Reading config: ", arg).AtInfo().WriteToLog()
|
||||||
r, err := LoadArg(arg)
|
r, err := LoadArg(arg)
|
||||||
common.Must(err)
|
common.Must(err)
|
||||||
c, err := serial.DecodeJSONConfig(r)
|
c, err := serial.DecodeJSONConfig(r)
|
||||||
@ -47,6 +49,8 @@ func LoadArg(arg string) (out io.Reader, err error) {
|
|||||||
var data []byte
|
var data []byte
|
||||||
if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
|
if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
|
||||||
data, err = FetchHTTPContent(arg)
|
data, err = FetchHTTPContent(arg)
|
||||||
|
} else if arg == "stdin:" {
|
||||||
|
data, err = ioutil.ReadAll(os.Stdin)
|
||||||
} else {
|
} else {
|
||||||
data, err = ioutil.ReadFile(arg)
|
data, err = ioutil.ReadFile(arg)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user