Пример #1
0
 /**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     $rir_id = $this->rir_id;
     $as_number = $this->as_number;
     $this->cli->br()->comment('Looking up and adding: AS' . $as_number);
     $asnWhois = new Whois($as_number);
     $parsedWhois = $asnWhois->parse();
     $asn = new ASN();
     // Skip null results
     if (is_null($parsedWhois) === true) {
         // Save the null entry
         $asn->rir_id = $rir_id;
         $asn->asn = $as_number;
         $asn->raw_whois = $asnWhois->raw();
         $asn->save();
         return;
     }
     $asn->rir_id = $rir_id;
     $asn->asn = $as_number;
     $asn->name = empty($parsedWhois->name) !== true ? $parsedWhois->name : null;
     $asn->description = isset($parsedWhois->description[0]) ? $parsedWhois->description[0] : $asn->name;
     $asn->description_full = count($parsedWhois->description) > 0 ? json_encode($parsedWhois->description) : json_encode([$asn->description]);
     // Insert PeerDB Info if we get any
     if ($peerDb = $this->peeringDBData) {
         $asn->website = $peerDb->website;
         $asn->looking_glass = $peerDb->looking_glass;
         $asn->traffic_estimation = $peerDb->info_traffic;
         $asn->traffic_ratio = $peerDb->info_ratio;
     }
     $asn->counrty_code = $parsedWhois->counrty_code;
     $asn->owner_address = json_encode($parsedWhois->address);
     $asn->raw_whois = $asnWhois->raw();
     $asn->save();
     // Save ASN Emails
     foreach ($parsedWhois->emails as $email) {
         $asnEmail = new ASNEmail();
         $asnEmail->asn_id = $asn->id;
         $asnEmail->email_address = $email;
         // Check if its an abuse email
         if (in_array($email, $parsedWhois->abuse_emails)) {
             $asnEmail->abuse_email = true;
         }
         $asnEmail->save();
     }
     $this->cli->br()->comment($asn->asn . ' - ' . $asn->description . ' [' . $asn->name . ']');
 }
Пример #2
0
 /**
  * Execute the job.
  *
  * @return void
  */
 public function handle()
 {
     // Setting a brand new index name
     $entityIndexName = config('elasticquent.default_index');
     $versionedIndex = $entityIndexName . '_' . time();
     Config::set('elasticquent.default_index', $versionedIndex);
     // create new index
     ASN::createIndex();
     $this->reindexClass(IPv4PrefixWhois::class);
     $this->reindexClass(IPv6PrefixWhois::class);
     $this->reindexClass(IX::class, $withRelated = false);
     $this->reindexClass(ASN::class);
     $this->hotSwapIndices($versionedIndex, $entityIndexName);
 }
