Skip to content

Commit

Permalink
Fix a bug websocket relatedt to HiJack interface
Browse files Browse the repository at this point in the history
  • Loading branch information
alinz committed Aug 29, 2024
1 parent da5ab26 commit 9767f4f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Baker is a dynamic HTTP reverse proxy with a focus on extensibility and flexibil
- Configurable rate limiter per domain and path.
- Prometheus metrics are available at `BAKER_METRICS_ADDRS/metrics`
- Static Configuration for those services that doesn't expose any config path
- Support Proxy WebSocket

# Usage

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/prometheus/client_golang v1.20.1
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.24.0
golang.org/x/time v0.6.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
17 changes: 15 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package baker

import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"math/rand"
"net"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -49,6 +51,17 @@ type trackResponseWriter struct {
w http.ResponseWriter
}

var _ http.Hijacker = (*trackResponseWriter)(nil)

func (t *trackResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := t.w.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("response writer does not support hijacking")
}

return h.Hijack()
}

var _ http.ResponseWriter = (*trackResponseWriter)(nil)

func (t *trackResponseWriter) Header() http.Header {
Expand Down Expand Up @@ -111,14 +124,14 @@ func (s *Server) handleWebSocket(w http.ResponseWriter, r *http.Request, contain

clientConn, _, err := websocket.Dial(r.Context(), targetURL.String(), nil)
if err != nil {
http.Error(w, "Error connecting to backend server", http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("Error connecting to backend server: %s", err), http.StatusInternalServerError)
return
}
defer clientConn.Close(websocket.StatusNormalClosure, "")

serverConn, err := websocket.Accept(w, r, nil)
if err != nil {
http.Error(w, "Error accepting WebSocket connection", http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("Error connecting to backend server: %s", err), http.StatusInternalServerError)
return
}
defer serverConn.Close(websocket.StatusNormalClosure, "")
Expand Down

0 comments on commit 9767f4f

Please sign in to comment.