Skip to content

Commit 31f094e

Browse files
committed
revert changes
1 parent 4b4dcc6 commit 31f094e

3 files changed

Lines changed: 56 additions & 48 deletions

File tree

README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
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-
To query a server, you call the `GameSpyQuery::query()` function, first argument is the IP, second argument is the port
5+
First you create a new GameSpyQuery instance, first argument is the IP address to query, second argument is the port to query
66
```php
7-
$query = GameSpyQuery::query("someserver.org", 19132);
7+
$query = new GameSpyQuery("someserver.org", 19132);
88
```
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
9+
Then we query the server
1010
```php
11-
$query = GameSpyQuery::query("someserver.org", 19132, 5); // it will wait for 5 seconds, if there was no response it would close the connection
11+
$query->query();
1212
```
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:
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:
1820
```php
1921
$query->get("hostname"); // Server MOTD
20-
$query->get("gametype"); // Game type, e.g SMP or CMP
21-
$query->get("game_id"); // Game edition
22+
$query->get("gametype"); // Game type, not sure what that means
23+
$query->get("game_id"); // I think that's the game edition
2224
$query->get("version"); // Version of minecraft the server is running on
2325
$query->get("server_engine"); // Server software being used
2426
$query->get("plugins"); // Plugins list with their version
@@ -28,10 +30,6 @@ $query->get("maxplayers"); // Max number of players
2830
$query->get("whitelist"); // On if whitelist is turned on, otherwise off
2931
$query->get("hostip"); // Host ip
3032
$query->get("hostport"); // Host port
31-
$query->get("players"); // List of online players on the server
33+
$query->get("players"); // List of online players names
3234
```
3335
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: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,78 +21,74 @@ class GameSpyQuery
2121
* @var string $ip
2222
* IP to query
2323
*/
24-
private string $ip;
24+
private $ip;
2525

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

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

3836
/**
3937
* @param string $ip IP to query
4038
* @param int $port Port to query
4139
*/
42-
public function __construct(string $ip, int $port, $statusRaw){
40+
public function __construct(string $ip, int $port){
4341
$this->ip = $ip;
4442
$this->port = $port;
45-
$this->statusRaw = $statusRaw;
4643
}
4744

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

56-
if($errno and $socket !== false) {
57-
fclose($socket);
53+
if($errno and $this->socket !== false) {
54+
fclose($this->socket);
5855
throw new GameSpyQueryException($errstr, $errno);
5956
}
60-
elseif($socket === false) {
57+
elseif($this->socket === false) {
6158
throw new GameSpyQueryException($errstr, $errno);
6259
}
63-
stream_set_timeout($socket, $timeout);
60+
stream_set_timeout($this->socket, $timeout);
6461
$sessionId = rand();
6562

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

6865
if(empty($challengeToken)){
6966
throw new GameSpyQueryException("Failed to retrieve challenge token"); //tbh i think i'm abusing exceptions
7067
}
7168

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

7975
/**
8076
* @param $sessionId
8177
* @return string|bool
8278
* @throws GameSpyQueryException
8379
*/
84-
private static function handshake($socket, $sessionId){
80+
private function handshake($sessionId){
8581
$command = pack("n", 65277);
8682
$command .= pack("c", 9);
8783
$command .= pack("N", $sessionId);
8884
$command .= pack("xxxx");
8985

9086
$length = strlen($command);
91-
if ($length !== fwrite($socket, $command, $length)){
87+
if ($length !== fwrite($this->socket, $command, $length)){
9288
throw new GameSpyQueryException("Failed to write to socket");
9389
}
9490

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

9793
if($response === false){
9894
throw new GameSpyQueryException("Failed to read from socket");
@@ -107,19 +103,19 @@ private static function handshake($socket, $sessionId){
107103
* @return string|bool
108104
* @throws GameSpyQueryException
109105
*/
110-
private static function retrieveStatus($socket, int $sessionId, int $challengeToken){
106+
private function retrieveStatus(int $sessionId, int $challengeToken){
111107
$command = pack("n", 65277);
112108
$command .= pack("c", 0);
113109
$command .= pack("N", $sessionId);
114110
$command .= pack("N", $challengeToken);
115111
$command .= pack("xxxx");
116112

117113
$length = strlen($command);
118-
if ($length !== fwrite($socket, $command, $length)){
114+
if ($length !== fwrite($this->socket, $command, $length)){
119115
throw new GameSpyQueryException("Failed to write to socket");
120116
}
121117

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

124120
if($response === false){
125121
throw new GameSpyQueryException("Failed to read from socket");
@@ -129,7 +125,7 @@ private static function retrieveStatus($socket, int $sessionId, int $challengeTo
129125
}
130126

131127
/**
132-
* Gets data by its key
128+
* Gets data by it's key
133129
* @param string $key The key of the data you want to get
134130
* @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
135131
*/
@@ -160,15 +156,15 @@ public function get(string $key){
160156
return $data[$pos + 1];
161157
}
162158
return false;
163-
159+
164160
}
165161
}
166162

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

174170
/**
@@ -178,10 +174,24 @@ public function getIp(): string{
178174
return $this->ip;
179175
}
180176

177+
/**
178+
* @param string $ip
179+
*/
180+
public function setIp(string $ip): void{
181+
$this->ip = $ip;
182+
}
183+
181184
/**
182185
* @return int
183186
*/
184187
public function getPort(): int{
185188
return $this->port;
186189
}
190+
191+
/**
192+
* @param int $port
193+
*/
194+
public function setPort(int $port): void{
195+
$this->port = $port;
196+
}
187197
}

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: 2.0.0
2+
version: 1.0.0
33
antigen: mmm545\libgamespyquery
4-
php: [8.0]
4+
php: 7.0
55
author: mmm545

0 commit comments

Comments
 (0)