Пример #3
0
 public function countryReport($country_code)
 {
     $asnData = [];
     $allocatedAsns = $this->ipUtils->getAllocatedAsns($country_code);
     // Sort out the ASN keys
     foreach ($allocatedAsns as $allocatedAsn) {
         $asnData[$allocatedAsn->asn] = ['asn' => $allocatedAsn->asn, 'name' => null, 'description' => null, 'date_allocated' => $allocatedAsn->date_allocated, 'ipv4_prefixes' => 0, 'ipv6_prefixes' => 0, 'ipv4_peers' => 0, 'ipv6_peers' => 0];
     }
     $asnArray = array_keys($asnData);
     $asnMetas = ASN::whereIn('asn', $asnArray)->get();
     foreach ($asnMetas as $asnMeta) {
         $asnData[$asnMeta->asn]['name'] = $asnMeta->name;
         $asnData[$asnMeta->asn]['description'] = $asnMeta->description;
     }
     $ipv4Prefixes = IPv4BgpPrefix::select(DB::raw('asn, COUNT(asn) as count'))->whereIn('asn', $asnArray)->groupBy('asn')->get();
     foreach ($ipv4Prefixes as $ipv4Prefix) {
         $asnData[$ipv4Prefix->asn]['ipv4_prefixes'] = $ipv4Prefix->count;
     }
     $ipv6Prefixes = IPv6BgpPrefix::select(DB::raw('asn, COUNT(asn) as count'))->whereIn('asn', $asnArray)->groupBy('asn')->get();
     foreach ($ipv6Prefixes as $ipv6Prefix) {
         $asnData[$ipv6Prefix->asn]['ipv6_prefixes'] = $ipv6Prefix->count;
     }
     $seenIpv4Peers = [];
     $ipv4Peers = IPv4Peer::select(DB::raw('asn_1 as asn, asn_2 as peer, COUNT(asn_1) as count'))->whereIn('asn_1', $asnArray)->groupBy('asn_1')->get();
     foreach ($ipv4Peers as $ipv4Peer) {
         if (isset($seenIpv4Peers[$ipv4Peer->asn][$ipv4Peer->peer]) === true) {
             continue;
         }
         $asnData[$ipv4Peer->asn]['ipv4_peers'] += $ipv4Peer->count;
         $seenIpv4Peers[$ipv4Peer->asn][$ipv4Peer->peer] = true;
     }
     $ipv4Peers = IPv4Peer::select(DB::raw('asn_2 as asn, asn_1 as peer, COUNT(asn_2) as count'))->whereIn('asn_2', $asnArray)->groupBy('asn_2')->get();
     foreach ($ipv4Peers as $ipv4Peer) {
         if (isset($seenIpv4Peers[$ipv4Peer->asn][$ipv4Peer->peer]) === true) {
             continue;
         }
         $asnData[$ipv4Peer->asn]['ipv4_peers'] += $ipv4Peer->count;
         $seenIpv4Peers[$ipv4Peer->asn][$ipv4Peer->peer] = true;
     }
     $seenIpv6Peers = [];
     $ipv6Peers = IPv6Peer::select(DB::raw('asn_1 as asn, asn_2 as peer, COUNT(asn_1) as count'))->whereIn('asn_1', $asnArray)->groupBy('asn_1')->get();
     foreach ($ipv6Peers as $ipv6Peer) {
         if (isset($seenIpv4Peers[$ipv6Peer->asn][$ipv6Peer->peer]) === true) {
             continue;
         }
         $asnData[$ipv6Peer->asn]['ipv6_peers'] += $ipv6Peer->count;
     }
     $ipv6Peers = IPv6Peer::select(DB::raw('asn_2 as asn, asn_1 as peer, COUNT(asn_2) as count'))->whereIn('asn_2', $asnArray)->groupBy('asn_2')->get();
     foreach ($ipv6Peers as $ipv6Peer) {
         if (isset($seenIpv6Peers[$ipv6Peer->asn][$ipv6Peer->peer]) === true) {
             continue;
         }
         $asnData[$ipv6Peer->asn]['ipv6_peers'] += $ipv6Peer->count;
     }
     return $this->sendData(array_values($asnData));
 }
Пример #4
0
 private function updateASN()
 {
     $this->cli->br()->comment('===================================================');
     $this->cli->br()->comment('Adding newly allocated ASNs to queue')->br();
     $sourceAsns = $this->getAllAsns();
     $asns = [];
     foreach ($sourceAsns as $sourceAsn) {
         foreach ($sourceAsn as $asnObj) {
             if (isset($asns[$asnObj->asn]) === false) {
                 $asns[$asnObj->asn] = isset($asnObj->rir_id) ? $asnObj->rir_id : null;
             }
         }
     }
     $seenAsns = DB::table('asns')->pluck('asn');
     $seenAsns = array_flip($seenAsns);
     foreach ($asns as $as_number => $rir_id) {
         // Lets check if the ASN has already been looked at in the past
         if (isset($seenAsns[$as_number]) !== true) {
             // Dispatch a new job into queue
             $this->dispatch(new EnterASNs($as_number, $rir_id, $this->getPeeringDbInfo($as_number)));
         }
     }
     // Ok, now that we are done with new allocations, lets update the old records
     $oldAsns = ASN::where('updated_at', '<', Carbon::now()->subMonth())->orderBy('updated_at', 'ASC')->limit(2000)->get();
     $oldAsns->shuffle();
     foreach ($oldAsns as $oldAsn) {
         $this->dispatch(new UpdateASNs($oldAsn, $this->getPeeringDbInfo($oldAsn->asn)));
     }
 }
