Skip to content

Commit

Permalink
Customize WebSocket Mask
Browse files Browse the repository at this point in the history
  • Loading branch information
mozafari committed Jun 15, 2019
1 parent efaec21 commit 205c119
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/neovisionaries/ws/client/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@
*/
public class WebSocket
{
public static boolean useMask = true;
private static final long DEFAULT_CLOSE_DELAY = 10 * 1000L;
private final WebSocketFactory mWebSocketFactory;
private final SocketConnector mSocketConnector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ public void write(WebSocketFrame frame) throws IOException
writeFrame1(frame);
writeFrameExtendedPayloadLength(frame);

// Generate a random masking key.
byte[] maskingKey = Misc.nextBytes(4);

// Write the masking key.
write(maskingKey);

// Write the payload.
writeFramePayload(frame, maskingKey);
if (WebSocket.useMask) {
// Generate a random masking key.
byte[] maskingKey = Misc.nextBytes(4);
write(maskingKey);

// Write the payload.
writeFramePayload(frame, maskingKey);
} else {
writeFramePayload(frame, null);
}
}


Expand All @@ -70,7 +73,12 @@ private void writeFrame0(WebSocketFrame frame) throws IOException
private void writeFrame1(WebSocketFrame frame) throws IOException
{
// Frames sent from a client are always masked.
int b = 0x80;
int b;
if (WebSocket.useMask) {
b = 0x80;
} else {
b = 0x00;
}

int len = frame.getPayloadLength();

Expand Down Expand Up @@ -133,7 +141,12 @@ private void writeFramePayload(WebSocketFrame frame, byte[] maskingKey) throws I
for (int i = 0; i < payload.length; ++i)
{
// Mask
int b = (payload[i] ^ maskingKey[i % 4]) & 0xFF;
int b;
if (WebSocket.useMask) {
b = (payload[i] ^ maskingKey[i % 4]) & 0xFF;
} else {
b = (payload[i]) & 0xFF;
}

// Write
write(b);
Expand Down

0 comments on commit 205c119

Please sign in to comment.