Search in sources :

Example 81 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project netty by netty.

the class HAProxyIntegrationTest method testBasicCase.

@Test
public void testBasicCase() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<HAProxyMessage> msgHolder = new AtomicReference<HAProxyMessage>();
    LocalAddress localAddress = new LocalAddress("HAProxyIntegrationTest");
    EventLoopGroup group = new DefaultEventLoopGroup();
    ServerBootstrap sb = new ServerBootstrap();
    sb.channel(LocalServerChannel.class).group(group).childHandler(new ChannelInitializer() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(new HAProxyMessageDecoder());
            ch.pipeline().addLast(new SimpleChannelInboundHandler<HAProxyMessage>() {

                @Override
                protected void channelRead0(ChannelHandlerContext ctx, HAProxyMessage msg) throws Exception {
                    msgHolder.set(msg.retain());
                    latch.countDown();
                }
            });
        }
    });
    Channel serverChannel = sb.bind(localAddress).sync().channel();
    Bootstrap b = new Bootstrap();
    Channel clientChannel = b.channel(LocalChannel.class).handler(HAProxyMessageEncoder.INSTANCE).group(group).connect(localAddress).sync().channel();
    try {
        HAProxyMessage message = new HAProxyMessage(HAProxyProtocolVersion.V1, HAProxyCommand.PROXY, HAProxyProxiedProtocol.TCP4, "192.168.0.1", "192.168.0.11", 56324, 443);
        clientChannel.writeAndFlush(message).sync();
        assertTrue(latch.await(5, TimeUnit.SECONDS));
        HAProxyMessage readMessage = msgHolder.get();
        assertEquals(message.protocolVersion(), readMessage.protocolVersion());
        assertEquals(message.command(), readMessage.command());
        assertEquals(message.proxiedProtocol(), readMessage.proxiedProtocol());
        assertEquals(message.sourceAddress(), readMessage.sourceAddress());
        assertEquals(message.destinationAddress(), readMessage.destinationAddress());
        assertEquals(message.sourcePort(), readMessage.sourcePort());
        assertEquals(message.destinationPort(), readMessage.destinationPort());
        readMessage.release();
    } finally {
        clientChannel.close().sync();
        serverChannel.close().sync();
        group.shutdownGracefully().sync();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) Test(org.junit.jupiter.api.Test)

Example 82 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project LogHub by fbacchella.

the class ServerFactory method addChildhandlers.

