1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00

fix early data listener bug

This commit is contained in:
Shelikhoo 2021-04-05 19:34:22 +01:00
parent e413653b1d
commit 3a77bbdf65
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316

View File

@ -3,9 +3,13 @@
package websocket package websocket
import ( import (
"bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"encoding/base64"
"io"
"net/http" "net/http"
"strings"
"sync" "sync"
"time" "time"
@ -20,8 +24,9 @@ import (
) )
type requestHandler struct { type requestHandler struct {
path string path string
ln *Listener ln *Listener
earlyDataEnabled bool
} }
var upgrader = &websocket.Upgrader{ var upgrader = &websocket.Upgrader{
@ -34,10 +39,22 @@ var upgrader = &websocket.Upgrader{
} }
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != h.path { var earlyData io.Reader
writer.WriteHeader(http.StatusNotFound) if !h.earlyDataEnabled {
return if request.URL.Path != h.path {
writer.WriteHeader(http.StatusNotFound)
return
}
} else {
if strings.HasPrefix(request.URL.RequestURI(), h.path) {
earlyDataStr := request.URL.RequestURI()[len(h.path):]
earlyData = base64.NewDecoder(base64.RawURLEncoding, bytes.NewReader([]byte(earlyDataStr)))
} else {
writer.WriteHeader(http.StatusNotFound)
return
}
} }
conn, err := upgrader.Upgrade(writer, request, nil) conn, err := upgrader.Upgrade(writer, request, nil)
if err != nil { if err != nil {
newError("failed to convert to WebSocket connection").Base(err).WriteToLog() newError("failed to convert to WebSocket connection").Base(err).WriteToLog()
@ -52,8 +69,12 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
Port: int(0), Port: int(0),
} }
} }
if earlyData == nil {
h.ln.addConn(newConnection(conn, remoteAddr))
} else {
h.ln.addConn(newConnectionWithEarlyData(conn, remoteAddr, earlyData))
}
h.ln.addConn(newConnection(conn, remoteAddr))
} }
type Listener struct { type Listener struct {
@ -114,11 +135,16 @@ func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSet
} }
l.listener = listener l.listener = listener
var useEarlyData = false
if wsSettings.MaxEarlyData != 0 {
useEarlyData = true
}
l.server = http.Server{ l.server = http.Server{
Handler: &requestHandler{ Handler: &requestHandler{
path: wsSettings.GetNormalizedPath(), path: wsSettings.GetNormalizedPath(),
ln: l, ln: l,
earlyDataEnabled: useEarlyData,
}, },
ReadHeaderTimeout: time.Second * 4, ReadHeaderTimeout: time.Second * 4,
MaxHeaderBytes: 2048, MaxHeaderBytes: 2048,