Skip to content

Commit

Permalink
Fix Samp add server by domain (#593)
Browse files Browse the repository at this point in the history
* Update Samp.php

* Update Server.php

* Update Server.php

* Fix test responses to reflect changes

* Create Issue588.php

* Update Issue588.php

* Update Issue588.php

* Update Samp.php

* Update Issue588.php

* Move queryTest to TestBase and dry issue tests,
Fix stub in isse 588 test,
  • Loading branch information
Patrick authored Feb 18, 2022
1 parent af6641c commit 71634b3
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 133 deletions.
17 changes: 11 additions & 6 deletions src/GameQ/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,18 @@ protected function checkAndSetIpPort($ip_address)
);
}

// Validate the IPv4 value, if FALSE is not a valid IP, maybe a hostname. Try to resolve
if (!filter_var($this->ip, FILTER_VALIDATE_IP, ['flags' => FILTER_FLAG_IPV4,])
&& $this->ip === gethostbyname($this->ip)
) {
// Validate the IPv4 value, if FALSE is not a valid IP, maybe a hostname.
if (! filter_var($this->ip, FILTER_VALIDATE_IP, ['flags' => FILTER_FLAG_IPV4,])) {
// Try to resolve the hostname to IPv4
$resolved = gethostbyname($this->ip);

// When gethostbyname() fails it returns the original string
// so if ip and the result from gethostbyname() are equal this failed.
throw new Exception("Unable to resolve the host '{$this->ip}' to an IP address.");
if ($this->ip === $resolved) {
// so if ip and the result from gethostbyname() are equal this failed.
throw new Exception("Unable to resolve the host '{$this->ip}' to an IP address.");
} else {
$this->ip = $resolved;
}
}
}
}
Expand Down
35 changes: 1 addition & 34 deletions tests/Issues/Issue307.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,7 @@ public function test1()

$filePath = sprintf('%s/Providers/307.txt', __DIR__);

// Create a mock server
$server = // Create a mock server
$this->getMockBuilder('\GameQ\Server')
->setConstructorArgs([
[
\GameQ\Server::SERVER_HOST => '127.0.0.1:27015',
\GameQ\Server::SERVER_TYPE => 'csgo',
],
])
->enableProxyingToOriginalMethods()
->getMock();

// Invoke beforeSend function
$server->protocol()->beforeSend($server);

// Set the packet response as if we have really queried it
$server->protocol()->packetResponse(explode(PHP_EOL . '||' . PHP_EOL, file_get_contents($filePath)));

// Create a mock GameQ
$gq_mock = $this->getMockBuilder('\GameQ\GameQ')
->enableProxyingToOriginalMethods()
->getMock();
$gq_mock->setOption('debug', false);

// Reflect on GameQ class so we can parse
$gameq = new \ReflectionClass($gq_mock);

// Get the parse method so we can call it
$method = $gameq->getMethod('doParseResponse');

// Set the method to accessible
$method->setAccessible(true);

$testResult = $method->invoke($gq_mock, $server);
$testResult = $this->queryTest('127.0.0.1:27015', 'csgo', explode(PHP_EOL . '||' . PHP_EOL, file_get_contents($filePath)));

$this->assertEquals($testResult['gq_online'], 1);
}
Expand Down
35 changes: 1 addition & 34 deletions tests/Issues/Issue382.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,7 @@ public function test1()
*/
$filePath = sprintf('%s/Providers/382.txt', __DIR__);

// Create a mock server
$server =
$this->getMockBuilder('\GameQ\Server')
->setConstructorArgs([
[
\GameQ\Server::SERVER_HOST => '58.162.184.102:2302',
\GameQ\Server::SERVER_TYPE => 'arma3',
],
])
->enableProxyingToOriginalMethods()
->getMock();

// Invoke beforeSend function
$server->protocol()->beforeSend($server);

// Set the packet response as if we have really queried it
$server->protocol()->packetResponse(explode(PHP_EOL . '||' . PHP_EOL, file_get_contents($filePath)));

// Create a mock GameQ
$gq_mock = $this->getMockBuilder('\GameQ\GameQ')
->enableProxyingToOriginalMethods()
->getMock();
$gq_mock->setOption('debug', false);

// Reflect on GameQ class so we can parse
$gameq = new \ReflectionClass($gq_mock);

// Get the parse method so we can call it
$method = $gameq->getMethod('doParseResponse');

// Set the method to accessible
$method->setAccessible(true);

$testResult = $method->invoke($gq_mock, $server);
$testResult = $this->queryTest('58.162.184.102:2302', 'arma3', explode(PHP_EOL . '||' . PHP_EOL, file_get_contents($filePath)));

$this->assertEquals($testResult['gq_online'], true);
}
Expand Down
62 changes: 62 additions & 0 deletions tests/Issues/Issue588.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* This file is part of GameQ.
*
* GameQ 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.
*
* GameQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace GameQ\Tests\Issues;

use GameQ\Tests\TestBase;

