List of usage examples for io.netty.handler.ssl.util SelfSignedCertificate SelfSignedCertificate
public SelfSignedCertificate() throws CertificateException
From source file:com.vmware.xenon.common.test.VerificationHost.java
License:Open Source License
public static void createAndAttachSSLClient(ServiceHost h) throws Throwable { // we create a random userAgent string to validate host to host communication when // the client appears to be from an external, non-Xenon source. ServiceClient client = NettyHttpServiceClient.create(UUID.randomUUID().toString(), null, h.getScheduledExecutor(), h); SSLContext clientContext = SSLContext.getInstance(ServiceClient.TLS_PROTOCOL_NAME); clientContext.init(null, InsecureTrustManagerFactory.INSTANCE.getTrustManagers(), null); client.setSSLContext(clientContext); h.setClient(client);//from ww w.ja v a2 s. c o m SelfSignedCertificate ssc = new SelfSignedCertificate(); h.setCertificateFileReference(ssc.certificate().toURI()); h.setPrivateKeyFileReference(ssc.privateKey().toURI()); }
From source file:com.vmware.xenon.common.TestServiceHost.java
License:Open Source License
@Test public void httpScheme() throws Throwable { setUp(true);//from w w w . j a v a 2 s .com // SSL config for https SelfSignedCertificate ssc = new SelfSignedCertificate(); this.host.setCertificateFileReference(ssc.certificate().toURI()); this.host.setPrivateKeyFileReference(ssc.privateKey().toURI()); assertEquals("before starting, scheme is NONE", ServiceHost.HttpScheme.NONE, this.host.getCurrentHttpScheme()); this.host.setPort(0); this.host.setSecurePort(0); this.host.start(); ServiceRequestListener httpListener = this.host.getListener(); ServiceRequestListener httpsListener = this.host.getSecureListener(); assertTrue("http listener should be on", httpListener.isListening()); assertTrue("https listener should be on", httpsListener.isListening()); assertEquals(ServiceHost.HttpScheme.HTTP_AND_HTTPS, this.host.getCurrentHttpScheme()); assertTrue("public uri scheme should be HTTP", this.host.getPublicUri().getScheme().equals("http")); httpsListener.stop(); assertTrue("http listener should be on ", httpListener.isListening()); assertFalse("https listener should be off", httpsListener.isListening()); assertEquals(ServiceHost.HttpScheme.HTTP_ONLY, this.host.getCurrentHttpScheme()); assertTrue("public uri scheme should be HTTP", this.host.getPublicUri().getScheme().equals("http")); httpListener.stop(); assertFalse("http listener should be off", httpListener.isListening()); assertFalse("https listener should be off", httpsListener.isListening()); assertEquals(ServiceHost.HttpScheme.NONE, this.host.getCurrentHttpScheme()); // re-start listener even host is stopped, verify getCurrentHttpScheme only httpsListener.start(0, ServiceHost.LOOPBACK_ADDRESS); assertFalse("http listener should be off", httpListener.isListening()); assertTrue("https listener should be on", httpsListener.isListening()); assertEquals(ServiceHost.HttpScheme.HTTPS_ONLY, this.host.getCurrentHttpScheme()); httpsListener.stop(); this.host.stop(); // set HTTP port to disabled, restart host. Verify scheme is HTTPS only. We must // set both HTTP and secure port, to null out the listeners from the host instance. this.host.setPort(ServiceHost.PORT_VALUE_LISTENER_DISABLED); this.host.setSecurePort(0); VerificationHost.createAndAttachSSLClient(this.host); this.host.start(); httpListener = this.host.getListener(); httpsListener = this.host.getSecureListener(); assertTrue("http listener should be null, default port value set to disabled", httpListener == null); assertTrue("https listener should be on", httpsListener.isListening()); assertEquals(ServiceHost.HttpScheme.HTTPS_ONLY, this.host.getCurrentHttpScheme()); assertTrue("public uri scheme should be HTTPS", this.host.getPublicUri().getScheme().equals("https")); }
From source file:com.xxx.netty.run.SecureChatServer.java
License:Apache License
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:root-context.xml");// loading //jedis = context.getBean(RedisInitBean.class).getSingletonInstance(); SecureChatServer chatServer = context.getBean(SecureChatServer.class); // SelfSignedCertificate???? SelfSignedCertificate ssc = new SelfSignedCertificate(); // ???//from w ww . ja v a 2 s . c om SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap serverBootstrap = new ServerBootstrap();// ????? serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new SecureChatServerInitializer(sslCtx)); if (null != args && args.length > 1 && args[0].matches("\\d")) { chatServer.PORT = Integer.parseInt(args[0]); } LOGGER.debug("SSL TCP server started on port:{}", chatServer.PORT); serverBootstrap.bind(chatServer.PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); context = null; } }
From source file:com.yahoo.ads.pb.network.netty.NettyPistachioServer.java
License:Open Source License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from w ww .ja v a2s .c o m*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } 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 NettyPistachioServerInitializer(sslCtx)); b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.zy.learning.netty.websocket.WebSocketServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from w w w . j a 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)).childHandler(new ProtocolDetectorInitializer()); //.childHandler(new WebSocketServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); System.out.println("Open your web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:" + PORT + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:connexion.ServerSocket.java
public static void bind(int port) throws InterruptedException, SSLException, CertificateException { // Configure SSL. SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); // Configure Group bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ServerInitializer(sslCtx)); b.bind(port).sync().channel().closeFuture().sync(); }
From source file:de.dfki.kiara.netty.AbstractTransport.java
License:Open Source License
protected SslContext createServerSslContext() throws CertificateException, SSLException { if (SSL) {//from w w w . ja v a2s . c o m SelfSignedCertificate ssc = new SelfSignedCertificate(); return SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } else { return null; } }
From source file:de.ocarthon.core.network.tcp.TCPServer.java
License:Apache License
public void initBootstrap() throws CertificateException, SSLException { SelfSignedCertificate cert = new SelfSignedCertificate(); this.serverSslContext = SslContext.newServerContext(cert.certificate(), cert.privateKey()); this.bootstrap = new ServerBootstrap(); this.bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<Channel>() { @Override//from w ww . j a va2 s. co m protected void initChannel(Channel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (useTls) { p.addLast(serverSslContext.newHandler(ch.alloc())); } p.addLast(lengthPrepender); p.addLast(new LengthFieldBasedFrameDecoder(65535, 0, lengthBytes, 0, lengthBytes)); if (pipelineCodec != null) { pipelineCodec.accept(p); } p.addLast(handler); } }); }
From source file:demo.netty.discard.DiscardServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from ww w. j a v a 2 s . 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)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast(new DiscardServerHandler()); } }); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(PORT).sync(); // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to gracefully // shut down your server. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:dpfmanager.shell.modules.server.core.HttpServer.java
License:Open Source License
public void start() throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from w w w .j a v a 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); b.channel(NioServerSocketChannel.class); b.handler(new LoggingHandler(LogLevel.INFO)); b.childHandler(new HttpServerInitializer(sslCtx, context)); Channel ch = b.bind(PORT).sync().channel(); context.send(BasicConfig.MODULE_MESSAGE, new LogMessage(getClass(), Level.DEBUG, DPFManagerProperties.getBundle().getString("startedServer").replace("%1", getServerUri()), true)); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }