Class NATProxy

java.lang.Object
one.pkg.tinyutils.network.nat.NATProxy

public class NATProxy extends Object
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    NATProxy(int bufferSize)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Retrieves the STUN mapping result for the default port.
    getSTUNMapping(int localPort)
    Establishes a STUN (Session Traversal Utilities for NAT) mapping by communicating
    boolean
    Indicates whether the NATProxy is currently running.
    void
    startTCPForward(int internalPort)
    Starts TCP forwarding by automatically detecting STUN mapping and forwarding to the specified internal port.
    void
    startTCPForward(int listenPort, String internalHost, int internalPort)
    Starts a TCP forwarding proxy that listens for incoming TCP connections on the specified port and forwards them to the internal host/port.
    void
    startUDPForward(int internalPort)
    Starts UDP forwarding by automatically detecting STUN mapping and forwarding to the specified internal port.
    void
    startUDPForward(NATProxy.STUNMappingResult mappingResult, String internalHost, int internalPort)
    Starts forwarding UDP packets between an external STUN-mapped socket and an internal host/port.
    void
    Stops the NATProxy service and performs cleanup operations.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NATProxy

      public NATProxy()
    • NATProxy

      public NATProxy(int bufferSize)
  • Method Details

    • getSTUNMapping

      public NATProxy.STUNMappingResult getSTUNMapping() throws Exception
      Retrieves the STUN mapping result for the default port. The method determines the public IP address and public port
      Throws:
      Exception
    • getSTUNMapping

      public NATProxy.STUNMappingResult getSTUNMapping(int localPort) throws Exception
      Establishes a STUN (Session Traversal Utilities for NAT) mapping by communicating
      Throws:
      Exception
    • startUDPForward

      public void startUDPForward(int internalPort) throws Exception
      Starts UDP forwarding by automatically detecting STUN mapping and forwarding to the specified internal port.
      Parameters:
      internalPort - The port number of the internal destination to which packets are forwarded.
      Throws:
      Exception - If an error occurs during STUN detection or UDP forwarding setup.
    • startUDPForward

      public void startUDPForward(NATProxy.STUNMappingResult mappingResult, String internalHost, int internalPort) throws Exception
      Starts forwarding UDP packets between an external STUN-mapped socket and an internal host/port. This method spawns a new thread for handling the UDP packet forwarding process.
      Parameters:
      mappingResult - The result of the STUN mapping, which contains the external socket to use for forwarding.
      internalHost - The hostname or IP address of the internal destination to which packets are forwarded.
      internalPort - The port number of the internal destination to which packets are forwarded.
      Throws:
      Exception - If an error occurs during the setup or execution of the UDP forwarding process.
      IllegalStateException - If the proxy is already running.
    • startTCPForward

      public void startTCPForward(int internalPort) throws Exception
      Starts TCP forwarding by automatically detecting STUN mapping and forwarding to the specified internal port. The listen port is determined by the STUN mapping's local port.
      Parameters:
      internalPort - The port number of the internal destination to which connections are forwarded.
      Throws:
      Exception - If an error occurs during STUN detection or TCP forwarding setup.
    • startTCPForward

      public void startTCPForward(int listenPort, String internalHost, int internalPort) throws Exception
      Starts a TCP forwarding proxy that listens for incoming TCP connections on the specified port and forwards them to the internal host/port.
      Throws:
      Exception
    • stop

      public void stop()
      Stops the NATProxy service and performs cleanup operations.

      This method halts all ongoing processes of the NATProxy, including shutting down sockets used for UDP and TCP communication

    • isRunning

      public boolean isRunning()
      Indicates whether the NATProxy is currently running.
      Returns:
      true if the NATProxy is running, false otherwise.