Пример #1
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));
 }
Пример #2
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 . ']');
 }