List of usage examples for io.netty.handler.ssl.util SelfSignedCertificate SelfSignedCertificate
public SelfSignedCertificate() throws CertificateException
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) {/*ww w . ja v a 2 s.co m*/ 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) {/*from w ww .j a va 2s. c o m*/ 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 {//ww w. ja va2s.com 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 2s . c om // 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 www . j a va2 s . c om*/ 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();//from w w w. j av a 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();/*from ww w .j a v a2 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 secureSendFile() throws CertificateException, SSLException, InterruptedException { Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").getFile()); SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslServer = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); SslContext sslClient = SslContextBuilder.forClient().trustManager(ssc.cert()).build(); AtomicReference<String> uploaded = new AtomicReference<>(); NettyContext context = HttpServer.create(opt -> opt.sslContext(sslServer)) .newRouter(/*from ww w .j a v a 2 s. c om*/ r -> r.post("/upload", (req, resp) -> req.receive().aggregate().asString().doOnNext(uploaded::set) .then(resp.status(201).sendString(Mono.just("Received File")).then()))) .block(); HttpClientResponse response = HttpClient .create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)) .post("/upload", r -> r.sendFile(largeFile)).block(Duration.ofSeconds(120)); context.dispose(); context.onClose().block(); String responseBody = response.receive().aggregate().asString().block(); assertThat(response.status().code()).isEqualTo(201); assertThat(responseBody).isEqualTo("Received File"); assertThat(uploaded.get()) .startsWith( "This is an UTF-8 file that is larger than 1024 bytes.\n" + "It contains accents like .") .contains("1024 mark here -><- 1024 mark here").endsWith("End of File"); }
From source file:reactor.ipc.netty.http.server.HttpServerTests.java
License:Open Source License
@Test public void secureSendFile() throws CertificateException, SSLException, InterruptedException { Path largeFile = Paths.get(getClass().getResource("/largeFile.txt").getFile()); 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.sendFile(largeFile)).block(); HttpClientResponse response = HttpClient .create(opt -> opt.port(context.address().getPort()).sslContext(sslClient)).get("/foo") .block(Duration.ofSeconds(120)); context.dispose();/*from w w w .j a va 2s .c om*/ context.onClose().block(); String body = response.receive().aggregate().asString().block(); assertThat(body) .startsWith( "This is an UTF-8 file that is larger than 1024 bytes.\n" + "It contains accents like .") .contains("1024 mark here -><- 1024 mark here").endsWith("End of File"); }
From source file:reactor.ipc.netty.NettyOutboundTest.java
License:Open Source License
@Test public void sendFileWithTlsUsesChunkedFile() throws URISyntaxException, NoSuchAlgorithmException, SSLException, CertificateException { SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); final SslHandler sslHandler = sslCtx.newHandler(ByteBufAllocator.DEFAULT); List<Class<?>> messageWritten = new ArrayList<>(2); List<Object> clearMessages = new ArrayList<>(2); EmbeddedChannel channel = new EmbeddedChannel( //outbound: pipeline reads inverted //bytes are encrypted sslHandler,//from w w w.ja va2 s . com //capture the chunks unencrypted, transform as Strings: new MessageToMessageEncoder<ByteBuf>() { @Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { clearMessages.add(msg.toString(CharsetUtil.UTF_8)); out.add(msg.retain()); //the encoder will release the buffer, make sure it is retained for SslHandler } }, //transform the ChunkedFile into ByteBuf chunks: new ChunkedWriteHandler(), //helps to ensure a ChunkedFile was written outs new MessageToMessageEncoder<Object>() { @Override protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception { messageWritten.add(msg.getClass()); //passing the ChunkedFile through this method releases it, which is undesired ReferenceCountUtil.retain(msg); out.add(msg); } }); NettyContext mockContext = () -> channel; NettyOutbound outbound = new NettyOutbound() { @Override public NettyContext context() { return mockContext; } @Override public FileChunkedStrategy getFileChunkedStrategy() { return FILE_CHUNKED_STRATEGY_1024_NOPIPELINE; } }; channel.writeOneOutbound(1); try { outbound.sendFile(Paths.get(getClass().getResource("/largeFile.txt").toURI())).then() .block(Duration.ofSeconds(1)); //TODO investigate why this hangs } catch (IllegalStateException e) { if (!"Timeout on blocking read for 1000 MILLISECONDS".equals(e.getMessage())) throw e; System.err.println(e); } assertThat(messageWritten).containsExactly(Integer.class, ChunkedFile.class); assertThat(clearMessages).hasSize(2).element(0).asString().startsWith( "This is an UTF-8 file that is larger than 1024 bytes.\nIt contains accents like .\nGARBAGE") .endsWith("1024 mark here ->"); assertThat(clearMessages).element(1).asString().startsWith("<- 1024 mark here").endsWith("End of File"); }