Class SuperSocket

java.lang.Object
net.targetr.wtm3.net.SuperSocket
All Implemented Interfaces:
Closeable, AutoCloseable

public class SuperSocket extends Object implements Closeable
Custom implementation of a socket supporting both plain and secure connections on same port.
Author:
Dr Michael Gardiner
  • Constructor Details

    • SuperSocket

      public SuperSocket(Socket socket) throws IOException
      Creates a new SuperSocket from a regular socket.

      For plain text only.

      Parameters:
      socket - The Socket to upgrade to a SuperSocket.
      Throws:
      IOException - if an I/O error occurs
    • SuperSocket

      public SuperSocket(Socket socket, boolean secure, String alias, InputStream rawIn, byte[] overread, int overreadLen) throws IOException
      Creates a new SuperSocket and sets up TLS according to paramters.

      For TLS or plain text.

      Parameters:
      socket - The Socket to upgrade to a SuperSocket.
      secure - The connection will use TLS.
      alias - The alias to read from a keystore to find a certificate.
      rawIn - The raw, unwrapped InputStream that will have been sniffed previously.
      overread - A buffer containing initial bytes of the InputStream read during sniffing.
      overreadLen - The number of bytes in the buffer read during sniffing.
      Throws:
      IOException - if an I/O error occurs
  • Method Details

    • startTls

      public void startTls(String alias, ByteArrayInputStream consumed) throws IOException
      Starts TLS on a socket that has already had data consumed.
      Parameters:
      alias - The alias to a certificate in the keystore.
      consumed - An input stream providing the already consumed data or null.
      Throws:
      IOException - if an I/O error occurs
    • getInputStream

      public PushbackInputStream getInputStream() throws IOException
      Get the InputStream of data received from the client.
      Returns:
      stream of data from the client.
      Throws:
      IOException - if an I/O error occurs
    • getOutputStream

      public MonitoredOutputStream getOutputStream() throws IOException
      Get the OutputStream for sending data to the client.
      Returns:
      stream of data to send to client.
      Throws:
      IOException - if an I/O error occurs
    • close

      public void close() throws IOException
      Closes the socket.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getSocket

      public Socket getSocket()
      Returns the raw socket connected to the remote client.
      Returns:
      Socket or SSLSocket connected to remote client.
    • isSecure

      public boolean isSecure()
      Query if this socket is secure.
      Returns:
      true if socket is secure.
    • isLocal

      public boolean isLocal()
      Returns true if the socket is connected to a local address.
      Returns:
      true if the socket is connected to a local address.
    • getInetAddress

      public InetAddress getInetAddress()
      Returns the address of the remote client.
      Returns:
      Address of the remote client.
    • setSoTimeout

      public void setSoTimeout(int timeout) throws SocketException
      Set the socket timeout.

      This is the number of miliseconds a read will block before a timeout exception.

      Parameters:
      timeout - Milliseconds to block on an InputStream read operation.
      Throws:
      SocketException - if an error occurs in the underlying protocol
    • setInputInterception

      public void setInputInterception(OutputStream interception) throws IOException
      Directs all input communications to an output stream.

      Used for debugging and storing wire communications.

      Parameters:
      interception - Output stream to send bytes from request.
      Throws:
      IOException - if an I/O error occurs
    • setOutputInterception

      public void setOutputInterception(OutputStream interception) throws IOException
      Directs all output communications to an output stream.

      Used for debugging and storing wire communications.

      Parameters:
      interception - Output stream to send bytes from response.
      Throws:
      IOException - if an I/O error occurs