Implement HTTP hijacker for switching from HTTP to WebSocket

This commit is contained in:
2024-09-03 21:07:03 +07:00
parent d31688e1ba
commit b9c82fdbd6
2 changed files with 48 additions and 0 deletions

View File

@ -41,6 +41,24 @@ func (w *wrapper) WriteHeader(code int) {
func Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.Header.Get("upgrade") == "websocket" {
hijacker, ok := writer.(http.Hijacker)
if !ok {
http.Error(writer, "Hijacking not supported", http.StatusInternalServerError)
return
}
hijack, _, err := hijacker.Hijack()
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
defer hijack.Close()
rw := NewHijackWriter(hijack)
app.Server.Logger.Info(fmt.Sprintf("%s %s %s %v", utils.ClientIP(request), "WEBSOCKET", request.RequestURI, http.StatusSwitchingProtocols))
next.ServeHTTP(rw, request)
return
}
address := strings.Split(utils.Getenv("CORS_LIST"), ",")
for _, addr := range address {