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
|
|
|
|
18
|
|
|
// get bad ip ranges |
19
|
|
|
$ips = $this->getAssets(); |
20
|
|
|
if ($this->assetsBuilt) { |
21
|
|
|
foreach ($ips as $ipRange) { |
22
|
|
|
if ($long <= $ipRange[1] && $long >= $ipRange[0]) { |
23
|
|
|
return true; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
} else { |
27
|
|
|
foreach ($ips as $ipRange) { |
28
|
|
|
if ($long <= ip2long($ipRange[1]) && $long >= ip2long($ipRange[0])) { |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function setDefaultTarget() |
38
|
|
|
{ |
39
|
|
|
$this->target = IpTools::getClientIp(); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function getDefaultAssets() |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
['59.62.0.0', '59.63.255.255'], // CHINANET JIANGXI PROVINCE NETWORK |
48
|
|
|
['59.172.0.0', '59.173.255.255'], // CHINANET Hubei province network |
49
|
|
|
['61.146.178.0', '61.146.178.255'], // MAINT-CHINANET-GD |
50
|
|
|
['62.210.0.0', '62.210.255.255'], // IE-POOL-BUSINESS-HOSTING Franch |
51
|
|
|
['86.123.0.0', '86.123.255.255'], // RDS Residential ROMANIA |
52
|
|
|
['89.122.0.0', '89.122.255.255'], // Romtelecom Data Network Romania |
53
|
|
|
['106.4.0.0', '106.7.255.255'], // CHINANET JIANGXI PROVINCE NETWORK |
54
|
|
|
['115.148.0.0', '115.254.255.255'], // many attempts, just block them all |
55
|
|
|
['117.21.0.0', '117.21.255.255'], // CHINANET JIANGXI PROVINCE NETWORK |
56
|
|
|
['117.41.0.0', '117.41.255.255'], // CHINANET JIANGXI PROVINCE NETWORK |
57
|
|
|
['146.148.128.0', '146.148.255.255'], // GCHAO LLC |
58
|
|
|
['148.251.0.0', '148.251.255.255'], // Hetzner Online GmbH |
59
|
|
|
['178.239.165.0', '178.239.165.255'], // COBALT-NETWORKS English |
60
|
|
|
['182.96.0.0', '182.111.255.255'], // CHINANET JIANGXI PROVINCE NETWORK |
61
|
|
|
['218.64.0.0', '218.65.127.255'], // CHINANET JIANGXI PROVINCE NETWORK |
62
|
|
|
['202.75.32.0', '202.75.63.254'], // TM VADS DC Hosting |
63
|
|
|
['222.208.0.0', '222.215.255.255'], // CHINANET Sichuan province network |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected $assetsBuilt = false; |
68
|
|
|
|
69
|
|
|
public function getAssetsBuilt() |
70
|
|
|
{ |
71
|
|
|
return $this->assetsBuilt; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setAssetsBuilt($assetsBuilt) |
75
|
|
|
{ |
76
|
|
|
$this->assetsBuilt = $assetsBuilt; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/* |
|
|
|
|
82
|
|
|
public function setAssets($assets) |
83
|
|
|
{ |
84
|
|
|
if (!is_array($assets)) { |
85
|
|
|
throw new \Exception(static::class.' assets must be an array'); |
86
|
|
|
} |
87
|
|
|
$this->assets = $assets; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
*/ |
92
|
|
|
} |
93
|
|
|
|
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.