Skip to content

Latest commit

 

History

History
187 lines (139 loc) · 5.26 KB

doc.md

File metadata and controls

187 lines (139 loc) · 5.26 KB

Documentation



Methods

openPortTCP

Opens a TCP port on the gateway. Return true, if operation was successful

Parameters

  • port - TCP port (0-65535)
  • name - UPnP rule description (if value is not set, will default name)
  • duration - duration rule in seconds (0 if is not set)(Read this!)

openPortUDP

Opens a UDP port on the gateway. Return true, if operation was successful

Parameters

  • port - UDP port (0-65535)
  • name - UPnP rule description (if value is not set, will default name)
  • duration - duration rule in seconds (0 if is not set)(Read this!)

closePortTCP

Closes a TCP port on the gateway. Return true, if operation was successful. Most gateways seem to refuse to do this

Parameters

  • port - TCP port (0-65535)

closePortUDP

Closes a UDP port on the gateway. Return true, if operation was successful. Most gateways seem to refuse to do this

Parameters

  • port - UDP port (0-65535)

isMappedTCP

Checks if a TCP port is mapped. Return true if the port is mapped

Parameters

  • port - TCP port (0-65535)

isMappedUDP

Checks if a UDP port is mapped. Return true if the port is mapped

Parameters

  • port - UDP port (0-65535)

getExternalIP

Gets the external IP address of the default gateway. Return external IP address as string, or null if not available

getLocalIP

Gets the internal IP address of this machine. Return internal IP address as string, or null if not available

getDefaultGatewayIP

Gets the IP address of the router. Return internal IP address as string, or null if not available

getPortMappings

Gets the port mappings of the router. Recommended to use this method with caution due to its labor-intensive nature. Return Set port mappings as PortMappingEntity, or empty if not available

customCommand

Can run custom upnp command WANIPConnection:1. Return map of strings response, or null, if error

Parameters

  • params - request parameters, Map<String, String>

Examples

Open 23456 TCP port

    import com.dosse.upnp.UPnP;

    public class Main {
        public static void main(String[] args) {
            UPnP.openPortTCP(23456);
        }
    }

Open 23456 UDP port

    import com.dosse.upnp.UPnP;

    public class Main {
        public static void main(String[] args) {
            UPnP.openPortUDP(23456);
        }
    }

Open 23456 TCP port with name 'Ultimate port (TCP 23456)' for 3 minute

    import com.dosse.upnp.UPnP;

    public class Main {
        public static void main(String[] args) {
            UPnP.openPortTCP(23456, "Ultimate port (TCP 23456)", 3 * 60);
        }
    }

Open 23456 TCP port for 3 minute with repeat timer (Recommend)

    import com.dosse.upnp.UPnP;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class Main {
        public static void main(String[] args) {
          int duration = 180;
          int taskPeriod = duration - 10; // -10 - time margin to keep the port open
          TimerTask task = new TimerTask() {
            public void run() {
              UPnP.closePortTCP(23456);
              UPnP.openPortTCP(23456, duration);
            }
          };
          new Timer().scheduleAtFixedRate(task, 0, taskPeriod * 1_000L);
        }
    }

Check if port TCP 23333 is open

    import com.dosse.upnp.UPnP;
    
    public class Main {
        public static void main(String[] args) {
            int port = 23333;
            if (UPnP.isMappedTCP(port)) {
                System.out.printf("Port %s is opened%n", port);
            } else {
                System.out.printf("Port %s is closed or unavailable%n", port);
            }
        }
    }

Get a description of all port mappings

    import com.dosse.upnp.UPnP;
    
    public class Main {
        public static void main(String[] args) {
            UPnP.getPortMappings().forEach(portMappingEntity -> System.out.println(portMappingEntity.getDescription()));
        }
    }

Attention

1. Duration '0'

Be careful, if you open a port indefinitely and do not close it during the program (or when the program crashes), the port will remain open until you reboot the router/UPnP or close the port from the program