Skip to content

Commit d11f9c8

Browse files
committed
chore: sync vless encryption code
1 parent e54ca7c commit d11f9c8

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

docs/config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ proxies: # socks5
639639
uuid: uuid
640640
network: tcp
641641
encryption: "8min-vless-mlkem768client-bas64RawURLEncoding" # 复用八分钟后协商新的 sharedKey,需小于服务端的值
642-
# encryption: "8min-aes128xor-mlkem768client-bas64RawURLEncoding"
642+
# encryption: "8min-xored-mlkem768client-bas64RawURLEncoding"
643643
tls: false #可以不开启tls
644644
udp: true
645645

@@ -1348,7 +1348,7 @@ listeners:
13481348
# ws-path: "/" # 如果不为空则开启 websocket 传输层
13491349
# grpc-service-name: "GunService" # 如果不为空则开启 grpc 传输层
13501350
# decryption: "10min-vless-mlkem768seed-bas64RawURLEncoding" # 同时允许 1-RTT 模式与十分钟复用的 0-RTT 模式, 后面base64字符串可由可由 mihomo generate vless-mlkem768 命令生成
1351-
# decryption: "10min-aes128xor-mlkem768seed-bas64RawURLEncoding"
1351+
# decryption: "10min-xored-mlkem768seed-bas64RawURLEncoding"
13521352
# 下面两项如果填写则开启 tls(需要同时填写)
13531353
# certificate: ./server.crt
13541354
# private-key: ./server.key

listener/inbound/vless_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ func TestInboundVless_Encryption(t *testing.T) {
103103
}
104104
testInboundVless(t, inboundOptions, outboundOptions)
105105
})
106-
t.Run("-aes128xor-", func(t *testing.T) {
106+
t.Run("-xored-", func(t *testing.T) {
107107
inboundOptions := inbound.VlessOption{
108-
Decryption: "10min-aes128xor-mlkem768seed-" + seedBase64,
108+
Decryption: "10min-xored-mlkem768seed-" + seedBase64,
109109
}
110110
outboundOptions := outbound.VlessOption{
111-
Encryption: "8min-aes128xor-mlkem768client-" + clientBase64,
111+
Encryption: "8min-xored-mlkem768client-" + clientBase64,
112112
}
113113
testInboundVless(t, inboundOptions, outboundOptions)
114114
})

transport/vless/encryption/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
// https://github.com/XTLS/Xray-core/commit/5c611420487a92f931faefc01d4bf03869f477f6
77
// https://github.com/XTLS/Xray-core/commit/23d7aad461d232bc5bed52dd6aaa731ecd88ad35
88
// https://github.com/XTLS/Xray-core/commit/3c20bddfcfd8999be5f9a2ac180dc959950e4c61
9+
// https://github.com/XTLS/Xray-core/commit/1720be168fa069332c418503d30341fc6e01df7f
910
package encryption

transport/vless/encryption/factory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func NewClient(encryption string) (*ClientInstance, error) {
3131
var xor uint32
3232
switch s[1] {
3333
case "vless":
34-
case "aes128xor":
34+
case "xored":
3535
xor = 1
3636
default:
3737
return nil, fmt.Errorf("invaild vless encryption value: %s", encryption)
@@ -76,7 +76,7 @@ func NewServer(decryption string) (*ServerInstance, error) {
7676
var xor uint32
7777
switch s[1] {
7878
case "vless":
79-
case "aes128xor":
79+
case "xored":
8080
xor = 1
8181
default:
8282
return nil, fmt.Errorf("invaild vless decryption value: %s", decryption)

transport/vless/encryption/xor.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import (
1010

1111
type XorConn struct {
1212
net.Conn
13-
key []byte
14-
ctr cipher.Stream
15-
peerCtr cipher.Stream
13+
key []byte
14+
ctr cipher.Stream
15+
peerCtr cipher.Stream
16+
isHeader bool
17+
skipNext bool
1618
}
1719

1820
func NewXorConn(conn net.Conn, key []byte) *XorConn {
1921
return &XorConn{Conn: conn, key: key[:16]}
22+
//chacha20.NewUnauthenticatedCipher()
2023
}
2124

22-
func (c *XorConn) Write(b []byte) (int, error) {
25+
func (c *XorConn) Write(b []byte) (int, error) { // two records at most
2326
if len(b) == 0 {
2427
return 0, nil
2528
}
@@ -30,7 +33,13 @@ func (c *XorConn) Write(b []byte) (int, error) {
3033
rand.Read(iv)
3134
c.ctr = cipher.NewCTR(block, iv)
3235
}
33-
c.ctr.XORKeyStream(b, b) // caller MUST discard b
36+
t, l, _ := DecodeHeader(b)
37+
if t != 23 {
38+
l += 10 // 5+l+5
39+
} else {
40+
l = 5
41+
}
42+
c.ctr.XORKeyStream(b[:l], b[:l]) // caller MUST discard b
3443
if iv != nil {
3544
b = append(iv, b...)
3645
}
@@ -43,7 +52,7 @@ func (c *XorConn) Write(b []byte) (int, error) {
4352
return len(b), nil
4453
}
4554

46-
func (c *XorConn) Read(b []byte) (int, error) {
55+
func (c *XorConn) Read(b []byte) (int, error) { // 5-bytes, data, 5-bytes...
4756
if len(b) == 0 {
4857
return 0, nil
4958
}
@@ -54,10 +63,24 @@ func (c *XorConn) Read(b []byte) (int, error) {
5463
}
5564
block, _ := aes.NewCipher(c.key)
5665
c.peerCtr = cipher.NewCTR(block, peerIv)
66+
c.isHeader = true
67+
}
68+
if _, err := io.ReadFull(c.Conn, b); err != nil {
69+
return 0, err
5770
}
58-
n, err := c.Conn.Read(b)
59-
if n > 0 {
60-
c.peerCtr.XORKeyStream(b[:n], b[:n])
71+
if c.skipNext {
72+
c.skipNext = false
73+
return len(b), nil
6174
}
62-
return n, err
75+
c.peerCtr.XORKeyStream(b, b)
76+
if c.isHeader {
77+
if t, _, _ := DecodeHeader(b); t == 23 { // always 5-bytes
78+
c.skipNext = true
79+
} else {
80+
c.isHeader = false
81+
}
82+
} else {
83+
c.isHeader = true
84+
}
85+
return len(b), nil
6386
}

0 commit comments

Comments
 (0)