github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/infra/conf/vmess.go (about) 1 package conf 2 3 import ( 4 "encoding/json" 5 "strings" 6 7 "github.com/golang/protobuf/proto" 8 9 "v2ray.com/core/common/protocol" 10 "v2ray.com/core/common/serial" 11 "v2ray.com/core/proxy/vmess" 12 "v2ray.com/core/proxy/vmess/inbound" 13 "v2ray.com/core/proxy/vmess/outbound" 14 ) 15 16 type VMessAccount struct { 17 ID string `json:"id"` 18 AlterIds uint16 `json:"alterId"` 19 Security string `json:"security"` 20 } 21 22 // Build implements Buildable 23 func (a *VMessAccount) Build() *vmess.Account { 24 var st protocol.SecurityType 25 switch strings.ToLower(a.Security) { 26 case "aes-128-gcm": 27 st = protocol.SecurityType_AES128_GCM 28 case "chacha20-poly1305": 29 st = protocol.SecurityType_CHACHA20_POLY1305 30 case "auto": 31 st = protocol.SecurityType_AUTO 32 case "none": 33 st = protocol.SecurityType_NONE 34 default: 35 st = protocol.SecurityType_AUTO 36 } 37 return &vmess.Account{ 38 Id: a.ID, 39 AlterId: uint32(a.AlterIds), 40 SecuritySettings: &protocol.SecurityConfig{ 41 Type: st, 42 }, 43 } 44 } 45 46 type VMessDetourConfig struct { 47 ToTag string `json:"to"` 48 } 49 50 // Build implements Buildable 51 func (c *VMessDetourConfig) Build() *inbound.DetourConfig { 52 return &inbound.DetourConfig{ 53 To: c.ToTag, 54 } 55 } 56 57 type FeaturesConfig struct { 58 Detour *VMessDetourConfig `json:"detour"` 59 } 60 61 type VMessDefaultConfig struct { 62 AlterIDs uint16 `json:"alterId"` 63 Level byte `json:"level"` 64 } 65 66 // Build implements Buildable 67 func (c *VMessDefaultConfig) Build() *inbound.DefaultConfig { 68 config := new(inbound.DefaultConfig) 69 config.AlterId = uint32(c.AlterIDs) 70 if config.AlterId == 0 { 71 config.AlterId = 32 72 } 73 config.Level = uint32(c.Level) 74 return config 75 } 76 77 type VMessInboundConfig struct { 78 Users []json.RawMessage `json:"clients"` 79 Features *FeaturesConfig `json:"features"` 80 Defaults *VMessDefaultConfig `json:"default"` 81 DetourConfig *VMessDetourConfig `json:"detour"` 82 SecureOnly bool `json:"disableInsecureEncryption"` 83 } 84 85 // Build implements Buildable 86 func (c *VMessInboundConfig) Build() (proto.Message, error) { 87 config := &inbound.Config{ 88 SecureEncryptionOnly: c.SecureOnly, 89 } 90 91 if c.Defaults != nil { 92 config.Default = c.Defaults.Build() 93 } 94 95 if c.DetourConfig != nil { 96 config.Detour = c.DetourConfig.Build() 97 } else if c.Features != nil && c.Features.Detour != nil { 98 config.Detour = c.Features.Detour.Build() 99 } 100 101 config.User = make([]*protocol.User, len(c.Users)) 102 for idx, rawData := range c.Users { 103 user := new(protocol.User) 104 if err := json.Unmarshal(rawData, user); err != nil { 105 return nil, newError("invalid VMess user").Base(err) 106 } 107 account := new(VMessAccount) 108 if err := json.Unmarshal(rawData, account); err != nil { 109 return nil, newError("invalid VMess user").Base(err) 110 } 111 user.Account = serial.ToTypedMessage(account.Build()) 112 config.User[idx] = user 113 } 114 115 return config, nil 116 } 117 118 type VMessOutboundTarget struct { 119 Address *Address `json:"address"` 120 Port uint16 `json:"port"` 121 Users []json.RawMessage `json:"users"` 122 } 123 type VMessOutboundConfig struct { 124 Receivers []*VMessOutboundTarget `json:"vnext"` 125 } 126 127 // Build implements Buildable 128 func (c *VMessOutboundConfig) Build() (proto.Message, error) { 129 config := new(outbound.Config) 130 131 if len(c.Receivers) == 0 { 132 return nil, newError("0 VMess receiver configured") 133 } 134 serverSpecs := make([]*protocol.ServerEndpoint, len(c.Receivers)) 135 for idx, rec := range c.Receivers { 136 if len(rec.Users) == 0 { 137 return nil, newError("0 user configured for VMess outbound") 138 } 139 if rec.Address == nil { 140 return nil, newError("address is not set in VMess outbound config") 141 } 142 spec := &protocol.ServerEndpoint{ 143 Address: rec.Address.Build(), 144 Port: uint32(rec.Port), 145 } 146 for _, rawUser := range rec.Users { 147 user := new(protocol.User) 148 if err := json.Unmarshal(rawUser, user); err != nil { 149 return nil, newError("invalid VMess user").Base(err) 150 } 151 account := new(VMessAccount) 152 if err := json.Unmarshal(rawUser, account); err != nil { 153 return nil, newError("invalid VMess user").Base(err) 154 } 155 user.Account = serial.ToTypedMessage(account.Build()) 156 spec.User = append(spec.User, user) 157 } 158 serverSpecs[idx] = spec 159 } 160 config.Receiver = serverSpecs 161 return config, nil 162 }