Browse Source

proper handle stdin

vcptr 6 years ago
parent
commit
75f0879c12
4 changed files with 17 additions and 3 deletions
  1. 9 2
      config.go
  2. 2 0
      infra/control/config.go
  3. 2 1
      main/json/config_json.go
  4. 4 0
      main/jsonem/jsonem.go

+ 9 - 2
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)

+ 2 - 0
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)
 	}

+ 2 - 1
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()
 				}

+ 4 - 0
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)
 	}