@Override
public void addChildhandlers(ChannelConsumer<ServerBootstrap, ServerChannel, SA> source) {
    ChannelHandler handler = new ChannelInitializer<CC>() {

        @Override
        public void initChannel(CC ch) throws Exception {
            try {
                source.addHandlers(ch.pipeline());
            } catch (Exception e) {
                logger.error("Netty handler failed: {}", e.getMessage());
                logger.throwing(Level.DEBUG, e);
            }
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            source.exception(ctx, cause);
        }
    };
    bootstrap.childHandler(handler);
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ChannelInitializer(io.netty.channel.ChannelInitializer)

Example 83 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project flink by apache.

the class RestServerEndpoint method start.

/**
 * Starts this REST server endpoint.
 *
 * @throws Exception if we cannot start the RestServerEndpoint
 */
public final void start() throws Exception {
    synchronized (lock) {
        Preconditions.checkState(state == State.CREATED, "The RestServerEndpoint cannot be restarted.");
        log.info("Starting rest endpoint.");
        final Router router = new Router();
        final CompletableFuture<String> restAddressFuture = new CompletableFuture<>();
        handlers = initializeHandlers(restAddressFuture);
        /* sort the handlers such that they are ordered the following:
             * /jobs
             * /jobs/overview
             * /jobs/:jobid
             * /jobs/:jobid/config
             * /:*
             */
        Collections.sort(handlers, RestHandlerUrlComparator.INSTANCE);
        checkAllEndpointsAndHandlersAreUnique(handlers);
        handlers.forEach(handler -> registerHandler(router, handler, log));
        ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws ConfigurationException {
                RouterHandler handler = new RouterHandler(router, responseHeaders);
                // SSL should be the first handler in the pipeline
                if (isHttpsEnabled()) {
                    ch.pipeline().addLast("ssl", new RedirectingSslHandler(restAddress, restAddressFuture, sslHandlerFactory));
                }
                ch.pipeline().addLast(new HttpServerCodec()).addLast(new FileUploadHandler(uploadDir)).addLast(new FlinkHttpObjectAggregator(maxContentLength, responseHeaders));
                for (InboundChannelHandlerFactory factory : inboundChannelHandlerFactories) {
                    Optional<ChannelHandler> channelHandler = factory.createHandler(configuration, responseHeaders);
                    if (channelHandler.isPresent()) {
                        ch.pipeline().addLast(channelHandler.get());
                    }
                }
                ch.pipeline().addLast(new ChunkedWriteHandler()).addLast(handler.getName(), handler).addLast(new PipelineErrorHandler(log, responseHeaders));
            }
        };
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1, new ExecutorThreadFactory("flink-rest-server-netty-boss"));
        NioEventLoopGroup workerGroup = new NioEventLoopGroup(0, new ExecutorThreadFactory("flink-rest-server-netty-worker"));
        bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(initializer);
        Iterator<Integer> portsIterator;
        try {
            portsIterator = NetUtils.getPortRangeFromString(restBindPortRange);
        } catch (IllegalConfigurationException e) {
            throw e;
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid port range definition: " + restBindPortRange);
        }
        int chosenPort = 0;
        while (portsIterator.hasNext()) {
            try {
                chosenPort = portsIterator.next();
                final ChannelFuture channel;
                if (restBindAddress == null) {
                    channel = bootstrap.bind(chosenPort);
                } else {
                    channel = bootstrap.bind(restBindAddress, chosenPort);
                }
                serverChannel = channel.syncUninterruptibly().channel();
                break;
            } catch (final Exception e) {
                // otherwise
                if (!(e instanceof java.net.BindException)) {
                    throw e;
                }
            }
        }
        if (serverChannel == null) {
            throw new BindException("Could not start rest endpoint on any port in port range " + restBindPortRange);
        }
        log.debug("Binding rest endpoint to {}:{}.", restBindAddress, chosenPort);
        final InetSocketAddress bindAddress = (InetSocketAddress) serverChannel.localAddress();
        final String advertisedAddress;
        if (bindAddress.getAddress().isAnyLocalAddress()) {
            advertisedAddress = this.restAddress;
        } else {
            advertisedAddress = bindAddress.getAddress().getHostAddress();
        }
        port = bindAddress.getPort();
        log.info("Rest endpoint listening at {}:{}", advertisedAddress, port);
        restBaseUrl = new URL(determineProtocol(), advertisedAddress, port, "").toString();
        restAddressFuture.complete(restBaseUrl);
        state = State.RUNNING;
        startInternal();
    }
}
Also used : SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) NioServerSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) RedirectingSslHandler(org.apache.flink.runtime.net.RedirectingSslHandler) ChannelHandler(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler) BindException(java.net.BindException) URL(java.net.URL) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelInitializer(org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer) NioEventLoopGroup(org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) NioServerSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocketChannel) InboundChannelHandlerFactory(org.apache.flink.runtime.io.network.netty.InboundChannelHandlerFactory) Router(org.apache.flink.runtime.rest.handler.router.Router) BindException(java.net.BindException) ServerBootstrap(org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) ConfigurationException(org.apache.flink.util.ConfigurationException) BindException(java.net.BindException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException) ExecutorThreadFactory(org.apache.flink.util.concurrent.ExecutorThreadFactory) PipelineErrorHandler(org.apache.flink.runtime.rest.handler.PipelineErrorHandler) RouterHandler(org.apache.flink.runtime.rest.handler.router.RouterHandler) ChunkedWriteHandler(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpServerCodec)

Example 84 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project zuul by Netflix.

the class ServerTest method getListeningSockets.

