Skip to content
This repository was archived by the owner on Dec 6, 2019. It is now read-only.

Commit 9340bf6

Browse files
committed
Added support for SRV lookups
1 parent 1441491 commit 9340bf6

4 files changed

Lines changed: 57 additions & 39 deletions

File tree

app/Console/Commands/Ping.php

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,20 @@
55
use Illuminate\Console\Command;
66
use xPaw\MinecraftPing;
77
use xPaw\MinecraftPingException;
8+
use \App\Server;
89

910
class Ping extends Command {
1011

11-
const DEFAULT_MINECRAFT_PORT = 25565;
12-
13-
/**
14-
* The name and signature of the console command.
15-
*
16-
* @var string
17-
*/
1812
protected $signature = 'app:ping {address?}';
19-
20-
/**
21-
* The console command description.
22-
*
23-
* @var string
24-
*/
2513
protected $description = 'Ping all server instances from the database';
2614

27-
/**
28-
* Create a new command instance.
29-
*
30-
* @return void
31-
*/
32-
public function __construct() {
33-
parent::__construct();
34-
}
35-
36-
/**
37-
* Execute the console command.
38-
*
39-
* @return mixed
40-
*/
4115
public function handle() {
4216
$this->info("Pinging server instances");
4317

4418
$address = $this->argument("address");
4519
if ($address) {
4620
$this->info("Pinging server: " . $address);
47-
$server = \App\Server::where("address", '=', $address)->first();
21+
$server = Server::where("address", '=', $address)->first();
4822
if ($server) {
4923
$this->pingServer($server);
5024
} else {
@@ -54,7 +28,7 @@ public function handle() {
5428
return;
5529
}
5630

57-
$servers = \App\Server::all();
31+
$servers = Server::orderBy("updated_at", "asc")->paginate(15464);
5832
$bar = $this->output->createProgressBar($servers->count());
5933

6034
/* @var $server \App\Server */
@@ -69,12 +43,18 @@ public function handle() {
6943
$this->output->writeln("");
7044
}
7145

72-
function pingServer($server) {
46+
function pingServer(Server $server) {
7347
try {
7448
//the minecraft ping packets from existing libraries doesn't seem to be fast enough
75-
$server->ping = self::pingDomain($server->address);
49+
$address = $server->address;
50+
$ip = $address;
51+
$port = Server::DEFAULT_PORT;
7652

77-
$ping = new MinecraftPing($server->address, self::DEFAULT_MINECRAFT_PORT, 1);
53+
$this->Get_Minecraft_IP($address, $ip, $port);
54+
55+
$server->ping = self::pingDomain($ip, $port);
56+
57+
$ping = new MinecraftPing($ip, $port, 1);
7858
$result = $ping->Query();
7959

8060
$this->parsePingData($server, $result);
@@ -83,9 +63,8 @@ function pingServer($server) {
8363
$this->error($server->address . " " . $exception->getMessage());
8464

8565
//Reset the these data online if the server was online before
86-
if ($server->online) {
66+
if ($server->online || is_null($server->online)) {
8767
$server->online = 0;
88-
$server->players = 0;
8968
$server->save();
9069
}
9170
} finally {
@@ -102,10 +81,10 @@ function pingServer($server) {
10281
* motd
10382
* favicon
10483
*
105-
* @param \App\Server $server
84+
* @param Server $server
10685
* @param array $data
10786
*/
108-
function parsePingData($server, $data) {
87+
function parsePingData(Server $server, array $data) {
10988
$motd = $data['description'];
11089
if (is_array($motd)) {
11190
$motd = \MinecraftJsonColors::convertToLegacy($motd);
@@ -166,10 +145,10 @@ public static function createJavaUuid($striped) {
166145
return implode('-', $components);
167146
}
168147

169-
function pingDomain($domain) {
148+
function pingDomain($domain, $port) {
170149
//https://stackoverflow.com/questions/9841635/how-to-ping-a-server-port-with-php
171150
$starttime = microtime(true);
172-
$file = fsockopen($domain, self::DEFAULT_MINECRAFT_PORT, $errno, $errstr, 1);
151+
$file = fsockopen($domain, $port, $errno, $errstr, 1);
173152
$stoptime = microtime(true);
174153
$status = 0;
175154

@@ -199,6 +178,39 @@ function saveIcon($hostname, $favicon) {
199178
}
200179
}
201180

181+
//extracted from https://github.com/xPaw/PHP-Minecraft-Query/issues/34
182+
function Get_Minecraft_IP($addr, &$ip, &$port) {
183+
if (ip2long($addr) !== FALSE) {
184+
//server address is an ip
185+
return $ip = $addr;
186+
}
187+
188+
$port = Server::DEFAULT_PORT;
189+
190+
$result = dns_get_record('_minecraft._tcp.' . $addr, DNS_SRV);
191+
$this->info(count($result));
192+
193+
if (count($result) > 0) {
194+
if (array_key_exists('target', $result[0])) {
195+
$addr = $result[0]['target'];
196+
}
197+
198+
if (array_key_exists('port', $result[0])) {
199+
$port = $result[0]['port'];
200+
}
201+
202+
$this->info("Found SRV-Record");
203+
}
204+
205+
$result = dns_get_record($addr, DNS_A);
206+
207+
if (count($result) > 0 && array_key_exists('ip', $result[0])) {
208+
$ip = $result[0]['ip'];
209+
$this->info("Found A-Record");
210+
} else {
211+
$ip = $addr;
212+
}
213+
}
202214
// /**
203215
// * needs enabled-qurey=true
204216
// *

app/Console/Kernel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Kernel extends ConsoleKernel {
1515
protected $commands = [
1616
// Commands\Inspire::class,
1717
Commands\Ping::class,
18+
// Commands\NameResolve::class,
19+
// Commands\NameHistory::class,
1820
];
1921

2022
/**
@@ -26,7 +28,7 @@ class Kernel extends ConsoleKernel {
2628
protected function schedule(Schedule $schedule) {
2729
// $schedule->command('inspire')
2830
// ->hourly();
29-
$schedule->command('app:ping')->hourly()->sendOutputTo(storage_path() . '/logs/ping.log', true);
31+
$schedule->command('app:ping')->everyThirtyMinutes()->sendOutputTo(storage_path() . '/logs/ping.log', true);
3032

3133
// $schedule->command('queue:work --daemon')->everyMinute()->withoutOverlapping();
3234
}

app/Player.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
class Player extends Model {
2424

25+
const VALID_USERNAME = "/^\w{2,16}$/";
26+
2527
/**
2628
* The database table used by the model.
2729
*

app/Server.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
*/
3737
class Server extends Model {
3838

39+
const DEFAULT_PORT = 25565;
40+
3941
use SoftDeletes;
4042

4143
/**

0 commit comments

Comments
 (0)