Example usage for io.netty.handler.ssl SslHandler SslHandler

List of usage examples for io.netty.handler.ssl SslHandler SslHandler

Introduction

In this page you can find the example usage for io.netty.handler.ssl SslHandler SslHandler.

Prototype

public SslHandler(SSLEngine engine) 

Source Link

Document

Creates a new instance which runs all delegated tasks directly on the EventExecutor .

Usage

From source file:com.xx_dev.apn.proxy.ApnProxyRemoteForwardChannelInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel channel) throws Exception {

    ApnProxyRemote apnProxyRemote = uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get().getRemote();

    channel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY)
            .set(uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get());

    ChannelPipeline pipeline = channel.pipeline();

    pipeline.addLast("idlestate", new IdleStateHandler(0, 0, 3, TimeUnit.MINUTES));
    pipeline.addLast("idlehandler", new ApnProxyIdleHandler());

    if (apnProxyRemote.getRemoteListenType() == ApnProxyListenType.SSL) {
        SSLEngine engine = ApnProxySSLContextFactory.createClientSSLEnginForRemoteAddress(
                apnProxyRemote.getRemoteHost(), apnProxyRemote.getRemotePort());
        engine.setUseClientMode(true);/*w w w . ja  va 2  s . co m*/

        pipeline.addLast("ssl", new SslHandler(engine));
    } else if (apnProxyRemote.getRemoteListenType() == ApnProxyListenType.AES) {
        byte[] key = ((ApnProxyAESRemote) apnProxyRemote).getKey();
        byte[] iv = ((ApnProxyAESRemote) apnProxyRemote).getIv();

        pipeline.addLast("apnproxy.encrypt", new ApnProxyAESEncoder(key, iv));
        pipeline.addLast("apnproxy.decrypt", new ApnProxyAESDecoder(key, iv));
    }

    pipeline.addLast("codec", new HttpClientCodec());

    pipeline.addLast(ApnProxyRemoteForwardHandler.HANDLER_NAME,
            new ApnProxyRemoteForwardHandler(uaChannel, remoteChannelInactiveCallback));

}

From source file:com.xx_dev.apn.proxy.ApnProxyServerChannelInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel channel) throws Exception {
    ChannelPipeline pipeline = channel.pipeline();

    pipeline.addLast("idlestate", new IdleStateHandler(0, 0, 3, TimeUnit.MINUTES));
    pipeline.addLast("idlehandler", new ApnProxyIdleHandler());

    pipeline.addLast("datalog", new LoggingHandler("PRE_BYTE_LOGGER", LogLevel.DEBUG));

    if (ApnProxyConfig.getConfig().getListenType() == ApnProxyListenType.SSL) {
        SSLEngine engine = ApnProxySSLContextFactory.createServerSSLSSLEngine();
        pipeline.addLast("apnproxy.encrypt", new SslHandler(engine));
    } else if (ApnProxyConfig.getConfig().getListenType() == ApnProxyListenType.AES) {
        byte[] key = ApnProxyConfig.getConfig().getKey();
        byte[] iv = ApnProxyConfig.getConfig().getIv();
        pipeline.addLast("apnproxy.encrypt", new ApnProxyAESEncoder(key, iv));
        pipeline.addLast("apnproxy.decrypt", new ApnProxyAESDecoder(key, iv));
    }//from   w  ww . j a v a  2  s .  c o  m

    pipeline.addLast("log", new LoggingHandler("BYTE_LOGGER", LogLevel.INFO));

    pipeline.addLast("codec", new HttpServerCodec());

    pipeline.addLast(ApnProxyPreHandler.HANDLER_NAME, new ApnProxyPreHandler());

    pipeline.addLast(ApnProxySchemaHandler.HANDLER_NAME, new ApnProxySchemaHandler());

}

From source file:com.xx_dev.apn.proxy.ApnProxyTunnelChannelInitializer.java

License:Apache License

/**
 * @see io.netty.channel.ChannelInitializer#initChannel(io.netty.channel.Channel)
 */// ww w.j  a  v  a 2 s .c  om
