json config parser

This commit is contained in:
Darien Raymond
2016-10-17 14:35:13 +02:00
parent e866ff24a4
commit 5a311cbe08
85 changed files with 9357 additions and 9052 deletions
+2 -5
View File
@@ -4,14 +4,13 @@ import (
"testing"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/loader"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy/vmess"
. "v2ray.com/core/proxy/vmess/encoding"
"v2ray.com/core/testing/assert"
"github.com/golang/protobuf/ptypes"
)
func TestRequestSerialization(t *testing.T) {
@@ -25,9 +24,7 @@ func TestRequestSerialization(t *testing.T) {
Id: uuid.New().String(),
AlterId: 0,
}
anyAccount, err := ptypes.MarshalAny(account)
assert.Error(err).IsNil()
user.Account = anyAccount
user.Account = loader.NewTypedSettings(account)
expectedRequest := &protocol.RequestHeader{
Version: 1,
+10
View File
@@ -1,3 +1,13 @@
package inbound
import ()
func (this *Config) GetDefaultValue() *DefaultConfig {
if this.GetDefault() == nil {
return &DefaultConfig{
AlterId: 32,
Level: 0,
}
}
return this.Default
}
-85
View File
@@ -1,85 +0,0 @@
// +build json
package inbound
import (
"encoding/json"
"errors"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/vmess"
)
func (this *DetourConfig) UnmarshalJSON(data []byte) error {
type JsonDetourConfig struct {
ToTag string `json:"to"`
}
jsonConfig := new(JsonDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return errors.New("VMess|Inbound: Failed to parse detour config: " + err.Error())
}
this.To = jsonConfig.ToTag
return nil
}
type FeaturesConfig struct {
Detour *DetourConfig `json:"detour"`
}
func (this *DefaultConfig) UnmarshalJSON(data []byte) error {
type JsonDefaultConfig struct {
AlterIDs uint16 `json:"alterId"`
Level byte `json:"level"`
}
jsonConfig := new(JsonDefaultConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return errors.New("VMess|Inbound: Failed to parse default config: " + err.Error())
}
this.AlterId = uint32(jsonConfig.AlterIDs)
if this.AlterId == 0 {
this.AlterId = 32
}
this.Level = uint32(jsonConfig.Level)
return nil
}
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Users []json.RawMessage `json:"clients"`
Features *FeaturesConfig `json:"features"`
Defaults *DefaultConfig `json:"default"`
DetourConfig *DetourConfig `json:"detour"`
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return errors.New("VMess|Inbound: Failed to parse config: " + err.Error())
}
this.Default = jsonConfig.Defaults
if this.Default == nil {
this.Default = &DefaultConfig{
Level: 0,
AlterId: 32,
}
}
this.Detour = jsonConfig.DetourConfig
// Backward compatibility
if jsonConfig.Features != nil && jsonConfig.DetourConfig == nil {
this.Detour = jsonConfig.Features.Detour
}
this.User = make([]*protocol.User, len(jsonConfig.Users))
for idx, rawData := range jsonConfig.Users {
user := new(protocol.User)
if err := json.Unmarshal(rawData, user); err != nil {
return errors.New("VMess|Inbound: Invalid user: " + err.Error())
}
account := new(vmess.Account)
if err := json.Unmarshal(rawData, account); err != nil {
return errors.New("VMess|Inbound: Invalid user: " + err.Error())
}
user.Account = loader.NewTypedSettings(account)
this.User[idx] = user
}
return nil
}
+1 -1
View File
@@ -268,7 +268,7 @@ func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.
packetDispatcher: space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
clients: allowedClients,
detours: config.Detour,
usersByEmail: NewUserByEmail(config.User, config.Default),
usersByEmail: NewUserByEmail(config.User, config.GetDefaultValue()),
meta: meta,
}
-73
View File
@@ -1,73 +0,0 @@
// +build json
package outbound
import (
"encoding/json"
"errors"
"v2ray.com/core/common"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy/vmess"
)
func (this *Config) UnmarshalJSON(data []byte) error {
type RawConfigTarget struct {
Address *v2net.IPOrDomain `json:"address"`
Port v2net.Port `json:"port"`
Users []json.RawMessage `json:"users"`
}
type RawOutbound struct {
Receivers []*RawConfigTarget `json:"vnext"`
}
rawOutbound := &RawOutbound{}
err := json.Unmarshal(data, rawOutbound)
if err != nil {
return errors.New("VMessOut: Failed to parse config: " + err.Error())
}
if len(rawOutbound.Receivers) == 0 {
log.Error("VMessOut: 0 VMess receiver configured.")
return common.ErrBadConfiguration
}
serverSpecs := make([]*protocol.ServerEndpoint, len(rawOutbound.Receivers))
for idx, rec := range rawOutbound.Receivers {
if len(rec.Users) == 0 {
log.Error("VMess: 0 user configured for VMess outbound.")
return common.ErrBadConfiguration
}
if rec.Address == nil {
log.Error("VMess: Address is not set in VMess outbound config.")
return common.ErrBadConfiguration
}
if rec.Address.AsAddress().String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
rec.Address.Address = &v2net.IPOrDomain_Ip{
Ip: serial.Uint32ToBytes(757086633, nil),
}
}
spec := &protocol.ServerEndpoint{
Address: rec.Address,
Port: uint32(rec.Port),
}
for _, rawUser := range rec.Users {
user := new(protocol.User)
if err := json.Unmarshal(rawUser, user); err != nil {
log.Error("VMess|Outbound: Invalid user: ", err)
return err
}
account := new(vmess.Account)
if err := json.Unmarshal(rawUser, account); err != nil {
log.Error("VMess|Outbound: Invalid user: ", err)
return err
}
user.Account = loader.NewTypedSettings(account)
spec.User = append(spec.User, user)
}
serverSpecs[idx] = spec
}
this.Receiver = serverSpecs
return nil
}
-38
View File
@@ -1,38 +0,0 @@
// +build json
package outbound_test
import (
"encoding/json"
"testing"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/vmess"
. "v2ray.com/core/proxy/vmess/outbound"
"v2ray.com/core/testing/assert"
)
func TestConfigTargetParsing(t *testing.T) {
assert := assert.On(t)
rawJson := `{
"vnext": [{
"address": "127.0.0.1",
"port": 80,
"users": [
{
"id": "e641f5ad-9397-41e3-bf1a-e8740dfed019",
"email": "love@v2ray.com",
"level": 255
}
]
}]
}`
config := new(Config)
err := json.Unmarshal([]byte(rawJson), &config)
assert.Error(err).IsNil()
specPB := config.Receiver[0]
spec := protocol.NewServerSpecFromPB(vmess.NewAccount, *specPB)
assert.Destination(spec.Destination()).EqualsString("tcp:127.0.0.1:80")
}