Class SNIHostnameCapture

java.lang.Object
org.newsclub.net.unix.ssl.SNIHostnameCapture

public final class SNIHostnameCapture extends Object
Helper class to simplify retrieving the requested SNI hostname sent from an SSL client to an SSL server.
Author:
Christian Kohlschütter
  • Field Details

    • ACCEPT_ANY_HOSTNAME

      public static final SNIMatcher ACCEPT_ANY_HOSTNAME
      SNIMatcher that accepts and matches "any hostname".
  • Method Details

    • configure

      public static SNIHostnameCapture configure(SSLSocket sslSocket, SNIMatcher hostnameMatcher)
      Configures the given server SSLSocket to match the given hostname pattern.

      The matched hostname is then accessible after the handshake is completed, via getHostname(). If no hostname was retrieved, null is assumed.

      Parameters:
      sslSocket - The socket to configure.
      hostnameMatcher - The matcher to use.
      Returns:
      The SNIHostnameCapture instance.
      See Also:
    • configure

      public static SNIHostnameCapture configure(SSLSocket sslSocket, SNIMatcher hostnameMatcher, Supplier<String> defaultHostnameSupplier)
      Configures the given server SSLSocket to match the given hostname pattern.

      The matched hostname is then accessible after the handshake is completed, via getHostname(). If no hostname was retrieved, a fallback is retrieved via the given supplier.

      Parameters:
      sslSocket - The socket to configure.
      hostnameMatcher - The matcher to use.
      defaultHostnameSupplier - The supplier for a default hostname, or null.
      Returns:
      The SNIHostnameCapture instance.
      See Also:
    • isComplete

      public boolean isComplete(long timeout, TimeUnit unit) throws InterruptedException
      Checks if a hostname can be returned by calling getHostname() (which most likely means the handshake is complete), optionally waiting up to the given time interval if not yet complete.
      Parameters:
      timeout - The maximum timeout value to wait for.
      unit - Timeout unit.
      Returns:
      true if getHostname() will not throw an IllegalStateException.
      Throws:
      InterruptedException - if the current thread was interrupted while waiting.
    • isComplete

      public boolean isComplete()
      Checks if a hostname can be returned by calling getHostname() (which most likely means the handshake is complete).
      Returns:
      true if getHostname() will not throw an IllegalStateException.
    • getHostname

      public String getHostname()
      Returns the hostname (either the retrieved one, or a fallback), which could also be null.

      If the method is called before the handshake is complete (check with isComplete() or simply call after SSLSocket.startHandshake()), an IllegalStateException is thrown.

      Returns:
      The hostname, or null.
      Throws:
      IllegalStateException - if the method was called before a hostname could be retrieved.