2016-01-15 06:43:06 -05:00
|
|
|
// +build json
|
|
|
|
|
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-06-11 16:52:37 -04:00
|
|
|
"errors"
|
2016-01-15 06:43:06 -05:00
|
|
|
|
2016-09-17 18:41:21 -04:00
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/log"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/proxy/registry"
|
|
|
|
"v2ray.com/core/proxy/vmess"
|
2016-09-17 18:41:21 -04:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2016-01-15 06:43:06 -05:00
|
|
|
)
|
|
|
|
|
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-09-17 18:46:16 -04:00
|
|
|
return errors.New("VMess|Inbound: Failed to parse detour config: " + err.Error())
|
2016-01-18 19:21:07 -05:00
|
|
|
}
|
2016-09-24 17:11:58 -04:00
|
|
|
this.To = jsonConfig.ToTag
|
2016-01-18 19:21:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-24 17:11:58 -04:00
|
|
|
type FeaturesConfig struct {
|
|
|
|
Detour *DetourConfig `json:"detour"`
|
2016-01-18 19:21:07 -05:00
|
|
|
}
|
|
|
|
|
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-09-17 18:46:16 -04:00
|
|
|
return errors.New("VMess|Inbound: Failed to parse default config: " + err.Error())
|
2016-02-25 08:38:41 -05:00
|
|
|
}
|
2016-09-24 17:11:58 -04:00
|
|
|
this.AlterId = uint32(jsonConfig.AlterIDs)
|
|
|
|
if this.AlterId == 0 {
|
|
|
|
this.AlterId = 32
|
2016-02-25 08:38:41 -05:00
|
|
|
}
|
2016-09-17 18:41:21 -04:00
|
|
|
this.Level = uint32(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-09-17 18:46:16 -04:00
|
|
|
return errors.New("VMess|Inbound: Failed to parse config: " + err.Error())
|
2016-01-18 19:21:07 -05:00
|
|
|
}
|
2016-09-24 17:11:58 -04:00
|
|
|
this.Default = jsonConfig.Defaults
|
|
|
|
if this.Default == nil {
|
|
|
|
this.Default = &DefaultConfig{
|
|
|
|
Level: 0,
|
|
|
|
AlterId: 32,
|
2016-02-25 08:38:41 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-24 17:11:58 -04:00
|
|
|
this.Detour = jsonConfig.DetourConfig
|
2016-04-24 16:54:41 -04:00
|
|
|
// Backward compatibility
|
2016-09-24 17:11:58 -04:00
|
|
|
if jsonConfig.Features != nil && jsonConfig.DetourConfig == nil {
|
|
|
|
this.Detour = jsonConfig.Features.Detour
|
2016-04-24 16:40:43 -04:00
|
|
|
}
|
2016-09-24 17:11:58 -04:00
|
|
|
this.User = make([]*protocol.User, len(jsonConfig.Users))
|
2016-07-25 11:36:24 -04:00
|
|
|
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())
|
|
|
|
}
|
2016-09-17 18:41:21 -04:00
|
|
|
account := new(vmess.AccountPB)
|
2016-07-25 11:36:24 -04:00
|
|
|
if err := json.Unmarshal(rawData, account); err != nil {
|
|
|
|
return errors.New("VMess|Inbound: Invalid user: " + err.Error())
|
|
|
|
}
|
2016-09-17 18:41:21 -04:00
|
|
|
anyAccount, err := ptypes.MarshalAny(account)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("VMess|Inbound: Failed to create account: ", err)
|
|
|
|
return common.ErrBadConfiguration
|
|
|
|
}
|
|
|
|
user.Account = anyAccount
|
2016-09-24 17:11:58 -04:00
|
|
|
this.User[idx] = user
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:21:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 06:43:06 -05:00
|
|
|
func init() {
|
2016-08-17 17:30:15 -04:00
|
|
|
registry.RegisterInboundConfig("vmess", func() interface{} { return new(Config) })
|
2016-01-15 06:43:06 -05:00
|
|
|
}
|