Navigation | Overlay |
---|---|
t Navigate files | h Toggle hits |
y Change url to tip of branch | m Toggle misses |
b / v Jump to prev/next hit line | p Toggle partial |
z / x Jump to prev/next missed or partial line | 1..9 Toggle flags |
shift + o Open current page in GitHub | a Toggle all on |
/ or ? Show keyboard shortcuts dialog | c Toggle context lines or commits |
1 |
<?php
|
|
2 |
/**
|
|
3 |
* This file is part of the Shieldon package.
|
|
4 |
*
|
|
5 |
* (c) Terry L. <contact@terryl.in>
|
|
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 <contact@terryl.in>
|
|
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 | 3 |
public function setIp(string $ip, $queryRdns = false): void |
70 |
{
|
|
71 | 3 |
$this->ip = $ip; |
72 |
|
|
73 | 3 |
set_ip($this->ip); |
74 |
|
|
75 | 3 |
if ($queryRdns) { |
76 |
|
|
77 |
// Check if your IP is from localhost, perhaps your are in development
|
|
78 |
// environment?
|
|
79 |
if ( |
|
80 | 3 |
substr($this->ip, 0, 8) === '192.168.' || |
81 | 3 |
substr($this->ip, 0, 6) === '127.0.' |
82 |
) { |
|
83 | 3 |
$this->setRdns('localhost'); |
84 |
} else { |
|
85 | 3 |
$this->setRdns(gethostbyaddr($this->ip)); |
86 |
}
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Get current set IP.
|
|
92 |
*
|
|
93 |
* @return string
|
|
94 |
*/
|
|
95 | 3 |
public function getIp(): string |
96 |
{
|
|
97 | 3 |
return $this->ip; |
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Set a RDNS record for the check.
|
|
102 |
*
|
|
103 |
* @param string $rdns Reserve DNS record for that IP address.
|
|
104 |
*
|
|
105 |
* @return void
|
|
106 |
*/
|
|
107 | 3 |
public function setRdns($rdns): void |
108 |
{
|
|
109 | 3 |
$this->rdns = $rdns; |
110 |
}
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Get IP resolved hostname.
|
|
114 |
*
|
|
115 |
* @return string
|
|
116 |
*/
|
|
117 | 3 |
public function getRdns(): string |
118 |
{
|
|
119 | 3 |
return $this->rdns; |
120 |
}
|
|
121 |
}
|
Read our documentation on viewing source code .