1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-08 18:36:45 -05:00
v2fly/proxy/vmess/outbound/json/outbound.go

86 lines
2.4 KiB
Go
Raw Normal View History

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