|
| 1 | +package encryption |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// NewClient new client from encryption string |
| 12 | +// maybe return a nil *ClientInstance without any error, that means don't need to encrypt |
| 13 | +func NewClient(encryption string) (*ClientInstance, error) { |
| 14 | + switch encryption { |
| 15 | + case "", "none": // We will not reject empty string like xray-core does, because we need to ensure compatibility |
| 16 | + return nil, nil |
| 17 | + } |
| 18 | + if s := strings.SplitN(encryption, "-", 4); len(s) == 4 && s[2] == "mlkem768client" { |
| 19 | + var minutes uint32 |
| 20 | + if s[0] != "1rtt" { |
| 21 | + t := strings.TrimSuffix(s[0], "min") |
| 22 | + if t == s[0] { |
| 23 | + return nil, fmt.Errorf("invaild vless encryption value: %s", encryption) |
| 24 | + } |
| 25 | + i, err := strconv.Atoi(t) |
| 26 | + if err != nil { |
| 27 | + return nil, fmt.Errorf("invaild vless encryption value: %s", encryption) |
| 28 | + } |
| 29 | + minutes = uint32(i) |
| 30 | + } |
| 31 | + var xor uint32 |
| 32 | + switch s[1] { |
| 33 | + case "vless": |
| 34 | + case "aes128xor": |
| 35 | + xor = 1 |
| 36 | + default: |
| 37 | + return nil, fmt.Errorf("invaild vless encryption value: %s", encryption) |
| 38 | + } |
| 39 | + b, err := base64.RawURLEncoding.DecodeString(s[3]) |
| 40 | + if err != nil { |
| 41 | + return nil, fmt.Errorf("invaild vless encryption value: %s", encryption) |
| 42 | + } |
| 43 | + if len(b) == MLKEM768ClientLength { |
| 44 | + client := &ClientInstance{} |
| 45 | + if err = client.Init(b, xor, time.Duration(minutes)*time.Minute); err != nil { |
| 46 | + return nil, fmt.Errorf("failed to use mlkem768seed: %w", err) |
| 47 | + } |
| 48 | + return client, nil |
| 49 | + } else { |
| 50 | + return nil, fmt.Errorf("invaild vless encryption value: %s", encryption) |
| 51 | + } |
| 52 | + } |
| 53 | + return nil, fmt.Errorf("invaild vless encryption value: %s", encryption) |
| 54 | +} |
| 55 | + |
| 56 | +// NewServer new server from decryption string |
| 57 | +// maybe return a nil *ServerInstance without any error, that means don't need to decrypt |
| 58 | +func NewServer(decryption string) (*ServerInstance, error) { |
| 59 | + switch decryption { |
| 60 | + case "", "none": // We will not reject empty string like xray-core does, because we need to ensure compatibility |
| 61 | + return nil, nil |
| 62 | + } |
| 63 | + if s := strings.SplitN(decryption, "-", 4); len(s) == 4 && s[2] == "mlkem768seed" { |
| 64 | + var minutes uint32 |
| 65 | + if s[0] != "1rtt" { |
| 66 | + t := strings.TrimSuffix(s[0], "min") |
| 67 | + if t == s[0] { |
| 68 | + return nil, fmt.Errorf("invaild vless decryption value: %s", decryption) |
| 69 | + } |
| 70 | + i, err := strconv.Atoi(t) |
| 71 | + if err != nil { |
| 72 | + return nil, fmt.Errorf("invaild vless decryption value: %s", decryption) |
| 73 | + } |
| 74 | + minutes = uint32(i) |
| 75 | + } |
| 76 | + var xor uint32 |
| 77 | + switch s[1] { |
| 78 | + case "vless": |
| 79 | + case "aes128xor": |
| 80 | + xor = 1 |
| 81 | + default: |
| 82 | + return nil, fmt.Errorf("invaild vless decryption value: %s", decryption) |
| 83 | + } |
| 84 | + b, err := base64.RawURLEncoding.DecodeString(s[3]) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("invaild vless decryption value: %s", decryption) |
| 87 | + } |
| 88 | + if len(b) == MLKEM768SeedLength { |
| 89 | + server := &ServerInstance{} |
| 90 | + if err = server.Init(b, xor, time.Duration(minutes)*time.Minute); err != nil { |
| 91 | + return nil, fmt.Errorf("failed to use mlkem768seed: %w", err) |
| 92 | + } |
| 93 | + return server, nil |
| 94 | + } else { |
| 95 | + return nil, fmt.Errorf("invaild vless decryption value: %s", decryption) |
| 96 | + } |
| 97 | + } |
| 98 | + return nil, fmt.Errorf("invaild vless decryption value: %s", decryption) |
| 99 | +} |
0 commit comments