1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00

fix header-based websocket earlydata compat

This commit is contained in:
dyhkwong 2022-10-22 16:02:30 +08:00 committed by Shelikhoo
parent 882a363b85
commit dca2f2bd83
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316

View File

@ -38,6 +38,7 @@ var upgrader = &websocket.Upgrader{
} }
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
var earlyDataStr string
var earlyData io.Reader var earlyData io.Reader
if !h.earlyDataEnabled { // nolint: gocritic if !h.earlyDataEnabled { // nolint: gocritic
if request.URL.Path != h.path { if request.URL.Path != h.path {
@ -49,11 +50,11 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
writer.WriteHeader(http.StatusNotFound) writer.WriteHeader(http.StatusNotFound)
return return
} }
earlyDataStr := request.Header.Get(h.earlyDataHeaderName) earlyDataStr = request.Header.Get(h.earlyDataHeaderName)
earlyData = base64.NewDecoder(base64.RawURLEncoding, bytes.NewReader([]byte(earlyDataStr))) earlyData = base64.NewDecoder(base64.RawURLEncoding, bytes.NewReader([]byte(earlyDataStr)))
} else { } else {
if strings.HasPrefix(request.URL.RequestURI(), h.path) { if strings.HasPrefix(request.URL.RequestURI(), h.path) {
earlyDataStr := request.URL.RequestURI()[len(h.path):] earlyDataStr = request.URL.RequestURI()[len(h.path):]
earlyData = base64.NewDecoder(base64.RawURLEncoding, bytes.NewReader([]byte(earlyDataStr))) earlyData = base64.NewDecoder(base64.RawURLEncoding, bytes.NewReader([]byte(earlyDataStr)))
} else { } else {
writer.WriteHeader(http.StatusNotFound) writer.WriteHeader(http.StatusNotFound)
@ -61,7 +62,12 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
} }
} }
conn, err := upgrader.Upgrade(writer, request, nil) responseHeader := http.Header{}
if h.earlyDataEnabled && h.earlyDataHeaderName != "" {
responseHeader.Set(h.earlyDataHeaderName, earlyDataStr)
}
conn, err := upgrader.Upgrade(writer, request, responseHeader)
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()
return return