-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathactions.php
145 lines (121 loc) · 4.03 KB
/
actions.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
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
<?php
class actions
{
public static function execute()
{
Header('X-Accel-Buffering: no');// nginx-1.5.6 及其以上版本支持
$config = App::getConfig();
self::checkRateLimit($config['site']['rateLimit']);
$commands = $config['site']['commands'];
$param = $_GET + $_POST;
$host = isset($param['host']) ? $param['host'] : '';
$cmd = isset($param['cmd']) ? $param['cmd'] : '';
if (stripos($host, 'localhost') !== FALSE)
{
echo "<script>parent.alert('请输入正确的IP地址或域名');</script>";
echo '<script>parent.req_complete()</script>';
exit;
}
$ip = gethostbyname($host);
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE || $ip == '127.0.0.1')
{
echo "<script>parent.alert('请输入正确的IP地址或域名');</script>";
echo '<script>parent.req_complete()</script>';
exit;
}
if (isset($commands[$cmd]))
{
call_user_func(array(__CLASS__, $cmd), $ip, $commands[$cmd]);
echo '<script>parent.req_complete()</script>';
}
else
{
echo "<script>parent.alert('无效命令');</script>";
echo '<script>parent.req_complete()</script>';
exit;
}
}
private static function checkRateLimit($rateLimit)
{
if (isset($rateLimit['enable']) && $rateLimit['enable'])
{
$option = [
'ip' => $_SERVER['REMOTE_ADDR'],
'ua' => $_SERVER['HTTP_USER_AGENT']
];
$rateLimitClass = 'ratelimit\\' . $rateLimit['provider']['class'];
if (empty($rateLimit['minute']))
{
$rateLimit['minute'] = 100; //每分钟限制100次
}
/**
* @var RateLimitInterface $rateLimitInterface
*/
$rateLimitInterface = new $rateLimitClass($option);
if ($rateLimitInterface->allow($rateLimit['minute']) === FALSE)
{
exit('<script>parent.alert("操作太频繁了")</script>');
}
}
}
protected static function host($host, $cmd)
{
$args = [
'cmd' => $cmd,
'ip' => $host,
];
echo '<script>parent.update_start()</script>';
ob_flush();
flush();
$response = command\host::execute($args);
$response = trim($response);
echo "<script>parent.update_view('{$response}')</script>";
ob_flush();
flush();
}
protected static function ping($host, $cmd)
{
$args = [
'cmd' => $cmd,
'ip' => $host,
];
echo '<script>parent.update_start()</script>';
ob_flush();
flush();
command\ping::execute($args, function($line){
echo '<script>parent.update_view("' . trim($line) . '")</script>';
ob_flush();
flush();
});
}
protected static function traceroute($host, $cmd)
{
$args = [
'cmd' => $cmd,
'ip' => $host,
];
echo '<script>parent.update_start()</script>';
ob_flush();
flush();
command\traceroute::execute($args, function($line, $json) {
echo '<script>parent.update_view("' . trim($line) . '", ' . json_encode($json) . ')</script>';
ob_flush();
flush();
});
}
protected static function ping6($host, $cmd)
{
$args = [
'cmd' => $cmd,
'ip' => $host,
];
echo '<script>parent.update_start()</script>';
ob_flush();
flush();
command\ping::execute($args, function($line){
echo '<script>parent.update_view("' . trim($line) . '")</script>';
ob_flush();
flush();
});
}
}