Skip to content

Commit

Permalink
Add SRV support
Browse files Browse the repository at this point in the history
Closes #34, #119
  • Loading branch information
xPaw committed Nov 6, 2016
1 parent 295d1b1 commit d718415
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ Query allows to request a full list of servers' plugins and players, however thi
## RCON
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.

## SRV DNS record
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).

## Example
```php
<?php
Expand Down Expand Up @@ -80,8 +83,5 @@ If the server has query enabled (`enable-query`), then you can use `MinecraftQue
?>
```

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

## License
[MIT](LICENSE)
32 changes: 31 additions & 1 deletion src/MinecraftPing.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ class MinecraftPing
private $ServerPort;
private $Timeout;

public function __construct( $Address, $Port = 25565, $Timeout = 2 )
public function __construct( $Address, $Port = 25565, $Timeout = 2, $ResolveSRV = true )
{
$this->ServerAddress = $Address;
$this->ServerPort = (int)$Port;
$this->Timeout = (int)$Timeout;

if( $ResolveSRV )
{
$this->ResolveSRV();
}

$this->Connect( );
}

Expand Down Expand Up @@ -210,4 +215,29 @@ private function ReadVarInt( )

return $i;
}

private function ResolveSRV()
{
if( ip2long( $this->ServerAddress ) !== false )
{
return;
}

$Record = dns_get_record( '_minecraft._tcp.' . $this->ServerAddress, DNS_SRV );

if( empty( $Record ) )
{
return;
}

if( isset( $Record[ 0 ][ 'target' ] ) )
{
$this->ServerAddress = $Record[ 0 ][ 'target' ];
}

if( isset( $Record[ 0 ][ 'port' ] ) )
{
$this->ServerPort = $Record[ 0 ][ 'port' ];
}
}
}
27 changes: 26 additions & 1 deletion src/MinecraftQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ class MinecraftQuery
private $Players;
private $Info;

public function Connect( $Ip, $Port = 25565, $Timeout = 3 )
public function Connect( $Ip, $Port = 25565, $Timeout = 3, $ResolveSRV = true )
{
if( !is_int( $Timeout ) || $Timeout < 0 )
{
throw new \InvalidArgumentException( 'Timeout must be an integer.' );
}

if( $ResolveSRV )
{
$this->ResolveSRV( $Ip, $Port );
}

$this->Socket = @FSockOpen( 'udp://' . $Ip, (int)$Port, $ErrNo, $ErrStr, $Timeout );

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

return SubStr( $Data, 5 );
}

private function ResolveSRV( &$Address, &$Port )
{
if( ip2long( $Address ) !== false )
{
return;
}

$Record = dns_get_record( '_minecraft._tcp.' . $Address, DNS_SRV );

if( empty( $Record ) )
{
return;
}

if( isset( $Record[ 0 ][ 'target' ] ) )
{
$Address = $Record[ 0 ][ 'target' ];
}
}
}

0 comments on commit d718415

Please sign in to comment.