@Test
public void getListeningSockets() throws Exception {
    ServerStatusManager ssm = mock(ServerStatusManager.class);
    Map<NamedSocketAddress, ChannelInitializer<?>> initializers = new HashMap<>();
    final List<NioSocketChannel> nioChannels = Collections.synchronizedList(new ArrayList<NioSocketChannel>());
    ChannelInitializer<Channel> init = new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) {
            LOGGER.info("Channel: " + ch.getClass().getName() + ", isActive=" + ch.isActive() + ", isOpen=" + ch.isOpen());
            if (ch instanceof NioSocketChannel) {
                nioChannels.add((NioSocketChannel) ch);
            }
        }
    };
    initializers.put(new NamedSocketAddress("test", new InetSocketAddress(0)), init);
    // The port to channel map keys on the port, post bind. This should be unique even if InetAddress is same
    initializers.put(new NamedSocketAddress("test2", new InetSocketAddress(0)), init);
    ClientConnectionsShutdown ccs = new ClientConnectionsShutdown(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), GlobalEventExecutor.INSTANCE, /* discoveryClient= */
    null);
    EventLoopGroupMetrics elgm = new EventLoopGroupMetrics(Spectator.globalRegistry());
    EventLoopConfig elc = new EventLoopConfig() {

        @Override
        public int eventLoopCount() {
            return 1;
        }

        @Override
        public int acceptorCount() {
            return 1;
        }
    };
    Server s = new Server(new NoopRegistry(), ssm, initializers, ccs, elgm, elc);
    s.start();
    List<NamedSocketAddress> addrs = s.getListeningAddresses();
    assertEquals(2, addrs.size());
    for (NamedSocketAddress address : addrs) {
        assertTrue(address.unwrap() instanceof InetSocketAddress);
        final int port = ((InetSocketAddress) address.unwrap()).getPort();
        assertNotEquals(port, 0);
        checkConnection(port);
    }
    await().atMost(1, SECONDS).until(() -> nioChannels.size() == 2);
    s.stop();
    assertEquals(2, nioChannels.size());
    for (NioSocketChannel ch : nioChannels) {
        assertTrue("isShutdown", ch.isShutdown());
    }
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ServerStatusManager(com.netflix.netty.common.status.ServerStatusManager) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroupMetrics(com.netflix.netty.common.metrics.EventLoopGroupMetrics) NoopRegistry(com.netflix.spectator.api.NoopRegistry) ChannelInitializer(io.netty.channel.ChannelInitializer) Test(org.junit.Test)

Example 85 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project zuul by Netflix.

the class SampleServerStartup method chooseAddrsAndChannels.

