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 support for permessage-deflate when sending or receiving messages. #43

Open
wants to merge 2 commits 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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ require("gwsockets")
GWSockets.addVerifyPath( "/etc/ssl/certs" )
```

* If you would like to enable the `permessage-deflate` extension which allows you to send and receive compressed messages, you can enable it with the following functions:

```LUA
-- Do note this will only be enabled if the websocket server supports permessage-deflate and enables it during handshake.
WEBSOCKET:setMessageCompression(true)
```

* You can also disable context takeover during compression, which will prevent re-using the same compression context over multiple messages.
This will decrease the memory usage at the cost of a worse compression ratio.

```LUA
WEBSOCKET:setDisableContextTakeover(true)
```

*WARNING:* Enabling compression over encrypted connections (`WSS://`) may make you vulnerable to [CRIME](https://en.wikipedia.org/wiki/CRIME)/[BREACH](https://en.wikipedia.org/wiki/BREACH) attacks.
Make sure you know what you are doing, or avoid sending sensitive information over websocket messages.

* Next add any cookies or headers you would like to send with the initial request (Optional)

```LUA
Expand Down
32 changes: 32 additions & 0 deletions src/GLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,34 @@ LUA_FUNCTION(socketSetHeader)
return 0;
}

LUA_FUNCTION(socketSetMessageCompression)
{
GWSocket* socket = getCppObject<GWSocket>(LUA);
if (socket->state != STATE_DISCONNECTED)
{
LUA->ThrowError("Cannot set message compression for an already connected websocket");
}

LUA->CheckType(2, Type::BOOL);
socket->setPerMessageDeflate(LUA->GetBool(2));

return 0;
}

LUA_FUNCTION(socketSetDisableContextTakeover)
{
GWSocket* socket = getCppObject<GWSocket>(LUA);
if (socket->state != STATE_DISCONNECTED)
{
LUA->ThrowError("Cannot set compression takeover for an already connected websocket");
}

LUA->CheckType(2, Type::BOOL);
socket->setDisableContextTakeover(LUA->GetBool(2));

return 0;
}

LUA_FUNCTION(socketIsConnected)
{
GWSocket* socket = getCppObject<GWSocket>(LUA);
Expand Down Expand Up @@ -422,6 +450,10 @@ GMOD_MODULE_OPEN()
LUA->SetField(-2, "setCookie");
LUA->PushCFunction(socketSetHeader);
LUA->SetField(-2, "setHeader");
LUA->PushCFunction(socketSetMessageCompression);
LUA->SetField(-2, "setMessageCompression");
LUA->PushCFunction(socketSetDisableContextTakeover);
LUA->SetField(-2, "setDisableContextTakeover");
LUA->PushCFunction(socketIsConnected);
LUA->SetField(-2, "isConnected");
LUA->PushCFunction(socketClearQueue);
Expand Down
12 changes: 11 additions & 1 deletion src/GWSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,14 @@ bool GWSocket::setHeader(std::string key, std::string value)
}
this->headers[key] = value;
return true;
}
}

void GWSocket::setPerMessageDeflate(bool value)
{
perMessageDeflate = value;
}

void GWSocket::setDisableContextTakeover(bool value)
{
disableContextTakeover = value;
}
5 changes: 5 additions & 0 deletions src/GWSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class GWSocket
void write(std::string message);
bool setCookie(std::string key, std::string value);
bool setHeader(std::string key, std::string value);
void setPerMessageDeflate(bool value);
void setDisableContextTakeover(bool value);

BlockingQueue<GWSocketMessageIn> messageQueue;
bool isConnected() { return state == STATE_CONNECTED; };
bool canBeDeleted() { return state == STATE_DISCONNECTED; };
Expand All @@ -78,6 +81,8 @@ class GWSocket
std::string host;
unsigned int port;
std::atomic<SocketState> state{ STATE_DISCONNECTED };
bool perMessageDeflate;
bool disableContextTakeover;

static std::unique_ptr<boost::asio::io_context> ioc; //Needs to be initialized on module load
protected:
Expand Down
14 changes: 12 additions & 2 deletions src/SSLWebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SSLWebSocket : public GWSocket
void closeSocket();
void sslHandshakeComplete(const boost::system::error_code& ec, std::string host, std::string path, std::function<void(websocket::request_type&)> decorator);
bool verifyCertificate(bool preverified, boost::asio::ssl::verify_context& ctx);
std::atomic<websocket::stream<ssl::stream<tcp::socket>>*> ws{ nullptr };
std::atomic<websocket::stream<ssl::stream<tcp::socket>, true>*> ws{ nullptr };
//This is not an atomic function, it only ensures visibility.
//Callers have to make sure that atomicity is not required/ensured otherwise
void resetWS()
Expand All @@ -38,8 +38,18 @@ class SSLWebSocket : public GWSocket
{
delete ws;
}
ws = new websocket::stream<ssl::stream<tcp::socket>>(*ioc, *sslContext);

auto newWS = new websocket::stream<ssl::stream<tcp::socket>, true>(*ioc, *sslContext);

// Set permessage-deflate options for message compression if requested.
websocket::permessage_deflate opts;
opts.client_enable = perMessageDeflate;
opts.client_no_context_takeover = disableContextTakeover;
newWS->set_option(opts);

ws = newWS;
}

websocket::stream<ssl::stream<tcp::socket>>* getWS()
{
return this->ws.load();
Expand Down
12 changes: 11 additions & 1 deletion src/WebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ class WebSocket : public GWSocket
{
delete ws;
}
ws = new websocket::stream<tcp::socket>(*ioc);

auto newWS = new websocket::stream<tcp::socket, true>(*ioc);

// Set permessage-deflate options for message compression if requested.
websocket::permessage_deflate opts;
opts.client_enable = perMessageDeflate;
opts.client_no_context_takeover = disableContextTakeover;
newWS->set_option(opts);

ws = newWS;
}

websocket::stream<tcp::socket>* getWS()
{
return this->ws.load();
Expand Down