Example usage for io.netty.handler.ssl.util SelfSignedCertificate privateKey

List of usage examples for io.netty.handler.ssl.util SelfSignedCertificate privateKey

Introduction

In this page you can find the example usage for io.netty.handler.ssl.util SelfSignedCertificate privateKey.

Prototype

File privateKey

To view the source code for io.netty.handler.ssl.util SelfSignedCertificate privateKey.

Click Source Link

Usage

From source file:org.springframework.cloud.stream.app.websocket.sink.WebsocketSinkServerInitializer.java

License:Apache License

private SslContext configureSslContext() throws CertificateException, SSLException {
    if (properties.isSsl()) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        return SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {//  ww  w . j av a 2s.c  o m
        return null;
    }
}

From source file:org.springframework.http.server.reactive.bootstrap.ReactorHttpsServer.java

License:Apache License

@Override
protected void initServer() throws Exception {

    SelfSignedCertificate cert = new SelfSignedCertificate();
    SslContextBuilder builder = SslContextBuilder.forServer(cert.certificate(), cert.privateKey());

    this.reactorHandler = createHttpHandlerAdapter();
    this.reactorServer = reactor.netty.http.server.HttpServer.create().host(getHost()).port(getPort())
            .secure(spec -> spec.sslContext(builder).defaultConfiguration(DefaultConfigurationType.TCP));
}

From source file:org.wso2.esb.integration.common.utils.servers.Http2Server.java

License:Open Source License

public void startServer() throws Exception {
    final SslContext sslCtx;
    if (SSL) {//  ww  w . java2 s. com
        SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK;
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(provider)
                .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
                .applicationProtocolConfig(new ApplicationProtocolConfig(Protocol.ALPN,
                        SelectorFailureBehavior.NO_ADVERTISE, SelectedListenerFailureBehavior.ACCEPT,
                        ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1))
                .build();
    } else {
        sslCtx = null;
    }
    group = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
            .childHandler(new Http2ServerInitializer(sslCtx));

    b.bind("127.0.0.5", PORT).sync().channel();

}

From source file:org.wyb.smtp.mosmtp.SmtpServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//from  w  ww.ja v a 2 s  .com
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new SmtpServerInitializer(sslCtx, new DummyMessageHandler()));

        b.bind(PORT).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:org.wyb.sows.server.WebSocketServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    PropertyConfigurator.configure("./config/serverlog.config");
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//  w ww  . ja va 2s.c  om
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
                .option(ChannelOption.SO_KEEPALIVE, true)
                .childHandler(new WebSocketServerInitializer(sslCtx, new SimpleAuthHandler()));

        Channel ch = b.bind(PORT).sync().channel();
        logger.info("WebSocketServer is started.");
        ch.closeFuture().sync();
        logger.info("WebSocketServer is closed.");
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        logger.info("EventLoopGroups are shutdown.");
    }
}

From source file:org.wyb.trade.TickCollectServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    String driver = System.getProperty("driver");
    String url = System.getProperty("url");
    String username = System.getProperty("username");
    String password = System.getProperty("password");

    TickDao dao = new DBTickDao(driver, url, username, password);
    try {/*from   ww  w.ja v a 2  s.c  om*/
        dao.connect();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                //.handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new SmtpServerInitializer(sslCtx, new TickMessageHandler(dao)));

        b.bind(PORT).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        dao.disconnect();
    }
}

From source file:org.xwiki.contrib.websocket.internal.NettyWebSocketService.java

License:Open Source License

