-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedl.php
350 lines (289 loc) · 11 KB
/
edl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* Copyright (c) 2022 David Ramsden
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
/**
* Will return:
* 200 - No errors/issues.
* 400 - Malformed request.
* 503 - Error with external resource (either fetching or with the data it returned).
*
* Any errors (400 or 503) will be logged using error_log().
*
*/
error_reporting(E_ERROR | E_WARNING | E_PARSE);
include('functions.inc.php');
/**
* Takes an array of IP addresses.
* Removes any duplicates.
* Ensures at least 1 IP address is present.
* Displays IPs, one per line.
*
* @param array $ips An array of valid IP addresses.
*/
function display_ips($ips) {
// Remove duplicates.
$ips = array_unique($ips);
// Ensure there is at least 1 IP.
if (!count($ips)) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = ". urldecode($_GET['service']) . ": No IPs to return.");
http_response_code(503);
exit();
}
foreach ($ips as $ip) {
echo "$ip\n";
}
}
header('Content-type: text/plain');
switch (urldecode($_GET['vendor'])) {
case 'polycom':
$service = !empty($_GET['service']) ? urldecode($_GET['service']) : 'global';
// Get all DNS A records associated with the service.
if (($results = dns_get_record('edge-' . $service . '.plcm.vc', DNS_A)) === false) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = $service: dns_get_record() failed.");
http_response_code(503);
exit();
}
// Validate each result.
$ips = array();
foreach ($results as $result) {
if (($valid_ip = validate_ipv4($result['ip']))) {
array_push($ips, $valid_ip);
}
}
display_ips($ips);
break;
case 'microsoft':
$service = !empty($_GET['service']) ? urldecode($_GET['service']) : '';
// Fetch JSON.
// Note: If IPv6 should be included, change 'NoIPv6=true' to 'NoIPv6=false' below.
if (($json = json_decode(fetch_url('https://endpoints.office.com/endpoints/WorldWide?ClientRequestId=' . generate_guid() . '&NoIPv6=true&Instance=Worldwide'))) === null) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = $service: Fetching JSON failed.");
http_response_code(503);
exit();
}
$ips = array();
foreach ($json as $element) {
foreach ($element as $key => $val) {
// Only interested in IPs.
if ($key === 'ips') {
// If a specific service has been requested and it matches the serviceArea.
if (!empty($service) && preg_match('/' . $element->{'serviceArea'} . '/i', $service)) {
$ips = array_merge($ips, $val);
// Otherwise no service was requested so the serviceArea doesn't matter.
} elseif (empty($service)) {
$ips = array_merge($ips, $val);
}
}
}
}
// Validate the IPs.
foreach ($ips as $key => $val) {
if (($valid_ip = validate_ipv4($val))) {
$ips[$key] = $valid_ip;
} else {
// Remove element from array.
unset($ips[$key]);
}
}
display_ips($ips);
break;
case 'okta':
$service = !empty($_GET['service']) ? urldecode($_GET['service']) : '';
// Fetch JSON.
if (($json = json_decode(fetch_url("https://s3.amazonaws.com/okta-ip-ranges/ip_ranges.json"))) === null) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = " . urldecode($_GET['service']) . ": Fetching JSON failed.");
http_response_code(503);
exit();
}
$ips = array();
foreach ($json as $name => $element) {
foreach ($element as $key => $val) {
// Only interested in IPs.
if ($key === 'ip_ranges' ) {
// If a specific service has been requested and it matches the serviceArea.
if (!empty($service) && preg_match('/' . $service . '/i', $name)) {
$ips = array_merge($ips, $val);
// Otherwise no service was requested so the serviceArea doesn't matter.
} elseif (empty($service)) {
$ips = array_merge($ips, $val);
}
}
}
}
// Validate the IPs.
foreach ($ips as $key => $val) {
if (($valid_ip = validate_ipv4($val))) {
$ips[$key] = $valid_ip;
} else {
// Remove element from array.
unset($ips[$key]);
}
}
display_ips($ips);
break;
case 'zscaler':
$zscloud = !empty($_GET['zscloud']) ? urldecode($_GET['zscloud']) : 'zscloud.net';
switch (urldecode($_GET['service'])) {
case 'pac':
// Fetch JSON.
if (($json = json_decode(fetch_url("https://api.config.zscaler.com/$zscloud/pac/json"))) === null) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = " . urldecode($_GET['service']) . ": Fetching JSON failed.");
http_response_code(503);
exit();
}
// Validate IPs.
$ips = array();
foreach ($json->{'ip'} as $ip) {
if (($ip = validate_ipv4($ip))) {
array_push($ips, $ip);
}
}
display_ips($ips);
break;
case 'cenr':
// Fetch JSON.
if (($json = json_decode(fetch_url("https://api.config.zscaler.com/$zscloud/cenr/json"))) === null) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = " . urldecode($_GET['service']) . ": Fetching JSON failed.");
http_response_code(503);
exit();
}
$ips = array();
foreach ($json->{$zscloud} as $continent => $cities) {
foreach ($cities as $city => $datacentres) {
foreach ($datacentres as $datacentre) {
// Validate IP.
if (($ip = validate_ipv4($datacentre->{'range'}))) {
array_push($ips, $ip);
}
}
}
}
display_ips($ips);
break;
case 'hub':
// This could break. Zscaler do not provide an API for obtaining the Hub IPs.
// This uses the JSON returned to a client's web browser when viewing the Firewall Config. Requirements page.
// Because it's not an official API, it's subject to change.
// Fetch JSON.
if (($json = json_decode(fetch_url("https://api.config.zscaler.com/api/getdata/$zscloud/all/fcr"))) === null) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = " . urldecode($_GET['service']) . ": Fetching JSON failed.");
http_response_code(503);
exit();
}
$ips = array();
// Hub IPs are currently at index 9.
foreach ($json->{'data'}[9]->{'body'}->{'json'}->{'rows'}[0]->{'cols'} as $key => $val) {
foreach ($val as $key => $val) {
// This is the "Required" column of the Hub IPs.
if ($key === 'required' && !empty($val)) {
// Validate IP.
if (($ip = validate_ipv4($val))) {
array_push($ips, $ip);
}
}
}
}
// FQDNs to resolve and include in the Hub IPs.
$fqdns = array("mobile.$zscloud", "login.$zscloud");
foreach ($fqdns as $fqdn) {
if (($results = dns_get_record($fqdn, DNS_A)) === false) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = $service: dns_get_record() failed.");
continue;
}
foreach ($results as $result) {
if (($valid_ip = validate_ipv4($result['ip']))) {
array_push($ips, $valid_ip);
}
}
}
display_ips($ips);
break;
default:
// Service requested is unknown.
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = " . urldecode($_GET['service']) . ": Service is not known.");
http_response_code(400);
exit();
break;
}
break;
case 'aws':
$service = !empty($_GET['service']) ? urldecode($_GET['service']) : 'amazon';
// Fetch JSON.
if (($json = json_decode(fetch_url('https://ip-ranges.amazonaws.com/ip-ranges.json'))) === null) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = $service: Fetching JSON failed.");
http_response_code(503);
exit();
}
$ips = array();
// Iterate all IPv4 prefixes.
foreach ($json->{'prefixes'} as $element) {
// Find service that has been requested.
if (preg_match('/' . $element->{'service'} . '/i', $service)) {
// Check if a region has been requested and if this service belongs to that region.
if (!empty($_GET['region']) && preg_match('/' . $element->{'region'} . '/i', urldecode($_GET['region']))) {
if (($ip = validate_ipv4($element->{'ip_prefix'}))) {
array_push($ips, $ip);
}
// Otherwise no region was requested, so just return the IP prefix for the service.
} elseif (empty($_GET['region'])) {
if (($ip = validate_ipv4($element->{'ip_prefix'}))) {
array_push($ips, $ip);
}
}
}
}
display_ips($ips);
break;
case 'gcp':
$service = !empty($_GET['service']) ? urldecode($_GET['service']) : 'google cloud';
// Fetch JSON.
if (($json = json_decode(fetch_url('https://www.gstatic.com/ipranges/cloud.json'))) === null) {
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = $service: Fetching JSON failed.");
http_response_code(503);
exit();
}
$ips = array();
// Iterate all prefixes.
foreach ($json->{'prefixes'} as $element) {
// Find service that has been requested.
if (preg_match('/' . $element->{'service'} . '/i', $service)) {
// Check if a scope has been requested and if this service belongs to that scope.
if (!empty($_GET['scope']) && preg_match('/' . $element->{'scope'} . '/i', urldecode($_GET['scope']))) {
if (($ip = validate_ipv4($element->{'ipv4Prefix'}))) {
array_push($ips, $ip);
}
// Otherwise no scope was requested, so just return the IPv4 prefix for the service.
} elseif (empty($_GET['scope'])) {
if (($ip = validate_ipv4($element->{'ipv4Prefix'}))) {
array_push($ips, $ip);
}
}
}
}
display_ips($ips);
break;
default:
// Vendor requested is unknown.
error_log("vendor = " . urldecode($_GET['vendor']) . ", service = " . urldecode($_GET['service']) . ": Vendor is not known.");
http_response_code(400);
exit();
break;
}
?>