/**
* Class Issue588
*
* Test for issue #588 - https://github.com/Austinb/GameQ/issues/588
*
* @package GameQ\Tests\Issues
*/
class Issue588 extends TestBase
{
/**
* Setup to create our stub
*/
public function setUp()
{
$this->stub = $this->getMockBuilder('\GameQ\GameQ')
->enableProxyingToOriginalMethods()
->setMethods(['__get', '__set'])
->getMock();
}

/**
* Test for issue where hostnames are not correctly resolved to IP
*/
public function test1()
{
// Test single add server
$this->stub->addServer([
\GameQ\Server::SERVER_HOST => 'game.samp-mobile.com:7777',
\GameQ\Server::SERVER_TYPE => 'Samp',
]);

$this->stub->addFilter('normalize');

// We do fail here
$this->stub->process();

// Clear the servers
$this->assertTrue(true);
}
}
55 changes: 0 additions & 55 deletions tests/Protocols/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,59 +72,4 @@ public function loadData()

return $providers;
}

/**
* Generic query test function to simulate testing of protocol classes
*
* @param string $host
* @param string $protocol
* @param array $responses
* @param bool $debug
* @param array $server_options
*
* @return mixed
*/
protected function queryTest($host, $protocol, $responses, $debug = false, $server_options = [])
{

// Create a mock server
$server = $this->getMockBuilder('\GameQ\Server')
->setConstructorArgs([
[
\GameQ\Server::SERVER_HOST => $host,
\GameQ\Server::SERVER_TYPE => $protocol,
\GameQ\Server::SERVER_OPTIONS => $server_options,
],
])
->enableProxyingToOriginalMethods()
->getMock();

// Invoke beforeSend function
$server->protocol()->beforeSend($server);

// Set the packet response as if we have really queried it
$server->protocol()->packetResponse($responses);

// Create a mock GameQ
$gq_mock = $this->getMockBuilder('\GameQ\GameQ')
->enableProxyingToOriginalMethods()
->getMock();
$gq_mock->setOption('debug', $debug);
$gq_mock->removeFilter('normalize');

// Reflect on GameQ class so we can parse
$gameq = new \ReflectionClass($gq_mock);

// Get the parse method so we can call it
$method = $gameq->getMethod('doParseResponse');

// Set the method to accessible
$method->setAccessible(true);

$testResult = $method->invoke($gq_mock, $server);

unset($server, $gq_mock, $gameq, $method);

return $testResult;
}
}
2 changes: 1 addition & 1 deletion tests/Protocols/Providers/Arma/1_result.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sygsky.no-ip.org:2302":{"currentVersion":"118","dedicated":"1","difficulty":"1","equalModRequired":"0","gameState":"7","gamemode":"openplaying","gamename":"armedass","gametype":"COOP","gamever":"1.18","gq_address":"sygsky.no-ip.org","gq_joinlink":"","gq_name":"ArmA Armed Assault","gq_online":true,"gq_port_client":2302,"gq_port_query":2302,"gq_protocol":"gamespy2","gq_transport":"udp","gq_type":"arma","hostname":"Red Engineers ACE Arma-1 server","language":"65561","mapname":"sara","maxplayers":"10","mission":"co@10 Domination!ACE East [4.5+] RA","mod":"CA;@ACE;@SIX_Pack3;@ai_spotting","num_players":2,"num_teams":0,"numplayers":"2","numteams":"0","param1":"22","param2":"8","password":"0","platform":"win","players":[{"player":"Rokse [LT]","team":"","score":"56","deaths":"58"},{"player":"EngineerACE","team":"","score":"39","deaths":"43"}],"requiredVersion":"118","sv_battleye":"0","timelimit":"15"}}
{"sygsky.no-ip.org:2302":{"currentVersion":"118","dedicated":"1","difficulty":"1","equalModRequired":"0","gameState":"7","gamemode":"openplaying","gamename":"armedass","gametype":"COOP","gamever":"1.18","gq_address":"80.240.222.67","gq_joinlink":"","gq_name":"ArmA Armed Assault","gq_online":true,"gq_port_client":2302,"gq_port_query":2302,"gq_protocol":"gamespy2","gq_transport":"udp","gq_type":"arma","hostname":"Red Engineers ACE Arma-1 server","language":"65561","mapname":"sara","maxplayers":"10","mission":"co@10 Domination!ACE East [4.5+] RA","mod":"CA;@ACE;@SIX_Pack3;@ai_spotting","num_players":2,"num_teams":0,"numplayers":"2","numteams":"0","param1":"22","param2":"8","password":"0","platform":"win","players":[{"player":"Rokse [LT]","team":"","score":"56","deaths":"58"},{"player":"EngineerACE","team":"","score":"39","deaths":"43"}],"requiredVersion":"118","sv_battleye":"0","timelimit":"15"}}
2 changes: 1 addition & 1 deletion tests/Protocols/Providers/Teeworlds/1_result.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"ddracepro.net:8000":{"dedicated":true,"flags":"0","game_descr":"DDRace","gq_address":"ddracepro.net","gq_joinlink":"steam:\/\/connect\/ddracepro.net:8000\/","gq_name":"Teeworlds Server","gq_online":true,"gq_port_client":8000,"gq_port_query":8000,"gq_protocol":"teeworlds","gq_transport":"udp","gq_type":"teeworlds","hostname":"ddracepro.net - Gores\/HDP\/Verification\/other solo Maps #1","map":"gores","maxplayers":"16","maxplayers_total":"16","num_players":"0","num_players_total":"0","version":"0.6 trunk, 1.15a"}}
{"ddracepro.net:8000":{"dedicated":true,"flags":"0","game_descr":"DDRace","gq_address":"195.154.113.141","gq_joinlink":"steam:\/\/connect\/195.154.113.141:8000\/","gq_name":"Teeworlds Server","gq_online":true,"gq_port_client":8000,"gq_port_query":8000,"gq_protocol":"teeworlds","gq_transport":"udp","gq_type":"teeworlds","hostname":"ddracepro.net - Gores\/HDP\/Verification\/other solo Maps #1","map":"gores","maxplayers":"16","maxplayers_total":"16","num_players":"0","num_players_total":"0","version":"0.6 trunk, 1.15a"}}
2 changes: 1 addition & 1 deletion tests/Protocols/Providers/Terraria/1_result.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"t.shadowrain.net:7777":{"dedicated":1,"game_port":7777,"gq_address":"t.shadowrain.net","gq_joinlink":"steam:\/\/connect\/t.shadowrain.net:7777\/","gq_name":"Terraria","gq_online":true,"gq_port_client":7777,"gq_port_query":7878,"gq_protocol":"tshock","gq_transport":"tcp","gq_type":"terraria","hostname":"","maxplayers":86,"numplayers":7,"password":0,"players":[{"nickname":"Woozer","username":"Woozer","group":"default","active":1,"state":10,"team":0},{"nickname":"G0D","username":"G0D","group":"default","active":1,"state":10,"team":5},{"nickname":"dood123","username":"dood123","group":"default","active":1,"state":10,"team":0},{"nickname":"Shpee","username":"Shpee","group":"default","active":1,"state":10,"team":0},{"nickname":"Ford_j_Ford","username":"Ford_j_Ford","group":"default","active":1,"state":10,"team":5},{"nickname":"Hi-im-jimmy","username":"Hi-im-jimmy","group":"Member+1","active":1,"state":10,"team":3},{"nickname":"Biofighter","username":"Biofighter","group":"default","active":1,"state":10,"team":0}],"rules":{"AutoSave":0,"DisableBuild":0,"DisableClownBombs":0,"DisableDungeonGuardian":0,"DisableInvisPvP":1,"DisableSnowBalls":0,"DisableTombstones":1,"EnableWhitelist":0,"HardcoreOnly":0,"PvPMode":"normal","SpawnProtection":1,"SpawnProtectionRadius":325,"ServerSideInventory":0},"serverversion":"v1.3.2.1","uptime":"8.15:10:51","world":"ShadowRain ~ VII"}}
{"t.shadowrain.net:7777":{"dedicated":1,"game_port":7777,"gq_address":"83.233.14.21","gq_joinlink":"steam:\/\/connect\/83.233.14.21:7777\/","gq_name":"Terraria","gq_online":true,"gq_port_client":7777,"gq_port_query":7878,"gq_protocol":"tshock","gq_transport":"tcp","gq_type":"terraria","hostname":"","maxplayers":86,"numplayers":7,"password":0,"players":[{"nickname":"Woozer","username":"Woozer","group":"default","active":1,"state":10,"team":0},{"nickname":"G0D","username":"G0D","group":"default","active":1,"state":10,"team":5},{"nickname":"dood123","username":"dood123","group":"default","active":1,"state":10,"team":0},{"nickname":"Shpee","username":"Shpee","group":"default","active":1,"state":10,"team":0},{"nickname":"Ford_j_Ford","username":"Ford_j_Ford","group":"default","active":1,"state":10,"team":5},{"nickname":"Hi-im-jimmy","username":"Hi-im-jimmy","group":"Member+1","active":1,"state":10,"team":3},{"nickname":"Biofighter","username":"Biofighter","group":"default","active":1,"state":10,"team":0}],"rules":{"AutoSave":0,"DisableBuild":0,"DisableClownBombs":0,"DisableDungeonGuardian":0,"DisableInvisPvP":1,"DisableSnowBalls":0,"DisableTombstones":1,"EnableWhitelist":0,"HardcoreOnly":0,"PvPMode":"normal","SpawnProtection":1,"SpawnProtectionRadius":325,"ServerSideInventory":0},"serverversion":"v1.3.2.1","uptime":"8.15:10:51","world":"ShadowRain ~ VII"}}
Loading

0 comments on commit 71634b3

Please sign in to comment.