mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 08:44:01 +00:00
Separate UPnPNetworkInterface from UPnP implementation
This commit is contained in:
parent
3d0b21f30c
commit
80bf948588
@ -64,7 +64,7 @@ use pocketmine\network\Network;
|
||||
use pocketmine\network\query\DedicatedQueryNetworkInterface;
|
||||
use pocketmine\network\query\QueryHandler;
|
||||
use pocketmine\network\query\QueryInfo;
|
||||
use pocketmine\network\upnp\UPnP;
|
||||
use pocketmine\network\upnp\UPnPNetworkInterface;
|
||||
use pocketmine\permission\BanList;
|
||||
use pocketmine\permission\DefaultPermissions;
|
||||
use pocketmine\player\GameMode;
|
||||
@ -1109,7 +1109,7 @@ class Server{
|
||||
|
||||
if((bool) $this->configGroup->getProperty("network.upnp-forwarding", false)){
|
||||
try{
|
||||
$this->network->registerInterface(new UPnP($this->logger, Internet::getInternalIP(), $this->getPort()));
|
||||
$this->network->registerInterface(new UPnPNetworkInterface($this->logger, Internet::getInternalIP(), $this->getPort()));
|
||||
}catch(\RuntimeException $e){
|
||||
$this->logger->alert("UPnP portforward failed: " . $e->getMessage());
|
||||
}
|
||||
|
@ -55,7 +55,6 @@ declare(strict_types=1);
|
||||
*/
|
||||
namespace pocketmine\network\upnp;
|
||||
|
||||
use pocketmine\network\NetworkInterface;
|
||||
use pocketmine\utils\AssumptionFailedError;
|
||||
use pocketmine\utils\Internet;
|
||||
use function count;
|
||||
@ -80,7 +79,7 @@ use const SOCKET_ETIMEDOUT;
|
||||
use const SOL_SOCKET;
|
||||
use const SOL_UDP;
|
||||
|
||||
class UPnP implements NetworkInterface{
|
||||
class UPnP{
|
||||
private const MAX_DISCOVERY_ATTEMPTS = 3;
|
||||
|
||||
private static function makePcreError() : \RuntimeException{
|
||||
@ -188,36 +187,13 @@ class UPnP implements NetworkInterface{
|
||||
return $serviceURL;
|
||||
}
|
||||
|
||||
/** @var string */
|
||||
private $ip;
|
||||
/** @var int */
|
||||
private $port;
|
||||
|
||||
/** @var string|null */
|
||||
private $serviceURL = null;
|
||||
/** @var \Logger */
|
||||
private $logger;
|
||||
|
||||
public function __construct(\Logger $logger, string $ip, int $port){
|
||||
if(!Internet::$online){
|
||||
throw new \RuntimeException("Server is offline");
|
||||
}
|
||||
|
||||
$this->ip = $ip;
|
||||
$this->port = $port;
|
||||
$this->logger = new \PrefixedLogger($logger, "UPnP Port Forwarder");
|
||||
}
|
||||
|
||||
public function start() : void{
|
||||
$this->logger->info("Attempting to portforward...");
|
||||
$this->serviceURL = self::getServiceUrl();
|
||||
|
||||
public static function portForward(string $serviceURL, int $port) : void{
|
||||
$body =
|
||||
'<u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">' .
|
||||
'<NewRemoteHost></NewRemoteHost>' .
|
||||
'<NewExternalPort>' . $this->port . '</NewExternalPort>' .
|
||||
'<NewExternalPort>' . $port . '</NewExternalPort>' .
|
||||
'<NewProtocol>UDP</NewProtocol>' .
|
||||
'<NewInternalPort>' . $this->port . '</NewInternalPort>' .
|
||||
'<NewInternalPort>' . $port . '</NewInternalPort>' .
|
||||
'<NewInternalClient>' . Internet::getInternalIP() . '</NewInternalClient>' .
|
||||
'<NewEnabled>1</NewEnabled>' .
|
||||
'<NewPortMappingDescription>PocketMine-MP</NewPortMappingDescription>' .
|
||||
@ -234,29 +210,16 @@ class UPnP implements NetworkInterface{
|
||||
'SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"'
|
||||
];
|
||||
|
||||
if(Internet::postURL($this->serviceURL, $contents, 3, $headers, $err) === null){
|
||||
if(Internet::postURL($serviceURL, $contents, 3, $headers, $err) === null){
|
||||
throw new \RuntimeException("Failed to portforward using UPnP: " . $err);
|
||||
}
|
||||
|
||||
$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->serviceURL === null){
|
||||
return;
|
||||
}
|
||||
public static function removePortForward(string $serviceURL, int $port) : void{
|
||||
$body =
|
||||
'<u:DeletePortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">' .
|
||||
'<NewRemoteHost></NewRemoteHost>' .
|
||||
'<NewExternalPort>' . $this->port . '</NewExternalPort>' .
|
||||
'<NewExternalPort>' . $port . '</NewExternalPort>' .
|
||||
'<NewProtocol>UDP</NewProtocol>' .
|
||||
'</u:DeletePortMapping>';
|
||||
|
||||
@ -270,6 +233,6 @@ class UPnP implements NetworkInterface{
|
||||
'SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#DeletePortMapping"'
|
||||
];
|
||||
|
||||
Internet::postURL($this->serviceURL, $contents, 3, $headers);
|
||||
Internet::postURL($serviceURL, $contents, 3, $headers);
|
||||
}
|
||||
}
|
||||
|
75
src/network/upnp/UPnPNetworkInterface.php
Normal file
75
src/network/upnp/UPnPNetworkInterface.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\network\upnp;
|
||||
|
||||
use pocketmine\network\NetworkInterface;
|
||||
use pocketmine\utils\Internet;
|
||||
|
||||
final class UPnPNetworkInterface implements NetworkInterface{
|
||||
|
||||
/** @var string */
|
||||
private $ip;
|
||||
/** @var int */
|
||||
private $port;
|
||||
|
||||
/** @var string|null */
|
||||
private $serviceURL = null;
|
||||
/** @var \Logger */
|
||||
private $logger;
|
||||
|
||||
public function __construct(\Logger $logger, string $ip, int $port){
|
||||
if(!Internet::$online){
|
||||
throw new \RuntimeException("Server is offline");
|
||||
}
|
||||
|
||||
$this->ip = $ip;
|
||||
$this->port = $port;
|
||||
$this->logger = new \PrefixedLogger($logger, "UPnP Port Forwarder");
|
||||
}
|
||||
|
||||
public function start() : void{
|
||||
$this->logger->info("Attempting to portforward...");
|
||||
$this->serviceURL = UPnP::getServiceUrl();
|
||||
|
||||
UPnP::portForward($this->serviceURL, $this->port);
|
||||
|
||||
$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->serviceURL === null){
|
||||
return;
|
||||
}
|
||||
|
||||
UPnP::removePortForward($this->serviceURL, $this->port);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user