【问题标题】:Can't get working ip check code(single rule is working, multiple not)无法获得有效的 ip 检查代码(单个规则有效,多个无效)
【发布时间】:2021-12-27 08:45:16
【问题描述】:

需要将所有 Tor 用户从我的页面转发出去,并在 Tor 列表中检查 ip。 单项检查适用于 ipv4,但不适用于 ipv6 和多个列表检查。 无法理解我在哪里得到错误。 代码:

$torip = file("https://check.torproject.org/torbulkexitlist", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$torexits = file("https://lists.fissionrelays.net/tor/exits-ipv6.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$tornode = file("https://lists.fissionrelays.net/tor/relays-ipv6.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$client_ip = $_SERVER['REMOTE_ADDR'];
if (in_array($client_ip, $torip)){ 
header('Location: https://www.google.com/'); 
}
if (in_array($client_ip, $tornode)){
header('Location: https://www.google.com/'); 
}
if (in_array($client_ip, $torexits)){
header('Location: https://www.google.com/'); 
}

正在尝试不同的方式

if(in_array($client_ip, $torip) or in_array($client_ip, $tornode) or in_array($client_ip, $torexits))

如果 ... elseif .. elseif

same 可以通过 tor 进入列表中的 ip 并且无法理解问题出在哪里。 感谢大家的帮助。

UDP: 代码部分

$tornode = file("https://lists.fissionrelays.net/tor/relays-ipv6.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$client_ip = $_SERVER['REMOTE_ADDR'];
if (in_array($client_ip, $tornode)){
header('Location: https://www.google.com/');
die();  
}

工作 100% - 问题 - 如何以正确的方式添加其他列表?

【问题讨论】:

  • $torip $tornode $torexits 的结果是什么?是数组吗?如果它的数组为什么不循环遍历它并在标题位置之前进行验证
  • @AtoliGaming - file() 将始终返回一个数组,其中每一行作为一个元素(如果发生错误,则返回 false)。为什么他们需要遍历它们?他们正在使用in_array() 来检查客户端的IP 是否在每个数组中。
  • 对不起,我弄错了你的目标,所以你只需要检查数组是否存在。 ipv6 和多列表检查的结果是什么?
  • 我们需要将所有的 Tor 用户(从列表中获得 ip 的用户)转发离开我们的网页
  • 当您测试代码时,client-ip 实际上是否在这些 IP 地址列表中?也就是说,您的代码是否正常工作,也许列表不正确?

标签: php arrays tor


【解决方案1】:

这里有几件事...

  1. 我希望您不会在每次有人访问您的页面时都下载这些列表。您应该在短时间内缓存列表下载的结果,而不是不断下载。

  2. 您需要的唯一裂变继电器列表是exits.txt。如https://fissionrelays.net/lists 所述,exits.txt 包含 IPv4 和 IPv6 出口节点。下载那个而不是 exits-ipv6.txt 和 relays-ipv6.txt。

  3. 阻止未退出的 Tor 中继是不正确的。来自中继 IP 的命中不是 Tor 流量。例如,我在家里运行一个不允许出口流量的警卫中继。它的 IP 出现在中继列表中,但它不允许任何 Tor 出口流量,因此来自该 IP 的任何命中都不是来自 Tor。

如果您想使用多个列表,那很好。我建议以下步骤来满足您的需求:

1. Download & combine all lists every 10+ minutes
2. De-duplicate and sort the list
3. Save to a file
4. Write a function to search the cached file.

对于1-3,您可以使用https://gitlab.com/fissionrelays/lists/-/blob/master/tor.php,这是裂变中继提供的下载器来下载他们的列表。它也可以排序。

如果您的列表排序正确,您可以对列表进行二分搜索以获得更好的结果,但这更高级且没有必要。

提示,下载列表时,不要使用file()作为数组下载。请改用file_get_contents(),并将每个列表附加到另一个列表上。下载并合并所有列表后,将它们处理成一个数组(跳过重复项),然后对列表进行排序。

这是一个二进制搜索功能,您可以使用它来更快地搜索排序列表。

/**
 * Perform binary search of a sorted array.
 * Credit: http://php.net/manual/en/function.array-search.php#39115
 *
 * Tested by [VigilanTor](https://wordpress.org/plugins/vigilantor/) for accuracy and efficiency
 *
 * @param string $needle String to search for
 * @param array $haystack Array to search within
 * @return boolean|number false if not found, or index if found
 */
protected function arrayBinarySearch($needle, $haystack)
{
    $high = count($haystack);
    $low = 0;

    while ($high - $low > 1){
        $probe = ($high + $low) / 2;
        if ($haystack[$probe] < $needle){
            $low = $probe;
        } else{
            $high = $probe;
        }
    }

    if ($high == count($haystack) || $haystack[$high] != $needle) {
        return false;
    } else {
        return $high;
    }
}

此外,请确保在发送标头(位置)重定向后调用exit(),因为您想终止请求并立即重定向而不运行额外的 PHP 代码。

if (in_array($needle, $haystack)) {
    header('Location: https://www.google.com/');
    exit; // Redirect now and don't run any further PHP code
}

最后,如果您想假设所有 Tor 流量都是“坏的”,那很好。考虑一条错误消息,而不是在没有解释的情况下默默地重定向流量,这是不好的用户体验。许多人使用 Tor 进行随意浏览,因此您实际上是在没有给出任何理由的情况下从您的网站引导这些用户。

【讨论】:

  • 非常感谢您的回答。并单独感谢您的解释。
  • 非常欢迎!祝你好运,如果您对此有更多疑问,请随时发表评论。感谢您接受答案。
猜你喜欢
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 2018-08-29
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多