allow stdin as input source

pull/314/head
Darien Raymond 8 years ago
parent 1ee76d31fa
commit fd50e8e12b
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -1,6 +1,8 @@
package point package point
import ( import (
"io"
"v2ray.com/core/app/dns" "v2ray.com/core/app/dns"
"v2ray.com/core/app/router" "v2ray.com/core/app/router"
"v2ray.com/core/common" "v2ray.com/core/common"
@ -75,15 +77,15 @@ type Config struct {
TransportConfig *transport.Config TransportConfig *transport.Config
} }
type ConfigLoader func(init string) (*Config, error) type ConfigLoader func(input io.Reader) (*Config, error)
var ( var (
configLoader ConfigLoader configLoader ConfigLoader
) )
func LoadConfig(init string) (*Config, error) { func LoadConfig(input io.Reader) (*Config, error) {
if configLoader == nil { if configLoader == nil {
return nil, common.ErrBadConfiguration return nil, common.ErrBadConfiguration
} }
return configLoader(init) return configLoader(input)
} }

@ -5,8 +5,7 @@ package point
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io"
"os"
"strings" "strings"
"v2ray.com/core/app/dns" "v2ray.com/core/app/dns"
@ -260,16 +259,10 @@ func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func JsonLoadConfig(file string) (*Config, error) { func JsonLoadConfig(input io.Reader) (*Config, error) {
fixedFile := os.ExpandEnv(file)
rawConfig, err := ioutil.ReadFile(fixedFile)
if err != nil {
log.Error("Point: Failed to read server config file (", file, "): ", file, err)
return nil, err
}
jsonConfig := &Config{} jsonConfig := &Config{}
err = json.Unmarshal(rawConfig, jsonConfig) decoder := json.NewDecoder(input)
err := decoder.Decode(jsonConfig)
if err != nil { if err != nil {
log.Error("Point: Failed to load server config: ", err) log.Error("Point: Failed to load server config: ", err)
return nil, err return nil, err

@ -4,6 +4,7 @@ package point_test
import ( import (
"encoding/json" "encoding/json"
"io"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -14,13 +15,19 @@ import (
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
) )
func OpenFile(file string, assert *assert.Assert) io.Reader {
input, err := os.Open(file)
assert.Error(err).IsNil()
return input
}
func TestClientSampleConfig(t *testing.T) { func TestClientSampleConfig(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
GOPATH := os.Getenv("GOPATH") GOPATH := os.Getenv("GOPATH")
baseDir := filepath.Join(GOPATH, "src", "v2ray.com", "core", "tools", "release", "config") baseDir := filepath.Join(GOPATH, "src", "v2ray.com", "core", "tools", "release", "config")
pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) pointConfig, err := LoadConfig(OpenFile(filepath.Join(baseDir, "vpoint_socks_vmess.json"), assert))
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Pointer(pointConfig.InboundConfig).IsNotNil() assert.Pointer(pointConfig.InboundConfig).IsNotNil()
@ -40,7 +47,7 @@ func TestServerSampleConfig(t *testing.T) {
GOPATH := os.Getenv("GOPATH") GOPATH := os.Getenv("GOPATH")
baseDir := filepath.Join(GOPATH, "src", "v2ray.com", "core", "tools", "release", "config") baseDir := filepath.Join(GOPATH, "src", "v2ray.com", "core", "tools", "release", "config")
pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json")) pointConfig, err := LoadConfig(OpenFile(filepath.Join(baseDir, "vpoint_vmess_freedom.json"), assert))
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Pointer(pointConfig.InboundConfig).IsNotNil() assert.Pointer(pointConfig.InboundConfig).IsNotNil()

@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
@ -38,6 +39,7 @@ var (
logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error") logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error")
version = flag.Bool("version", false, "Show current version of V2Ray.") version = flag.Bool("version", false, "Show current version of V2Ray.")
test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.") test = flag.Bool("test", false, "Test config file only, without launching V2Ray server.")
format = flag.String("format", "json", "Format of input file.")
) )
func init() { func init() {
@ -68,7 +70,20 @@ func startV2Ray() *point.Point {
log.Error("Config file is not set.") log.Error("Config file is not set.")
return nil return nil
} }
config, err := point.LoadConfig(configFile) var configInput io.Reader
if configFile == "stdin:" {
configInput = os.Stdin
} else {
fixedFile := os.ExpandEnv(configFile)
file, err := os.Open(fixedFile)
if err != nil {
log.Error("Config file not readable: ", err)
return nil
}
defer file.Close()
configInput = file
}
config, err := point.LoadConfig(configInput)
if err != nil { if err != nil {
log.Error("Failed to read config file (", configFile, "): ", configFile, err) log.Error("Failed to read config file (", configFile, "): ", configFile, err)
return nil return nil

Loading…
Cancel
Save