1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-02 16:56:02 -04:00
v2fly/proxy/vmess/outbound/config_json.go

85 lines
2.3 KiB
Go
Raw Normal View History

// +build json
package outbound
import (
"encoding/json"
2016-06-11 16:52:37 -04:00
"errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common"
"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/registry"
"v2ray.com/core/proxy/vmess"
2016-09-17 18:41:21 -04:00
"github.com/golang/protobuf/ptypes"
)
func (this *Config) UnmarshalJSON(data []byte) error {
2016-07-25 10:48:09 -04:00
type RawConfigTarget struct {
2016-10-12 12:43:55 -04:00
Address *v2net.IPOrDomain `json:"address"`
2016-08-26 18:04:35 -04:00
Port v2net.Port `json:"port"`
Users []json.RawMessage `json:"users"`
2016-07-25 10:48:09 -04:00
}
type RawOutbound struct {
2016-07-25 10:48:09 -04:00
Receivers []*RawConfigTarget `json:"vnext"`
}
rawOutbound := &RawOutbound{}
err := json.Unmarshal(data, rawOutbound)
if err != nil {
2016-06-11 16:52:37 -04:00
return errors.New("VMessOut: Failed to parse config: " + err.Error())
}
if len(rawOutbound.Receivers) == 0 {
2016-06-11 16:52:37 -04:00
log.Error("VMessOut: 0 VMess receiver configured.")
2016-08-18 02:21:20 -04:00
return common.ErrBadConfiguration
}
2016-10-12 12:43:55 -04:00
serverSpecs := make([]*protocol.ServerEndpoint, len(rawOutbound.Receivers))
2016-07-25 10:48:09 -04:00
for idx, rec := range rawOutbound.Receivers {
if len(rec.Users) == 0 {
log.Error("VMess: 0 user configured for VMess outbound.")
2016-08-18 02:21:20 -04:00
return common.ErrBadConfiguration
2016-07-25 10:48:09 -04:00
}
if rec.Address == nil {
log.Error("VMess: Address is not set in VMess outbound config.")
2016-08-18 02:21:20 -04:00
return common.ErrBadConfiguration
2016-07-25 10:48:09 -04:00
}
2016-08-26 18:04:35 -04:00
if rec.Address.AsAddress().String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
2016-10-12 12:43:55 -04:00
rec.Address.Address = &v2net.IPOrDomain_Ip{
2016-08-26 18:04:35 -04:00
Ip: serial.Uint32ToBytes(757086633, nil),
}
2016-07-25 10:48:09 -04:00
}
2016-10-12 12:43:55 -04:00
spec := &protocol.ServerEndpoint{
2016-09-24 17:11:58 -04:00
Address: rec.Address,
Port: uint32(rec.Port),
}
2016-07-25 11:36:24 -04: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
}
2016-10-12 12:43:55 -04:00
account := new(vmess.Account)
2016-07-25 11:36:24 -04:00
if err := json.Unmarshal(rawUser, account); err != nil {
log.Error("VMess|Outbound: Invalid user: ", err)
return err
}
2016-09-17 18:41:21 -04:00
anyAccount, err := ptypes.MarshalAny(account)
if err != nil {
log.Error("VMess|Outbound: Failed to create account: ", err)
return common.ErrBadConfiguration
}
user.Account = anyAccount
2016-09-25 16:57:27 -04:00
spec.User = append(spec.User, user)
2016-07-25 10:48:09 -04:00
}
serverSpecs[idx] = spec
}
2016-09-24 17:11:58 -04:00
this.Receiver = serverSpecs
return nil
}
func init() {
registry.RegisterOutboundConfig("vmess", func() interface{} { return new(Config) })
}