public function whois() { if (isset($this->attributes['whois']) !== true) { $this->attributes['whois'] = IPv6PrefixWhois::where('ip', $this->ip)->where('cidr', $this->cidr)->first(); } return $this->attributes['whois']; }
public function prefix(Request $request, $ip, $cidr) { $ipVersion = $this->ipUtils->getInputType($ip); if ($ipVersion === 4) { $prefixWhoisClass = IPv4PrefixWhois::class; } else { if ($ipVersion === 6) { $prefixWhoisClass = IPv6PrefixWhois::class; } else { $data = $this->makeStatus('Malformed input', false); return $this->respond($data); } } $prefixes = $this->ipUtils->getPrefixesFromBgpTable($ip, $cidr); if ($prefixes->count() === 0) { if ($ipVersion === 4) { $prefix = IPv4PrefixWhois::where('ip', $ip)->where('cidr', $cidr)->first(); } else { $prefix = IPv6PrefixWhois::where('ip', $ip)->where('cidr', $cidr)->first(); } $prefixWhois = $prefix; } else { $prefix = $prefixes[0]; $prefixWhois = $prefixWhoisClass::where('ip', $prefix->ip)->where('cidr', $prefix->cidr)->first(); } if (is_null($prefix) === true) { $data = $this->makeStatus('Prefix not found in BGP table or malformed', false); return $this->respond($data); } $allocation = $this->ipUtils->getAllocationEntry($prefix->ip, $prefix->cidr); $geoip = $this->ipUtils->geoip($prefix->ip); $relatedPrefixes = $this->ipUtils->getRealatedPrefixes($prefix->ip, $prefix->cidr); $output['prefix'] = $prefix->ip . '/' . $prefix->cidr; $output['ip'] = $prefix->ip; $output['cidr'] = $prefix->cidr; $output['asns'] = []; $asnArray = []; foreach ($prefixes as $prefixData) { if (isset($asnArray[$prefixData->asn]) === true) { // Make sure we dont have said upstream already in our array if (in_array($prefixData->upstream_asn, $asnArray[$prefixData->asn]) !== true) { $asnArray[$prefixData->asn][] = $prefixData->upstream_asn; } } else { $asnArray[$prefixData->asn][] = $prefixData->upstream_asn; } } foreach ($asnArray as $baseAsn => $upstreamArray) { $asn = ASN::where('asn', $baseAsn)->first(); $asnData['asn'] = $baseAsn; $asnData['name'] = $asn->name; $asnData['description'] = $asn->description; $asnData['country_code'] = empty($asn->counrty_code) !== true ? $asn->counrty_code : null; $asnData['prefix_upstreams'] = []; foreach ($upstreamArray as $upstreamAsn) { $asn = ASN::where('asn', $upstreamAsn)->first(); $upstreamAsnData['asn'] = $upstreamAsn; $upstreamAsnData['name'] = isset($asn->name) ? $asn->name : null; $upstreamAsnData['description'] = isset($asn->description) ? $asn->description : null; $upstreamAsnData['country_code'] = empty($asn->counrty_code) !== true ? $asn->counrty_code : null; $asnData['prefix_upstreams'][] = $upstreamAsnData; } $output['asns'][] = $asnData; } $output['name'] = $prefixWhois ? $prefixWhois->name : null; $output['description_short'] = $prefixWhois ? $prefixWhois->description : null; $output['description_full'] = $prefixWhois ? $prefixWhois->description_full : null; $output['email_contacts'] = $prefixWhois ? $prefixWhois->email_contacts : null; $output['abuse_contacts'] = $prefixWhois ? $prefixWhois->abuse_contacts : null; $output['owner_address'] = $prefixWhois ? $prefixWhois->owner_address : null; $output['country_codes']['whois_country_code'] = $prefixWhois ? $prefixWhois->counrty_code : null; $output['country_codes']['rir_allocation_country_code'] = $allocation ? $allocation->country_code : null; $output['country_codes']['maxmind_country_code'] = $geoip ? $geoip->country->isoCode : null; $output['rir_allocation']['rir_name'] = empty($allocation->rir_name) !== true ? $allocation->rir_name : null; $output['rir_allocation']['country_code'] = isset($allocation->country_code) ? $allocation->country_code : null; $output['rir_allocation']['ip'] = isset($allocation->ip) ? $allocation->ip : null; $output['rir_allocation']['cidr'] = isset($allocation->cidr) ? $allocation->cidr : null; $output['rir_allocation']['prefix'] = isset($allocation->ip) && isset($allocation->cidr) ? $allocation->ip . '/' . $allocation->cidr : null; $output['rir_allocation']['date_allocated'] = isset($allocation->date_allocated) ? $allocation->date_allocated . ' 00:00:00' : null; $output['rir_allocation']['allocation_status'] = isset($allocation->status) ? $allocation->status : null; $output['maxmind']['country_code'] = $geoip ? $geoip->country->isoCode : null; $output['maxmind']['city'] = $geoip ? $geoip->city->name : null; $output['related_prefixes'] = []; foreach ($relatedPrefixes as $relatedPrefix) { $relatedPrefixWhois = $relatedPrefix->whois(); $relatedPrefixData['prefix'] = $relatedPrefix->ip . '/' . $relatedPrefix->cidr; $relatedPrefixData['ip'] = $relatedPrefix->ip; $relatedPrefixData['cidr'] = $relatedPrefix->cidr; $relatedPrefixData['name'] = $relatedPrefixWhois ? $relatedPrefixWhois->name : null; $relatedPrefixData['description'] = $relatedPrefixWhois ? $relatedPrefixWhois->description : null; $relatedPrefixData['country_code'] = $relatedPrefixWhois ? $relatedPrefixWhois->counrty_code : null; $output['related_prefixes'][] = $relatedPrefixData; } if ($request->has('with_raw_whois') === true) { $output['raw_whois'] = $prefixWhois ? $prefixWhois->raw_whois : null; } if ($request->has('with_dns') === true) { $output['dns'] = $this->ipUtils->getPrefixDns($output['prefix']); } $output['date_updated'] = isset($prefixWhois->updated_at) ? (string) $prefixWhois->updated_at : (isset($prefix->updated_at) ? $prefix->updated_at : null); return $this->sendData($output); }