@@ -2,11 +2,15 @@ package sing_vless
2
2
3
3
import (
4
4
"context"
5
+ "encoding/base64"
5
6
"errors"
7
+ "fmt"
6
8
"net"
7
9
"net/http"
8
10
"reflect"
11
+ "strconv"
9
12
"strings"
13
+ "time"
10
14
"unsafe"
11
15
12
16
"github.com/metacubex/mihomo/adapter/inbound"
@@ -19,6 +23,7 @@ import (
19
23
"github.com/metacubex/mihomo/listener/sing"
20
24
"github.com/metacubex/mihomo/log"
21
25
"github.com/metacubex/mihomo/transport/gun"
26
+ "github.com/metacubex/mihomo/transport/vless/encryption"
22
27
mihomoVMess "github.com/metacubex/mihomo/transport/vmess"
23
28
24
29
"github.com/metacubex/sing-vmess/vless"
@@ -45,10 +50,11 @@ func init() {
45
50
}
46
51
47
52
type Listener struct {
48
- closed bool
49
- config LC.VlessServer
50
- listeners []net.Listener
51
- service * vless.Service [string ]
53
+ closed bool
54
+ config LC.VlessServer
55
+ listeners []net.Listener
56
+ service * vless.Service [string ]
57
+ decryption * encryption.ServerInstance
52
58
}
53
59
54
60
func New (config LC.VlessServer , tunnel C.Tunnel , additions ... inbound.Addition ) (sl * Listener , err error ) {
@@ -80,7 +86,34 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition)
80
86
return it .Flow
81
87
}))
82
88
83
- sl = & Listener {false , config , nil , service }
89
+ sl = & Listener {config : config , service : service }
90
+
91
+ if s := strings .Split (config .Decryption , "-mlkem768seed-" ); len (s ) == 2 {
92
+ var minutes uint32
93
+ if s [0 ] != "1rtt" {
94
+ t := strings .TrimSuffix (s [0 ], "min" )
95
+ if t == s [0 ] {
96
+ return nil , fmt .Errorf ("invaild vless decryption value: %s" , config .Decryption )
97
+ }
98
+ i , err := strconv .Atoi (t )
99
+ if err != nil {
100
+ return nil , fmt .Errorf ("invaild vless decryption value: %s" , config .Decryption )
101
+ }
102
+ minutes = uint32 (i )
103
+ }
104
+ b , err := base64 .RawURLEncoding .DecodeString (s [1 ])
105
+ if err != nil {
106
+ return nil , fmt .Errorf ("invaild vless decryption value: %s" , config .Decryption )
107
+ }
108
+ if len (b ) == 64 {
109
+ sl .decryption = & encryption.ServerInstance {}
110
+ if err = sl .decryption .Init (b , time .Duration (minutes )* time .Minute ); err != nil {
111
+ return nil , fmt .Errorf ("failed to use mlkem768seed: %w" , err )
112
+ }
113
+ } else {
114
+ return nil , fmt .Errorf ("invaild vless decryption value: %s" , config .Decryption )
115
+ }
116
+ }
84
117
85
118
tlsConfig := & tlsC.Config {}
86
119
var realityBuilder * reality.Builder
@@ -149,8 +182,8 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition)
149
182
} else {
150
183
l = tlsC .NewListener (l , tlsConfig )
151
184
}
152
- } else {
153
- return nil , errors .New ("disallow using Vless without both certificates/reality config" )
185
+ } else if sl . decryption == nil {
186
+ return nil , errors .New ("disallow using Vless without any certificates/reality/decryption config" )
154
187
}
155
188
sl .listeners = append (sl .listeners , l )
156
189
@@ -201,6 +234,13 @@ func (l *Listener) AddrList() (addrList []net.Addr) {
201
234
202
235
func (l * Listener ) HandleConn (conn net.Conn , tunnel C.Tunnel , additions ... inbound.Addition ) {
203
236
ctx := sing .WithAdditions (context .TODO (), additions ... )
237
+ if l .decryption != nil {
238
+ var err error
239
+ conn , err = l .decryption .Handshake (conn )
240
+ if err != nil {
241
+ return
242
+ }
243
+ }
204
244
err := l .service .NewConnection (ctx , conn , metadata.Metadata {
205
245
Protocol : "vless" ,
206
246
Source : metadata .SocksaddrFromNet (conn .RemoteAddr ()),
0 commit comments