1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.portunification;
17
18 import io.netty.bootstrap.ServerBootstrap;
19 import io.netty.channel.ChannelInitializer;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.MultiThreadIoEventLoopGroup;
22 import io.netty.channel.nio.NioIoHandler;
23 import io.netty.channel.socket.SocketChannel;
24 import io.netty.channel.socket.nio.NioServerSocketChannel;
25 import io.netty.handler.logging.LogLevel;
26 import io.netty.handler.logging.LoggingHandler;
27 import io.netty.handler.ssl.SslContext;
28 import io.netty.handler.ssl.SslContextBuilder;
29 import io.netty.pkitesting.CertificateBuilder;
30 import io.netty.pkitesting.X509Bundle;
31
32
33
34
35
36
37
38
39 public final class PortUnificationServer {
40
41 static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));
42
43 public static void main(String[] args) throws Exception {
44
45 X509Bundle ssc = new CertificateBuilder()
46 .subject("cn=localhost")
47 .setIsCertificateAuthority(true)
48 .buildSelfSigned();
49 final SslContext sslCtx = SslContextBuilder.forServer(ssc.toKeyManagerFactory())
50 .build();
51
52 EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
53 EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
54 try {
55 ServerBootstrap b = new ServerBootstrap();
56 b.group(bossGroup, workerGroup)
57 .channel(NioServerSocketChannel.class)
58 .handler(new LoggingHandler(LogLevel.INFO))
59 .childHandler(new ChannelInitializer<SocketChannel>() {
60 @Override
61 public void initChannel(SocketChannel ch) throws Exception {
62 ch.pipeline().addLast(new PortUnificationServerHandler(sslCtx));
63 }
64 });
65
66
67 b.bind(PORT).sync().channel().closeFuture().sync();
68 } finally {
69 bossGroup.shutdownGracefully();
70 workerGroup.shutdownGracefully();
71 }
72 }
73 }