UPnP: error messages on failure to portforward instead of silently failing

This commit is contained in:
Dylan K. Taylor
2018-03-31 12:51:17 +01:00
parent 69c54de460
commit 37d085f793
2 changed files with 20 additions and 17 deletions

View File

@ -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{