Пример #5
0
 private function processAsnGraph($inputAsn, $ipVersion, $upstreams)
 {
     $relations = [];
     foreach ($upstreams as $upstream) {
         foreach ($upstream['bgp_paths'] as $path) {
             $asns = explode(' ', $path);
             foreach ($asns as $key => $asn) {
                 $asnList[] = $asn;
                 // If its the last ASN, then stop
                 if (isset($asns[$key + 1]) !== true) {
                     continue;
                 }
                 if (isset($relations[$asn . '-' . $asns[$key + 1]]) === true) {
                     $relations[$asn . '-' . $asns[$key + 1]]['weight']++;
                     continue;
                 }
                 $relations[$asn . '-' . $asns[$key + 1]] = ['weight' => 1, 'asn1' => $asn, 'asn2' => $asns[$key + 1]];
             }
         }
     }
     $keyAsns = [];
     $realRelation = [];
     foreach ($relations as $relation) {
         $keyAsns[$relation['asn1']][] = $relation;
     }
     $asnsData = ASN::whereIn('asn', array_unique($asnList))->get();
     foreach ($keyAsns as $groupedAsns) {
         $highestNumber = $groupedAsns[0]['weight'];
         foreach ($groupedAsns as $asn) {
             if ($asn['weight'] > $highestNumber) {
                 $highestNumber = $asn['weight'];
             }
         }
         // Set the lowest to be 1;
         if ($highestNumber > $this->maxLineThickness) {
             $devider = $highestNumber / $this->maxLineThickness;
         } else {
             $devider = 1;
         }
         foreach ($groupedAsns as $asn) {
             $asn['weight'] = $asn['weight'] / $devider;
             $realRelation[] = $asn;
         }
     }
     $outputGraphvizText = 'digraph "AS' . $inputAsn . ' ' . $ipVersion . ' Upstream Graph" {' . PHP_EOL;
     $outputGraphvizText .= 'rankdir=LR;' . PHP_EOL;
     $outputGraphvizText .= 'node [style=filled,fillcolor="#ffffff",fontcolor="#2C94B3"];' . PHP_EOL;
     $processedAsn = [];
     foreach ($realRelation as $relation) {
         // Add labels and hyperlinks
         if (isset($processedAsn[$relation['asn1']]) !== true) {
             $asnMeta = $asnsData->where('asn', (int) $relation['asn1'])->first();
             if (is_null($asnMeta) !== true) {
                 $countryCode = empty($asnMeta->counrty_code) !== true ? ' [' . $asnMeta->counrty_code . ']' : '';
                 $description = strlen($asnMeta->description) > 35 ? $asnMeta->name : $asnMeta->description;
                 $description = str_replace("'", "", $description);
                 $outputGraphvizText .= 'AS' . $relation['asn1'] . ' ';
                 $outputGraphvizText .= '[';
                 $outputGraphvizText .= 'tooltip="AS' . $asnMeta->asn . ' ~ ' . addslashes($description) . $countryCode . '" ';
                 $outputGraphvizText .= 'URL="https://bgpview.io/asn/' . $asnMeta->asn . '" ';
                 $outputGraphvizText .= 'fontcolor="#2C94B3" ';
                 $outputGraphvizText .= ']' . PHP_EOL;
                 $processedAsn[$relation['asn1']] = true;
             }
         }
         if (isset($processedAsn[$relation['asn2']]) !== true) {
             $asnMeta = $asnsData->where('asn', (int) $relation['asn2'])->first();
             if (is_null($asnMeta) !== true) {
                 $countryCode = empty($asnMeta->counrty_code) !== true ? ' [' . $asnMeta->counrty_code . ']' : '';
                 $description = strlen($asnMeta->description) > 35 ? $asnMeta->name : $asnMeta->description;
                 $description = str_replace("'", "", $description);
                 $outputGraphvizText .= 'AS' . $relation['asn2'] . ' ';
                 $outputGraphvizText .= '[';
                 $outputGraphvizText .= 'tooltip="AS' . $asnMeta->asn . ' ~ ' . addslashes($description) . $countryCode . '" ';
                 $outputGraphvizText .= 'URL="https://bgpview.io/asn/' . $asnMeta->asn . '" ';
                 $outputGraphvizText .= ']' . PHP_EOL;
                 $processedAsn[$relation['asn2']] = true;
             }
         }
         $outputGraphvizText .= 'AS' . $relation['asn1'] . ' -> AS' . $relation['asn2'] . ' [ penwidth = ' . $relation['weight'] . ' ];' . PHP_EOL;
     }
     $outputGraphvizText .= 'AS' . $inputAsn . '[fontcolor="#880000"]' . PHP_EOL;
     $outputGraphvizText .= '}' . PHP_EOL;
     exec('echo \'' . $outputGraphvizText . '\' | dot -Tsvg -o ' . public_path() . '/assets/graphs/AS' . $inputAsn . '_' . $ipVersion . '.svg');
     $this->cli->comment('Generated graph for AS' . $inputAsn . ' [' . $ipVersion . ']');
 }