@Override
protected void initChannel(SocketChannel channel) throws Exception {
    ApnProxyRemote apnProxyRemote = uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get().getRemote();

    channel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY)
            .set(uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get());

    ChannelPipeline pipeline = channel.pipeline();

    pipeline.addLast("idlestate", new IdleStateHandler(0, 0, 3, TimeUnit.MINUTES));
    pipeline.addLast("idlehandler", new ApnProxyIdleHandler());

    if (apnProxyRemote.getRemoteListenType() == ApnProxyListenType.SSL) {
        SSLEngine engine = ApnProxySSLContextFactory.createClientSSLEnginForRemoteAddress(
                apnProxyRemote.getRemoteHost(), apnProxyRemote.getRemotePort());
        engine.setUseClientMode(true);

        pipeline.addLast("ssl", new SslHandler(engine));
    } else if (apnProxyRemote.getRemoteListenType() == ApnProxyListenType.AES) {
        byte[] key = ((ApnProxyAESRemote) apnProxyRemote).getKey();
        byte[] iv = ((ApnProxyAESRemote) apnProxyRemote).getIv();

        pipeline.addLast("apnproxy.encrypt", new ApnProxyAESEncoder(key, iv));
        pipeline.addLast("apnproxy.decrypt", new ApnProxyAESDecoder(key, iv));
    }

    if (apnProxyRemote.getRemoteListenType() == ApnProxyListenType.PLAIN) {
        // nothing to do
    }

    pipeline.addLast(new ApnProxyRelayHandler(apnProxyRemote.getRemoteAddr() + " --> UA", uaChannel));

}

From source file:com.xx_dev.speed_test.SpeedTestHttpClientChannelInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // Create a default pipeline implementation.
    ChannelPipeline p = ch.pipeline();//from w  w  w  . j  ava 2 s .c  o m

    if (isSSL) {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, null, null);
        SSLEngine engine = sslcontext.createSSLEngine();
        engine.setUseClientMode(true);
        p.addLast("ssl", new SslHandler(engine));
    }

    p.addLast("codec", new HttpClientCodec());

    // Remove the following line if you don't want automatic content decompression.
    p.addLast("inflater", new HttpContentDecompressor());

    p.addLast("handler", new SpeedTestHttpClientHandler(resultPrintWriter));
}

From source file:com.zextras.modules.chat.server.LocalXmppConnectionProviderImpl.java

License:Open Source License

@Override
public Channel openConnection(String host, int port, final ChannelHandler channelHandler) throws IOException {
    ChannelHandler handler = new ChannelInitializer<SocketChannel>() {
        @Override/*from w  w w . ja v a 2  s .  c o  m*/
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            SSLEngine sslEngine = mZimbraSSLContextProvider.get().createSSLEngine();
            sslEngine.setUseClientMode(true);
            SslHandler sslHandler = new SslHandler(sslEngine);
            socketChannel.pipeline().addFirst("ssl", sslHandler);
            socketChannel.pipeline().addLast("handler", channelHandler);
        }
    };
    ChannelFuture channelFuture = new Bootstrap().channel(NioSocketChannel.class).group(new NioEventLoopGroup())
            .handler(handler).connect(host, port);

    try {
        channelFuture.sync();
        if (!channelFuture.isSuccess()) {
            throw channelFuture.cause();
        }

        return channelFuture.channel();
    } catch (Throwable t) {
        throw new IOException(t);
    }
}

From source file:com.zextras.modules.chat.services.LocalXmppService.java

License:Open Source License

