This repository was archived by the owner on Mar 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.php
More file actions
199 lines (176 loc) · 5.06 KB
/
index.php
File metadata and controls
199 lines (176 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace Kers;
use Kers\Utils;
use xPaw\MinecraftPing;
use xPaw\MinecraftPingException;
require __DIR__ . '/utils.class.php';
$Utils = new Utils();
header("Access-Control-Allow-Origin: *");
header('Content-type: application/json');
error_reporting(0);
$api_version = 'v0.5.76';
// 获取当前运行脚本的服务器的IP地址
if ($_REQUEST['get_nodeip']) {
$api_nodeIP = $_SERVER['SERVER_ADDR'];
} else {
$api_nodeIP = 'N/A';
}
$array = [
'code' => 201,
'api_version' => $api_version,
'status' => 'offine',
'ip' => 'N/A',
// 'real' => 'N/A',
// 'location' => 'N/A',
'node_ip' => $api_nodeIP,
'port' => 'N/A',
'favicon' => 'N/A',
'motd' => 'N/A',
'agreement' => 'N/A',
'version' => 'N/A',
'online' => 0,
'max' => 0,
'gamemode' => 'N/A',
'delay' => 'N/A',
'client' => 'N/A'
];
if (!$Utils->hasEmpty($_REQUEST['ip'], $_REQUEST['port'])) {
$ip = $_REQUEST['ip'];
$port = $_REQUEST['port'];
if (!isset($_REQUEST['java'])) {
// 服务器IP入口
define('MQ_SERVER_ADDR', $_REQUEST['ip'] );
define('MQ_SERVER_PORT', $_REQUEST['port']);
define('MQ_TIMEOUT', 1);
// 将所有内容显示在浏览器中,因为有些人无法查看日志以查找错误。
// error_reporting( E_ALL | E_STRICT );
// ini_set( 'display_errors', '1' );
require __DIR__ . '/src/MinecraftPing.php';
require __DIR__ . '/src/MinecraftPingException.php';
$Timer = microtime( true );
$Info = false;
$Query = null;
try
{
$Query = new MinecraftPing( MQ_SERVER_ADDR, MQ_SERVER_PORT, MQ_TIMEOUT );
$Info = $Query->Query( );
if( $Info === false )
{
/*
* 如果这个服务器版本低于1.7,我们可以尝试使用较旧的协议重新查询它
* 这个函数返回的数据格式可能与1.7版本的输出格式不同,如果你想匹配1.7的输出,你需要自己手动映射这些内容
* 如果你确定这个服务器使用的是较旧的版本,那么你可以直接调用QueryOldPre17,避免调用Query()和重新连接部分
*/
$Query->Close( );
$Query->Connect( );
$Info = $Query->QueryOldPre17( );
}
} catch (MinecraftPingException $e) {
$array['code'] = 201;
$Exception = $e;
}
if( $Query !== null )
{
$Query->Close( );
}
$Timer = number_format( microtime(true) - $Timer, 4, '.', '');
// JAVA数据返回
if (isset($Exception)) {
$array = [
'code' => 201,
'api_version' => $api_version,
'status' => 'offine',
'ip' => 'N/A',
// 'real' => 'N/A',
// 'location' => 'N/A',
'node_ip' => $api_nodeIP,
'port' => 'N/A',
'favicon' => 'N/A',
'motd' => 'N/A',
'agreement' => 'N/A',
'version' => 'N/A',
'online' => 0,
'max' => 0,
'gamemode' => 'N/A',
'delay' => 'N/A',
'client' => 'N/A'
];
} else {
// 获取所有已被分割的motd的'text'键的值并将这些值拼接成一个字符串
if (isset($Info['description']['extra']) && is_array($Info['description']['extra'])) {
$textValues = array_column($Info['description']['extra'], 'text');
} else {
$textValues = $Info['description'];
};
if (isset($Info['favicon']) && ($_REQUEST['get_favicon'] ?? false)) {
$favicon = $Info['favicon'];
} else {
$favicon = 'N/A';
}
$concatenatedText = implode($textValues);
$real = gethostbyname($ip);
$array = [
'code' => 200,
'api_version' => $api_version,
'status' => 'online',
'ip' => $ip,
// 'real' => $real,
// 'location' => $Utils->getLocation($real),
'node_ip' => $api_nodeIP,
'port' => $port,
'favicon' => $favicon,
'motd' => $concatenatedText,
'agreement' => $Info['version']['protocol'],
'version' => $Info['version']['name'],
'online' => $Info['players']['online'],
'max' => $Info['players']['max'],
'gamemode' => 'N/A',
'delay' => round($Timer, 3) * 1000,
'client' => 'JE'
];
}
} else {
// 基岩版查询逻辑
$t1 = microtime(true);
if ($handle = stream_socket_client("udp://{$ip}:{$port}", $errno, $errstr, 2)) {
stream_set_timeout($handle, 2);
fwrite($handle, hex2bin('0100000000240D12D300FFFF00FEFEFEFEFDFDFDFD12345678') . "\n");
$result = strstr(fread($handle, 1024), "MCPE");
fclose($handle);
$data = explode(";", $result);
$data['1'] = preg_replace("/§[a-z A-Z 0-9]{1}/s", '', $data['1']);
if (!$Utils->hasEmpty($data, $data['1'])) {
$t2 = microtime(true);
$real = gethostbyname($ip);
$array = [
'code' => 200,
'api_version' => $api_version,
'status' => 'online',
'ip' => $ip,
// 'real' => $real,
// 'location' => $Utils->getLocation($real),
'node_ip' => $api_nodeIP,
'port' => $port,
'favicon' => 'N/A',
'motd' => $data['1'],
'agreement' => $data['2'],
'version' => $data['3'],
'online' => $data['4'],
'max' => $data['5'],
'gamemode' => $data['8'],
'delay' => round($t2 - $t1, 3) * 1000,
'client' => 'BE'
];
} else {
$array['code'] = 203;
}
} else {
$array['code'] = 202;
}
}
} else {
$array['code'] = 201;
}
// 通过JSON格式返回
exit(json_encode($array, JSON_UNESCAPED_UNICODE));
?>