@Override
protected Map<NamedSocketAddress, ChannelInitializer<?>> chooseAddrsAndChannels(ChannelGroup clientChannels) {
    Map<NamedSocketAddress, ChannelInitializer<?>> addrsToChannels = new HashMap<>();
    SocketAddress sockAddr;
    String metricId;
    {
        @Deprecated int port = new DynamicIntProperty("zuul.server.port.main", 7001).get();
        sockAddr = new SocketAddressProperty("zuul.server.addr.main", "=" + port).getValue();
        if (sockAddr instanceof InetSocketAddress) {
            metricId = String.valueOf(((InetSocketAddress) sockAddr).getPort());
        } else {
            // Just pick something.   This would likely be a UDS addr or a LocalChannel addr.
            metricId = sockAddr.toString();
        }
    }
    SocketAddress pushSockAddr;
    {
        int pushPort = new DynamicIntProperty("zuul.server.port.http.push", 7008).get();
        pushSockAddr = new SocketAddressProperty("zuul.server.addr.http.push", "=" + pushPort).getValue();
    }
    String mainListenAddressName = "main";
    ServerSslConfig sslConfig;
    ChannelConfig channelConfig = defaultChannelConfig(mainListenAddressName);
    ChannelConfig channelDependencies = defaultChannelDependencies(mainListenAddressName);
    /* These settings may need to be tweaked depending if you're running behind an ELB HTTP listener, TCP listener,
         * or directly on the internet.
         */
    switch(SERVER_TYPE) {
        /* The below settings can be used when running behind an ELB HTTP listener that terminates SSL for you
             * and passes XFF headers.
             */
        case HTTP:
            channelConfig.set(CommonChannelConfigKeys.allowProxyHeadersWhen, StripUntrustedProxyHeadersHandler.AllowWhen.ALWAYS);
            channelConfig.set(CommonChannelConfigKeys.preferProxyProtocolForClientIp, false);
            channelConfig.set(CommonChannelConfigKeys.isSSlFromIntermediary, false);
            channelConfig.set(CommonChannelConfigKeys.withProxyProtocol, false);
            addrsToChannels.put(new NamedSocketAddress("http", sockAddr), new ZuulServerChannelInitializer(metricId, channelConfig, channelDependencies, clientChannels));
            logAddrConfigured(sockAddr);
            break;
        /* The below settings can be used when running behind an ELB TCP listener with proxy protocol, terminating
             * SSL in Zuul.
             */
        case HTTP2:
            sslConfig = ServerSslConfig.withDefaultCiphers(loadFromResources("server.cert"), loadFromResources("server.key"), WWW_PROTOCOLS);
            channelConfig.set(CommonChannelConfigKeys.allowProxyHeadersWhen, StripUntrustedProxyHeadersHandler.AllowWhen.NEVER);
            channelConfig.set(CommonChannelConfigKeys.preferProxyProtocolForClientIp, true);
            channelConfig.set(CommonChannelConfigKeys.isSSlFromIntermediary, false);
            channelConfig.set(CommonChannelConfigKeys.serverSslConfig, sslConfig);
            channelConfig.set(CommonChannelConfigKeys.sslContextFactory, new BaseSslContextFactory(registry, sslConfig));
            addHttp2DefaultConfig(channelConfig, mainListenAddressName);
            addrsToChannels.put(new NamedSocketAddress("http2", sockAddr), new Http2SslChannelInitializer(metricId, channelConfig, channelDependencies, clientChannels));
            logAddrConfigured(sockAddr, sslConfig);
            break;
        /* The below settings can be used when running behind an ELB TCP listener with proxy protocol, terminating
             * SSL in Zuul.
             *
             * Can be tested using certs in resources directory:
             *  curl https://localhost:7001/test -vk --cert src/main/resources/ssl/client.cert:zuul123 --key src/main/resources/ssl/client.key
             */
        case HTTP_MUTUAL_TLS:
            sslConfig = new ServerSslConfig(WWW_PROTOCOLS, ServerSslConfig.getDefaultCiphers(), loadFromResources("server.cert"), loadFromResources("server.key"), ClientAuth.REQUIRE, loadFromResources("truststore.jks"), loadFromResources("truststore.key"), false);
            channelConfig.set(CommonChannelConfigKeys.allowProxyHeadersWhen, StripUntrustedProxyHeadersHandler.AllowWhen.NEVER);
            channelConfig.set(CommonChannelConfigKeys.preferProxyProtocolForClientIp, true);
            channelConfig.set(CommonChannelConfigKeys.isSSlFromIntermediary, false);
            channelConfig.set(CommonChannelConfigKeys.withProxyProtocol, true);
            channelConfig.set(CommonChannelConfigKeys.serverSslConfig, sslConfig);
            channelConfig.set(CommonChannelConfigKeys.sslContextFactory, new BaseSslContextFactory(registry, sslConfig));
            addrsToChannels.put(new NamedSocketAddress("http_mtls", sockAddr), new Http1MutualSslChannelInitializer(metricId, channelConfig, channelDependencies, clientChannels));
            logAddrConfigured(sockAddr, sslConfig);
            break;
        /* Settings to be used when running behind an ELB TCP listener with proxy protocol as a Push notification
             * server using WebSockets */
        case WEBSOCKET:
            channelConfig.set(CommonChannelConfigKeys.allowProxyHeadersWhen, StripUntrustedProxyHeadersHandler.AllowWhen.NEVER);
            channelConfig.set(CommonChannelConfigKeys.preferProxyProtocolForClientIp, true);
            channelConfig.set(CommonChannelConfigKeys.isSSlFromIntermediary, false);
            channelConfig.set(CommonChannelConfigKeys.withProxyProtocol, true);
            channelDependencies.set(ZuulDependencyKeys.pushConnectionRegistry, pushConnectionRegistry);
            addrsToChannels.put(new NamedSocketAddress("websocket", sockAddr), new SampleWebSocketPushChannelInitializer(metricId, channelConfig, channelDependencies, clientChannels));
            logAddrConfigured(sockAddr);
            // port to accept push message from the backend, should be accessible on internal network only.
            addrsToChannels.put(new NamedSocketAddress("http.push", pushSockAddr), pushSenderInitializer);
            logAddrConfigured(pushSockAddr);
            break;
        /* Settings to be used when running behind an ELB TCP listener with proxy protocol as a Push notification
             * server using Server Sent Events (SSE) */
        case SSE:
            channelConfig.set(CommonChannelConfigKeys.allowProxyHeadersWhen, StripUntrustedProxyHeadersHandler.AllowWhen.NEVER);
            channelConfig.set(CommonChannelConfigKeys.preferProxyProtocolForClientIp, true);
            channelConfig.set(CommonChannelConfigKeys.isSSlFromIntermediary, false);
            channelConfig.set(CommonChannelConfigKeys.withProxyProtocol, true);
            channelDependencies.set(ZuulDependencyKeys.pushConnectionRegistry, pushConnectionRegistry);
            addrsToChannels.put(new NamedSocketAddress("sse", sockAddr), new SampleSSEPushChannelInitializer(metricId, channelConfig, channelDependencies, clientChannels));
            logAddrConfigured(sockAddr);
            // port to accept push message from the backend, should be accessible on internal network only.
            addrsToChannels.put(new NamedSocketAddress("http.push", pushSockAddr), pushSenderInitializer);
            logAddrConfigured(pushSockAddr);
            break;
    }
    return Collections.unmodifiableMap(addrsToChannels);
}
Also used : ServerSslConfig(com.netflix.netty.common.ssl.ServerSslConfig) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) SampleSSEPushChannelInitializer(com.netflix.zuul.sample.push.SampleSSEPushChannelInitializer) DynamicIntProperty(com.netflix.config.DynamicIntProperty) ChannelConfig(com.netflix.netty.common.channel.config.ChannelConfig) Http2SslChannelInitializer(com.netflix.zuul.netty.server.http2.Http2SslChannelInitializer) BaseSslContextFactory(com.netflix.zuul.netty.ssl.BaseSslContextFactory) SampleWebSocketPushChannelInitializer(com.netflix.zuul.sample.push.SampleWebSocketPushChannelInitializer) ChannelInitializer(io.netty.channel.ChannelInitializer) Http2SslChannelInitializer(com.netflix.zuul.netty.server.http2.Http2SslChannelInitializer) SampleSSEPushChannelInitializer(com.netflix.zuul.sample.push.SampleSSEPushChannelInitializer) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SampleWebSocketPushChannelInitializer(com.netflix.zuul.sample.push.SampleWebSocketPushChannelInitializer)

Aggregations

ChannelInitializer (io.netty.channel.ChannelInitializer)86 Channel (io.netty.channel.Channel)59 Bootstrap (io.netty.bootstrap.Bootstrap)42 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)39 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)36 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)32 InetSocketAddress (java.net.InetSocketAddress)32 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)31 ChannelFuture (io.netty.channel.ChannelFuture)30 ChannelPipeline (io.netty.channel.ChannelPipeline)26 EventLoopGroup (io.netty.channel.EventLoopGroup)26 LocalServerChannel (io.netty.channel.local.LocalServerChannel)21 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)21 LocalChannel (io.netty.channel.local.LocalChannel)20 SocketChannel (io.netty.channel.socket.SocketChannel)18 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)17 SslHandler (io.netty.handler.ssl.SslHandler)17 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)14 Map (java.util.Map)12 CountDownLatch (java.util.concurrent.CountDownLatch)12