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

httpd: allow websockets VNC connection on http port if run with -enablehttpproxy #492

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libvncserver/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ httpProcessInput(rfbScreenInfoPtr rfbScreen)
rfbScreen->httpSock = RFB_INVALID_SOCKET;
return;
}
#ifdef LIBVNCSERVER_WITH_WEBSOCKETS
if (strstr(buf, "\r\nUpgrade: websocket\r\n")) {
/* websocket connection */
rfbLog("httpd: client asked to upgrade to websockets connection\n");
rfbNewWebSocketsClient(rfbScreen,rfbScreen->httpSock,buf);
rfbScreen->httpSock = RFB_INVALID_SOCKET;
return;
}
#endif
}

if (strncmp(buf, "GET ", 4)) {
Expand Down
6 changes: 6 additions & 0 deletions libvncserver/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ void rfbRedrawAfterHideCursor(rfbClientPtr cl,sraRegionPtr updateRegion);

rfbClientPtr rfbClientIteratorHead(rfbClientIteratorPtr i);

#ifdef LIBVNCSERVER_WITH_WEBSOCKETS
/* from websockets.c */

rfbBool webSocketsHandshake(rfbClientPtr cl, char *scheme, const char *httpHeaders);
#endif

/* from tight.c */

#ifdef LIBVNCSERVER_HAVE_LIBZ
Expand Down
23 changes: 19 additions & 4 deletions libvncserver/rfbserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ rfbSetProtocolVersion(rfbScreenInfoPtr rfbScreen, int major_, int minor_)
static rfbClientPtr
rfbNewTCPOrUDPClient(rfbScreenInfoPtr rfbScreen,
rfbSocket sock,
rfbBool isUDP)
rfbBool isUDP,
const char *httpHeaders)
{
rfbProtocolVersionMsg pv;
rfbClientIteratorPtr iterator;
Expand Down Expand Up @@ -470,10 +471,18 @@ rfbNewTCPOrUDPClient(rfbScreenInfoPtr rfbScreen,
#endif

#ifdef LIBVNCSERVER_WITH_WEBSOCKETS
if (httpHeaders) {
/* Do websocket handshake with HTTP headers previously read */
if (!webSocketsHandshake(cl, "ws", httpHeaders)) {
rfbCloseClient(cl);
rfbClientConnectionGone(cl);
return NULL;
}
}
/*
* Wait a few ms for the client to send WebSockets connection (TLS/SSL or plain)
*/
if (!webSocketsCheck(cl)) {
else if (!webSocketsCheck(cl)) {
/* Error reporting handled in webSocketsHandshake */
rfbCloseClient(cl);
rfbClientConnectionGone(cl);
Expand Down Expand Up @@ -530,14 +539,20 @@ rfbClientPtr
rfbNewClient(rfbScreenInfoPtr rfbScreen,
rfbSocket sock)
{
return(rfbNewTCPOrUDPClient(rfbScreen,sock,FALSE));
return(rfbNewTCPOrUDPClient(rfbScreen,sock,FALSE,NULL));
}

rfbClientPtr
rfbNewUDPClient(rfbScreenInfoPtr rfbScreen)
{
return((rfbScreen->udpClient=
rfbNewTCPOrUDPClient(rfbScreen,rfbScreen->udpSock,TRUE)));
rfbNewTCPOrUDPClient(rfbScreen,rfbScreen->udpSock,TRUE,NULL)));
}

rfbClientPtr
rfbNewWebSocketsClient(rfbScreenInfoPtr rfbScreen, rfbSocket sock, const char *httpHeaders)
{
return(rfbNewTCPOrUDPClient(rfbScreen,sock,FALSE,httpHeaders));
}

/*
Expand Down
17 changes: 11 additions & 6 deletions libvncserver/websockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "crypto.h"
#include "ws_decode.h"
#include "base64.h"
#include "private.h"

#if 0
#include <sys/syscall.h>
Expand Down Expand Up @@ -92,8 +93,6 @@ struct timeval
;
#endif

static rfbBool webSocketsHandshake(rfbClientPtr cl, char *scheme);

static int webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst);

static int ws_read(void *cl, char *buf, size_t len);
Expand Down Expand Up @@ -157,15 +156,15 @@ webSocketsCheck (rfbClientPtr cl)

rfbLog("Got '%s' WebSockets handshake\n", scheme);

if (!webSocketsHandshake(cl, scheme)) {
if (!webSocketsHandshake(cl, scheme, NULL)) {
return FALSE;
}
/* Start WebSockets framing */
return TRUE;
}

static rfbBool
webSocketsHandshake(rfbClientPtr cl, char *scheme)
rfbBool
webSocketsHandshake(rfbClientPtr cl, char *scheme, const char *httpHeaders)
{
char *buf, *response, *line;
int n, linestart = 0, len = 0, llen, base64 = FALSE;
Expand All @@ -189,7 +188,13 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
}

while (len < WEBSOCKETS_MAX_HANDSHAKE_LEN-1) {
if ((n = rfbReadExactTimeout(cl, buf+len, 1,
if (httpHeaders) {
buf[len] = httpHeaders[0];
if (!httpHeaders[0])
break;
httpHeaders++;
}
else if ((n = rfbReadExactTimeout(cl, buf+len, 1,
WEBSOCKETS_CLIENT_SEND_WAIT_MS)) <= 0) {
if ((n < 0) && (errno == ETIMEDOUT)) {
break;
Expand Down
1 change: 1 addition & 0 deletions rfb/rfb.h
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ extern rfbBool webSocketCheckDisconnect(rfbClientPtr cl);
extern int webSocketsEncode(rfbClientPtr cl, const char *src, int len, char **dst);
extern int webSocketsDecode(rfbClientPtr cl, char *dst, int len);
extern rfbBool webSocketsHasDataInBuffer(rfbClientPtr cl);
extern rfbClientPtr rfbNewWebSocketsClient(rfbScreenInfoPtr rfbScreen, rfbSocket sock, const char *httpHeaders);
#endif

/* rfbserver.c */
Expand Down