Merge remote-tracking branch 'origin/3.5'

This commit is contained in:
Dylan K. Taylor 2019-01-29 12:53:38 +00:00
commit b6ecab1d49
3 changed files with 38 additions and 5 deletions

View File

@ -29,9 +29,7 @@ namespace pocketmine\network\upnp;
use pocketmine\utils\Internet;
use pocketmine\utils\Utils;
use function class_exists;
use function gethostbyname;
use function is_object;
use function trim;
abstract class UPnP{
@ -51,7 +49,7 @@ abstract class UPnP{
throw new \RuntimeException("UPnP requires the com_dotnet extension");
}
$myLocalIP = gethostbyname(trim(`hostname`));
$myLocalIP = Internet::getInternalIP();
/** @noinspection PhpUndefinedClassInspection */
$com = new \COM("HNetCfg.NATUPnP");

View File

@ -32,10 +32,17 @@ use function curl_init;
use function curl_setopt_array;
use function explode;
use function preg_match;
use function socket_close;
use function socket_connect;
use function socket_create;
use function socket_getsockname;
use function socket_last_error;
use function socket_strerror;
use function strip_tags;
use function strtolower;
use function substr;
use function trim;
use const AF_INET;
use const CURLINFO_HEADER_SIZE;
use const CURLINFO_HTTP_CODE;
use const CURLOPT_AUTOREFERER;
@ -51,6 +58,8 @@ use const CURLOPT_RETURNTRANSFER;
use const CURLOPT_SSL_VERIFYHOST;
use const CURLOPT_SSL_VERIFYPEER;
use const CURLOPT_TIMEOUT_MS;
use const SOCK_DGRAM;
use const SOL_UDP;
class Internet{
public static $ip = false;
@ -98,6 +107,28 @@ class Internet{
return false;
}
/**
* Returns the machine's internal network IP address. If the machine is not behind a router, this may be the same
* as the external IP.
*
* @return string
* @throws InternetException
*/
public static function getInternalIP() : string{
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
try{
if(!@socket_connect($sock, "8.8.8.8", 65534)){
throw new InternetException("Failed to get internal IP: " . trim(socket_strerror(socket_last_error($sock))));
}
if(!@socket_getsockname($sock, $name)){
throw new InternetException("Failed to get internal IP: " . trim(socket_strerror(socket_last_error($sock))));
}
return $name;
}finally{
socket_close($sock);
}
}
/**
* GETs an URL using cURL
* NOTE: This is a blocking operation and can take a significant amount of time. It is inadvisable to use this method on the main thread.

View File

@ -31,8 +31,8 @@ use pocketmine\lang\Language;
use pocketmine\lang\LanguageNotFoundException;
use pocketmine\utils\Config;
use pocketmine\utils\Internet;
use pocketmine\utils\InternetException;
use function fgets;
use function gethostbyname;
use function sleep;
use function strtolower;
use function trim;
@ -204,7 +204,11 @@ LICENSE;
if($externalIP === false){
$externalIP = "unknown (server offline)";
}
$internalIP = gethostbyname(trim(`hostname`));
try{
$internalIP = Internet::getInternalIP();
}catch(InternetException $e){
$internalIP = "unknown (" . $e->getMessage() . ")";
}
$this->error($this->lang->translateString("ip_warning", ["EXTERNAL_IP" => $externalIP, "INTERNAL_IP" => $internalIP]));
$this->error($this->lang->get("ip_confirm"));