1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.commandexec.ssl.server;
21
22 import io.netty.bootstrap.ServerBootstrap;
23 import io.netty.channel.ChannelFuture;
24 import io.netty.channel.EventLoopGroup;
25 import io.netty.channel.nio.NioEventLoopGroup;
26 import io.netty.util.concurrent.DefaultEventExecutorGroup;
27 import io.netty.util.concurrent.EventExecutorGroup;
28 import org.waarp.commandexec.utils.LocalExecDefaultResult;
29 import org.waarp.common.crypto.ssl.WaarpSecureKeyStore;
30 import org.waarp.common.crypto.ssl.WaarpSslContextFactory;
31 import org.waarp.common.logging.SysErrLogger;
32 import org.waarp.common.logging.WaarpLoggerFactory;
33 import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
34 import org.waarp.common.utility.DetectionUtils;
35 import org.waarp.common.utility.WaarpNettyUtil;
36 import org.waarp.common.utility.WaarpThreadFactory;
37
38 import java.net.InetAddress;
39 import java.net.InetSocketAddress;
40
41
42
43
44 public class LocalExecSslServer {
45
46 static final EventLoopGroup workerGroup = new NioEventLoopGroup();
47 static final EventExecutorGroup executor =
48 new DefaultEventExecutorGroup(DetectionUtils.numberThreads(),
49 new WaarpThreadFactory("LocalExecServer"));
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public static void main(final String[] args) throws Exception {
67 WaarpLoggerFactory.setDefaultFactoryIfNotSame(
68 new WaarpSlf4JLoggerFactory(null));
69 int port = 9999;
70 InetAddress addr;
71 long delay = LocalExecDefaultResult.MAXWAITPROCESS;
72 final String keyStoreFilename;
73 final String keyStorePasswd;
74 final String keyPassword;
75 String trustStoreFilename = null;
76 String trustStorePasswd = null;
77 final byte[] loop = { 127, 0, 0, 1 };
78 addr = InetAddress.getByAddress(loop);
79 if (args.length >= 3) {
80 keyStoreFilename = args[0];
81 keyStorePasswd = args[1];
82 keyPassword = args[2];
83 if (args.length >= 4) {
84 port = Integer.parseInt(args[3]);
85 if (args.length >= 6) {
86 trustStoreFilename = args[4];
87 trustStorePasswd = args[5];
88 if (args.length >= 7) {
89 addr = InetAddress.getByName(args[6]);
90 if (args.length > 7) {
91 delay = Long.parseLong(args[7]);
92 }
93 }
94 }
95 }
96 } else {
97 SysErrLogger.FAKE_LOGGER.syserr(
98 "Need at least 3 arguments: Filename " + "KeyStorePswd KeyPswd");
99 return;
100 }
101
102 try {
103 final ServerBootstrap bootstrap = new ServerBootstrap();
104 WaarpNettyUtil.setServerBootstrap(bootstrap, workerGroup, workerGroup,
105 30000);
106
107
108 final WaarpSecureKeyStore WaarpSecureKeyStoreNew =
109 new WaarpSecureKeyStore(keyStoreFilename, keyStorePasswd,
110 keyPassword);
111 if (trustStoreFilename != null) {
112
113 WaarpSecureKeyStoreNew.initTrustStore(trustStoreFilename,
114 trustStorePasswd, true);
115 } else {
116 WaarpSecureKeyStoreNew.initEmptyTrustStore();
117 }
118 final WaarpSslContextFactory waarpSslContextFactory =
119 new WaarpSslContextFactory(WaarpSecureKeyStoreNew, true);
120
121 bootstrap.childHandler(
122 new LocalExecSslServerInitializer(waarpSslContextFactory, delay,
123 executor));
124
125
126 final ChannelFuture future =
127 bootstrap.bind(new InetSocketAddress(addr, port));
128
129
130 future.channel().closeFuture().sync();
131 } finally {
132
133 workerGroup.shutdownGracefully();
134
135
136 workerGroup.terminationFuture().sync();
137 }
138 }
139 }