1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/proxy/vmess/outbound/config_json.go

73 lines
2.2 KiB
Go
Raw Normal View History

// +build json
package outbound
import (
"encoding/json"
2016-06-11 16:52:37 -04:00
"errors"
"github.com/v2ray/v2ray-core/common/log"
2016-07-25 10:48:09 -04: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 11:36:24 -04:00
"github.com/v2ray/v2ray-core/proxy/vmess"
)
func (this *Config) UnmarshalJSON(data []byte) error {
2016-07-25 10:48:09 -04:00
type RawConfigTarget struct {
Address *v2net.AddressJson `json:"address"`
Port v2net.Port `json:"port"`
2016-07-25 11:36:24 -04:00
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.")
return registry.ErrBadConfiguration
}
2016-07-25 10:48:09 -04: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.")
return registry.ErrBadConfiguration
2016-07-25 10:48:09 -04:00
}
if rec.Address == nil {
log.Error("VMess: Address is not set in VMess outbound config.")
return registry.ErrBadConfiguration
2016-07-25 10:48:09 -04:00
}
if rec.Address.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
2016-08-14 16:08:23 -04:00
rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(757086633, nil))
2016-07-25 10:48:09 -04:00
}
spec := protocol.NewServerSpec(v2net.TCPDestination(rec.Address.Address, rec.Port), protocol.AlwaysValid())
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
}
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 10:48:09 -04:00
spec.AddUser(user)
}
serverSpecs[idx] = spec
}
this.Receivers = serverSpecs
return nil
}
func init() {
registry.RegisterOutboundConfig("vmess", func() interface{} { return new(Config) })
}