@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { if (!acceptForeignIp) { if (!((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().isLoopbackAddress()) { ByteBuf cb = Unpooled.wrappedBuffer((QosConstants.BR_STR + "Foreign Ip Not Permitted." + QosConstants.BR_STR).getBytes()); ctx.writeAndFlush(cb).addListener(ChannelFutureListener.CLOSE); } } } }
public String remoteName() { InetSocketAddress addr = (InetSocketAddress) chctx.channel().remoteAddress(); if (addr == null) return null; // Use hostString that does not trigger a DNS resolution return addr.getHostString(); }
@Override protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) throws Exception { final InetAddress remoteIp = remoteAddress.getAddress(); if (!connected.add(remoteIp)) { return false; } else { ctx.channel().closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { connected.remove(remoteIp); } }); return true; } } }
private boolean handleNewChannel(ChannelHandlerContext ctx) throws Exception { @SuppressWarnings("unchecked") T remoteAddress = (T) ctx.channel().remoteAddress(); // If the remote address is not available yet, defer the decision. if (remoteAddress == null) { return false; } // No need to keep this handler in the pipeline anymore because the decision is going to be made now. // Also, this will prevent the subsequent events from being handled by this handler. ctx.pipeline().remove(this); if (accept(ctx, remoteAddress)) { channelAccepted(ctx, remoteAddress); } else { ChannelFuture rejectedFuture = channelRejected(ctx, remoteAddress); if (rejectedFuture != null) { rejectedFuture.addListener(ChannelFutureListener.CLOSE); } else { ctx.close(); } } return true; }
private void protocolViolation(ChannelHandlerContext ctx, CorruptedFrameException ex) { state = State.CORRUPT; if (ctx.channel().isActive()) { Object closeMessage; if (receivedClosingHandshake) { closeMessage = Unpooled.EMPTY_BUFFER; } else { closeMessage = new CloseWebSocketFrame(1002, null); } ctx.writeAndFlush(closeMessage).addListener(ChannelFutureListener.CLOSE); } throw ex; }
private void sendGoAwayFrame(ChannelHandlerContext ctx, ChannelPromise future) { // Avoid NotYetConnectedException if (!ctx.channel().isActive()) { ctx.close(future); return; } ChannelFuture f = sendGoAwayFrame(ctx, SpdySessionStatus.OK); if (spdySession.noActiveStreams()) { f.addListener(new ClosingChannelFutureListener(ctx, future)); } else { closeSessionFutureListener = new ClosingChannelFutureListener(ctx, future); } // FIXME: Close the connection forcibly after timeout. }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof WebSocketHandshakeException) { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(cause.getMessage().getBytes())); ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { ctx.fireExceptionCaught(cause); ctx.close(); } }
private void handleDisconnectMessage(ChannelHandlerContext ctx, ProxyMessage proxyMessage) { Channel realServerChannel = ctx.channel().attr(Constants.NEXT_CHANNEL).get(); logger.debug("handleDisconnectMessage, {}", realServerChannel); if (realServerChannel != null) { ctx.channel().attr(Constants.NEXT_CHANNEL).remove(); ClientChannelMannager.returnProxyChanel(ctx.channel()); realServerChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } }
@Override public void handlerAdded( ChannelHandlerContext ctx ) throws Exception { // Sometimes the connect event will have happened before adding, the channel will be active then if ( ctx.channel().isActive() ) { SslHandler sslHandler = createSslHandler( ctx, (InetSocketAddress) ctx.channel().remoteAddress() ); replaceSelfWith( sslHandler ); sslHandler.handlerAdded( ctx ); } }
public final void sendHttpResponse(HttpRequest req, ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); resp.headers().add("Content-Length", "0"); final boolean closeConn = ((status != OK) || (! HttpUtil.isKeepAlive(req))); if (closeConn) { resp.headers().add(HttpHeaderNames.CONNECTION, "Close"); } final ChannelFuture cf = ctx.channel().writeAndFlush(resp); if (closeConn) { cf.addListener(ChannelFutureListener.CLOSE); } }
private String getRemoteAddress(ChannelHandlerContext ctx) { if (ctx.channel().remoteAddress() == null) { return null; } InetSocketAddress inetAddress = (InetSocketAddress) ctx.channel().remoteAddress(); return inetAddress.getAddress().getHostAddress() + ":" + inetAddress.getPort(); }