-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_status_code.php
65 lines (55 loc) · 2.63 KB
/
url_status_code.php
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
<?php
class StatusCode {
private string $update_url = 'https://api.github.com/repos/DUQIA/Akylor/releases';
private string $current_version = 'v0.0.02';
// 获取状态码,并判断
private function code(string $command): array|false
{
// 使用 escapeshellcmd 和 escapeshellarg 进行命令安全处理
$command = escapeshellcmd($command);
// 使用 exec 获取输出和返回码
exec($command, $output, $return_var);
return ($return_var === 0) ? $output : false;
}
// 获取更新信息
public function getUpdate(): string
{
$command = 'curl -s -H "User-Agent: Mozilla/5.0 (compatible; PHP script)" ' . escapeshellarg($this->update_url);
$output = $this->code($command);
$versions_data = ($output === false || !isset($output[0])) ? array($this->current_version, $this->current_version) : array($output, $this->current_version);
$version_data = json_decode($versions_data[0][0], true);
// 获取更新
if (empty($version_data[0]['name'])) {
return $versions_data[1] .'<a href="https://github.com/DUQIA/Akylor/releases" alt="update" target="_blank" style="cursor: pointer; color: red;"><strong>获取失败</strong></a>';
} elseif ($version_data[0]['name'] !== $versions_data[1]) {
$htmlUrl = htmlspecialchars($version_data[0]['html_url'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
return $versions_data[1] .'<a href="' . $htmlUrl . '" alt="update" target="_blank" style="cursor: pointer; color: blue;"><strong>获取更新</strong></a>';
} else {
return $versions_data[1];
}
}
// whois 获取IP信息
public function getIp(string $ip): string
{
if ($ip === 'invalid Ip') {
return 'Unknown';
}
$command = 'timeout 1 whois ' . escapeshellarg($ip) . ' | grep -i country';
$output = $this->code($command);
if ($output !== false && isset($output[0])) {
$pattern = '/country:\s+(\w+)/';
preg_match($pattern, $output[0], $matches);
// 国家代码转换
$filePath = __DIR__ . '/js/names.json';
if (!file_exists($filePath)) {
die('File does not exist');
}
$compatable = file_get_contents($filePath);
$country = json_decode($compatable, true);
foreach ($country as $key => $value) {
return (!isset($matches) && $key === $matches[1]) ? $value : 'Unknown';
}
}
return 'Unknown';
}
}