PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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
22declare(strict_types=1);
23
25
29
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");
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}
static portForward(string $serviceURL, string $internalIP, int $internalPort, int $externalPort)
Definition: UPnP.php:195