1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-30 15:57:35 -04:00
v2fly/proxy/vmess/inbound/config_json.go

99 lines
2.8 KiB
Go
Raw Normal View History

// +build json
package inbound
import (
"encoding/json"
2016-06-11 16:52:37 -04:00
"errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/registry"
"v2ray.com/core/proxy/vmess"
)
2016-01-18 19:21:07 -05:00
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 {
2016-06-11 16:52:37 -04:00
return errors.New("VMessIn: Failed to parse detour config: " + err.Error())
2016-01-18 19:21:07 -05:00
}
this.ToTag = jsonConfig.ToTag
return nil
}
func (this *FeaturesConfig) UnmarshalJSON(data []byte) error {
type JsonFeaturesConfig struct {
Detour *DetourConfig `json:"detour"`
}
jsonConfig := new(JsonFeaturesConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 16:52:37 -04:00
return errors.New("VMessIn: Failed to parse features config: " + err.Error())
2016-01-18 19:21:07 -05:00
}
this.Detour = jsonConfig.Detour
return nil
}
2016-02-25 08:38:41 -05:00
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 {
2016-06-11 16:52:37 -04:00
return errors.New("VMessIn: Failed to parse default config: " + err.Error())
2016-02-25 08:38:41 -05:00
}
this.AlterIDs = jsonConfig.AlterIDs
if this.AlterIDs == 0 {
this.AlterIDs = 32
}
2016-05-07 14:26:29 -04:00
this.Level = protocol.UserLevel(jsonConfig.Level)
2016-02-25 08:38:41 -05:00
return nil
}
2016-01-18 19:21:07 -05:00
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
2016-07-25 11:36:24 -04:00
Users []json.RawMessage `json:"clients"`
Features *FeaturesConfig `json:"features"`
Defaults *DefaultConfig `json:"default"`
DetourConfig *DetourConfig `json:"detour"`
2016-01-18 19:21:07 -05:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 16:52:37 -04:00
return errors.New("VMessIn: Failed to parse config: " + err.Error())
2016-01-18 19:21:07 -05:00
}
2016-04-24 16:54:41 -04:00
this.Features = jsonConfig.Features // Backward compatibility
2016-02-25 08:38:41 -05:00
this.Defaults = jsonConfig.Defaults
if this.Defaults == nil {
this.Defaults = &DefaultConfig{
2016-05-07 14:26:29 -04:00
Level: protocol.UserLevel(0),
2016-02-25 08:38:41 -05:00
AlterIDs: 32,
}
}
2016-06-01 16:33:45 -04:00
this.DetourConfig = jsonConfig.DetourConfig
2016-04-24 16:54:41 -04:00
// Backward compatibility
2016-04-24 16:40:43 -04:00
if this.Features != nil && this.DetourConfig == nil {
this.DetourConfig = this.Features.Detour
}
2016-07-25 11:36:24 -04:00
this.AllowedUsers = 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 = account
this.AllowedUsers[idx] = user
}
2016-01-18 19:21:07 -05:00
return nil
}
func init() {
registry.RegisterInboundConfig("vmess", func() interface{} { return new(Config) })
}