gpt4 book ai didi

php - 提高我的 ip 黑名单-白名单脚本的效率

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:41:19 25 4
gpt4 key购买 nike

我的脚本会打开两个文件:whitelist.txt 和 blacklist.txt,其中包含 IP 地址。

我想将 blacklist.txt 中不存在于 whitelist.txt 中的所有 ip 实例添加到变量中。

此脚本最多包含 2 个通配符。

它现在运行 37 分钟,我希望它能更快。

$blacklist = file_get_contents("blacklist.txt");
$whitelist = file_get_contents("whitelist.txt");

$black_ips = explode("\n", $blacklist);
$white_ips = explode("\n", $whitelist);

$wildcard = array();
for($i = 0; $i < 256; $i++) {
$wildcard[] = $i;
}

foreach($black_ips as $bkey => $black) {
if(stristr($black, ".")) {

foreach ($white_ips as $wkey => $white) {
$count = substr_count($white, '*');

if($count) {
switch($count){
case 1:
foreach ($wildcard as $i) {
if(substr($white, 0, strlen($white) - 1) . $i == $black){
continue 4;
}
}
break;
case 2:
foreach ($wildcard as $i) {
foreach ($wildcard as $k) {
if(substr($white, 0, strlen($white) - 3) . $i . '.' . $k == $black){
continue 5;
}
}
}
break;
}
}
else if($black == $white) {
continue 2;
}
}
$nginxdeny .= "deny " . $black . ";\n";
}
}

最佳答案

这段代码是否满足您的需求?

$white = array(
'192.168.*.*',
'10.10.10.*',
);

$black = array(
'192.168.8.8',
'10.10.10.3',
'10.10.1.2',
);

$patterns = array();
foreach ($white as $subnetwork) {
$patterns[] = str_replace(array('.', '*'), array('\\.', '(\d{1,3})'), $subnetwork);
}

$notMatched = array();
foreach ($black as $ip) {
foreach ($patterns as $pattern) {
if (preg_match("/^{$pattern}$/", $ip)) {
continue 2;
}
}
$notMatched[] = $ip;
}

var_dump($notMatched);

输出:

array(1) {
[0]=>
string(9) "10.10.1.2"
}

关于php - 提高我的 ip 黑名单-白名单脚本的效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26948977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com