Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add websocket support #962

Merged
merged 3 commits into from
Aug 15, 2023
Merged

Conversation

ciarams87
Copy link
Member

@ciarams87 ciarams87 commented Aug 14, 2023

Proposed changes

Problem: As a user, I want to be able to use NKG to proxy WebSocket traffic, so that I can connect to applications using the WebSocket protocol

Solution: When the "upgrade" header is set, configure it to what is set in the request, and configure the "connection" header to "upgrade". When the "upgrade" header is not set, configure the "connection" header to "close. See https://nginx.org/en/docs/http/websocket.html. This additionally requires that "upgrade" and "connection" headers cannot be overridden using header filters.

Testing: Updated unit-tests, confirmed conformance tests are still passing, and manual testing:
Tested HTTP and WebSocket connections:

WebSocket

* WebSocket connection established
* Preparing request to ws://cafe.example.com/
* Current time is 2023-08-14T15:01:51.443Z
* Using HTTP 1.1

> GET / HTTP/1.1
> host: cafe.example.com:80
> Sec-WebSocket-Version: 13
> Sec-WebSocket-Key: Ul3XldhcMNzcY3c3pvzHcQ==
> Connection: Upgrade
> Upgrade: websocket
> Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

< HTTP/1.1 101 Switching Protocols
< server: nginx/1.25.1
< date: Mon, 14 Aug 2023 15:01:51 GMT
< connection: upgrade
< upgrade: websocket
< sec-websocket-accept: 1zoiZ45TTpCrRx7HuiFgR/nsCD0=

HTTP connection using http-header-filter example:

  1. With upgrade header set
❯ curl -s --resolve echo.example.com:$GW_PORT:$GW_IP http://echo.example.com:$GW_PORT/headers -H "My-Cool-Header:my-client-value" -H "My-Overwrite-Header:dont-see-this" -H "Upgrade:anything"
Headers:
  header 'Accept-Encoding' is 'compress'
  header 'My-cool-header' is 'my-client-value,this-is-an-appended-value'
  header 'My-Overwrite-Header' is 'this-is-the-only-value'
  header 'Host' is 'echo.example.com'
  header 'Upgrade' is 'anything'
  header 'Connection' is 'upgrade'
  header 'Accept' is '*/*'
  1. Without upgrade header set
❯ curl -s --resolve echo.example.com:$GW_PORT:$GW_IP http://echo.example.com:$GW_PORT/headers -H "My-Cool-Header:my-client-value" -H "My-Overwrite-Header:dont-see-this"
Headers:
  header 'Accept-Encoding' is 'compress'
  header 'My-cool-header' is 'my-client-value,this-is-an-appended-value'
  header 'My-Overwrite-Header' is 'this-is-the-only-value'
  header 'Host' is 'echo.example.com'
  header 'Connection' is 'close'
  header 'Accept' is '*/*'

Closes #315

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork

@ciarams87 ciarams87 requested a review from a team as a code owner August 14, 2023 16:11
@github-actions github-actions bot added the enhancement New feature or request label Aug 14, 2023
@ciarams87 ciarams87 merged commit b4d6a33 into nginxinc:main Aug 15, 2023
22 checks passed
@ciarams87 ciarams87 deleted the feat/add-websocket-support branch August 15, 2023 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Websocket connections rejected
3 participants