ip = $ip; $this->port = $port; $this->logger = new \PrefixedLogger($logger, "UPnP Port Forwarder"); } public function start() : void{ /** @noinspection PhpUndefinedClassInspection */ $com = new \COM("HNetCfg.NATUPnP"); /** @noinspection PhpUndefinedFieldInspection */ if(!is_object($com->StaticPortMappingCollection)){ throw new \RuntimeException("UPnP unsupported or network discovery is not enabled"); } /** @noinspection PhpUndefinedFieldInspection */ $this->staticPortMappingCollection = $com->StaticPortMappingCollection; try{ $this->staticPortMappingCollection->Add($this->port, "UDP", $this->port, $this->ip, true, "PocketMine-MP"); }catch(\com_exception $e){ throw new \RuntimeException($e->getMessage(), 0, $e); } $this->logger->info("Forwarded $this->ip:$this->port to external port $this->port"); } public function setName(string $name) : void{ } public function tick() : void{ } public function shutdown() : void{ if($this->staticPortMappingCollection !== null){ try{ /** @noinspection PhpUndefinedFieldInspection */ $this->staticPortMappingCollection->Remove($this->port, "UDP"); }catch(\com_exception $e){ //TODO: should this really be silenced? } } } }