List of usage examples for io.netty.handler.ssl.util SelfSignedCertificate certificate
File certificate
To view the source code for io.netty.handler.ssl.util SelfSignedCertificate certificate.
Click Source Link
From source file:lee.study.server.BoomOnlineServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from w ww. j av 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(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new WebSocketServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:main.java.ch.epfl.lpd.ServerThread.java
License:Apache License
public void run() { // Configure SSL. final SslContext sslCtx; if (SSL) {/* w w w .ja v a 2 s .co m*/ SelfSignedCertificate ssc; try { ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } catch (Exception e) { // TODO Auto-generated catch block logger.error("Got exception", e); } } 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("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); p.addLast("decoder", new StringDecoder()); p.addLast("encoder", new StringEncoder()); //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new ServerHandler()); } }); // Start the server. ChannelFuture f; try { f = b.bind(PORT).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } catch (Exception e) { // TODO Auto-generated catch block logger.error("Got exception", e); } } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:majordodo.network.netty.NettyChannelAcceptor.java
License:Apache License
public void start() throws Exception { boolean useOpenSSL = NetworkUtils.isOpenSslAvailable(); if (ssl) {/* w ww .j av a2 s . c om*/ if (sslCertFile == null) { LOGGER.log(Level.SEVERE, "start SSL with self-signed auto-generated certificate, useOpenSSL:" + useOpenSSL); if (sslCiphers != null) { LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers); } SelfSignedCertificate ssc = new SelfSignedCertificate(); try { sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()) .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK).ciphers(sslCiphers) .build(); } finally { ssc.delete(); } } else { LOGGER.log(Level.SEVERE, "start SSL with certificate " + sslCertFile.getAbsolutePath() + " chain file " + sslCertChainFile.getAbsolutePath() + " , useOpenSSL:" + useOpenSSL); if (sslCiphers != null) { LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers); } sslCtx = SslContextBuilder.forServer(sslCertChainFile, sslCertFile, sslCertPassword) .sslProvider(useOpenSSL ? SslProvider.OPENSSL : SslProvider.JDK).ciphers(sslCiphers) .build(); } } if (NetworkUtils.isEnableEpollNative()) { bossGroup = new EpollEventLoopGroup(workerThreads); workerGroup = new EpollEventLoopGroup(workerThreads); LOGGER.log(Level.INFO, "Using netty-native-epoll network type"); } else { bossGroup = new NioEventLoopGroup(workerThreads); workerGroup = new NioEventLoopGroup(workerThreads); } ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel( NetworkUtils.isEnableEpollNative() ? EpollServerSocketChannel.class : NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { NettyChannel session = new NettyChannel("client", ch, callbackExecutor, null); if (acceptor != null) { acceptor.createConnection(session); } // ch.pipeline().addLast(new LoggingHandler()); // Add SSL handler first to encrypt and decrypt everything. if (ssl) { ch.pipeline().addLast(sslCtx.newHandler(ch.alloc())); } ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4)); ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); // ch.pipeline().addLast("messageencoder", new DodoMessageEncoder()); ch.pipeline().addLast("messagedecoder", new DodoMessageDecoder()); ch.pipeline().addLast(new InboundMessageHandler(session)); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(host, port).sync(); // (7) this.channel = f.channel(); }
From source file:me.hrps.rp.preview.chat.service.WebSocketServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {//from ww w.ja va 2s.co 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 WebSocketServerInitializer()); 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:me.jesonlee.jjfsserver.httpserver.HttpStaticFileServer.java
License:Apache License
public static void main(String[] args) throws Exception { port = Integer.parseInt(System.getProperty("httpPort")); if (port < 1000 || port > 65535) { logger.error("port can not less than 1000 or beyond 65535"); System.exit(1);/* ww w .ja v a 2 s .co m*/ } // Configure SSL. final SslContext sslCtx; if (SSL) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(SslProvider.JDK, 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 HttpStaticFileServerInitializer(sslCtx)); Channel ch = b.bind(port).sync().channel(); System.out.println("http??" + port); logger.info("http??" + port); 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:me.netty.http.HttpServer.java
License:Apache License
public void start() throws Exception { //?/*from ww w. j a va 2s. com*/ serverContext.initContext(); // Configure SSL. final SslContext sslCtx; if (SSL) { SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK; SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(provider) /* NOTE: the cipher filter may not include all ciphers required by the HTTP/2 specification. * Please refer to the HTTP/2 specification for cipher requirements. */ .ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE) .applicationProtocolConfig(new ApplicationProtocolConfig(Protocol.ALPN, // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers. SelectorFailureBehavior.NO_ADVERTISE, // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers. SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)) .build(); } else { sslCtx = null; } // Configure the http2Orhttp. //CPU??workwork? // ????? int threads = Runtime.getRuntime().availableProcessors() * 2; EventLoopGroup bossGroup = new NioEventLoopGroup(threads); EventLoopGroup workerGroup = new NioEventLoopGroup(threads); try { ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new Http2ServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); logger.info("Open your HTTP/2-enabled web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:" + PORT + '/'); ch.closeFuture().sync().addListener(new GenericFutureListener<Future<? super Void>>() { public void operationComplete(Future<? super Void> future) throws Exception { logger.info("service has shutdown"); } }); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:net.kuujo.copycat.netty.NettyTcpProtocolServer.java
License:Apache License
@Override public synchronized CompletableFuture<Void> listen() { final CompletableFuture<Void> future = new CompletableFuture<>(); final SslContext sslContext; if (protocol.isSsl()) { try {// w w w . j a v a 2s .c om SelfSignedCertificate ssc = new SelfSignedCertificate(); sslContext = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } catch (SSLException | CertificateException e) { future.completeExceptionally(e); return future; } } else { sslContext = null; } serverGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(protocol.getThreads()); final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(serverGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (sslContext != null) { pipeline.addLast(sslContext.newHandler(channel.alloc())); } pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4)); pipeline.addLast("bytesDecoder", new ByteArrayDecoder()); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("bytesEncoder", new ByteArrayEncoder()); pipeline.addLast("handler", new ServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128); if (protocol.getSendBufferSize() > -1) { bootstrap.option(ChannelOption.SO_SNDBUF, protocol.getSendBufferSize()); } if (protocol.getReceiveBufferSize() > -1) { bootstrap.option(ChannelOption.SO_RCVBUF, protocol.getReceiveBufferSize()); } bootstrap.option(ChannelOption.TCP_NODELAY, true); bootstrap.option(ChannelOption.SO_REUSEADDR, true); bootstrap.option(ChannelOption.SO_KEEPALIVE, true); bootstrap.option(ChannelOption.SO_BACKLOG, protocol.getAcceptBacklog()); if (protocol.getTrafficClass() > -1) { bootstrap.option(ChannelOption.IP_TOS, protocol.getTrafficClass()); } bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); // Bind and start to accept incoming connections. bootstrap.bind(host, port).addListener((ChannelFutureListener) channelFuture -> { channelFuture.channel().closeFuture().addListener(closeFuture -> { workerGroup.shutdownGracefully(); }); if (channelFuture.isSuccess()) { channel = channelFuture.channel(); future.complete(null); } else { future.completeExceptionally(channelFuture.cause()); } }); return future; }
From source file:net.kuujo.copycat.netty.protocol.impl.TcpProtocolServer.java
License:Apache License
@Override public CompletableFuture<Void> start() { final CompletableFuture<Void> future = new CompletableFuture<>(); // TODO: Configure proper SSL trust store. final SslContext sslContext; if (protocol.isSsl()) { try {//from ww w . j av a2 s. c o m SelfSignedCertificate ssc = new SelfSignedCertificate(); sslContext = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } catch (SSLException | CertificateException e) { future.completeExceptionally(e); return future; } } else { sslContext = null; } final EventLoopGroup serverGroup = new NioEventLoopGroup(); final EventLoopGroup workerGroup = new NioEventLoopGroup(protocol.getThreads()); final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(serverGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (sslContext != null) { pipeline.addLast(sslContext.newHandler(channel.alloc())); } pipeline.addLast(new ObjectEncoder(), new ObjectDecoder( ClassResolvers.softCachingConcurrentResolver(getClass().getClassLoader())), new TcpProtocolServerHandler(TcpProtocolServer.this)); } }).option(ChannelOption.SO_BACKLOG, 128); if (protocol.getSendBufferSize() > -1) { bootstrap.option(ChannelOption.SO_SNDBUF, protocol.getSendBufferSize()); } if (protocol.getReceiveBufferSize() > -1) { bootstrap.option(ChannelOption.SO_RCVBUF, protocol.getReceiveBufferSize()); } bootstrap.option(ChannelOption.TCP_NODELAY, protocol.isNoDelay()); bootstrap.option(ChannelOption.SO_REUSEADDR, protocol.isReuseAddress()); bootstrap.option(ChannelOption.SO_KEEPALIVE, protocol.isKeepAlive()); bootstrap.option(ChannelOption.SO_BACKLOG, protocol.getAcceptBacklog()); if (protocol.getTrafficClass() > -1) { bootstrap.option(ChannelOption.IP_TOS, protocol.getTrafficClass()); } bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); // Bind and start to accept incoming connections. bootstrap.bind(protocol.getHost(), protocol.getPort()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { channelFuture.channel().closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { workerGroup.shutdownGracefully(); } }); if (channelFuture.isSuccess()) { channel = channelFuture.channel(); future.complete(null); } else { future.completeExceptionally(channelFuture.cause()); } } }); return future; }
From source file:net.NettyEngine4.file.HttpStaticFileServer.java
License:Apache License
public static void main(String[] args) throws Exception { PropertyConfigurator.configure(Config.DEFAULT_VALUE.FILE_PATH.LOG4J); // Configure SSL. final SslContext sslCtx; if (SSL) {//from w w w. j a va 2 s.com SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(SslProvider.JDK, 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 HttpStaticFileServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); LOGGER.debug("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:net.petercashel.nettyCore.ssl.SSLContextProvider.java
License:Apache License
public static SslContext getSelfServer() { SelfSignedCertificate ssc = null; try {/*from ww w . j a v a 2 s . c o m*/ ssc = new SelfSignedCertificate(); } catch (CertificateException e) { // TODO Auto-generated catch block e.printStackTrace(); } SslContext sslCtx = null; try { sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } catch (SSLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sslCtx; }