mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-08-31 07:21:17 +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)){
|
if($this->getProperty("network.upnp-forwarding", false)){
|
||||||
$this->logger->info("[UPnP] Trying to port forward...");
|
$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;
|
$this->tickCounter = 0;
|
||||||
|
@ -30,29 +30,28 @@ use pocketmine\utils\Utils;
|
|||||||
|
|
||||||
abstract class UPnP{
|
abstract class UPnP{
|
||||||
|
|
||||||
public static function PortForward(int $port) : bool{
|
public static function PortForward(int $port) : void{
|
||||||
if(!Utils::$online){
|
if(!Utils::$online){
|
||||||
return false;
|
throw new \RuntimeException("Server is offline");
|
||||||
}
|
}
|
||||||
if(Utils::getOS() != "win" or !class_exists("COM")){
|
if(Utils::getOS() !== "win"){
|
||||||
return false;
|
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`));
|
$myLocalIP = gethostbyname(trim(`hostname`));
|
||||||
try{
|
|
||||||
/** @noinspection PhpUndefinedClassInspection */
|
/** @noinspection PhpUndefinedClassInspection */
|
||||||
$com = new \COM("HNetCfg.NATUPnP");
|
$com = new \COM("HNetCfg.NATUPnP");
|
||||||
/** @noinspection PhpUndefinedFieldInspection */
|
/** @noinspection PhpUndefinedFieldInspection */
|
||||||
if($com === false or !is_object($com->StaticPortMappingCollection)){
|
if($com === false or !is_object($com->StaticPortMappingCollection)){
|
||||||
return false;
|
throw new \RuntimeException("Failed to portforward (unsupported?)");
|
||||||
}
|
|
||||||
/** @noinspection PhpUndefinedFieldInspection */
|
|
||||||
$com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP");
|
|
||||||
}catch(\Throwable $e){
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
/** @noinspection PhpUndefinedFieldInspection */
|
||||||
|
$com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function RemovePortForward(int $port) : bool{
|
public static function RemovePortForward(int $port) : bool{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user