Package one.pkg.tinyutils.network.nat
Class NATProxy
java.lang.Object
one.pkg.tinyutils.network.nat.NATProxy
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the STUN mapping result for the default port.getSTUNMapping(int localPort) Establishes a STUN (Session Traversal Utilities for NAT) mapping by communicatingbooleanIndicates whether the NATProxy is currently running.voidstartTCPForward(int internalPort) Starts TCP forwarding by automatically detecting STUN mapping and forwarding to the specified internal port.voidstartTCPForward(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.voidstartUDPForward(int internalPort) Starts UDP forwarding by automatically detecting STUN mapping and forwarding to the specified internal port.voidstartUDPForward(NATProxy.STUNMappingResult mappingResult, String internalHost, int internalPort) Starts forwarding UDP packets between an external STUN-mapped socket and an internal host/port.voidstop()Stops the NATProxy service and performs cleanup operations.
-
Constructor Details
-
NATProxy
public NATProxy() -
NATProxy
public NATProxy(int bufferSize)
-
-
Method Details
-
getSTUNMapping
Retrieves the STUN mapping result for the default port. The method determines the public IP address and public port- Throws:
Exception
-
getSTUNMapping
Establishes a STUN (Session Traversal Utilities for NAT) mapping by communicating- Throws:
Exception
-
startUDPForward
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
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
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.
-