Skip to content

Commit d718415

Browse files
committed
Add SRV support
Closes #34, #119
1 parent 295d1b1 commit d718415

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Query allows to request a full list of servers' plugins and players, however thi
2424
## RCON
2525
It is possible to send console commands to a Minecraft server remotely using the [Source RCON protocol](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol). Use [PHP Source Query](https://github.com/xPaw/PHP-Source-Query-Class) library for your RCON needs.
2626

27+
## SRV DNS record
28+
This library automatically tries to resolve SRV records. If you do not wish to do so, pass `false` as the fourth param in the constructor (after timeout param).
29+
2730
## Example
2831
```php
2932
<?php
@@ -80,8 +83,5 @@ If the server has query enabled (`enable-query`), then you can use `MinecraftQue
8083
?>
8184
```
8285

83-
Please note that this library does not resolve SRV records, you will need to do that yourself.
84-
Take a look at [this issue](https://github.com/xPaw/PHP-Minecraft-Query/issues/34) for an example script.
85-
8686
## License
8787
[MIT](LICENSE)

src/MinecraftPing.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ class MinecraftPing
3131
private $ServerPort;
3232
private $Timeout;
3333

34-
public function __construct( $Address, $Port = 25565, $Timeout = 2 )
34+
public function __construct( $Address, $Port = 25565, $Timeout = 2, $ResolveSRV = true )
3535
{
3636
$this->ServerAddress = $Address;
3737
$this->ServerPort = (int)$Port;
3838
$this->Timeout = (int)$Timeout;
3939

40+
if( $ResolveSRV )
41+
{
42+
$this->ResolveSRV();
43+
}
44+
4045
$this->Connect( );
4146
}
4247

@@ -210,4 +215,29 @@ private function ReadVarInt( )
210215

211216
return $i;
212217
}
218+
219+
private function ResolveSRV()
220+
{
221+
if( ip2long( $this->ServerAddress ) !== false )
222+
{
223+
return;
224+
}
225+
226+
$Record = dns_get_record( '_minecraft._tcp.' . $this->ServerAddress, DNS_SRV );
227+
228+
if( empty( $Record ) )
229+
{
230+
return;
231+
}
232+
233+
if( isset( $Record[ 0 ][ 'target' ] ) )
234+
{
235+
$this->ServerAddress = $Record[ 0 ][ 'target' ];
236+
}
237+
238+
if( isset( $Record[ 0 ][ 'port' ] ) )
239+
{
240+
$this->ServerPort = $Record[ 0 ][ 'port' ];
241+
}
242+
}
213243
}

src/MinecraftQuery.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ class MinecraftQuery
1818
private $Players;
1919
private $Info;
2020

21-
public function Connect( $Ip, $Port = 25565, $Timeout = 3 )
21+
public function Connect( $Ip, $Port = 25565, $Timeout = 3, $ResolveSRV = true )
2222
{
2323
if( !is_int( $Timeout ) || $Timeout < 0 )
2424
{
2525
throw new \InvalidArgumentException( 'Timeout must be an integer.' );
2626
}
2727

28+
if( $ResolveSRV )
29+
{
30+
$this->ResolveSRV( $Ip, $Port );
31+
}
32+
2833
$this->Socket = @FSockOpen( 'udp://' . $Ip, (int)$Port, $ErrNo, $ErrStr, $Timeout );
2934

3035
if( $ErrNo || $this->Socket === false )
@@ -190,4 +195,24 @@ private function WriteData( $Command, $Append = "" )
190195

191196
return SubStr( $Data, 5 );
192197
}
198+
199+
private function ResolveSRV( &$Address, &$Port )
200+
{
201+
if( ip2long( $Address ) !== false )
202+
{
203+
return;
204+
}
205+
206+
$Record = dns_get_record( '_minecraft._tcp.' . $Address, DNS_SRV );
207+
208+
if( empty( $Record ) )
209+
{
210+
return;
211+
}
212+
213+
if( isset( $Record[ 0 ][ 'target' ] ) )
214+
{
215+
$Address = $Record[ 0 ][ 'target' ];
216+
}
217+
}
193218
}

0 commit comments

Comments
 (0)