mirror of
https://github.com/1f349/violet.git
synced 2024-12-24 00:14:17 +00:00
Add filterWebsocketHeaders function
This commit is contained in:
parent
1f4f4414d5
commit
8aa82303ce
@ -4,6 +4,7 @@ import (
|
||||
"github.com/1f349/violet/logger"
|
||||
"github.com/gorilla/websocket"
|
||||
"net/http"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@ -59,7 +60,7 @@ func (s *Server) Upgrade(rw http.ResponseWriter, req *http.Request) {
|
||||
Logger.Info("Dialing", "url", req.URL)
|
||||
|
||||
// dial for internal connection
|
||||
ic, _, err := websocket.DefaultDialer.DialContext(req.Context(), req.URL.String(), nil)
|
||||
ic, _, err := websocket.DefaultDialer.DialContext(req.Context(), req.URL.String(), filterWebsocketHeaders(req.Header))
|
||||
if err != nil {
|
||||
Logger.Info("Failed to dial", "url", req.URL, "err", err)
|
||||
s.Remove(c)
|
||||
@ -84,6 +85,16 @@ func (s *Server) Upgrade(rw http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// filterWebsocketHeaders allows specific headers to forward to the underlying websocket connection
|
||||
func filterWebsocketHeaders(headers http.Header) (out http.Header) {
|
||||
for k, v := range headers {
|
||||
if k == "Origin" {
|
||||
out[k] = slices.Clone(v)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Server) wsRelay(done chan struct{}, a, b *websocket.Conn) {
|
||||
defer func() {
|
||||
close(done)
|
||||
|
Loading…
Reference in New Issue
Block a user