View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
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   * LocalExec server Main method.
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     * Takes 3 to 8 arguments (last 5 are optional arguments):<br>
53     * - mandatory arguments: filename keystorepaswwd keypassword<br>
54     * - if no more arguments are provided, it implies 127.0.0.1 + 9999 as port
55     * and no certificates<br>
56     * - optional arguments:<br>
57     * "port"<br>
58     * "port" "trustfilename" "trustpassword"<br>
59     * "port" "trustfilename" "trustpassword" "addresse"<br>
60     * "port" "trustfilename" "trustpassword" "addresse" "default delay"<br>
61     *
62     * @param args
63     *
64     * @throws Exception
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     // Configure the server.
102     try {
103       final ServerBootstrap bootstrap = new ServerBootstrap();
104       WaarpNettyUtil.setServerBootstrap(bootstrap, workerGroup, workerGroup,
105                                         30000);
106 
107       // Load the KeyStore (No certificates)
108       final WaarpSecureKeyStore WaarpSecureKeyStoreNew =
109           new WaarpSecureKeyStore(keyStoreFilename, keyStorePasswd,
110                                   keyPassword);
111       if (trustStoreFilename != null) {
112         // Include certificates
113         WaarpSecureKeyStoreNew.initTrustStore(trustStoreFilename,
114                                               trustStorePasswd, true);
115       } else {
116         WaarpSecureKeyStoreNew.initEmptyTrustStore();
117       }
118       final WaarpSslContextFactory waarpSslContextFactory =
119           new WaarpSslContextFactory(WaarpSecureKeyStoreNew, true);
120       // Configure the pipeline factory.
121       bootstrap.childHandler(
122           new LocalExecSslServerInitializer(waarpSslContextFactory, delay,
123                                             executor));
124 
125       // Bind and start to accept incoming connections only on local address.
126       final ChannelFuture future =
127           bootstrap.bind(new InetSocketAddress(addr, port));
128 
129       // Wait until the server socket is closed.
130       future.channel().closeFuture().sync();
131     } finally {
132       // Shut down all event loops to terminate all threads.
133       workerGroup.shutdownGracefully();
134 
135       // Wait until all threads are terminated.
136       workerGroup.terminationFuture().sync();
137     }
138   }
139 }