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

86 lines
2.4 KiB
Go
Raw Normal View History

2015-10-16 10:03:22 +00:00
package json
import (
"encoding/json"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
2016-01-02 22:08:36 +00:00
"github.com/v2ray/v2ray-core/proxy/internal"
proxyconfig "github.com/v2ray/v2ray-core/proxy/internal/config"
jsonconfig "github.com/v2ray/v2ray-core/proxy/internal/config/json"
2015-12-07 19:32:38 +00:00
"github.com/v2ray/v2ray-core/proxy/vmess"
vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json"
"github.com/v2ray/v2ray-core/proxy/vmess/outbound"
2015-10-16 10:03:22 +00:00
)
type ConfigTarget struct {
2015-12-16 22:53:38 +00:00
Destination v2net.Destination
Users []*vmessjson.ConfigUser
2015-10-16 10:03:22 +00:00
}
func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
2015-12-05 00:16:21 +00:00
type RawConfigTarget struct {
Address *v2netjson.Host `json:"address"`
2015-12-07 19:32:38 +00:00
Port v2net.Port `json:"port"`
Users []*vmessjson.ConfigUser `json:"users"`
2015-12-05 00:16:21 +00:00
}
2015-10-16 10:03:22 +00:00
var rawConfig RawConfigTarget
if err := json.Unmarshal(data, &rawConfig); err != nil {
return err
}
2015-12-05 00:16:21 +00:00
if len(rawConfig.Users) == 0 {
log.Error("0 user configured for VMess outbound.")
2016-01-02 22:08:36 +00:00
return internal.ErrorBadConfiguration
2015-12-05 00:16:21 +00:00
}
2015-10-16 12:15:28 +00:00
t.Users = rawConfig.Users
if rawConfig.Address == nil {
log.Error("Address is not set in VMess outbound config.")
2016-01-02 22:08:36 +00:00
return internal.ErrorBadConfiguration
2015-10-16 10:03:22 +00:00
}
t.Destination = v2net.TCPDestination(rawConfig.Address.Address(), rawConfig.Port)
2015-10-16 10:03:22 +00:00
return nil
}
type Outbound struct {
TargetList []*ConfigTarget `json:"vnext"`
}
2015-12-05 00:49:03 +00:00
func (this *Outbound) UnmarshalJSON(data []byte) error {
type RawOutbound struct {
TargetList []*ConfigTarget `json:"vnext"`
}
rawOutbound := &RawOutbound{}
err := json.Unmarshal(data, rawOutbound)
2015-12-05 00:16:21 +00:00
if err != nil {
return err
}
2015-12-05 00:49:03 +00:00
if len(rawOutbound.TargetList) == 0 {
2015-12-05 00:16:21 +00:00
log.Error("0 VMess receiver configured.")
2016-01-02 22:08:36 +00:00
return internal.ErrorBadConfiguration
2015-12-05 00:16:21 +00:00
}
2015-12-05 00:49:03 +00:00
this.TargetList = rawOutbound.TargetList
2015-12-05 00:16:21 +00:00
return nil
}
2015-12-07 19:32:38 +00:00
func (o *Outbound) Receivers() []*outbound.Receiver {
targets := make([]*outbound.Receiver, 0, 2*len(o.TargetList))
2015-10-16 10:03:22 +00:00
for _, rawTarget := range o.TargetList {
2015-12-07 19:32:38 +00:00
users := make([]vmess.User, 0, len(rawTarget.Users))
2015-10-16 10:03:22 +00:00
for _, rawUser := range rawTarget.Users {
users = append(users, rawUser)
}
2015-12-07 19:32:38 +00:00
targets = append(targets, &outbound.Receiver{
2015-12-16 22:53:38 +00:00
Destination: rawTarget.Destination,
Accounts: users,
2015-11-04 20:52:48 +00:00
})
2015-10-16 10:03:22 +00:00
}
return targets
}
func init() {
proxyconfig.RegisterOutboundConnectionConfig("vmess", jsonconfig.JsonConfigLoader(func() interface{} {
2015-10-16 10:03:22 +00:00
return new(Outbound)
}))
2015-10-16 10:03:22 +00:00
}