Skip to content

Commit 8598c29

Browse files
committed
revert back to the changes
1 parent c53a7c1 commit 8598c29

3 files changed

Lines changed: 48 additions & 56 deletions

File tree

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22
A query virion for Pocketmine-MP\
33
This virion uses GS4 to query servers which provides more info. Servers that don't have GS4 supported/enabled can't be queried using this virion, so you need to use another virion such as [libpmquery](https://github.com/jasonwynn10/libpmquery)
44
## Usage
5-
First you create a new GameSpyQuery instance, first argument is the IP address to query, second argument is the port to query
5+
To query a server, you call the `GameSpyQuery::query()` function, first argument is the IP, second argument is the port
66
```php
7-
$query = new GameSpyQuery("someserver.org", 19132);
7+
$query = GameSpyQuery::query("someserver.org", 19132);
88
```
9-
Then we query the server
9+
There's an additional (optional) third argument which is the timeout, it's simply how long it will wait for a response before it closes the connection. The timeout is in seconds
1010
```php
11-
$query->query();
11+
$query = GameSpyQuery::query("someserver.org", 19132, 5); // it will wait for 5 seconds, if there was no response it would close the connection
1212
```
13-
You can also set a timeout in seconds (optional)
14-
```php
15-
$query->query(5);
16-
```
17-
The `query()` function will throw a GameSpyQueryException if the destination IP and port can't be queried, so you need to surround it with a try-catch block\
18-
If everything worked correctly, you can use the `get()` function to get some info about the server\
19-
List of the data you can get:
13+
After the query is done, it will return a `GameSpyQuery` instance
14+
15+
The `query()` function will throw a GameSpyQueryException if the destination IP and port can't be queried, so you need to surround it with a try-catch block
16+
17+
After that, use the `get()` function to get info about the server. List of all the data you can get:
2018
```php
2119
$query->get("hostname"); // Server MOTD
22-
$query->get("gametype"); // Game type, not sure what that means
23-
$query->get("game_id"); // I think that's the game edition
20+
$query->get("gametype"); // Game type, e.g SMP or CMP
21+
$query->get("game_id"); // Game edition
2422
$query->get("version"); // Version of minecraft the server is running on
2523
$query->get("server_engine"); // Server software being used
2624
$query->get("plugins"); // Plugins list with their version
@@ -30,6 +28,10 @@ $query->get("maxplayers"); // Max number of players
3028
$query->get("whitelist"); // On if whitelist is turned on, otherwise off
3129
$query->get("hostip"); // Host ip
3230
$query->get("hostport"); // Host port
33-
$query->get("players"); // List of online players names
31+
$query->get("players"); // List of online players on the server
3432
```
3533
In case you want to get the raw response, you can use `getStatusRaw()`
34+
35+
## Notes
36+
- Do NOT query a server on the main thread, use an AsyncTask instead
37+
- The info returned by the server can be easily faked, e.g someone doesn't want people to see his highly classified core plugin name, so he removes that info from the query so no one can see it!

src/mmm545/libgamespyquery/GameSpyQuery.php

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,74 +21,78 @@ class GameSpyQuery
2121
* @var string $ip
2222
* IP to query
2323
*/
24-
private $ip;
24+
private string $ip;
2525

2626
/**
2727
* @var int $port
2828
* Port to query
2929
*/
30-
private $port;
31-
32-
private $socket;
30+
private int $port;
3331

34-
private $statusRaw;
32+
/**
33+
* @var string $statusRaw
34+
* The raw status data
35+
*/
36+
private string $statusRaw;
3537

3638
/**
3739
* @param string $ip IP to query
3840
* @param int $port Port to query
3941
*/
40-
public function __construct(string $ip, int $port){
42+
public function __construct(string $ip, int $port, $statusRaw){
4143
$this->ip = $ip;
4244
$this->port = $port;
45+
$this->statusRaw = $statusRaw;
4346
}
4447

4548
/**
4649
* Queries the destination IP and port.
4750
* @param int $timeout The connection timeout.
4851
* @throws GameSpyQueryException If the destination IP and port cannot be queried.
4952
*/
50-
public function query(int $timeout = 2){
51-
$this->socket = @fsockopen('udp://'.$this->ip, $this->port, $errno, $errstr, $timeout);
53+
public static function query(string $ip, int $port, int $timeout = 2) : GameSpyQuery{
54+
$socket = @fsockopen('udp://'.$ip, $port, $errno, $errstr, $timeout);
5255

53-
if($errno and $this->socket !== false) {
54-
fclose($this->socket);
56+
if($errno and $socket !== false) {
57+
fclose($socket);
5558
throw new GameSpyQueryException($errstr, $errno);
5659
}
57-
elseif($this->socket === false) {
60+
elseif($socket === false) {
5861
throw new GameSpyQueryException($errstr, $errno);
5962
}
60-
stream_set_timeout($this->socket, $timeout);
63+
stream_set_timeout($socket, $timeout);
6164
$sessionId = rand();
6265

63-
$challengeToken = $this->handshake($sessionId);
66+
$challengeToken = self::handshake($socket, $sessionId);
6467

6568
if(empty($challengeToken)){
6669
throw new GameSpyQueryException("Failed to retrieve challenge token"); //tbh i think i'm abusing exceptions
6770
}
6871

69-
$this->statusRaw = $this->retrieveStatus($sessionId, (int)$challengeToken);
70-
if(empty($this->statusRaw)){
72+
$statusRaw = self::retrieveStatus($socket, $sessionId, (int)$challengeToken);
73+
if(empty($statusRaw)){
7174
throw new GameSpyQueryException("Failed to retrieve server info");
7275
}
76+
return new self($ip, $port, $statusRaw);
7377
}
7478

7579
/**
7680
* @param $sessionId
7781
* @return string|bool
7882
* @throws GameSpyQueryException
7983
*/
80-
private function handshake($sessionId){
84+
private static function handshake($socket, $sessionId){
8185
$command = pack("n", 65277);
8286
$command .= pack("c", 9);
8387
$command .= pack("N", $sessionId);
8488
$command .= pack("xxxx");
8589

8690
$length = strlen($command);
87-
if ($length !== fwrite($this->socket, $command, $length)){
91+
if ($length !== fwrite($socket, $command, $length)){
8892
throw new GameSpyQueryException("Failed to write to socket");
8993
}
9094

91-
$response = fread($this->socket, 4096);
95+
$response = fread($socket, 4096);
9296

9397
if($response === false){
9498
throw new GameSpyQueryException("Failed to read from socket");
@@ -103,19 +107,19 @@ private function handshake($sessionId){
103107
* @return string|bool
104108
* @throws GameSpyQueryException
105109
*/
106-
private function retrieveStatus(int $sessionId, int $challengeToken){
110+
private static function retrieveStatus($socket, int $sessionId, int $challengeToken){
107111
$command = pack("n", 65277);
108112
$command .= pack("c", 0);
109113
$command .= pack("N", $sessionId);
110114
$command .= pack("N", $challengeToken);
111115
$command .= pack("xxxx");
112116

113117
$length = strlen($command);
114-
if ($length !== fwrite($this->socket, $command, $length)){
118+
if ($length !== fwrite($socket, $command, $length)){
115119
throw new GameSpyQueryException("Failed to write to socket");
116120
}
117121

118-
$response = fread($this->socket, 4096);
122+
$response = fread($socket, 4096);
119123

120124
if($response === false){
121125
throw new GameSpyQueryException("Failed to read from socket");
@@ -125,7 +129,7 @@ private function retrieveStatus(int $sessionId, int $challengeToken){
125129
}
126130

127131
/**
128-
* Gets data by it's key
132+
* Gets data by its key
129133
* @param string $key The key of the data you want to get
130134
* @return string|string[]|bool The return can be either a string or an array, depending on what data you want to get. Returns false if the key can't be found
131135
*/
@@ -156,15 +160,15 @@ public function get(string $key){
156160
return $data[$pos + 1];
157161
}
158162
return false;
159-
163+
160164
}
161165
}
162166

163167
/**
164-
* @return string The raw status response from the server, or false if the status data is null
168+
* @return string The raw status response from the server
165169
*/
166-
public function getStatusRaw(){
167-
return isset($this->statusRaw) ? $this->statusRaw : false;
170+
public function getStatusRaw(): string{
171+
return $this->statusRaw;
168172
}
169173

170174
/**
@@ -174,24 +178,10 @@ public function getIp(): string{
174178
return $this->ip;
175179
}
176180

177-
/**
178-
* @param string $ip
179-
*/
180-
public function setIp(string $ip): void{
181-
$this->ip = $ip;
182-
}
183-
184181
/**
185182
* @return int
186183
*/
187184
public function getPort(): int{
188185
return $this->port;
189186
}
190-
191-
/**
192-
* @param int $port
193-
*/
194-
public function setPort(int $port): void{
195-
$this->port = $port;
196-
}
197187
}

virion.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: libgamespyquery
2-
version: 1.0.0
2+
version: 2.0.0
33
antigen: mmm545\libgamespyquery
4-
php: 7.0
4+
php: [8.0]
55
author: mmm545

0 commit comments

Comments
 (0)