View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.net;
15  
16  import java.security.NoSuchAlgorithmException;
17  
18  import javax.net.ServerSocketFactory;
19  import javax.net.ssl.SSLContext;
20  
21  import ch.qos.logback.classic.LoggerContext;
22  import ch.qos.logback.classic.joran.JoranConfigurator;
23  import ch.qos.logback.core.net.ssl.ConfigurableSSLServerSocketFactory;
24  import ch.qos.logback.core.net.ssl.SSLParametersConfiguration;
25  
26  /**
27   * A {@link SimpleSocketServer} that supports SSL.
28   * 
29   * <pre>
30   *      &lt;b&gt;Usage:&lt;/b&gt; java ch.qos.logback.classic.net.SimpleSSLSocketServer port configFile
31   *                     allowedAddress [allowedAddress ...]
32   * </pre>
33   * 
34   * where <em>port</em> is a port number where the server listens,
35   * <em>configFile</em> is an XML configuration file fed to
36   * {@link JoranConfigurator}, and each <em>allowedAddress</em> is a client IP
37   * or CIDR range that is permitted to connect (e.g. {@code 192.168.1.10} or
38   * {@code 192.168.1.0/24}). At least one allowed address must be specified on
39   * the command line.
40   * 
41   * <p>
42   * Client IP whitelisting configured via
43   * {@link SimpleSocketServer#addAllowedClientAddress(String)} is mandatory and
44   * applies here as well when the server is embedded programmatically. An empty
45   * whitelist means no clients are allowed.
46   * </p>
47   * 
48   * When running the SimpleSSLServerFactory as shown above, it is necessary to
49   * configure JSSE system properties using {@code -Dname=value} on the
50   * command-line when starting the server. In particular, you will probably
51   * want/need to configure the following system properties:
52   * <ul>
53   * <li>javax.net.ssl.keyStore</li>
54   * <li>javax.net.ssl.keyStorePassword</li>
55   * <li>javax.net.ssl.keyStoreType</li>
56   * <li>javax.net.ssl.trustStore</li>
57   * <li>javax.net.ssl.trustStorePassword</li>
58   * <li>javax.net.ssl.trustStoreType</li>
59   * </ul>
60   * <p>
61   * See the <a href=
62   * "http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/JSSERefGuide.html#InstallationAndCustomization">
63   * Customizing the JSSE</a> in the JSSE Reference Guide for details on how to
64   * set these system properties.
65   * 
66   * @author Carl Harris
67   */
68  public class SimpleSSLSocketServer extends SimpleSocketServer {
69  
70      private final ServerSocketFactory socketFactory;
71  
72      public static void main(String argv[]) throws Exception {
73          doMain(SimpleSSLSocketServer.class, argv);
74      }
75  
76      /**
77       * Creates a new server using the default SSL context.
78       * 
79       * @param lc   logger context for received events
80       * @param port port on which the server is to listen
81       * @throws NoSuchAlgorithmException if the default SSL context cannot be created
82       */
83      public SimpleSSLSocketServer(LoggerContext lc, int port) throws NoSuchAlgorithmException {
84          this(lc, port, SSLContext.getDefault());
85      }
86  
87      /**
88       * Creates a new server using a custom SSL context.
89       * 
90       * @param lc         logger context for received events
91       * @param port       port on which the server is to listen
92       * @param sslContext custom SSL context
93       */
94      public SimpleSSLSocketServer(LoggerContext lc, int port, SSLContext sslContext) {
95          super(lc, port);
96          if (sslContext == null) {
97              throw new NullPointerException("SSL context required");
98          }
99          SSLParametersConfiguration parameters = new SSLParametersConfiguration();
100 
101         parameters.setContext(lc);
102         this.socketFactory = new ConfigurableSSLServerSocketFactory(parameters, sslContext.getServerSocketFactory());
103     }
104 
105     @Override
106     protected ServerSocketFactory getServerSocketFactory() {
107         return socketFactory;
108     }
109 
110 }