简体   繁体   English

无法正常工作 ip 校验码(单个规则有效,多个无效)

[英]Can't get working ip check code(single rule is working, multiple not)

need to forward all Tor users away from my page, with checking ip in tor lists.需要将所有 Tor 用户从我的页面转发出去,并在 Tor 列表中检查 ip。 Single check was working with ipv4 but not working with ipv6 and multiple list checking.单项检查适用于 ipv4,但不适用于 ipv6 和多个列表检查。 Can't understand where i get error.无法理解我在哪里得到错误。 code:代码:

$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/'); 
}

was trying different way's like尝试不同的方式就像

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

and if... elseif.. elseif如果... elseif.. elseif

same can get inside via tor with ip that is in list and can't understand where is the problem.同样可以通过 Tor 进入列表中的 ip 并且无法理解问题出在哪里。 Thank You to All for help.谢谢大家的帮助。

UDP: code part 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();  
}

is working 100% - question - how to add other list in checking in the right way?工作 100% - 问题 - 如何以正确的方式添加其他列表?

A few things here...这里有几件事...

  1. I hope you aren't downloading those lists every time someone visits your page.我希望您不会在每次有人访问您的页面时都下载这些列表。 You should be caching the results of the list downloads for a short time rather than constantly downloading.您应该在短时间内缓存列表下载的结果,而不是不断下载。

  2. The only fissionrelays list you need is exits.txt .您需要的唯一裂变继电器列表是exits.txt As outlined at https://fissionrelays.net/lists , exits.txt contains IPv4 & IPv6 exit nodes.https://fissionrelays.net/lists 所述,exits.txt 包含 IPv4 和 IPv6 出口节点。 Download that instead of exits-ipv6.txt and relays-ipv6.txt.下载那个而不是 exits-ipv6.txt 和 relays-ipv6.txt。

  3. It is incorrect to block Tor relays that are not exits.阻止未退出的 Tor 中继是不正确的。 Hits from a relay IP is not Tor traffic.来自中继 IP的命中不是Tor 流量。 For example, I run a guard relay at home that does not allow exit traffic.例如,我在家里运行一个不允许出口流量的警卫中继。 Its IP appears in the relay list, but it does not permit any Tor exit traffic so any hits from this IP is not coming from Tor.它的 IP 出现在中继列表中,但它不允许任何 Tor 出口流量,因此来自此 IP 的任何命中都不是来自 Tor。

If you want to use multiple lists, that's fine.如果你想使用多个列表,那很好。 I would suggest the following steps to meet your needs:我建议以下步骤来满足您的需求:

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.

For 1-3, you could use https://gitlab.com/fissionrelays/lists/-/blob/master/tor.php which is a downloader provided by fission relays to download their lists.对于 1-3,您可以使用https://gitlab.com/fissionrelays/lists/-/blob/master/tor.php这是裂变继电器提供的下载器来下载其列表。 It sorts as well.它也排序。

IF your lists are sorted correctly, you can binary search the list for better results, but this is more advanced and not necessary.如果您的列表排序正确,您可以对列表进行二分搜索以获得更好的结果,但这更高级且没有必要。

Hint, when downloading lists, don't use file() to download as arrays.提示,下载列表时,不要使用file()下载为 arrays。 Use file_get_contents() instead, and append each list onto the other.改用file_get_contents() ,并将 append 每个列表放在另一个列表上。 Once all lists are downloaded and combined, process them into an array (skipping dupes), and then sort the list.下载并组合所有列表后,将它们处理成一个数组(跳过重复项),然后对列表进行排序。

Here's a binary search function you can use to search the sorted list quicker.这是一个二进制搜索 function ,您可以使用它来更快地搜索排序列表。

/**
 * 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;
    }
}

Additionally, make sure to call exit() after sending a header(Location) redirect since you want to terminate the request and redirect immediately without running additional PHP code.此外,请确保在发送标头(位置)重定向后调用exit() ,因为您想终止请求并立即重定向而不运行额外的 PHP 代码。

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

Lastly, if you want to assume all Tor traffic is "bad" that's fine.最后,如果您想假设所有 Tor 流量都是“坏的”,那很好。 Consider an error message instead of silently redirecting traffic away without explanation which is bad user-experience.考虑一条错误消息,而不是在没有解释的情况下默默地重定向流量,这是不好的用户体验。 Many people use Tor for casual browsing, so you're effectively booting these users from your site with no reason given.许多人使用 Tor 进行随意浏览,因此您实际上是在没有给出任何理由的情况下从您的站点引导这些用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM