Added optional Windows UPnP port forwading

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-06 21:19:39 +01:00
parent 21594d699d
commit 89c0702a47
2 changed files with 70 additions and 0 deletions

View File

@ -90,6 +90,10 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run
define("DEBUG", $this->config["debug"]);
$this->server = new PocketMinecraftServer($this->getProperty("server-name"), $this->getProperty("gamemode"), false, $this->getProperty("port"), $this->getProperty("server-id"));
$this->server->api = $this;
if($this->getProperty("upnp-forwading") === true ){
console("[INFO] [UPnP] Trying to port forward...");
UPnP_PortForward($this->getProperty("port"));
}
if($this->getProperty("last-update") === false or ($this->getProperty("last-update") + 3600) < time()){
console("[INFO] Checking for new server version");
console("[INFO] Last check: ".date("Y-m-d H:i:s", $this->getProperty("last-update")));
@ -236,6 +240,7 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run
$config = $this->config;
$config["white-list"] = $config["white-list"] === true ? "true":"false";
$config["invisible"] = $config["invisible"] === true ? "true":"false";
$config["upnp-forwading"] = $config["upnp-forwading"] === true ? "true":"false";
$prop = "#Pocket Minecraft PHP server properties\r\n#".date("D M j H:i:s T Y")."\r\n";
foreach($config as $n => $v){
$prop .= $n."=".$v."\r\n";
@ -297,6 +302,10 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run
unregister_tick_function(array($this->server, "tick"));
$this->__destruct();
unset($this->server);
if($this->getProperty("upnp-forwading") === true ){
console("[INFO] [UPnP] Removing port forward...");
UPnP_RemovePortForward($this->getProperty("port"));
}
return $this->restart;
}

61
src/misc/utils/UPnP.php Normal file
View File

@ -0,0 +1,61 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
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.
*/
function UPnP_PortForward($port){
if(Utils::getOS() != "win" or !class_exists("COM")){
return false;
}
$port = (int) $port;
$myLocalIP = gethostbyname(trim(`hostname`));
try{
$com = new COM("HNetCfg.NATUPnP");
if($com === false){
return false;
}
$com->StaticPortMappingCollection->Add($port, "UDP", $port, $myLocalIP, true, "PocketMine-MP");
}catch(Exception $e){
return false;
}
return true;
}
function UPnP_RemovePortForward($port){
if(Utils::getOS() != "win" or !class_exists("COM")){
return false;
}
$port = (int) $port;
try{
$com = new COM("HNetCfg.NATUPnP") or false;
if($com === false){
return false;
}
$com->StaticPortMappingCollection->Remove($port, "UDP");
}catch(Exception $e){
return false;
}
return true;
}