Skip to content

Commit

Permalink
fix wsproxy keepHost bug (#1168)
Browse files Browse the repository at this point in the history
* fix wsproxy keepHost bug

* fix wsproxy keepHost bug

* update doc

* Updating the section of the documentation regarding WebSockets.

---------

Co-authored-by: chenyarong <[email protected]>
  • Loading branch information
cyrnicolase and chenyarong committed Dec 20, 2023
1 parent 4f0842a commit 2dc8e98
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/02.Tutorials/2.6.Websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ filters:
pools:
- servers:
- url: ws:https://127.0.0.1:12345
# keepHost: true, the `Host` will be the same as the original request
# If the backend is a load balancer, it would prove to be highly beneficial
keepHost: true
- url: ws:https://127.0.0.1:9095
- url: ws:https://127.0.0.1:9096
- url: ws:https://127.0.0.1:9097
Expand Down
5 changes: 4 additions & 1 deletion docs/07.Reference/7.02.Filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ name: proxy-example-1
pools:
- servers:
- url: ws:https://127.0.0.1:9095
# keepHost: true, the `Host` will be the same as the original request
# If the backend is a load balancer, it would prove to be highly beneficial
keepHost: true
- url: ws:https://127.0.0.1:9096
- url: ws:https://127.0.0.1:9097
loadBalance:
Expand Down Expand Up @@ -2165,4 +2168,4 @@ template: |
| Name | Type | Description | Required |
|------|------|-------------|----------|
| header | [httpheader.AdaptSpec](#httpheaderAdaptSpec) | Rules to revise request header | No |
| body | string | If provided the body of the original request is replaced by the value of this option. | No |
| body | string | If provided the body of the original request is replaced by the value of this option. | No |
6 changes: 6 additions & 0 deletions pkg/filters/proxies/httpproxy/wspool.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func (sp *WebSocketServerPool) dialServer(svr *Server, req *httpprot.Request) (*
CompressionMode: websocket.CompressionDisabled,
}

// only set host when server address is not host name OR
// server is explicitly told to keep the host of the request.
if !svr.AddrIsHostName || svr.KeepHost {
opts.Host = req.Host()
}

opts.HTTPHeader.Del("Sec-WebSocket-Origin")
opts.HTTPHeader.Del("Sec-WebSocket-Protocol")
opts.HTTPHeader.Del("Sec-WebSocket-Accept")
Expand Down

0 comments on commit 2dc8e98

Please sign in to comment.