Repository navigation
Expand file tree
/
Copy pathBlacklist.php
More file actions
executable file
·209 lines (189 loc) · 7.44 KB
/
Copy pathBlacklist.php
File metadata and controls
executable file
·209 lines (189 loc) · 7.44 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
200
201
202
203
204
205
206
207
208
209
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
class RecentViewsCounter_Blacklist
{
private static function isValidIpOrCidr($ip)
{
if (filter_var($ip, FILTER_VALIDATE_IP)) return true;
if (preg_match('#^(\d{1,3}\.){3}\d{1,3}/(\d{1,2})$#', $ip, $m)) {
$parts = explode('/', $ip);
$mask = intval($parts[1]);
return $mask >= 0 && $mask <= 32 && filter_var($parts[0], FILTER_VALIDATE_IP);
}
return false;
}
private static function ipInCidr($ip, $cidr)
{
$parts = explode('/', $cidr);
$network = ip2long($parts[0]);
$ip_long = ip2long($ip);
if ($network === false || $ip_long === false) return false;
$mask = -1 << (32 - intval($parts[1]));
return ($ip_long & $mask) === ($network & $mask);
}
/**
* 解析白名单配置(逗号/分号/空白分隔)
*/
public static function parseWhitelist($raw)
{
$list = array();
if (!is_string($raw) || trim($raw) === '') return $list;
foreach (preg_split('/[\s,;]+/', $raw) as $item) {
$item = trim($item);
if ($item !== '') $list[] = $item;
}
return $list;
}
/**
* IP 是否命中白名单:精确 IP、IPv4 CIDR、IPv6 CIDR/前缀;127.0.0.1 与 ::1 恒放行
*/
public static function isWhitelisted($ip, $raw = null)
{
if (empty($ip)) return false;
if ($ip === '127.0.0.1' || $ip === '::1') return true;
foreach (self::parseWhitelist($raw) as $rule) {
if ($rule === $ip) return true;
if (strpos($rule, '/') !== false) {
if (strpos($rule, ':') !== false) {
if (self::ipv6InCidr($ip, $rule)) return true;
} elseif (self::ipInCidr($ip, $rule)) {
return true;
}
} elseif (strpos($rule, ':') !== false) {
if (strpos($ip, $rule) === 0) return true; // IPv6 前缀写法,如 240e:b8f:96a:
}
}
return false;
}
private static function ipv6InCidr($ip, $cidr)
{
$parts = explode('/', $cidr);
if (count($parts) !== 2) return false;
$binIp = @inet_pton($ip);
$binNet = @inet_pton($parts[0]);
if ($binIp === false || $binNet === false || strlen($binIp) !== strlen($binNet)) return false;
$bits = intval($parts[1]);
if ($bits < 0 || $bits > strlen($binNet) * 8) return false;
$bytes = intdiv($bits, 8);
$rest = $bits % 8;
if ($bytes > 0 && substr($binIp, 0, $bytes) !== substr($binNet, 0, $bytes)) return false;
if ($rest > 0) {
$mask = (0xFF << (8 - $rest)) & 0xFF;
if ((ord($binIp[$bytes]) & $mask) !== (ord($binNet[$bytes]) & $mask)) return false;
}
return true;
}
public static function createTable()
{
try {
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$driver = $db->getAdapterName();
$isSqlite = stripos($driver, 'sqlite') !== false;
$auto_inc = $isSqlite ? 'AUTOINCREMENT' : 'AUTO_INCREMENT';
$sql = "CREATE TABLE IF NOT EXISTS `{$prefix}ip_blacklist` (
`id` INTEGER NOT NULL PRIMARY KEY {$auto_inc},
`ip` TEXT NOT NULL,
`reason` TEXT,
`created_time` INTEGER NOT NULL
)";
if (!$isSqlite) {
$sql .= " ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
}
$db->query($sql);
try {
$unique_sql = $isSqlite
? "CREATE UNIQUE INDEX IF NOT EXISTS `idx_ip_blacklist_ip` ON `{$prefix}ip_blacklist` (`ip`)"
: "ALTER TABLE `{$prefix}ip_blacklist` ADD UNIQUE INDEX `idx_ip_blacklist_ip` (`ip`)";
$db->query($unique_sql);
} catch (Exception $e) {
error_log('RecentViewsCounter_Blacklist: 唯一索引创建失败,可能存在重复IP: ' . $e->getMessage());
}
} catch (Exception $e) {
error_log('RecentViewsCounter_Blacklist::createTable error: ' . $e->getMessage());
throw $e;
}
}
public static function add($ip, $reason = '')
{
if (empty($ip) || !self::isValidIpOrCidr($ip)) return false;
try {
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$exists = $db->fetchRow($db->select('id')->from($prefix . 'ip_blacklist')->where('ip = ?', $ip));
if ($exists) return false;
$db->query($db->insert($prefix . 'ip_blacklist')->rows(array(
'ip' => $ip,
'reason' => $reason,
'created_time' => time()
)));
return true;
} catch (Exception $e) {
error_log('RecentViewsCounter_Blacklist::add error: ' . $e->getMessage());
return false;
}
}
public static function remove($ip)
{
if (empty($ip)) return false;
try {
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$db->query($db->delete($prefix . 'ip_blacklist')->where('ip = ?', $ip));
return true;
} catch (Exception $e) {
error_log('RecentViewsCounter_Blacklist::remove error: ' . $e->getMessage());
return false;
}
}
public static function isBlacklisted($ip)
{
if (empty($ip)) return false;
try {
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$row = $db->fetchRow($db->select('id')->from($prefix . 'ip_blacklist')->where('ip = ?', $ip));
if (!empty($row)) return true;
$cidrs = $db->fetchAll($db->select('ip')->from($prefix . 'ip_blacklist')->where('ip LIKE ?', '%/%'));
foreach ($cidrs as $cidr) {
if (self::ipInCidr($ip, $cidr['ip'])) return true;
}
return false;
} catch (Exception $e) {
error_log('RecentViewsCounter_Blacklist::isBlacklisted error: ' . $e->getMessage());
return false;
}
}
public static function getList($page = 1, $limit = 20)
{
try {
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$offset = ($page - 1) * $limit;
$total = $db->fetchRow($db->select('COUNT(*) as count')->from($prefix . 'ip_blacklist'));
$totalCount = $total ? intval($total['count']) : 0;
$data = $db->fetchAll($db->select('*')
->from($prefix . 'ip_blacklist')
->order('created_time', Typecho_Db::SORT_DESC)
->limit($limit)
->offset($offset));
if (!$data) $data = array();
return array('data' => $data, 'total' => $totalCount);
} catch (Exception $e) {
error_log('RecentViewsCounter_Blacklist::getList error: ' . $e->getMessage());
return array('data' => array(), 'total' => 0);
}
}
public static function getCount()
{
try {
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$row = $db->fetchRow($db->select('COUNT(*) as count')->from($prefix . 'ip_blacklist'));
return $row ? intval($row['count']) : 0;
} catch (Exception $e) {
error_log('RecentViewsCounter_Blacklist::getCount error: ' . $e->getMessage());
return 0;
}
}
}