v2ray-core/proxy/vmess/outbound/config_json.go

74 lines
2.2 KiB
Go
Raw Normal View History

// +build json
package outbound
import (
"encoding/json"
2016-06-11 20:52:37 +00:00
"errors"
2016-08-18 06:21:20 +00:00
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/log"
2016-07-25 14:48:09 +00:00
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/protocol"
"github.com/v2ray/v2ray-core/common/serial"
"github.com/v2ray/v2ray-core/proxy/registry"
2016-07-25 15:36:24 +00:00
"github.com/v2ray/v2ray-core/proxy/vmess"
)
func (this *Config) UnmarshalJSON(data []byte) error {
2016-07-25 14:48:09 +00:00
type RawConfigTarget struct {
Address *v2net.AddressJson `json:"address"`
Port v2net.Port `json:"port"`
2016-07-25 15:36:24 +00:00
Users []json.RawMessage `json:"users"`
2016-07-25 14:48:09 +00:00
}
type RawOutbound struct {
2016-07-25 14:48:09 +00:00
Receivers []*RawConfigTarget `json:"vnext"`
}
rawOutbound := &RawOutbound{}
err := json.Unmarshal(data, rawOutbound)
if err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("VMessOut: Failed to parse config: " + err.Error())
}
if len(rawOutbound.Receivers) == 0 {
2016-06-11 20:52:37 +00:00
log.Error("VMessOut: 0 VMess receiver configured.")
2016-08-18 06:21:20 +00:00
return common.ErrBadConfiguration
}
2016-07-25 14:48:09 +00:00
serverSpecs := make([]*protocol.ServerSpec, len(rawOutbound.Receivers))
for idx, rec := range rawOutbound.Receivers {
if len(rec.Users) == 0 {
log.Error("VMess: 0 user configured for VMess outbound.")
2016-08-18 06:21:20 +00:00
return common.ErrBadConfiguration
2016-07-25 14:48:09 +00:00
}
if rec.Address == nil {
log.Error("VMess: Address is not set in VMess outbound config.")
2016-08-18 06:21:20 +00:00
return common.ErrBadConfiguration
2016-07-25 14:48:09 +00:00
}
if rec.Address.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
2016-08-14 20:08:23 +00:00
rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(757086633, nil))
2016-07-25 14:48:09 +00:00
}
spec := protocol.NewServerSpec(v2net.TCPDestination(rec.Address.Address, rec.Port), protocol.AlwaysValid())
2016-07-25 15:36:24 +00:00
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 = account
2016-07-25 14:48:09 +00:00
spec.AddUser(user)
}
serverSpecs[idx] = spec
}
this.Receivers = serverSpecs
return nil
}
func init() {
registry.RegisterOutboundConfig("vmess", func() interface{} { return new(Config) })
}