| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- package conf
- import (
- "encoding/base64"
- "encoding/json"
- "path/filepath"
- "runtime"
- "strconv"
- "strings"
- "syscall"
- "github.com/xtls/xray-core/common/errors"
- "github.com/xtls/xray-core/common/net"
- "github.com/xtls/xray-core/common/protocol"
- "github.com/xtls/xray-core/common/serial"
- "github.com/xtls/xray-core/common/uuid"
- "github.com/xtls/xray-core/proxy/vless"
- "github.com/xtls/xray-core/proxy/vless/inbound"
- "github.com/xtls/xray-core/proxy/vless/outbound"
- "google.golang.org/protobuf/proto"
- )
- type VLessInboundFallback struct {
- Name string `json:"name"`
- Alpn string `json:"alpn"`
- Path string `json:"path"`
- Type string `json:"type"`
- Dest json.RawMessage `json:"dest"`
- Xver uint64 `json:"xver"`
- }
- type VLessInboundConfig struct {
- Clients []json.RawMessage `json:"clients"`
- Decryption string `json:"decryption"`
- Fallbacks []*VLessInboundFallback `json:"fallbacks"`
- Flow string `json:"flow"`
- }
- // Build implements Buildable
- func (c *VLessInboundConfig) Build() (proto.Message, error) {
- config := new(inbound.Config)
- config.Clients = make([]*protocol.User, len(c.Clients))
- switch c.Flow {
- case vless.None:
- c.Flow = ""
- case "", vless.XRV:
- default:
- return nil, errors.New(`VLESS "settings.flow" doesn't support "` + c.Flow + `" in this version`)
- }
- for idx, rawUser := range c.Clients {
- user := new(protocol.User)
- if err := json.Unmarshal(rawUser, user); err != nil {
- return nil, errors.New(`VLESS clients: invalid user`).Base(err)
- }
- account := new(vless.Account)
- if err := json.Unmarshal(rawUser, account); err != nil {
- return nil, errors.New(`VLESS clients: invalid user`).Base(err)
- }
- u, err := uuid.ParseString(account.Id)
- if err != nil {
- return nil, err
- }
- account.Id = u.String()
- switch account.Flow {
- case "":
- account.Flow = c.Flow
- case vless.None:
- account.Flow = ""
- case vless.XRV:
- default:
- return nil, errors.New(`VLESS clients: "flow" doesn't support "` + account.Flow + `" in this version`)
- }
- if account.Encryption != "" {
- return nil, errors.New(`VLESS clients: "encryption" should not be in inbound settings`)
- }
- if account.Reverse != nil && account.Reverse.Tag == "" {
- return nil, errors.New(`VLESS clients: "tag" can't be empty for "reverse"`)
- }
- user.Account = serial.ToTypedMessage(account)
- config.Clients[idx] = user
- }
- config.Decryption = c.Decryption
- if !func() bool {
- s := strings.Split(config.Decryption, ".")
- if len(s) < 4 || s[0] != "mlkem768x25519plus" {
- return false
- }
- switch s[1] {
- case "native":
- case "xorpub":
- config.XorMode = 1
- case "random":
- config.XorMode = 2
- default:
- return false
- }
- t := strings.SplitN(strings.TrimSuffix(s[2], "s"), "-", 2)
- i, err := strconv.Atoi(t[0])
- if err != nil {
- return false
- }
- config.SecondsFrom = int64(i)
- if len(t) == 2 {
- i, err := strconv.Atoi(t[1])
- if err != nil {
- return false
- }
- config.SecondsTo = int64(i)
- }
- padding := 0
- for _, r := range s[3:] {
- if len(r) < 20 {
- padding += len(r) + 1
- continue
- }
- if b, _ := base64.RawURLEncoding.DecodeString(r); len(b) != 32 && len(b) != 64 {
- return false
- }
- }
- config.Decryption = config.Decryption[27+len(s[2]):]
- if padding > 0 {
- config.Padding = config.Decryption[:padding-1]
- config.Decryption = config.Decryption[padding:]
- }
- return true
- }() && config.Decryption != "none" {
- if config.Decryption == "" {
- return nil, errors.New(`VLESS settings: please add/set "decryption":"none" to every settings`)
- }
- return nil, errors.New(`VLESS settings: unsupported "decryption": ` + config.Decryption)
- }
- if config.Decryption != "none" && c.Fallbacks != nil {
- return nil, errors.New(`VLESS settings: "fallbacks" can not be used together with "decryption"`)
- }
- for _, fb := range c.Fallbacks {
- var i uint16
- var s string
- if err := json.Unmarshal(fb.Dest, &i); err == nil {
- s = strconv.Itoa(int(i))
- } else {
- _ = json.Unmarshal(fb.Dest, &s)
- }
- config.Fallbacks = append(config.Fallbacks, &inbound.Fallback{
- Name: fb.Name,
- Alpn: fb.Alpn,
- Path: fb.Path,
- Type: fb.Type,
- Dest: s,
- Xver: fb.Xver,
- })
- }
- for _, fb := range config.Fallbacks {
- /*
- if fb.Alpn == "h2" && fb.Path != "" {
- return nil, errors.New(`VLESS fallbacks: "alpn":"h2" doesn't support "path"`)
- }
- */
- if fb.Path != "" && fb.Path[0] != '/' {
- return nil, errors.New(`VLESS fallbacks: "path" must be empty or start with "/"`)
- }
- if fb.Type == "" && fb.Dest != "" {
- if fb.Dest == "serve-ws-none" {
- fb.Type = "serve"
- } else if filepath.IsAbs(fb.Dest) || fb.Dest[0] == '@' {
- fb.Type = "unix"
- if strings.HasPrefix(fb.Dest, "@@") && (runtime.GOOS == "linux" || runtime.GOOS == "android") {
- fullAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) // may need padding to work with haproxy
- copy(fullAddr, fb.Dest[1:])
- fb.Dest = string(fullAddr)
- }
- } else {
- if _, err := strconv.Atoi(fb.Dest); err == nil {
- fb.Dest = "localhost:" + fb.Dest
- }
- if _, _, err := net.SplitHostPort(fb.Dest); err == nil {
- fb.Type = "tcp"
- }
- }
- }
- if fb.Type == "" {
- return nil, errors.New(`VLESS fallbacks: please fill in a valid value for every "dest"`)
- }
- if fb.Xver > 2 {
- return nil, errors.New(`VLESS fallbacks: invalid PROXY protocol version, "xver" only accepts 0, 1, 2`)
- }
- }
- return config, nil
- }
- type VLessOutboundVnext struct {
- Address *Address `json:"address"`
- Port uint16 `json:"port"`
- Users []json.RawMessage `json:"users"`
- }
- type VLessOutboundConfig struct {
- Address *Address `json:"address"`
- Port uint16 `json:"port"`
- Level uint32 `json:"level"`
- Email string `json:"email"`
- Id string `json:"id"`
- Flow string `json:"flow"`
- Seed string `json:"seed"`
- Encryption string `json:"encryption"`
- Reverse *vless.Reverse `json:"reverse"`
- Vnext []*VLessOutboundVnext `json:"vnext"`
- }
- // Build implements Buildable
- func (c *VLessOutboundConfig) Build() (proto.Message, error) {
- config := new(outbound.Config)
- if c.Address != nil {
- c.Vnext = []*VLessOutboundVnext{
- {
- Address: c.Address,
- Port: c.Port,
- Users: []json.RawMessage{{}},
- },
- }
- }
- if len(c.Vnext) != 1 {
- return nil, errors.New(`VLESS settings: "vnext" should have one and only one member. Multiple endpoints in "vnext" should use multiple VLESS outbounds and routing balancer instead`)
- }
- for _, rec := range c.Vnext {
- if rec.Address == nil {
- return nil, errors.New(`VLESS vnext: "address" is not set`)
- }
- if len(rec.Users) != 1 {
- return nil, errors.New(`VLESS vnext: "users" should have one and only one member. Multiple members in "users" should use multiple VLESS outbounds and routing balancer instead`)
- }
- spec := &protocol.ServerEndpoint{
- Address: rec.Address.Build(),
- Port: uint32(rec.Port),
- }
- for _, rawUser := range rec.Users {
- user := new(protocol.User)
- if c.Address != nil {
- user.Level = c.Level
- user.Email = c.Email
- } else {
- if err := json.Unmarshal(rawUser, user); err != nil {
- return nil, errors.New(`VLESS users: invalid user`).Base(err)
- }
- }
- account := new(vless.Account)
- if c.Address != nil {
- account.Id = c.Id
- account.Flow = c.Flow
- //account.Seed = c.Seed
- account.Encryption = c.Encryption
- account.Reverse = c.Reverse
- } else {
- if err := json.Unmarshal(rawUser, account); err != nil {
- return nil, errors.New(`VLESS users: invalid user`).Base(err)
- }
- }
- u, err := uuid.ParseString(account.Id)
- if err != nil {
- return nil, err
- }
- account.Id = u.String()
- switch account.Flow {
- case "", vless.XRV, vless.XRV + "-udp443":
- default:
- return nil, errors.New(`VLESS users: "flow" doesn't support "` + account.Flow + `" in this version`)
- }
- if !func() bool {
- s := strings.Split(account.Encryption, ".")
- if len(s) < 4 || s[0] != "mlkem768x25519plus" {
- return false
- }
- switch s[1] {
- case "native":
- case "xorpub":
- account.XorMode = 1
- case "random":
- account.XorMode = 2
- default:
- return false
- }
- switch s[2] {
- case "1rtt":
- case "0rtt":
- account.Seconds = 1
- default:
- return false
- }
- padding := 0
- for _, r := range s[3:] {
- if len(r) < 20 {
- padding += len(r) + 1
- continue
- }
- if b, _ := base64.RawURLEncoding.DecodeString(r); len(b) != 32 && len(b) != 1184 {
- return false
- }
- }
- account.Encryption = account.Encryption[27+len(s[2]):]
- if padding > 0 {
- account.Padding = account.Encryption[:padding-1]
- account.Encryption = account.Encryption[padding:]
- }
- return true
- }() && account.Encryption != "none" {
- if account.Encryption == "" {
- return nil, errors.New(`VLESS users: please add/set "encryption":"none" for every user`)
- }
- return nil, errors.New(`VLESS users: unsupported "encryption": ` + account.Encryption)
- }
- if account.Reverse != nil && account.Reverse.Tag == "" {
- return nil, errors.New(`VLESS clients: "tag" can't be empty for "reverse"`)
- }
- user.Account = serial.ToTypedMessage(account)
- spec.User = user
- break
- }
- config.Vnext = spec
- break
- }
- return config, nil
- }
|