Simpilify configuration files

This commit is contained in:
V2Ray
2015-10-06 23:11:08 +02:00
parent 78daf8a879
commit 72b4eeba8b
27 changed files with 251 additions and 208 deletions
+19
View File
@@ -0,0 +1,19 @@
package config
type Type string
const (
TypeInbound = Type("inbound")
TypeOutbound = Type("outbound")
)
type ConnectionConfig interface {
Protocol() string
Settings(configType Type) interface{}
}
type PointConfig interface {
Port() uint16
InboundConfig() ConnectionConfig
OutboundConfig() ConnectionConfig
}
+20
View File
@@ -0,0 +1,20 @@
package json
import (
"github.com/v2ray/v2ray-core/config"
)
type ConfigObjectCreator func() interface{}
var (
configCache = make(map[string]ConfigObjectCreator)
)
func getConfigKey(protocol string, cType config.Type) string {
return protocol + "_" + string(cType)
}
func RegisterConfigType(protocol string, cType config.Type, creator ConfigObjectCreator) {
// TODO: check name
configCache[getConfigKey(protocol, cType)] = creator
}
+16 -22
View File
@@ -4,30 +4,32 @@ import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/config"
)
type ConnectionConfig struct {
ProtocolString string `json:"protocol"`
File string `json:"file"`
ProtocolString string `json:"protocol"`
SettingsMessage json.RawMessage `json:"settings"`
}
func (config *ConnectionConfig) Protocol() string {
return config.ProtocolString
}
func (config *ConnectionConfig) Content() []byte {
if len(config.File) == 0 {
return nil
func (config *ConnectionConfig) Settings(configType config.Type) interface{} {
creator, found := configCache[getConfigKey(config.Protocol(), configType)]
if !found {
panic("Unknown protocol " + config.Protocol())
}
content, err := ioutil.ReadFile(config.File)
configObj := creator()
err := json.Unmarshal(config.SettingsMessage, configObj)
if err != nil {
panic(log.Error("Failed to read config file (%s): %v", config.File, err))
log.Error("Unable to parse connection config: %v", err)
panic("Failed to parse connection config.")
}
return content
return configObj
}
// Config is the config for Point server.
@@ -41,11 +43,11 @@ func (config *Config) Port() uint16 {
return config.PortValue
}
func (config *Config) InboundConfig() core.ConnectionConfig {
func (config *Config) InboundConfig() config.ConnectionConfig {
return config.InboundConfigValue
}
func (config *Config) OutboundConfig() core.ConnectionConfig {
func (config *Config) OutboundConfig() config.ConnectionConfig {
return config.OutboundConfigValue
}
@@ -53,24 +55,16 @@ func LoadConfig(file string) (*Config, error) {
fixedFile := os.ExpandEnv(file)
rawConfig, err := ioutil.ReadFile(fixedFile)
if err != nil {
log.Error("Failed to read point config file (%s): %v", file, err)
log.Error("Failed to read server config file (%s): %v", file, err)
return nil, err
}
config := &Config{}
err = json.Unmarshal(rawConfig, config)
if err != nil {
log.Error("Failed to load point config: %v", err)
log.Error("Failed to load server config: %v", err)
return nil, err
}
if !filepath.IsAbs(config.InboundConfigValue.File) && len(config.InboundConfigValue.File) > 0 {
config.InboundConfigValue.File = filepath.Join(filepath.Dir(fixedFile), config.InboundConfigValue.File)
}
if !filepath.IsAbs(config.OutboundConfigValue.File) && len(config.OutboundConfigValue.File) > 0 {
config.OutboundConfigValue.File = filepath.Join(filepath.Dir(fixedFile), config.OutboundConfigValue.File)
}
return config, err
}
+23 -17
View File
@@ -1,9 +1,15 @@
package json
package json_test
import (
"path/filepath"
"testing"
"github.com/v2ray/v2ray-core/config"
"github.com/v2ray/v2ray-core/config/json"
_ "github.com/v2ray/v2ray-core/proxy/freedom/config/json"
_ "github.com/v2ray/v2ray-core/proxy/socks/config/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess"
"github.com/v2ray/v2ray-core/testing/unit"
)
@@ -13,18 +19,18 @@ func TestClientSampleConfig(t *testing.T) {
// TODO: fix for Windows
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
config, err := LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json"))
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json"))
assert.Error(err).IsNil()
assert.Uint16(config.Port()).Positive()
assert.Pointer(config.InboundConfig()).IsNotNil()
assert.Pointer(config.OutboundConfig()).IsNotNil()
assert.Uint16(pointConfig.Port()).Positive()
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
assert.String(config.InboundConfig().Protocol()).Equals("socks")
assert.Int(len(config.InboundConfig().Content())).GreaterThan(0)
assert.String(pointConfig.InboundConfig().Protocol()).Equals("socks")
assert.Pointer(pointConfig.InboundConfig().Settings(config.TypeInbound)).IsNotNil()
assert.String(config.OutboundConfig().Protocol()).Equals("vmess")
assert.Int(len(config.OutboundConfig().Content())).GreaterThan(0)
assert.String(pointConfig.OutboundConfig().Protocol()).Equals("vmess")
assert.Pointer(pointConfig.OutboundConfig().Settings(config.TypeOutbound)).IsNotNil()
}
func TestServerSampleConfig(t *testing.T) {
@@ -33,16 +39,16 @@ func TestServerSampleConfig(t *testing.T) {
// TODO: fix for Windows
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
config, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json"))
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json"))
assert.Error(err).IsNil()
assert.Uint16(config.Port()).Positive()
assert.Pointer(config.InboundConfig()).IsNotNil()
assert.Pointer(config.OutboundConfig()).IsNotNil()
assert.Uint16(pointConfig.Port()).Positive()
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
assert.String(config.InboundConfig().Protocol()).Equals("vmess")
assert.Int(len(config.InboundConfig().Content())).GreaterThan(0)
assert.String(pointConfig.InboundConfig().Protocol()).Equals("vmess")
assert.Pointer(pointConfig.InboundConfig().Settings(config.TypeInbound)).IsNotNil()
assert.String(config.OutboundConfig().Protocol()).Equals("freedom")
assert.Int(len(config.OutboundConfig().Content())).Equals(0)
assert.String(pointConfig.OutboundConfig().Protocol()).Equals("freedom")
assert.Pointer(pointConfig.OutboundConfig().Settings(config.TypeOutbound)).IsNotNil()
}