mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-30 21:58:39 +00:00
HTTPUpgrade: Fix data loss
This commit is contained in:
@@ -1,17 +1,35 @@
|
|||||||
package httpupgrade
|
package httpupgrade
|
||||||
|
|
||||||
import "net"
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/xtls/xray-core/transport/internet/stat"
|
||||||
|
)
|
||||||
|
|
||||||
type connection struct {
|
type connection struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
remoteAddr net.Addr
|
remoteAddr net.Addr
|
||||||
|
remainData []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConnection(conn net.Conn, remoteAddr net.Addr) *connection {
|
func newConnection(conn net.Conn, remoteAddr net.Addr, remainData []byte) stat.Connection {
|
||||||
return &connection{
|
return stat.Connection(&connection{
|
||||||
Conn: conn,
|
Conn: conn,
|
||||||
remoteAddr: remoteAddr,
|
remoteAddr: remoteAddr,
|
||||||
|
remainData: remainData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *connection) Read(p []byte) (int, error) {
|
||||||
|
if len(c.remainData) > 0 {
|
||||||
|
n := copy(p, c.remainData)
|
||||||
|
c.remainData = c.remainData[n:]
|
||||||
|
if len(c.remainData) == 0 {
|
||||||
|
c.remainData = nil
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
}
|
}
|
||||||
|
return c.Conn.Read(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connection) RemoteAddr() net.Addr {
|
func (c *connection) RemoteAddr() net.Addr {
|
||||||
|
|||||||
@@ -100,7 +100,12 @@ func (s *server) upgrade(conn net.Conn) (stat.Connection, error) {
|
|||||||
}
|
}
|
||||||
remoteAddr = http_proto.ApplyTrustedXForwardedFor(req.Header, trustedXFF, remoteAddr)
|
remoteAddr = http_proto.ApplyTrustedXForwardedFor(req.Header, trustedXFF, remoteAddr)
|
||||||
|
|
||||||
return stat.Connection(newConnection(conn, remoteAddr)), nil
|
var remainData []byte
|
||||||
|
if remainLength := connReader.Buffered(); remainLength > 0 {
|
||||||
|
remainData = make([]byte, remainLength)
|
||||||
|
connReader.Read(remainData)
|
||||||
|
}
|
||||||
|
return newConnection(conn, remoteAddr, remainData), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) keepAccepting() {
|
func (s *server) keepAccepting() {
|
||||||
|
|||||||
Reference in New Issue
Block a user