private void initialize0() throws Exception {
    final SslContext sslCtx;
    if (this.conf.sslEnabled()) {
        if (this.conf.getCertChainFilename() != null) {
            // They provided a cert chain filename (and ostensibly a private key)
            // ssl w/ CA signed certificate.
            final File certChain = new File(this.conf.getCertChainFilename());
            final File privKey = new File(this.conf.getPrivateKeyFilename());
            checkCertChainAndPrivKey(certChain, privKey);
            sslCtx = SslContext.newServerContext(certChain, privKey);
        } else {/*from  w w w .  j  a va 2  s .  co  m*/
            // SSL enabled but no certificate specified, lets use a selfie
            this.logger.warn("websocket.ssl.enable = true but websocket.ssl.certChainFile "
                    + "is unspecified, generating a Self Signed Certificate.");
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
        }
    } else {
        sslCtx = null;
    }

    final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    final EventLoopGroup workerGroup = new NioEventLoopGroup();

    ServerBootstrap b = new ServerBootstrap();

    // get rid of silly lag
    b.childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE);

    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .handler(new LoggingHandler(LogLevel.INFO))
            .childHandler(new WebSocketServerInitializer(sslCtx, this));

    Channel ch = b.bind(this.conf.getBindTo(), this.conf.getPort()).sync().channel();

    ch.closeFuture().addListener(new GenericFutureListener<ChannelFuture>() {
        public void operationComplete(ChannelFuture f) {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    });
}

From source file:p2p_server.P2p_server.java

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    List<ChannelFuture> futures = new ArrayList<>();
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();

    try {/*from  w w  w.  jav a2s.  c o m*/
        ServerBootstrap appboot = new ServerBootstrap();
        appboot.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 8192).childHandler(new AppChildChannelHandler(sslCtx));

        appboot.option(ChannelOption.SO_REUSEADDR, true);
        appboot.option(ChannelOption.TCP_NODELAY, true);
        appboot.childOption(ChannelOption.SO_KEEPALIVE, true);
        appboot.childOption(ChannelOption.SO_RCVBUF, 512);
        appboot.childOption(ChannelOption.SO_SNDBUF, 512);

        ServerBootstrap devboot = new ServerBootstrap();
        devboot.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 8192).childHandler(new DevChildChannelHandler(sslCtx));

        devboot.option(ChannelOption.SO_REUSEADDR, true);
        devboot.option(ChannelOption.TCP_NODELAY, true);
        devboot.childOption(ChannelOption.SO_KEEPALIVE, true);
        devboot.childOption(ChannelOption.SO_RCVBUF, 512);
        devboot.childOption(ChannelOption.SO_SNDBUF, 512);

        //ChannelFuture f = boostrap.bind(port).sync();
        futures.add(devboot.bind(5560));
        futures.add(appboot.bind(5561));
        for (ChannelFuture f : futures) {
            f.sync();
        }

        for (ChannelFuture f : futures) {
            f.channel().closeFuture().sync();
        }
        // ???
        //   f.channel().closeFuture().sync();

    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();

    }

}

From source file:reactor.ipc.netty.http.client.HttpClientTest.java

License:Open Source License

@Test
public void sshExchangeRelativeGet() throws CertificateException, SSLException {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    SslContext sslClient = SslContextBuilder.forClient()
            //make the client to trust the self signed certificate
            .trustManager(ssc.cert()).build();

    NettyContext context = HttpServer.create(opt -> opt.sslContext(sslServer))
            .newHandler((req, resp) -> resp.sendString(Flux.just("hello ", req.uri()))).block();

    HttpClientResponse response = HttpClient
            .create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).get("/foo")
            .block(Duration.ofMillis(200));
    context.dispose();//ww w .  j  ava 2 s .c  o m
    context.onClose().block();

    String responseString = response.receive().aggregate().asString(CharsetUtil.UTF_8).block();
    assertThat(responseString).isEqualTo("hello /foo");
}

From source file:reactor.ipc.netty.http.client.HttpClientTest.java

License:Open Source License

@Test
public void sshExchangeAbsoluteGet() throws CertificateException, SSLException {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    SslContext sslClient = SslContextBuilder.forClient().trustManager(ssc.cert()).build();

    NettyContext context = HttpServer.create(opt -> opt.sslContext(sslServer))
            .newHandler((req, resp) -> resp.sendString(Flux.just("hello ", req.uri()))).block();

    HttpClientResponse response = HttpClient
            .create(opt -> opt.port(context.address().getPort()).sslContext(sslClient))
            .get("https://localhost:" + context.address().getPort() + "/foo").block(Duration.ofMillis(200));
    context.dispose();//www  . j av a 2 s. c om
    context.onClose().block();

    String responseString = response.receive().aggregate().asString(CharsetUtil.UTF_8).block();
    assertThat(responseString).isEqualTo("hello /foo");
}