2016-10-17 08:35:13 -04:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-12-07 15:43:41 -05:00
|
|
|
"strings"
|
|
|
|
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-10-17 08:35:13 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
"v2ray.com/core/proxy/vmess"
|
|
|
|
"v2ray.com/core/proxy/vmess/inbound"
|
|
|
|
"v2ray.com/core/proxy/vmess/outbound"
|
|
|
|
)
|
|
|
|
|
|
|
|
type VMessAccount struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
AlterIds uint16 `json:"alterId"`
|
2016-12-07 15:43:41 -05:00
|
|
|
Security string `json:"security"`
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *VMessAccount) Build() *vmess.Account {
|
2016-12-07 15:43:41 -05:00
|
|
|
var st protocol.SecurityType
|
|
|
|
switch strings.ToLower(v.Security) {
|
|
|
|
case "aes-128-gcm":
|
|
|
|
st = protocol.SecurityType_AES128_GCM
|
|
|
|
case "chacha20-poly1305":
|
|
|
|
st = protocol.SecurityType_CHACHA20_POLY1305
|
2016-12-11 08:58:53 -05:00
|
|
|
case "auto":
|
|
|
|
st = protocol.SecurityType_AUTO
|
2016-12-07 15:43:41 -05:00
|
|
|
case "none":
|
|
|
|
st = protocol.SecurityType_NONE
|
|
|
|
default:
|
|
|
|
st = protocol.SecurityType_LEGACY
|
|
|
|
}
|
2016-10-17 08:35:13 -04:00
|
|
|
return &vmess.Account{
|
2016-12-11 08:58:53 -05:00
|
|
|
Id: v.ID,
|
|
|
|
AlterId: uint32(v.AlterIds),
|
|
|
|
SecuritySettings: &protocol.SecurityConfig{
|
|
|
|
Type: st,
|
|
|
|
},
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type VMessDetourConfig struct {
|
|
|
|
ToTag string `json:"to"`
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *VMessDetourConfig) Build() *inbound.DetourConfig {
|
2016-10-17 08:35:13 -04:00
|
|
|
return &inbound.DetourConfig{
|
2016-11-27 15:39:09 -05:00
|
|
|
To: v.ToTag,
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type FeaturesConfig struct {
|
|
|
|
Detour *VMessDetourConfig `json:"detour"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type VMessDefaultConfig struct {
|
|
|
|
AlterIDs uint16 `json:"alterId"`
|
|
|
|
Level byte `json:"level"`
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *VMessDefaultConfig) Build() *inbound.DefaultConfig {
|
2016-10-17 08:35:13 -04:00
|
|
|
config := new(inbound.DefaultConfig)
|
2016-11-27 15:39:09 -05:00
|
|
|
config.AlterId = uint32(v.AlterIDs)
|
2016-10-17 08:35:13 -04:00
|
|
|
if config.AlterId == 0 {
|
|
|
|
config.AlterId = 32
|
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
config.Level = uint32(v.Level)
|
2016-10-17 08:35:13 -04:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
type VMessInboundConfig struct {
|
|
|
|
Users []json.RawMessage `json:"clients"`
|
|
|
|
Features *FeaturesConfig `json:"features"`
|
|
|
|
Defaults *VMessDefaultConfig `json:"default"`
|
|
|
|
DetourConfig *VMessDetourConfig `json:"detour"`
|
|
|
|
}
|
|
|
|
|
2016-12-15 05:51:09 -05:00
|
|
|
func (v *VMessInboundConfig) Build() (*serial.TypedMessage, error) {
|
2016-10-17 08:35:13 -04:00
|
|
|
config := new(inbound.Config)
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if v.Defaults != nil {
|
|
|
|
config.Default = v.Defaults.Build()
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if v.DetourConfig != nil {
|
|
|
|
config.Detour = v.DetourConfig.Build()
|
|
|
|
} else if v.Features != nil && v.Features.Detour != nil {
|
|
|
|
config.Detour = v.Features.Detour.Build()
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
config.User = make([]*protocol.User, len(v.Users))
|
|
|
|
for idx, rawData := range v.Users {
|
2016-10-17 08:35:13 -04:00
|
|
|
user := new(protocol.User)
|
|
|
|
if err := json.Unmarshal(rawData, user); err != nil {
|
2017-02-02 08:42:31 -05:00
|
|
|
return nil, errors.Base(err).Message("Config: Invalid VMess user.")
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
account := new(VMessAccount)
|
|
|
|
if err := json.Unmarshal(rawData, account); err != nil {
|
2017-02-02 08:42:31 -05:00
|
|
|
return nil, errors.Base(err).Message("Config: Invalid VMess user.")
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
2016-12-15 05:51:09 -05:00
|
|
|
user.Account = serial.ToTypedMessage(account.Build())
|
2016-10-17 08:35:13 -04:00
|
|
|
config.User[idx] = user
|
|
|
|
}
|
|
|
|
|
2016-12-15 05:51:09 -05:00
|
|
|
return serial.ToTypedMessage(config), nil
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type VMessOutboundTarget struct {
|
|
|
|
Address *Address `json:"address"`
|
|
|
|
Port uint16 `json:"port"`
|
|
|
|
Users []json.RawMessage `json:"users"`
|
|
|
|
}
|
|
|
|
type VMessOutboundConfig struct {
|
|
|
|
Receivers []*VMessOutboundTarget `json:"vnext"`
|
|
|
|
}
|
|
|
|
|
2016-12-15 05:51:09 -05:00
|
|
|
func (v *VMessOutboundConfig) Build() (*serial.TypedMessage, error) {
|
2016-10-17 08:35:13 -04:00
|
|
|
config := new(outbound.Config)
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
if len(v.Receivers) == 0 {
|
2017-02-02 08:42:31 -05:00
|
|
|
return nil, errors.New("Config: 0 VMess receiver configured.")
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
serverSpecs := make([]*protocol.ServerEndpoint, len(v.Receivers))
|
|
|
|
for idx, rec := range v.Receivers {
|
2016-10-17 08:35:13 -04:00
|
|
|
if len(rec.Users) == 0 {
|
2017-02-02 08:42:31 -05:00
|
|
|
return nil, errors.New("Config: 0 user configured for VMess outbound.")
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
if rec.Address == nil {
|
2017-02-02 08:42:31 -05:00
|
|
|
return nil, errors.New("Config: Address is not set in VMess outbound config.")
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
if rec.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) {
|
|
|
|
rec.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(757086633, nil))
|
|
|
|
}
|
|
|
|
spec := &protocol.ServerEndpoint{
|
|
|
|
Address: rec.Address.Build(),
|
|
|
|
Port: uint32(rec.Port),
|
|
|
|
}
|
|
|
|
for _, rawUser := range rec.Users {
|
|
|
|
user := new(protocol.User)
|
|
|
|
if err := json.Unmarshal(rawUser, user); err != nil {
|
2017-02-02 08:42:31 -05:00
|
|
|
return nil, errors.Base(err).Message("Config: Invalid VMess user.")
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
|
|
|
account := new(VMessAccount)
|
|
|
|
if err := json.Unmarshal(rawUser, account); err != nil {
|
2017-02-02 08:42:31 -05:00
|
|
|
return nil, errors.Base(err).Message("Config: Invalid VMess user.")
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|
2016-12-15 05:51:09 -05:00
|
|
|
user.Account = serial.ToTypedMessage(account.Build())
|
2016-10-17 08:35:13 -04:00
|
|
|
spec.User = append(spec.User, user)
|
|
|
|
}
|
|
|
|
serverSpecs[idx] = spec
|
|
|
|
}
|
|
|
|
config.Receiver = serverSpecs
|
2016-12-15 05:51:09 -05:00
|
|
|
return serial.ToTypedMessage(config), nil
|
2016-10-17 08:35:13 -04:00
|
|
|
}
|