You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/tools/conf/vmess.go

140 lines
3.8 KiB

package conf
import (
"encoding/json"
"v2ray.com/core/common/errors"
"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"`
}
8 years ago
func (v *VMessAccount) Build() *vmess.Account {
return &vmess.Account{
8 years ago
Id: v.ID,
AlterId: uint32(v.AlterIds),
}
}
type VMessDetourConfig struct {
ToTag string `json:"to"`
}
8 years ago
func (v *VMessDetourConfig) Build() *inbound.DetourConfig {
return &inbound.DetourConfig{
8 years ago
To: v.ToTag,
}
}
type FeaturesConfig struct {
Detour *VMessDetourConfig `json:"detour"`
}
type VMessDefaultConfig struct {
AlterIDs uint16 `json:"alterId"`
Level byte `json:"level"`
}
8 years ago
func (v *VMessDefaultConfig) Build() *inbound.DefaultConfig {
config := new(inbound.DefaultConfig)
8 years ago
config.AlterId = uint32(v.AlterIDs)
if config.AlterId == 0 {
config.AlterId = 32
}
8 years ago
config.Level = uint32(v.Level)
return config
}
type VMessInboundConfig struct {
Users []json.RawMessage `json:"clients"`
Features *FeaturesConfig `json:"features"`
Defaults *VMessDefaultConfig `json:"default"`
DetourConfig *VMessDetourConfig `json:"detour"`
}
8 years ago
func (v *VMessInboundConfig) Build() (*loader.TypedSettings, error) {
config := new(inbound.Config)
8 years ago
if v.Defaults != nil {
config.Default = v.Defaults.Build()
}
8 years ago
if v.DetourConfig != nil {
config.Detour = v.DetourConfig.Build()
} else if v.Features != nil && v.Features.Detour != nil {
config.Detour = v.Features.Detour.Build()
}
8 years ago
config.User = make([]*protocol.User, len(v.Users))
for idx, rawData := range v.Users {
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"`
}
8 years ago
func (v *VMessOutboundConfig) Build() (*loader.TypedSettings, error) {
config := new(outbound.Config)
8 years ago
if len(v.Receivers) == 0 {
return nil, errors.New("0 VMess receiver configured.")
}
8 years ago
serverSpecs := make([]*protocol.ServerEndpoint, len(v.Receivers))
for idx, rec := range v.Receivers {
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
}