json config parser

This commit is contained in:
Darien Raymond committed 2016-10-17 14:35:13 +02:00
1 parent e866ff24a4
commit 5a311cbe08
85 files changed
+9357 -9052

No files matched your search

+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,
}