2016-01-15 06:43:06 -05:00
|
|
|
// +build json
|
|
|
|
|
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-06-11 16:52:37 -04:00
|
|
|
"errors"
|
2016-01-15 06:43:06 -05:00
|
|
|
|
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"
|
2016-01-15 06:43:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (this *Config) UnmarshalJSON(data []byte) error {
|
2016-07-25 10:48:09 -04:00
|
|
|
type RawConfigTarget struct {
|
2016-08-26 18:04:35 -04:00
|
|
|
Address *v2net.AddressPB `json:"address"`
|
|
|
|
Port v2net.Port `json:"port"`
|
|
|
|
Users []json.RawMessage `json:"users"`
|
2016-07-25 10:48:09 -04:00
|
|
|
}
|
2016-01-15 06:43:06 -05:00
|
|
|
type RawOutbound struct {
|
2016-07-25 10:48:09 -04:00
|
|
|
Receivers []*RawConfigTarget `json:"vnext"`
|
2016-01-15 06:43:06 -05:00
|
|
|
}
|
|
|
|
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())
|
2016-01-15 06:43:06 -05:00
|
|
|
}
|
|
|
|
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-01-15 06:43:06 -05:00
|
|
|
}
|
2016-09-24 17:11:58 -04:00
|
|
|
serverSpecs := make([]*protocol.ServerSpecPB, 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}) {
|
|
|
|
rec.Address.Address = &v2net.AddressPB_Ip{
|
|
|
|
Ip: serial.Uint32ToBytes(757086633, nil),
|
|
|
|
}
|
2016-07-25 10:48:09 -04:00
|
|
|
}
|
2016-09-24 17:11:58 -04:00
|
|
|
spec := &protocol.ServerSpecPB{
|
|
|
|
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-09-17 18:41:21 -04:00
|
|
|
account := new(vmess.AccountPB)
|
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
|
2016-01-15 06:43:06 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-08-17 17:30:15 -04:00
|
|
|
registry.RegisterOutboundConfig("vmess", func() interface{} { return new(Config) })
|
2016-01-15 06:43:06 -05:00
|
|
|
}
|