1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-07 10:47:48 -05:00
v2fly/tools/conf/vmess.go

140 lines
3.8 KiB
Go
Raw Normal View History

2016-10-17 08:35:13 -04:00
package conf
import (
"encoding/json"
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-10-17 08:35:13 -04:00
"v2ray.com/core/common/loader"
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-11-27 15:39:09 -05:00
func (v *VMessAccount) Build() *vmess.Account {
2016-10-17 08:35:13 -04:00
return &vmess.Account{
2016-11-27 15:39:09 -05:00
Id: v.ID,
AlterId: uint32(v.AlterIds),
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-11-27 15:39:09 -05:00
func (v *VMessInboundConfig) Build() (*loader.TypedSettings, 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 {
return nil, errors.New("VMess|Inbound: Invalid user: " + err.Error())
}
account := new(VMessAccount)
if err := json.Unmarshal(rawData, account); err != nil {
return nil, errors.New("VMess|Inbound: Invalid user: " + err.Error())
}
user.Account = loader.NewTypedSettings(account.Build())
config.User[idx] = user
}
return loader.NewTypedSettings(config), nil
}
type VMessOutboundTarget struct {
Address *Address `json:"address"`
Port uint16 `json:"port"`
Users []json.RawMessage `json:"users"`
}
type VMessOutboundConfig struct {
Receivers []*VMessOutboundTarget `json:"vnext"`
}
2016-11-27 15:39:09 -05:00
func (v *VMessOutboundConfig) Build() (*loader.TypedSettings, 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 {
2016-10-17 08:35:13 -04:00
return nil, errors.New("0 VMess receiver configured.")
}
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 {
return nil, errors.New("0 user configured for VMess outbound.")
}
if rec.Address == nil {
return nil, errors.New("Address is not set in VMess outbound config.")
}
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 {
return nil, errors.New("VMess|Outbound: Invalid user: " + err.Error())
}
account := new(VMessAccount)
if err := json.Unmarshal(rawUser, account); err != nil {
return nil, errors.New("VMess|Outbound: Invalid user: " + err.Error())
}
user.Account = loader.NewTypedSettings(account.Build())
spec.User = append(spec.User, user)
}
serverSpecs[idx] = spec
}
config.Receiver = serverSpecs
return loader.NewTypedSettings(config), nil
}