Skip to content

Commit f57985a

Browse files
committed
Fix vision read
1 parent 28f9e80 commit f57985a

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

vless/vision.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type VisionConn struct {
5959
remainingPadding int
6060
currentCommand byte
6161
directRead bool
62-
remainingReader io.Reader
62+
remainingBuffers []*buf.Buffer
6363
}
6464

6565
func NewVisionConn(conn net.Conn, tlsConn net.Conn, userUUID [16]byte, logger logger.Logger) (*VisionConn, error) {
@@ -101,16 +101,20 @@ func NewVisionConn(conn net.Conn, tlsConn net.Conn, userUUID [16]byte, logger lo
101101
}
102102

103103
func (c *VisionConn) Read(p []byte) (n int, err error) {
104-
if c.remainingReader != nil {
105-
n, err = c.remainingReader.Read(p)
106-
if err == io.EOF {
107-
err = nil
108-
c.remainingReader = nil
104+
for len(c.remainingBuffers) > 0 {
105+
newN, _ := c.remainingBuffers[0].Read(p[n:])
106+
if c.remainingBuffers[0].IsEmpty() {
107+
c.remainingBuffers[0].Release()
108+
c.remainingBuffers = c.remainingBuffers[1:]
109109
}
110-
if n > 0 {
111-
return
110+
n += newN
111+
if n == len(p) {
112+
break
112113
}
113114
}
115+
if n > 0 {
116+
return
117+
}
114118
if c.directRead {
115119
return c.netConn.Read(p)
116120
}
@@ -132,9 +136,6 @@ func (c *VisionConn) Read(p []byte) (n int, err error) {
132136
if c.withinPaddingBuffers || c.numberOfPacketToFilter > 0 {
133137
buffers := c.unPadding(bufferBytes)
134138
if chunkBuffer != nil {
135-
buffers = common.Map(buffers, func(it *buf.Buffer) *buf.Buffer {
136-
return it.ToOwned()
137-
})
138139
chunkBuffer.Reset()
139140
}
140141
if c.remainingContent == 0 && c.remainingPadding == 0 {
@@ -173,14 +174,14 @@ func (c *VisionConn) Read(p []byte) (n int, err error) {
173174
if c.numberOfPacketToFilter > 0 {
174175
c.filterTLS(buf.ToSliceMulti(buffers))
175176
}
176-
c.remainingReader = io.MultiReader(common.Map(buffers, func(it *buf.Buffer) io.Reader { return it })...)
177+
c.remainingBuffers = buffers
177178
return c.Read(p)
178179
} else {
179180
if c.numberOfPacketToFilter > 0 {
180181
c.filterTLS([][]byte{bufferBytes})
181182
}
182183
if chunkBuffer != nil {
183-
c.remainingReader = chunkBuffer
184+
c.remainingBuffers = append(c.remainingBuffers, chunkBuffer)
184185
return c.Read(p)
185186
}
186187
return
@@ -339,13 +340,13 @@ func (c *VisionConn) unPadding(buffer []byte) []*buf.Buffer {
339340
}
340341
}
341342
if c.remainingContent == -1 && c.remainingPadding == -1 {
342-
return []*buf.Buffer{buf.As(buffer)}
343+
return []*buf.Buffer{buf.As(buffer).ToOwned()}
343344
}
344345
var buffers []*buf.Buffer
345346
for bufferIndex < len(buffer) {
346347
if c.remainingContent <= 0 && c.remainingPadding <= 0 {
347348
if c.currentCommand == 1 {
348-
buffers = append(buffers, buf.As(buffer[bufferIndex:]))
349+
buffers = append(buffers, buf.As(buffer[bufferIndex:]).ToOwned())
349350
break
350351
} else {
351352
paddingInfo := buffer[bufferIndex : bufferIndex+5]
@@ -360,7 +361,7 @@ func (c *VisionConn) unPadding(buffer []byte) []*buf.Buffer {
360361
if end > len(buffer)-bufferIndex {
361362
end = len(buffer) - bufferIndex
362363
}
363-
buffers = append(buffers, buf.As(buffer[bufferIndex:bufferIndex+end]))
364+
buffers = append(buffers, buf.As(buffer[bufferIndex:bufferIndex+end]).ToOwned())
364365
c.remainingContent -= end
365366
bufferIndex += end
366367
} else {

0 commit comments

Comments
 (0)