Completed
Push — master ( 90cf98...92b59c )
by H
02:10
created

IpRangeVoter::setDefaultTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Hongliang\Defender\Voter;
4
5
use Hongliang\Defender\Utility\IpTools;
6
7
class IpRangeVoter extends BaseVoter implements VoterInterface
8
{
9
    public function vote($ip = null)
10
    {
11
        $ip = $ip ?: $this->target;
12
        if (!$ip) {
13
            return false;
14
        }
15
        // convert ip to number
16
        $long = ip2long($ip);
17
        // get bad ip ranges
18
        $ips = $this->getAssets();
19
        foreach ($ips as $ipRange) {
20
            if ($long <= ip2long($ipRange[1]) && $long >= ip2long($ipRange[0])) {
21
                return true;
22
            }
23
        }
24
25
        return false;
26
    }
27
28
    protected function setDefaultTarget()
29
    {
30
        $this->target = IpTools::getClientIp();
31
32
        return $this;
33
    }
34
35
    protected function getDefaultAssets()
36
    {
37
        return [
38
            ['59.62.0.0', '59.63.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
39
            ['59.172.0.0', '59.173.255.255'], // CHINANET Hubei province network
40
            ['62.210.0.0', '62.210.255.255'], // IE-POOL-BUSINESS-HOSTING Franch
41
            ['86.123.0.0', '86.123.255.255'], // RDS Residential ROMANIA
42
            ['89.122.0.0', '89.122.255.255'], // Romtelecom Data Network Romania
43
            ['106.4.0.0', '106.7.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
44
            ['115.148.0.0', '115.254.255.255'], // many attempts, just block them all
45
            ['117.21.0.0', '117.21.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
46
            ['117.41.0.0', '117.41.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
47
            ['148.251.0.0', '148.251.255.255'], // Hetzner Online GmbH
48
            ['178.239.165.0', '178.239.165.255'], // COBALT-NETWORKS English
49
            ['182.96.0.0', '182.111.255.255'], // CHINANET JIANGXI PROVINCE NETWORK
50
            ['218.64.0.0', '218.65.127.255'], // CHINANET JIANGXI PROVINCE NETWORK
51
            ['202.75.32.0', '202.75.63.254'], // TM VADS DC Hosting
52
            ['222.208.0.0', '222.215.255.255'], // CHINANET Sichuan province network
53
        ];
54
    }
55
56
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
57
    public function setAssets($assets)
58
    {
59
        if (!is_array($assets)) {
60
            throw new \Exception(static::class.' assets must be an array');
61
        }
62
63
        $this->assets = $assets;
64
65
        return $this;
66
    }
67
    */
68
}
69