|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Shieldon package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Terry L. <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* php version 7.1.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @category Web-security |
|
13
|
|
|
* @package Shieldon |
|
14
|
|
|
* @author Terry Lin <[email protected]> |
|
15
|
|
|
* @copyright 2019 terrylinooo |
|
16
|
|
|
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT |
|
17
|
|
|
* @link https://github.com/terrylinooo/shieldon |
|
18
|
|
|
* @see https://shieldon.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types=1); |
|
22
|
|
|
|
|
23
|
|
|
namespace Shieldon\Firewall; |
|
24
|
|
|
|
|
25
|
|
|
use function substr; |
|
26
|
|
|
use function gethostbyaddr; |
|
27
|
|
|
use function Shieldon\Firewall\set_ip; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* IP Trait |
|
31
|
|
|
*/ |
|
32
|
|
|
trait IpTrait |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Public methods | Desctiotion |
|
36
|
|
|
* ----------------------|--------------------------------------------- |
|
37
|
|
|
* setIp | Set an IP address. |
|
38
|
|
|
* getIp | Get current set IP. |
|
39
|
|
|
* setRdns | Set a RDNS record for the check. |
|
40
|
|
|
* getRdns | Get IP resolved hostname. |
|
41
|
|
|
* ----------------------|--------------------------------------------- |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* IP address. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $ip = ''; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The RDNS recond of the Robot's IP address. |
|
53
|
|
|
* This is the most important value because that the IP of the most popular |
|
54
|
|
|
* search engines can be resolved to their domain name. |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $rdns = ''; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set an IP address. |
|
62
|
|
|
* If you want to deal with the proxy and CDN IPs. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $ip The IP address. |
|
65
|
|
|
* @param bool $queryRdns The option to query RDNS. |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
146 |
|
public function setIp(string $ip, $queryRdns = false): void |
|
70
|
|
|
{ |
|
71
|
146 |
|
$this->ip = $ip; |
|
72
|
|
|
|
|
73
|
146 |
|
set_ip($this->ip); |
|
74
|
|
|
|
|
75
|
146 |
|
if ($queryRdns) { |
|
76
|
|
|
// Check if your IP is from localhost, perhaps your are in development |
|
77
|
|
|
// environment? |
|
78
|
|
|
if (substr($this->ip, 0, 8) === '192.168.' || |
|
79
|
|
|
substr($this->ip, 0, 6) === '127.0.' |
|
80
|
92 |
|
) { |
|
81
|
92 |
|
$this->setRdns('localhost'); |
|
82
|
|
|
} else { |
|
83
|
84 |
|
$this->setRdns(gethostbyaddr($this->ip)); |
|
84
|
|
|
} |
|
85
|
9 |
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get current set IP. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getIp(): string |
|
94
|
|
|
{ |
|
95
|
13 |
|
return $this->ip; |
|
96
|
|
|
} |
|
97
|
13 |
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set a RDNS record for the check. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $rdns Reserve DNS record for that IP address. |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setRdns($rdns): void |
|
106
|
|
|
{ |
|
107
|
111 |
|
$this->rdns = $rdns; |
|
108
|
|
|
} |
|
109
|
111 |
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get IP resolved hostname. |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getRdns(): string |
|
116
|
|
|
{ |
|
117
|
2 |
|
return $this->rdns; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|