github.com/eagleql/xray-core@v1.4.4/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 "github.com/eagleql/xray-core/common/protocol" 10 "github.com/eagleql/xray-core/common/serial" 11 "github.com/eagleql/xray-core/common/uuid" 12 "github.com/eagleql/xray-core/proxy/vmess" 13 "github.com/eagleql/xray-core/proxy/vmess/inbound" 14 "github.com/eagleql/xray-core/proxy/vmess/outbound" 15 ) 16 17 type VMessAccount struct { 18 ID string `json:"id"` 19 AlterIds uint16 `json:"alterId"` 20 Security string `json:"security"` 21 } 22 23 // Build implements Buildable 24 func (a *VMessAccount) Build() *vmess.Account { 25 var st protocol.SecurityType 26 switch strings.ToLower(a.Security) { 27 case "aes-128-gcm": 28 st = protocol.SecurityType_AES128_GCM 29 case "chacha20-poly1305": 30 st = protocol.SecurityType_CHACHA20_POLY1305 31 case "auto": 32 st = protocol.SecurityType_AUTO 33 case "none": 34 st = protocol.SecurityType_NONE 35 case "zero": 36 st = protocol.SecurityType_ZERO 37 default: 38 st = protocol.SecurityType_AUTO 39 } 40 return &vmess.Account{ 41 Id: a.ID, 42 AlterId: uint32(a.AlterIds), 43 SecuritySettings: &protocol.SecurityConfig{ 44 Type: st, 45 }, 46 } 47 } 48 49 type VMessDetourConfig struct { 50 ToTag string `json:"to"` 51 } 52 53 // Build implements Buildable 54 func (c *VMessDetourConfig) Build() *inbound.DetourConfig { 55 return &inbound.DetourConfig{ 56 To: c.ToTag, 57 } 58 } 59 60 type FeaturesConfig struct { 61 Detour *VMessDetourConfig `json:"detour"` 62 } 63 64 type VMessDefaultConfig struct { 65 AlterIDs uint16 `json:"alterId"` 66 Level byte `json:"level"` 67 } 68 69 // Build implements Buildable 70 func (c *VMessDefaultConfig) Build() *inbound.DefaultConfig { 71 config := new(inbound.DefaultConfig) 72 config.AlterId = uint32(c.AlterIDs) 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 112 u, err := uuid.ParseString(account.ID) 113 if err != nil { 114 return nil, err 115 } 116 account.ID = u.String() 117 118 user.Account = serial.ToTypedMessage(account.Build()) 119 config.User[idx] = user 120 } 121 122 return config, nil 123 } 124 125 type VMessOutboundTarget struct { 126 Address *Address `json:"address"` 127 Port uint16 `json:"port"` 128 Users []json.RawMessage `json:"users"` 129 } 130 type VMessOutboundConfig struct { 131 Receivers []*VMessOutboundTarget `json:"vnext"` 132 } 133 134 // Build implements Buildable 135 func (c *VMessOutboundConfig) Build() (proto.Message, error) { 136 config := new(outbound.Config) 137 138 if len(c.Receivers) == 0 { 139 return nil, newError("0 VMess receiver configured") 140 } 141 serverSpecs := make([]*protocol.ServerEndpoint, len(c.Receivers)) 142 for idx, rec := range c.Receivers { 143 if len(rec.Users) == 0 { 144 return nil, newError("0 user configured for VMess outbound") 145 } 146 if rec.Address == nil { 147 return nil, newError("address is not set in VMess outbound config") 148 } 149 spec := &protocol.ServerEndpoint{ 150 Address: rec.Address.Build(), 151 Port: uint32(rec.Port), 152 } 153 for _, rawUser := range rec.Users { 154 user := new(protocol.User) 155 if err := json.Unmarshal(rawUser, user); err != nil { 156 return nil, newError("invalid VMess user").Base(err) 157 } 158 account := new(VMessAccount) 159 if err := json.Unmarshal(rawUser, account); err != nil { 160 return nil, newError("invalid VMess user").Base(err) 161 } 162 163 u, err := uuid.ParseString(account.ID) 164 if err != nil { 165 return nil, err 166 } 167 account.ID = u.String() 168 169 user.Account = serial.ToTypedMessage(account.Build()) 170 spec.User = append(spec.User, user) 171 } 172 serverSpecs[idx] = spec 173 } 174 config.Receiver = serverSpecs 175 return config, nil 176 }