diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 76191e64d..21166d227 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -2096,7 +2096,11 @@ class Server{ if($this->getProperty("network.upnp-forwarding", false)){ $this->logger->info("[UPnP] Trying to port forward..."); - UPnP::PortForward($this->getPort()); + try{ + UPnP::PortForward($this->getPort()); + }catch(\Throwable $e){ + $this->logger->alert("UPnP portforward failed: " . $e->getMessage()); + } } $this->tickCounter = 0; diff --git a/src/pocketmine/network/upnp/UPnP.php b/src/pocketmine/network/upnp/UPnP.php index 1a832eec5..780a2d5cf 100644 --- a/src/pocketmine/network/upnp/UPnP.php +++ b/src/pocketmine/network/upnp/UPnP.php @@ -30,29 +30,28 @@ use pocketmine\utils\Utils; abstract class UPnP{ - public static function PortForward(int $port) : bool{ + public static function PortForward(int $port) : void{ if(!Utils::$online){ - return false; + throw new \RuntimeException("Server is offline"); } - if(Utils::getOS() != "win" or !class_exists("COM")){ - return false; + if(Utils::getOS() !== "win"){ + throw new \RuntimeException("UPnP is only supported on Windows"); + } + if(!class_exists("COM")){ + throw new \RuntimeException("UPnP requires the com_dotnet extension"); } $myLocalIP = gethostbyname(trim(`hostname`)); - try{ - /** @noinspection PhpUndefinedClassInspection */ - $com = new \COM("HNetCfg.NATUPnP"); - /** @noinspection PhpUndefinedFieldInspection */ - if($com === false or !is_object($com->StaticPortMappingCollection)){ - return false; - } - /** @noinspection PhpUndefinedFieldInspection */ - $com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP"); - }catch(\Throwable $e){ - return false; + + /** @noinspection PhpUndefinedClassInspection */ + $com = new \COM("HNetCfg.NATUPnP"); + /** @noinspection PhpUndefinedFieldInspection */ + if($com === false or !is_object($com->StaticPortMappingCollection)){ + throw new \RuntimeException("Failed to portforward (unsupported?)"); } - return true; + /** @noinspection PhpUndefinedFieldInspection */ + $com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP"); } public static function RemovePortForward(int $port) : bool{