1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinzar\Firewall\Middleware; |
4
|
|
|
|
5
|
|
|
use Spinzar\Firewall\Abstracts\Middleware; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class Geo extends Middleware |
9
|
|
|
{ |
10
|
|
|
public function check($patterns) |
11
|
|
|
{ |
12
|
|
|
$places = ['continents', 'regions', 'countries', 'cities']; |
13
|
|
|
|
14
|
|
|
if ($this->isEmpty($places)) { |
15
|
|
|
return false; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
if (!$location = $this->getLocation()) { |
19
|
|
|
return false; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
foreach ($places as $place) { |
23
|
|
|
if (!$this->isFiltered($location, $place)) { |
24
|
|
|
continue; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return true; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function isEmpty($places) |
34
|
|
|
{ |
35
|
|
|
foreach ($places as $place) { |
36
|
|
|
if (!$list = config('firewall.middleware.' . $this->middleware . '.' . $place)) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (empty($list['allow']) && empty($list['block'])) { |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function isFiltered($location, $place) |
51
|
|
|
{ |
52
|
|
|
if (!$list = config('firewall.middleware.' . $this->middleware . '.' . $place)) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$s_place = Str::singular($place); |
57
|
|
|
|
58
|
|
|
if (!empty($list['allow']) && !in_array((string) $location->$s_place, (array) $list['allow'])) { |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (in_array((string) $location->$s_place, (array) $list['block'])) { |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function getLocation() |
70
|
|
|
{ |
71
|
|
|
$location = new \stdClass(); |
72
|
|
|
$location->continent = $location->country = $location->region = $location->city = null; |
73
|
|
|
|
74
|
|
|
$service = config('firewall.middleware.' . $this->middleware . '.service'); |
75
|
|
|
|
76
|
|
|
return $this->$service($location); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function ipapi($location) |
80
|
|
|
{ |
81
|
|
|
$response = $this->getResponse('http://ip-api.com/json/' . $this->ip() . '?fields=continent,country,regionName,city'); |
82
|
|
|
|
83
|
|
|
if (!is_object($response) || empty($response->country) || empty($response->city)) { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$location->continent = $response->continent; |
88
|
|
|
$location->country = $response->country; |
89
|
|
|
$location->region = $response->regionName; |
90
|
|
|
$location->city = $response->city; |
91
|
|
|
|
92
|
|
|
return $location; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function extremeiplookup($location) |
96
|
|
|
{ |
97
|
|
|
$response = $this->getResponse('https://extreme-ip-lookup.com/json/' . $this->ip()); |
98
|
|
|
|
99
|
|
|
if (!is_object($response) || empty($response->country) || empty($response->city)) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$location->continent = $response->continent; |
104
|
|
|
$location->country = $response->country; |
105
|
|
|
$location->region = $response->region; |
106
|
|
|
$location->city = $response->city; |
107
|
|
|
|
108
|
|
|
return $location; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function ipstack($location) |
112
|
|
|
{ |
113
|
|
|
$response = $this->getResponse('https://api.ipstack.com/' . $this->ip() . '?access_key=' . env('IPSTACK_KEY')); |
114
|
|
|
|
115
|
|
|
if (!is_object($response) || empty($response->country_name) || empty($response->region_name)) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$location->continent = $response->continent_name; |
120
|
|
|
$location->country = $response->country_name; |
121
|
|
|
$location->region = $response->region_name; |
122
|
|
|
$location->city = $response->city; |
123
|
|
|
|
124
|
|
|
return $location; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function ipdata($location) |
128
|
|
|
{ |
129
|
|
|
$response = $this->getResponse('https://api.ipdata.co/' . $this->ip() . '?api-key=' . env('IPSTACK_KEY')); |
130
|
|
|
|
131
|
|
|
if (!is_object($response) || empty($response->country_name) || empty($response->region_name)) { |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$location->continent = $response->continent_name; |
136
|
|
|
$location->country = $response->country_name; |
137
|
|
|
$location->region = $response->region_name; |
138
|
|
|
$location->city = $response->city; |
139
|
|
|
|
140
|
|
|
return $location; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function ipinfo($location) |
144
|
|
|
{ |
145
|
|
|
$response = $this->getResponse('https://ipinfo.io/' . $this->ip() . '/geo?token=' . env('IPINFO_KEY')); |
146
|
|
|
|
147
|
|
|
if (!is_object($response) || empty($response->country) || empty($response->city)) { |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$location->country = $response->country; |
152
|
|
|
$location->region = $response->region; |
153
|
|
|
$location->city = $response->city; |
154
|
|
|
|
155
|
|
|
return $location; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function getResponse($url) |
159
|
|
|
{ |
160
|
|
|
try { |
161
|
|
|
$ch = curl_init(); |
162
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
163
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
164
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 3); |
165
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); |
166
|
|
|
$content = curl_exec($ch); |
167
|
|
|
curl_close($ch); |
168
|
|
|
|
169
|
|
|
$response = json_decode($content); |
170
|
|
|
} catch (\ErrorException $e) { |
171
|
|
|
$response = null; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $response; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|