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
|
|
|
|
|
|
|
"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"
|
2016-08-17 17:30:15 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/registry"
|
2016-07-25 11:36:24 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
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 {
|
|
|
|
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
|
|
|
}
|
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-17 17:30:15 -04:00
|
|
|
return registry.ErrBadConfiguration
|
2016-01-15 06:43:06 -05:00
|
|
|
}
|
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.")
|
2016-08-17 17:30:15 -04:00
|
|
|
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.")
|
2016-08-17 17:30:15 -04:00
|
|
|
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
|
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
|
|
|
}
|