List of usage examples for io.netty.handler.ssl.util SelfSignedCertificate SelfSignedCertificate
public SelfSignedCertificate() throws CertificateException
From source file:reactor.ipc.netty.options.ServerOptions.java
License:Open Source License
/** * Enable SSL service with a self-signed certificate and allows extra * parameterization of the self signed {@link SslContextBuilder}. The builder is * then used to invoke {@link #sslContext(SslContext)}. * * @param configurator the builder callback to setup the self-signed {@link SslContextBuilder} * * @return {@code this}//w w w . ja va2s . c o m */ public ServerOptions sslSelfSigned(Consumer<? super SslContextBuilder> configurator) { Objects.requireNonNull(configurator, "configurator"); SelfSignedCertificate ssc; try { ssc = new SelfSignedCertificate(); SslContextBuilder builder = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()); configurator.accept(builder); return sslContext(builder.build()); } catch (Exception sslException) { throw Exceptions.bubble(sslException); } }
From source file:server.telnet.TelnetServer.java
License:Apache License
public Channel start() throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/* w ww .j a v a 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 TelnetServerInitializer(sslCtx)); // b.bind(PORT).sync();// .channel().closeFuture().sync(); Channel ch = b.bind(PORT).sync().channel(); return ch; } finally { // bossGroup.shutdownGracefully(); // workerGroup.shutdownGracefully(); } }
From source file:subterranean.crimson.server.network.ClientListener.java
License:Open Source License
public ClientListener(String n, int p, SecretKeySpec k, EncType enc, boolean u, boolean s) { SSL = s;//from w w w . j a v a2s . c o m PORT = p; UPNP = u; key = k; encryption = enc; connections = new ArrayList<Connection>(); if (SSL) { try { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } catch (CertificateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SSLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { sslCtx = null; } this.start(); }
From source file:taichu.kafka.test.netty4.example.EchoServer.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {// w ww .ja va2 s .co m SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } 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) // .option(ChannelOption., 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(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(PORT).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:test.httpupload.HttpUploadClient.java
License:Apache License
public static void main(String[] args) throws Exception { String postSimple, postFile, get; if (BASE_URL.endsWith("/")) { postSimple = BASE_URL + "formpost"; postFile = BASE_URL + "formpostmultipart"; get = BASE_URL + "formget"; } else {// www.j av a 2s .co m postSimple = BASE_URL + "/formpost"; postFile = BASE_URL + "/formpostmultipart"; get = BASE_URL + "/formget"; } URI uriSimple = new URI(postFile); String scheme = uriSimple.getScheme() == null ? "http" : uriSimple.getScheme(); String host = uriSimple.getHost() == null ? "127.0.0.1" : uriSimple.getHost(); int port = uriSimple.getPort(); if (port == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80; } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } } if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) { System.err.println("Only HTTP(S) is supported."); return; } final boolean ssl = "https".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } else { sslCtx = null; } URI uriFile = new URI(postFile); File file = new File(FILE); if (!file.canRead()) { throw new FileNotFoundException(FILE); } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); // setup the factory: here using a mixed memory/disk based on size threshold HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE); // Disk if MINSIZE exceed DiskFileUpload.deleteOnExitTemporaryFile = true; // should delete file on exit (in normal exit) DiskFileUpload.baseDirectory = null; // system temp directory DiskAttribute.deleteOnExitTemporaryFile = true; // should delete file on exit (in normal exit) DiskAttribute.baseDirectory = null; // system temp directory try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new HttpUploadClientIntializer(sslCtx)); // // Simple Get form: no factory used (not usable) List<Entry<String, String>> headers = formget(b, host, port, get, uriSimple); if (headers == null) { factory.cleanAllHttpData(); return; } // Simple Post form: factory used for big attributes List<InterfaceHttpData> bodylist = formpost(b, host, port, uriSimple, file, factory, headers); if (bodylist == null) { factory.cleanAllHttpData(); return; } // Multipart Post form: factory used formpostmultipart(b, host, port, uriFile, factory, headers, bodylist); } finally { // Shut down executor threads to exit. group.shutdownGracefully(); // Really clean all temporary files if they still exist factory.cleanAllHttpData(); } }
From source file:uidserver.UIDServer.java
/** * @param args the command line arguments *//*from w ww . ja va2s .co m*/ public static void main(String[] args) throws Exception { if (args.length == 1) { if (args[0].toLowerCase().contains("help")) { System.out.println("This script can only run in Java 1.8"); System.out.println("java -jar ./UIDServer.jar " + "./config.xml"); System.exit(0); } else { new Config(args[0]); } } else { new Config("D:/projects/UIDTest/server/config.xml"); } new UIDGenerator(); final int PORT = Integer.parseInt(System.getProperty("port", Config.port)); SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); 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 UIDServerInitializer(sslCtx)); b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:websocketx.server.WebSocketServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/* w w 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; } new Thread(new Runnable() { public void run() { while (true) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("channelGroup:" + WebSocketFrameHandler.channelGroup); System.out.println("channelGroup.isEmpty():" + WebSocketFrameHandler.channelGroup.isEmpty()); System.out.println("channelGroupMap:" + WebSocketFrameHandler.channelGroupMap); } } }).start(); 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(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:ws.wamp.jawampa.transport.netty.SimpleWampWebsocketListener.java
License:Apache License
public void start() { if (state != State.Intialized) return;/*from w w w.ja va2 s. co m*/ try { // Initialize SSL when required if (uri.getScheme().equalsIgnoreCase("wss") && sslCtx == null) { // Use a self signed certificate when we got none provided through the constructor SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } // Use well-known ports if not explicitly specified final int port; if (uri.getPort() == -1) { if (sslCtx != null) port = 443; else port = 80; } else port = uri.getPort(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, clientGroup).channel(NioServerSocketChannel.class) .childHandler(new WebSocketServerInitializer(uri, sslCtx)); channel = b.bind(uri.getHost(), port).sync().channel(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:ws.wamp.jawampa.transport.SimpleWampWebsocketListener.java
License:Apache License
public void start() { if (state != State.Intialized) return;//from ww w . ja v a2s . co m try { // Initialize SSL when required if (uri.getScheme().equalsIgnoreCase("wss") && sslCtx == null) { // Use a self signed certificate when we got none provided through the constructor SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } // Use well-known ports if not explicitly specified final int port; if (uri.getPort() == -1) { if (sslCtx != null) port = 443; else port = 80; } else port = uri.getPort(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, clientGroup).channel(NioServerSocketChannel.class) .childHandler(new WebSocketServerInitializer(uri, sslCtx)); channel = b.bind(uri.getHost(), port).sync().channel(); } catch (Exception e) { throw new RuntimeException(e); } finally { } }