Skip to content

Commit 76e40ba

Browse files
committed
chore: sync vless encryption code
1 parent 946b402 commit 76e40ba

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

transport/vless/encryption/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ func (i *ClientInstance) Handshake(conn net.Conn) (net.Conn, error) {
115115
if _, err := c.Conn.Write(clientHello); err != nil {
116116
return nil, err
117117
}
118-
// client can send more padding / NFS AEAD messages if needed
118+
// client can send more paddings / NFS AEAD messages if needed
119119

120-
_, t, l, err := ReadAndDiscardPaddings(c.Conn)
120+
_, t, l, err := ReadAndDiscardPaddings(c.Conn) // allow paddings before server hello
121121
if err != nil {
122122
return nil, err
123123
}
@@ -201,9 +201,9 @@ func (c *ClientConn) Read(b []byte) (int, error) {
201201
return 0, nil
202202
}
203203
if c.peerAead == nil {
204-
_, t, l, err := ReadAndDiscardPaddings(c.Conn)
204+
_, t, l, err := ReadAndDiscardPaddings(c.Conn) // allow paddings before random hello
205205
if err != nil {
206-
if c.instance != nil && strings.HasPrefix(err.Error(), "invalid header: ") { // from 0-RTT
206+
if c.instance != nil && strings.HasPrefix(err.Error(), "invalid header: ") { // 0-RTT's 0-RTT
207207
c.instance.Lock()
208208
if bytes.Equal(c.ticket, c.instance.ticket) {
209209
c.instance.expire = time.Now() // expired

transport/vless/encryption/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func DecodeHeader(h []byte) (t byte, l int, err error) {
4848
l = 0
4949
}
5050
if l < 17 || l > 17000 { // TODO: TLSv1.3 max length
51-
err = fmt.Errorf("invalid header: %v", h[:5]) // relied by client's Read()
51+
err = fmt.Errorf("invalid header: %v", h[:5]) // DO NOT CHANGE: relied by client's Read()
5252
}
5353
return
5454
}

transport/vless/encryption/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
// https://github.com/XTLS/Xray-core/commit/0fd7691d6b28e05922d7a5a9313d97745a51ea63
1111
// https://github.com/XTLS/Xray-core/commit/09cc92c61d9067e0d65c1cae9124664ecfc78f43
1212
// https://github.com/XTLS/Xray-core/commit/2807ee432a1fbeb301815647189eacd650b12a8b
13+
// https://github.com/XTLS/Xray-core/commit/bfe4820f2f086daf639b1957eb23dc13c843cad1
1314
package encryption

transport/vless/encryption/server.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (i *ServerInstance) Handshake(conn net.Conn) (net.Conn, error) {
9797
}
9898
c := &ServerConn{Conn: conn}
9999

100-
_, t, l, err := ReadAndDiscardPaddings(c.Conn)
100+
_, t, l, err := ReadAndDiscardPaddings(c.Conn) // allow paddings before client/ticket hello
101101
if err != nil {
102102
return nil, err
103103
}
@@ -117,9 +117,13 @@ func (i *ServerInstance) Handshake(conn net.Conn) (net.Conn, error) {
117117
s := i.sessions[[21]byte(peerTicketHello)]
118118
i.RUnlock()
119119
if s == nil {
120-
noise := make([]byte, randBetween(100, 1000))
121-
rand.Read(noise)
122-
c.Conn.Write(noise) // make client do new handshake
120+
noises := make([]byte, randBetween(100, 1000))
121+
var err error
122+
for err == nil {
123+
rand.Read(noises)
124+
_, _, err = DecodeHeader(noises)
125+
}
126+
c.Conn.Write(noises) // make client do new handshake
123127
return nil, errors.New("expired ticket")
124128
}
125129
if _, replay := s.randoms.LoadOrStore([32]byte(peerTicketHello[21:]), true); replay {
@@ -169,7 +173,7 @@ func (i *ServerInstance) Handshake(conn net.Conn) (net.Conn, error) {
169173
if _, err := c.Conn.Write(serverHello); err != nil {
170174
return nil, err
171175
}
172-
// server can send more padding / PFS AEAD messages if needed
176+
// server can send more paddings / PFS AEAD messages if needed
173177

174178
if i.minutes > 0 {
175179
i.Lock()
@@ -189,8 +193,8 @@ func (c *ServerConn) Read(b []byte) (int, error) {
189193
return 0, nil
190194
}
191195
if c.peerAead == nil {
192-
if c.peerRandom == nil { // from 1-RTT
193-
_, t, l, err := ReadAndDiscardPaddings(c.Conn)
196+
if c.peerRandom == nil { // 1-RTT's 0-RTT
197+
_, t, l, err := ReadAndDiscardPaddings(c.Conn) // allow paddings before ticket hello
194198
if err != nil {
195199
return 0, err
196200
}

transport/vless/encryption/xor.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func NewXorConn(conn net.Conn, key []byte) *XorConn {
2222
//chacha20.NewUnauthenticatedCipher()
2323
}
2424

25-
func (c *XorConn) Write(b []byte) (int, error) { // two records at most
25+
func (c *XorConn) Write(b []byte) (int, error) { // whole one/two records
2626
if len(b) == 0 {
2727
return 0, nil
2828
}
@@ -34,10 +34,10 @@ func (c *XorConn) Write(b []byte) (int, error) { // two records at most
3434
c.ctr = cipher.NewCTR(block, iv)
3535
}
3636
t, l, _ := DecodeHeader(b)
37-
if t != 23 {
38-
l += 10 // 5+l+5
39-
} else {
37+
if t == 23 { // single 23
4038
l = 5
39+
} else { // 1/0 + 23, or noises only
40+
l += 10
4141
}
4242
c.ctr.XORKeyStream(b[:l], b[:l]) // caller MUST discard b
4343
if iv != nil {
@@ -73,8 +73,8 @@ func (c *XorConn) Read(b []byte) (int, error) { // 5-bytes, data, 5-bytes...
7373
return len(b), nil
7474
}
7575
c.peerCtr.XORKeyStream(b, b)
76-
if c.isHeader {
77-
if t, _, _ := DecodeHeader(b); t == 23 { // always 5-bytes
76+
if c.isHeader { // always 5-bytes
77+
if t, _, _ := DecodeHeader(b); t == 23 {
7878
c.skipNext = true
7979
} else {
8080
c.isHeader = false

0 commit comments

Comments
 (0)