PocketMine-MP
5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
UPnPNetworkInterface.php
1
<?php
2
3
/*
4
*
5
* ____ _ _ __ __ _ __ __ ____
6
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10
*
11
* This program is free software: you can redistribute it and/or modify
12
* it under the terms of the GNU Lesser General Public License as published by
13
* the Free Software Foundation, either version 3 of the License, or
14
* (at your option) any later version.
15
*
16
* @author PocketMine Team
17
* @link http://www.pocketmine.net/
18
*
19
*
20
*/
21
22
declare(strict_types=1);
23
24
namespace
pocketmine\network\upnp
;
25
26
use
pocketmine\network\NetworkInterface
;
27
use
pocketmine\utils\Internet
;
28
use
pocketmine\utils\InternetException
;
29
30
final
class
UPnPNetworkInterface
implements
NetworkInterface
{
31
private
?
string
$serviceURL =
null
;
32
33
public
function
__construct(
34
private
\
Logger
$logger,
35
private
string
$ip,
36
private
int
$port
37
){
38
if
(!Internet::$online){
39
throw
new \RuntimeException(
"Server is offline"
);
40
}
41
42
$this->logger = new \PrefixedLogger($logger,
"UPnP Port Forwarder"
);
43
}
44
45
public
function
start
() : void{
46
$this->logger->info(
"Attempting to portforward..."
);
47
48
try
{
49
$this->serviceURL =
UPnP::getServiceUrl
();
50
UPnP::portForward
($this->serviceURL, Internet::getInternalIP(), $this->port, $this->port);
51
$this->logger->info(
"Forwarded $this->ip:$this->port to external port $this->port"
);
52
}
catch
(
UPnPException
|
InternetException
$e){
53
$this->logger->error(
"UPnP portforward failed: "
. $e->getMessage());
54
}
55
}
56
57
public
function
setName(
string
$name) : void{
58
59
}
60
61
public function tick() : void{
62
63
}
64
65
public function shutdown() : void{
66
if($this->serviceURL === null){
67
return
;
68
}
69
70
UPnP::removePortForward($this->serviceURL, $this->port);
71
}
72
}
pocketmine\network\upnp\UPnPException
Definition
UPnPException.php:26
pocketmine\network\upnp\UPnP\getServiceUrl
static getServiceUrl()
Definition
UPnP.php:107
pocketmine\network\upnp\UPnP\portForward
static portForward(string $serviceURL, string $internalIP, int $internalPort, int $externalPort)
Definition
UPnP.php:195
pocketmine\network\upnp\UPnPNetworkInterface
Definition
UPnPNetworkInterface.php:30
pocketmine\network\upnp\UPnPNetworkInterface\start
start()
Definition
UPnPNetworkInterface.php:45
pocketmine\utils\InternetException
Definition
InternetException.php:26
pocketmine\utils\Internet
Definition
Internet.php:66
Logger
Definition
Logger.php:18
pocketmine\network\NetworkInterface
Definition
NetworkInterface.php:32
pocketmine\network\upnp
Definition
UPnP.php:56
src
network
upnp
UPnPNetworkInterface.php
Generated by
1.12.0