Search in sources :

Example 1 with LineBasedFrameDecoder

use of io.netty.handler.codec.LineBasedFrameDecoder in project netty by netty.

the class FileServer method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc()));
                }
                p.addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(8192), new StringDecoder(CharsetUtil.UTF_8), new ChunkedWriteHandler(), new FileServerHandler());
            }
        });
        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();
        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 2 with LineBasedFrameDecoder

use of io.netty.handler.codec.LineBasedFrameDecoder in project netty by netty.

the class HttpProxyServer method authenticate.

private boolean authenticate(ChannelHandlerContext ctx, FullHttpRequest req) {
    assertThat(req.method(), is(HttpMethod.CONNECT));
    if (testMode != TestMode.INTERMEDIARY) {
        ctx.pipeline().addBefore(ctx.name(), "lineDecoder", new LineBasedFrameDecoder(64, false, true));
    }
    ctx.pipeline().remove(HttpObjectAggregator.class);
    ctx.pipeline().get(HttpServerCodec.class).removeInboundHandler();
    boolean authzSuccess = false;
    if (username != null) {
        CharSequence authz = req.headers().get(HttpHeaderNames.PROXY_AUTHORIZATION);
        if (authz != null) {
            String[] authzParts = authz.toString().split(" ", 2);
            ByteBuf authzBuf64 = Unpooled.copiedBuffer(authzParts[1], CharsetUtil.US_ASCII);
            ByteBuf authzBuf = Base64.decode(authzBuf64);
            String expectedAuthz = username + ':' + password;
            authzSuccess = "Basic".equals(authzParts[0]) && expectedAuthz.equals(authzBuf.toString(CharsetUtil.US_ASCII));
            authzBuf64.release();
            authzBuf.release();
        }
    } else {
        authzSuccess = true;
    }
    return authzSuccess;
}
Also used : LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with LineBasedFrameDecoder

use of io.netty.handler.codec.LineBasedFrameDecoder in project netty by netty.

the class Socks4ProxyServer method authenticate.

private boolean authenticate(ChannelHandlerContext ctx, Socks4CommandRequest req) {
    assertThat(req.type(), is(Socks4CommandType.CONNECT));
    if (testMode != TestMode.INTERMEDIARY) {
        ctx.pipeline().addBefore(ctx.name(), "lineDecoder", new LineBasedFrameDecoder(64, false, true));
    }
    boolean authzSuccess;
    if (username != null) {
        authzSuccess = username.equals(req.userId());
    } else {
        authzSuccess = true;
    }
    return authzSuccess;
}
Also used : LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder)

Example 4 with LineBasedFrameDecoder

use of io.netty.handler.codec.LineBasedFrameDecoder in project netty by netty.

the class RxtxClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(RxtxChannel.class).handler(new ChannelInitializer<RxtxChannel>() {

            @Override
            public void initChannel(RxtxChannel ch) throws Exception {
                ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(), new StringDecoder(), new RxtxClientHandler());
            }
        });
        ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) RxtxDeviceAddress(io.netty.channel.rxtx.RxtxDeviceAddress) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) RxtxChannel(io.netty.channel.rxtx.RxtxChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 5 with LineBasedFrameDecoder

use of io.netty.handler.codec.LineBasedFrameDecoder in project netty by netty.

the class SocketStartTlsTest method testStartTls.

private void testStartTls(ServerBootstrap sb, Bootstrap cb, boolean autoRead) throws Throwable {
    sb.childOption(ChannelOption.AUTO_READ, autoRead);
    cb.option(ChannelOption.AUTO_READ, autoRead);
    final EventExecutorGroup executor = SocketStartTlsTest.executor;
    SSLEngine sse = serverCtx.newEngine(PooledByteBufAllocator.DEFAULT);
    SSLEngine cse = clientCtx.newEngine(PooledByteBufAllocator.DEFAULT);
    final StartTlsServerHandler sh = new StartTlsServerHandler(sse, autoRead);
    final StartTlsClientHandler ch = new StartTlsClientHandler(cse, autoRead);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            ChannelPipeline p = sch.pipeline();
            p.addLast("logger", new LoggingHandler(LOG_LEVEL));
            p.addLast(new LineBasedFrameDecoder(64), new StringDecoder(), new StringEncoder());
            p.addLast(executor, sh);
        }
    });
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            ChannelPipeline p = sch.pipeline();
            p.addLast("logger", new LoggingHandler(LOG_LEVEL));
            p.addLast(new LineBasedFrameDecoder(64), new StringDecoder(), new StringEncoder());
            p.addLast(executor, ch);
        }
    });
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect().sync().channel();
    while (cc.isActive()) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    while (sh.channel.isActive()) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    sh.channel.close().awaitUninterruptibly();
    cc.close().awaitUninterruptibly();
    sc.close().awaitUninterruptibly();
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
        throw ch.exception.get();
    }
    if (sh.exception.get() != null) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null) {
        throw ch.exception.get();
    }
}
Also used : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) LoggingHandler(io.netty.handler.logging.LoggingHandler) SSLEngine(javax.net.ssl.SSLEngine) Channel(io.netty.channel.Channel) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) IOException(java.io.IOException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ChannelPipeline(io.netty.channel.ChannelPipeline) StringEncoder(io.netty.handler.codec.string.StringEncoder)

Aggregations

LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)5 StringDecoder (io.netty.handler.codec.string.StringDecoder)3 StringEncoder (io.netty.handler.codec.string.StringEncoder)3 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 OioEventLoopGroup (io.netty.channel.oio.OioEventLoopGroup)1 RxtxChannel (io.netty.channel.rxtx.RxtxChannel)1 RxtxDeviceAddress (io.netty.channel.rxtx.RxtxDeviceAddress)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)1 SslContext (io.netty.handler.ssl.SslContext)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1