mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-08-11 14:02:05 +00:00
parent
7f56f27505
commit
2ba76bd97d
@ -1268,9 +1268,8 @@ class Server{
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($this->getProperty("network.upnp-forwarding", false)){
|
if($this->getProperty("network.upnp-forwarding", false)){
|
||||||
$this->logger->info("[UPnP] Trying to port forward...");
|
|
||||||
try{
|
try{
|
||||||
UPnP::PortForward($this->getPort());
|
$this->network->registerInterface(new UPnP($this->logger, Internet::getInternalIP(), $this->getPort()));
|
||||||
}catch(\RuntimeException $e){
|
}catch(\RuntimeException $e){
|
||||||
$this->logger->alert("UPnP portforward failed: " . $e->getMessage());
|
$this->logger->alert("UPnP portforward failed: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
@ -1584,11 +1583,6 @@ class Server{
|
|||||||
|
|
||||||
$this->shutdown();
|
$this->shutdown();
|
||||||
|
|
||||||
if($this->getProperty("network.upnp-forwarding", false)){
|
|
||||||
$this->logger->info("[UPnP] Removing port forward...");
|
|
||||||
UPnP::RemovePortForward($this->getPort());
|
|
||||||
}
|
|
||||||
|
|
||||||
if($this->pluginManager instanceof PluginManager){
|
if($this->pluginManager instanceof PluginManager){
|
||||||
$this->getLogger()->debug("Disabling all plugins");
|
$this->getLogger()->debug("Disabling all plugins");
|
||||||
$this->pluginManager->disablePlugins();
|
$this->pluginManager->disablePlugins();
|
||||||
|
@ -26,19 +26,25 @@ declare(strict_types=1);
|
|||||||
*/
|
*/
|
||||||
namespace pocketmine\network\upnp;
|
namespace pocketmine\network\upnp;
|
||||||
|
|
||||||
|
use pocketmine\network\NetworkInterface;
|
||||||
use pocketmine\utils\Internet;
|
use pocketmine\utils\Internet;
|
||||||
use pocketmine\utils\Utils;
|
use pocketmine\utils\Utils;
|
||||||
use function class_exists;
|
use function class_exists;
|
||||||
use function is_object;
|
use function is_object;
|
||||||
|
|
||||||
abstract class UPnP{
|
class UPnP implements NetworkInterface{
|
||||||
|
|
||||||
/**
|
/** @var string */
|
||||||
* @param int $port
|
private $ip;
|
||||||
*
|
/** @var int */
|
||||||
* @throws \RuntimeException
|
private $port;
|
||||||
*/
|
|
||||||
public static function PortForward(int $port) : void{
|
/** @var object|null */
|
||||||
|
private $staticPortMappingCollection = null;
|
||||||
|
/** @var \Logger */
|
||||||
|
private $logger;
|
||||||
|
|
||||||
|
public function __construct(\Logger $logger, string $ip, int $port){
|
||||||
if(!Internet::$online){
|
if(!Internet::$online){
|
||||||
throw new \RuntimeException("Server is offline");
|
throw new \RuntimeException("Server is offline");
|
||||||
}
|
}
|
||||||
@ -48,48 +54,46 @@ abstract class UPnP{
|
|||||||
if(!class_exists("COM")){
|
if(!class_exists("COM")){
|
||||||
throw new \RuntimeException("UPnP requires the com_dotnet extension");
|
throw new \RuntimeException("UPnP requires the com_dotnet extension");
|
||||||
}
|
}
|
||||||
|
$this->ip = $ip;
|
||||||
|
$this->port = $port;
|
||||||
|
$this->logger = new \PrefixedLogger($logger, "UPnP Port Forwarder");
|
||||||
|
}
|
||||||
|
|
||||||
$myLocalIP = Internet::getInternalIP();
|
public function start() : void{
|
||||||
|
|
||||||
/** @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)){
|
||||||
throw new \RuntimeException("Failed to portforward using UPnP. Ensure that network discovery is enabled in Control Panel.");
|
throw new \RuntimeException("UPnP unsupported or network discovery is not enabled");
|
||||||
}
|
}
|
||||||
|
/** @noinspection PhpUndefinedFieldInspection */
|
||||||
|
$this->staticPortMappingCollection = $com->StaticPortMappingCollection;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
/** @noinspection PhpUndefinedFieldInspection */
|
$this->staticPortMappingCollection->Add($this->port, "UDP", $this->port, $this->ip, true, "PocketMine-MP");
|
||||||
$com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP");
|
|
||||||
}catch(\com_exception $e){
|
}catch(\com_exception $e){
|
||||||
throw new \RuntimeException($e->getMessage(), 0, $e);
|
throw new \RuntimeException($e->getMessage(), 0, $e);
|
||||||
}
|
}
|
||||||
|
$this->logger->info("Forwarded $this->ip:$this->port to external port $this->port");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function RemovePortForward(int $port) : bool{
|
public function setName(string $name) : void{
|
||||||
if(!Internet::$online){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if(Utils::getOS() != "win" or !class_exists("COM")){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @noinspection PhpUndefinedClassInspection */
|
}
|
||||||
$com = new \COM("HNetCfg.NATUPnP");
|
|
||||||
/** @noinspection PhpUndefinedFieldInspection */
|
|
||||||
if($com === false or !is_object($com->StaticPortMappingCollection)){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try{
|
public function tick() : void{
|
||||||
/** @noinspection PhpUndefinedFieldInspection */
|
|
||||||
$com->StaticPortMappingCollection->Remove($port, "UDP");
|
|
||||||
}catch(\com_exception $e){
|
|
||||||
//TODO: should this really be silenced?
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
}
|
||||||
|
|
||||||
|
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?
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user