2015-10-16 06:03:22 -04:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-10-30 17:42:24 -04:00
|
|
|
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
|
|
|
jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
|
2015-10-16 06:03:22 -04:00
|
|
|
vmessconfig "github.com/v2ray/v2ray-core/proxy/vmess/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConfigTarget struct {
|
2015-11-04 15:52:48 -05:00
|
|
|
Address v2net.Address
|
|
|
|
Users []*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 string `json:"address"`
|
|
|
|
Port v2net.Port `json:"port"`
|
|
|
|
Users []*ConfigUser `json:"users"`
|
|
|
|
}
|
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.")
|
|
|
|
return proxyconfig.BadConfiguration
|
|
|
|
}
|
2015-10-16 08:15:28 -04:00
|
|
|
t.Users = rawConfig.Users
|
2015-10-16 06:03:22 -04:00
|
|
|
ip := net.ParseIP(rawConfig.Address)
|
|
|
|
if ip == nil {
|
|
|
|
log.Error("Unable to parse IP: %s", rawConfig.Address)
|
2015-10-30 17:42:24 -04:00
|
|
|
return proxyconfig.BadConfiguration
|
2015-10-16 06:03:22 -04:00
|
|
|
}
|
|
|
|
t.Address = v2net.IPAddress(ip, rawConfig.Port)
|
|
|
|
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.")
|
|
|
|
return proxyconfig.BadConfiguration
|
|
|
|
}
|
2015-12-04 19:49:03 -05:00
|
|
|
this.TargetList = rawOutbound.TargetList
|
2015-12-04 19:16:21 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Outbound) Receivers() []*vmessconfig.Receiver {
|
|
|
|
targets := make([]*vmessconfig.Receiver, 0, 2*len(o.TargetList))
|
2015-10-16 06:03:22 -04:00
|
|
|
for _, rawTarget := range o.TargetList {
|
|
|
|
users := make([]vmessconfig.User, 0, len(rawTarget.Users))
|
|
|
|
for _, rawUser := range rawTarget.Users {
|
|
|
|
users = append(users, rawUser)
|
|
|
|
}
|
2015-12-04 19:16:21 -05:00
|
|
|
targets = append(targets, &vmessconfig.Receiver{
|
|
|
|
Address: rawTarget.Address,
|
|
|
|
Accounts: users,
|
2015-11-04 15:52:48 -05:00
|
|
|
})
|
2015-10-16 06:03:22 -04:00
|
|
|
}
|
|
|
|
return targets
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2015-10-30 17:42:24 -04:00
|
|
|
jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} {
|
2015-10-16 06:03:22 -04:00
|
|
|
return new(Outbound)
|
|
|
|
})
|
|
|
|
}
|