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.openr66.proxy.configuration;
21  
22  import io.netty.bootstrap.ServerBootstrap;
23  import io.netty.channel.ChannelFuture;
24  import io.netty.channel.group.DefaultChannelGroup;
25  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
26  import org.waarp.common.logging.WaarpLogger;
27  import org.waarp.common.logging.WaarpLoggerFactory;
28  import org.waarp.common.utility.WaarpNettyUtil;
29  import org.waarp.common.utility.WaarpShutdownHook;
30  import org.waarp.common.utility.WaarpSystemUtil;
31  import org.waarp.openr66.protocol.configuration.Configuration;
32  import org.waarp.openr66.protocol.configuration.Messages;
33  import org.waarp.openr66.proxy.network.NetworkServerInitializerProxy;
34  import org.waarp.openr66.proxy.network.ProxyBridge;
35  import org.waarp.openr66.proxy.network.ProxyEntry;
36  import org.waarp.openr66.proxy.network.ssl.NetworkSslServerInitializerProxy;
37  import org.waarp.openr66.proxy.protocol.http.HttpInitializer;
38  import org.waarp.openr66.proxy.protocol.http.adminssl.HttpSslInitializer;
39  
40  import java.net.InetSocketAddress;
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   *
46   */
47  public class ConfigurationProxyR66 extends Configuration {
48    /**
49     * Internal Logger
50     */
51    private static final WaarpLogger logger =
52        WaarpLoggerFactory.getLogger(ConfigurationProxyR66.class);
53  
54    /**
55     *
56     */
57    public ConfigurationProxyR66() {
58      // nothing
59    }
60  
61    @Override
62    public final void computeNbThreads() {
63      final int nb = Runtime.getRuntime().availableProcessors() + 1;
64      setServerThread(nb);
65      setClientThread(getServerThread() + 1);
66      setRunnerThread(10);
67    }
68  
69    @Override
70    public final void serverStartup() {
71      setServer(true);
72      getShutdownConfiguration().timeout = getTimeoutCon();
73      WaarpShutdownHook.addShutdownHook();
74      if (!isUseNOSSL() && !isUseSSL()) {
75        logger.error(
76            "OpenR66 has neither NOSSL nor SSL support included! Stop here!");
77        WaarpSystemUtil.systemExit(-1);
78        return;
79      }
80      pipelineInit();
81      serverPipelineInit();
82      r66Startup();
83      startHttpSupport();
84      try {
85        startMonitoring();
86      } catch (final WaarpDatabaseSqlException ignored) {
87        // nothing
88      }
89      logger.warn(toString());
90    }
91  
92    @Override
93    public final void r66Startup() {
94      logger.debug("Start R66: {}", getHostSslId());
95      // add into configuration
96      getConstraintLimitHandler().setServer(true);
97      // Global Server
98      serverChannelGroup =
99          new DefaultChannelGroup("OpenR66", subTaskGroup.next());
100     if (isUseNOSSL()) {
101       serverBootstrap = new ServerBootstrap();
102       WaarpNettyUtil.setServerBootstrap(serverBootstrap, serverGroup,
103                                         workerGroup, (int) getTimeoutCon(),
104                                         getBlockSize() + 64, false);
105       networkServerInitializer = new NetworkServerInitializerProxy(true);
106       serverBootstrap.childHandler(networkServerInitializer);
107       // FIXME take into account multiple address
108       final List<ChannelFuture> futures = new ArrayList<ChannelFuture>();
109       for (final ProxyEntry entry : ProxyEntry.proxyEntries.values()) {
110         if (!entry.isLocalSsl()) {
111           logger.debug("Future Activation: {}", entry.getLocalSocketAddress());
112           futures.add(serverBootstrap.bind(entry.getLocalSocketAddress()));
113         }
114       }
115       for (final ChannelFuture future : futures) {
116         future.syncUninterruptibly();
117         if (future.isSuccess()) {
118           bindNoSSL = future.channel();
119           serverChannelGroup.add(bindNoSSL);
120           logger.debug("Activation: {}", bindNoSSL.localAddress());
121         } else {
122           logger.warn(
123               Messages.getString("Configuration.NOSSLDeactivated") + " for " +
124               bindNoSSL.localAddress()); //$NON-NLS-2$
125         }
126       }
127     } else {
128       networkServerInitializer = null;
129       logger.warn(
130           Messages.getString("Configuration.NOSSLDeactivated")); //$NON-NLS-1$
131     }
132 
133     if (isUseSSL() && getHostSslId() != null) {
134       serverSslBootstrap = new ServerBootstrap();
135       WaarpNettyUtil.setServerBootstrap(serverSslBootstrap, serverGroup,
136                                         workerGroup, (int) getTimeoutCon(),
137                                         getBlockSize() + 64, false);
138       networkSslServerInitializer = new NetworkSslServerInitializerProxy(false);
139       serverSslBootstrap.childHandler(networkSslServerInitializer);
140       // FIXME take into account multiple address
141       final List<ChannelFuture> futures = new ArrayList<ChannelFuture>();
142       for (final ProxyEntry entry : ProxyEntry.proxyEntries.values()) {
143         if (entry.isLocalSsl()) {
144           logger.debug(
145               "Future SslActivation: " + entry.getLocalSocketAddress());
146           futures.add(serverSslBootstrap.bind(entry.getLocalSocketAddress()));
147         }
148       }
149       for (final ChannelFuture future : futures) {
150         future.syncUninterruptibly();
151         if (future.isSuccess()) {
152           bindSSL = future.channel();
153           serverChannelGroup.add(bindSSL);
154           logger.debug("SslActivation: {}", bindSSL.localAddress());
155         } else {
156           logger.warn(
157               Messages.getString("Configuration.SSLMODEDeactivated") + " for " +
158               bindSSL.localAddress()); //$NON-NLS-2$
159         }
160       }
161     } else {
162       networkSslServerInitializer = null;
163       logger.warn(
164           Messages.getString("Configuration.SSLMODEDeactivated")); //$NON-NLS-1$
165     }
166 
167     // Factory for TrafficShapingHandler
168     setupLimitHandler();
169     ProxyBridge.initialize();
170     setThriftService(null);
171   }
172 
173   @Override
174   public final void startHttpSupport() {
175     // Now start the HTTP support
176     logger.info(
177         Messages.getString("Configuration.HTTPStart") + getServerHttpport() +
178         //$NON-NLS-1$
179         " HTTPS: " + getServerHttpsPort());
180     httpChannelGroup =
181         new DefaultChannelGroup("HttpOpenR66", subTaskGroup.next());
182     if (getServerHttpport() > 0) {
183       // Configure the server.
184       httpBootstrap = new ServerBootstrap();
185       WaarpNettyUtil.setServerBootstrap(httpBootstrap, httpWorkerGroup,
186                                         httpWorkerGroup, (int) getTimeoutCon());
187       // Set up the event pipeline factory.
188       httpBootstrap.childHandler(new HttpInitializer(isUseHttpCompression()));
189       // Bind and start to accept incoming connections.
190       final ChannelFuture future =
191           httpBootstrap.bind(new InetSocketAddress(getServerHttpport()))
192                        .awaitUninterruptibly();
193       if (future.isSuccess()) {
194         httpChannelGroup.add(future.channel());
195       }
196     }
197     // Now start the HTTPS support
198     if (getServerHttpsPort() > 0) {
199       // Configure the server.
200       httpsBootstrap = new ServerBootstrap();
201       // Set up the event pipeline factory.
202       WaarpNettyUtil.setServerBootstrap(httpsBootstrap, httpWorkerGroup,
203                                         httpWorkerGroup, (int) getTimeoutCon());
204       httpsBootstrap.childHandler(
205           new HttpSslInitializer(isUseHttpCompression()));
206       // Bind and start to accept incoming connections.
207       final ChannelFuture future =
208           httpsBootstrap.bind(new InetSocketAddress(getServerHttpsPort()))
209                         .awaitUninterruptibly();
210       if (future.isSuccess()) {
211         httpChannelGroup.add(future.channel());
212       }
213     }
214   }
215 
216   @Override
217   public final void serverStop() {
218     super.serverStop();
219     ProxyBridge.transaction.closeAll();
220   }
221 
222 }