port_forwarder 1.0.0
port_forwarder: ^1.0.0 copied to clipboard
Manage router ports using UPnP(Universal Plug and Play), NAT-PCP(Port Control Protocol) or NAT-PMP(Port Mapping Protocol)
example/port_forwarder_example.dart
import 'package:port_forwarder/port_forwarder.dart';
const PortType _portType = PortType.tcp;
const int _portNumber = 8080;
void main() async {
final gateway = await Gateway.discover(protocols: {GatewayType.upnp});
if(gateway == null) {
print("No gateway found!");
return;
}
print("Internal gateway address: ${gateway.internalAddress}");
print("External gateway address: ${await gateway.externalAddress}");
try {
print("Opening port $_portNumber($_portType)...");
await gateway.openPort(protocol: _portType, externalPort: _portNumber);
print("Opened port!");
} on GatewayError catch(error) {
print("Cannot open port: $error");
return;
}
try {
final mapped = await gateway.isMapped(protocol: _portType, externalPort: _portNumber);
if(!mapped) {
print("isMapped = false when port is supposed to be open");
}
} on GatewayError catch(error) {
print("Cannot check if port is mapped: $error");
return;
}
try {
print("Closing port $_portNumber($_portType)...");
await gateway.closePort(protocol: _portType, externalPort: _portNumber);
print("Closed port!");
} on GatewayError catch(error) {
print("Cannot close port: $error");
return;
}
try {
final mapped = await gateway.isMapped(protocol: _portType, externalPort: _portNumber);
if(mapped) {
print("isMapped = true when port is not supposed to be open");
}
} on GatewayError catch(error) {
print("Cannot check if port is mapped: $error");
return;
}
}