protected boolean verifySecureToken(final FullHttpRequest request, final PushConnection conn) { final String secureToken = request.headers().get(SECURE_TOKEN_HEADER_NAME); if (Strings.isNullOrEmpty(secureToken)) { // caller is not asking to verify secure token return true; } return secureToken.equals(conn.getSecureToken()); }
public ChannelFuture sendPushMessage(String mesg) { return sendPushMessage(Unpooled.copiedBuffer(mesg, Charsets.UTF_8)); }
public void put(final String clientId, final PushConnection pushConnection) { pushConnection.setSecureToken(mintNewSecureToken()); clientPushConnectionMap.put(clientId, pushConnection); }
@Test public void testOneMessagePerSecond() throws InterruptedException{ final PushConnection conn = new PushConnection(PushProtocol.WEBSOCKET, null); for (int i=0; i < 5; i ++) { Assert.assertFalse(conn.isRateLimited()); Thread.sleep(1000); } }
if (pushConn.isRateLimited()) { sendHttpResponse(ctx, request, HttpResponseStatus.SERVICE_UNAVAILABLE, userAuth); logRateLimited(); final ChannelFuture clientFuture = pushConn.sendPushMessage(body); clientFuture.addListener(cf -> { HttpResponseStatus status;
@Override public final void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { this.ctx = ctx; if (! destroyed.get()) { if (evt == pushProtocol.getHandshakeCompleteEvent()) { pushConnection = new PushConnection(pushProtocol, ctx); // Unauthenticated connection, wait for small amount of time for a client to send auth token in // a first web socket frame, otherwise close connection ctx.executor().schedule(this::closeIfNotAuthenticated, UNAUTHENTICATED_CONN_TTL.get(), TimeUnit.SECONDS); logger.debug("WebSocket handshake complete."); } else if (evt instanceof PushUserAuth) { authEvent = (PushUserAuth) evt; if (authEvent.isSuccess()) { logger.debug("registering client {}", authEvent); ctx.pipeline().remove(PushAuthHandler.NAME); registerClient(ctx, authEvent, pushConnection, pushConnectionRegistry); logger.debug("Authentication complete {}", authEvent); } else { pushProtocol.sendErrorAndClose(ctx,1008, "Auth failed"); } } } super.userEventTriggered(ctx, evt); }
@Test public void testFourMessagesInSuccession() { final PushConnection conn = new PushConnection(PushProtocol.WEBSOCKET, null); Assert.assertFalse(conn.isRateLimited()); Assert.assertFalse(conn.isRateLimited()); Assert.assertFalse(conn.isRateLimited()); Assert.assertTrue(conn.isRateLimited()); }
if (pushConn.isRateLimited()) { sendHttpResponse(ctx, request, HttpResponseStatus.SERVICE_UNAVAILABLE, userAuth); logRateLimited(); final ChannelFuture clientFuture = pushConn.sendPushMessage(body); clientFuture.addListener(cf -> { HttpResponseStatus status;
@Override public final void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { this.ctx = ctx; if (! destroyed.get()) { if (evt == pushProtocol.getHandshakeCompleteEvent()) { pushConnection = new PushConnection(pushProtocol, ctx); // Unauthenticated connection, wait for small amount of time for a client to send auth token in // a first web socket frame, otherwise close connection ctx.executor().schedule(this::closeIfNotAuthenticated, UNAUTHENTICATED_CONN_TTL.get(), TimeUnit.SECONDS); logger.debug("WebSocket handshake complete."); } else if (evt instanceof PushUserAuth) { authEvent = (PushUserAuth) evt; if (authEvent.isSuccess()) { logger.debug("registering client {}", authEvent); ctx.pipeline().remove(PushAuthHandler.NAME); registerClient(ctx, authEvent, pushConnection, pushConnectionRegistry); logger.debug("Authentication complete {}", authEvent); } else { pushProtocol.sendErrorAndClose(ctx,1008, "Auth failed"); } } } super.userEventTriggered(ctx, evt); }
@Test public void testMessagesInBatches() throws InterruptedException{ final PushConnection conn = new PushConnection(PushProtocol.WEBSOCKET, null); Assert.assertFalse(conn.isRateLimited()); Assert.assertFalse(conn.isRateLimited()); Assert.assertFalse(conn.isRateLimited()); Assert.assertTrue(conn.isRateLimited()); Thread.sleep(2000); Assert.assertFalse(conn.isRateLimited()); Assert.assertFalse(conn.isRateLimited()); Assert.assertFalse(conn.isRateLimited()); Assert.assertTrue(conn.isRateLimited()); }
protected boolean verifySecureToken(final FullHttpRequest request, final PushConnection conn) { final String secureToken = request.headers().get(SECURE_TOKEN_HEADER_NAME); if (Strings.isNullOrEmpty(secureToken)) { // caller is not asking to verify secure token return true; } return secureToken.equals(conn.getSecureToken()); }
public ChannelFuture sendPushMessage(String mesg) { return sendPushMessage(Unpooled.copiedBuffer(mesg, Charsets.UTF_8)); }
public void put(final String clientId, final PushConnection pushConnection) { pushConnection.setSecureToken(mintNewSecureToken()); clientPushConnectionMap.put(clientId, pushConnection); }
@Test public void testThreeMessagesInSuccession() { final PushConnection conn = new PushConnection(PushProtocol.WEBSOCKET, null); Assert.assertFalse(conn.isRateLimited()); Assert.assertFalse(conn.isRateLimited()); Assert.assertFalse(conn.isRateLimited()); }
@Test public void testFirstThreeMessagesSuccess() { final PushConnection conn = new PushConnection(PushProtocol.WEBSOCKET, null); for (int i=0; i < 10; i ++) { if (i < 3) { Assert.assertFalse(conn.isRateLimited()); } else { Assert.assertTrue(conn.isRateLimited()); } } }