mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 16:51:42 +00:00
UPnP: error messages on failure to portforward instead of silently failing
This commit is contained in:
parent
69c54de460
commit
37d085f793
@ -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;
|
||||
|
@ -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{
|
||||
|
Loading…
x
Reference in New Issue
Block a user