@Override
public void run() {
    ChatLog.log.info("Listening on port " + DEFAULT_LOCAL_XMPP_PORT);
    EventLoopGroup acceptorGroup = new NioEventLoopGroup(4);
    EventLoopGroup channelWorkerGroup = new NioEventLoopGroup(8);

    Channel channel;/*from  w  ww.j  a v a 2s  .  c  om*/
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(acceptorGroup, channelWorkerGroup);
        bootstrap.channel(NioServerSocketChannel.class);
        ChannelHandler handler = new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                try {
                    SSLEngine sslEngine = mZimbraSSLContextProvider.get().createSSLEngine();
                    sslEngine.setUseClientMode(false);
                    SslHandler sslHandler = new SslHandler(sslEngine);
                    ch.pipeline().addFirst("ssl", sslHandler);
                    ch.pipeline().addLast(null, "SubTagTokenizer", new XmlSubTagTokenizer());
                    ch.pipeline().addLast(null, "XmlTagTokenizer", new XmlTagTokenizer());
                    ch.pipeline().addAfter("XmlTagTokenizer", "StanzaProcessor",
                            new ChannelInboundHandlerAdapter() {
                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                    mLocalXmppReceiver.processStanza((String) msg);
                                }
                            });
                } catch (Throwable t) {
                    ChatLog.log.warn("Unable to initializer XMPP connection: " + Utils.exceptionToString(t));
                    ch.close();
                }
            }
        };

        ChannelFuture channelFuture = bootstrap.childHandler(handler).option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 0).bind(DEFAULT_LOCAL_XMPP_PORT).sync();

        if (!channelFuture.isSuccess()) {
            throw channelFuture.cause();
        }

        channel = channelFuture.channel();
        mInitializationPromise.setSuccess(null);
    } catch (Throwable e) {
        mInitializationPromise.setFailure(e);
        return;
    }

    mLock.lock();
    try {
        while (!mStopRequested) {
            try {
                mWaitStopRequest.await();
            } catch (InterruptedException ignored) {
            }
        }

        channel.close().sync();

        acceptorGroup.shutdownGracefully().sync();
        channelWorkerGroup.shutdownGracefully().sync();
    } catch (InterruptedException ignored) {
    } finally {
        mLock.unlock();
    }
}

From source file:com.zh.revproxy.SecureProxyInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.

    //      pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));

    if (isSecureBackend) {
        LOGGER.info("Adding the SSL Handler to the pipeline");

        SSLEngine engine = SSLUtil.createClientSSLContext(trustStoreLocation, trustStorePassword)
                .createSSLEngine();// w w w .j  av a2  s  . com
        engine.setUseClientMode(true);

        pipeline.addLast("ssl", new SslHandler(engine));
    }

    // needed to forward the decoded request
    // to the backend in encoded form

    pipeline.addLast("encoder", new HttpClientCodec(104857600, 104857600, 104857600));
    pipeline.addLast(new HttpReverseProxyBackendHandler(inbound));
}

From source file:connection.bootstrap.Initialiser.java

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    SSLEngine engine = sslCtx.createSSLEngine();
    engine.setUseClientMode(false);/*from   w w  w  . ja v a 2  s . c  o  m*/

    pipeline.addLast("ssl", new SslHandler(engine));
    pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast("handler", new Handler());
    pipeline.addLast("endOfPipe", new EndOfPipe());
}

From source file:de.saxsys.synchronizefx.netty.base.NonValidatingSSLEngineFactory.java

License:Open Source License

/**
 * Creates a new {@link SslHandler} in client or server mode.
 * /* w  w w.  j  a  v a  2s. c om*/
 * @param clientMode if <code>true</code> a client engine is created, if <code>false</code> a server engine.
 * @return The new handler
 */
public static SslHandler createSslHandler(final boolean clientMode) {
    final SSLEngine engine = createEngine(clientMode);
    final SslHandler handler = new SslHandler(engine);
    handler.handshakeFuture().addListener(new GenericFutureListener<Future<? super Channel>>() {
        @Override
        public void operationComplete(final Future<? super Channel> future) throws Exception {
            LOG.debug("Using cipher " + engine.getSession().getCipherSuite()
                    + " for the encrypted connection to the server.");
        }
    });
    return handler;
}

From source file:fileShare.HttpUploadServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // Create a default pipeline implementation.
    ChannelPipeline pipeline = ch.pipeline();

    if (HttpUploadServer.isSSL) {
        SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
        engine.setUseClientMode(false);//from w w  w .ja v  a2  s  . c  o  m
        pipeline.addLast("ssl", new SslHandler(engine));
    }

    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("encoder", new HttpResponseEncoder());

    // Remove the following line if you don't want automatic content
    // compression.
    //pipeline.addLast("deflater", new HttpContentCompressor());

    pipeline.addLast("handler", new HttpUploadServerHandler());
}