Esempio n. 1
0
 public function getPrefixDns($prefix, $count = false)
 {
     $dnsModel = new DNSRecord();
     $rrTypes = array_flip($dnsModel::$rrTypes);
     $client = ClientBuilder::create()->setHosts(config('elasticquent.config.hosts'))->build();
     $prefixParts = explode('/', $prefix);
     if ($this->getInputType($prefixParts[0]) === 4) {
         $startIpDec = $this->ip2dec($prefixParts[0]);
         $endIpDec = $startIpDec + $this->IPv4cidrIpCount()[$prefixParts[1]];
     } else {
         $startIpDec = number_format($this->ip2dec($prefixParts[0]), 0, '', '');
         $endIpDec = $startIpDec + $this->IPv6cidrIpCount()[$prefixParts[1]];
         $endIpDec = number_format($endIpDec, 0, '', '');
     }
     $searchParams = ['index' => config('elasticquent.default_index') . '_dns', 'type' => $dnsModel->getTable(), 'body' => ['query' => ['bool' => ['must' => [['range' => ['ip_dec' => ['gte' => $startIpDec]]], ['range' => ['ip_dec' => ['lte' => $endIpDec]]]]]]]];
     $searchResults = $client->search($searchParams);
     if ($count === true) {
         return $searchResults['hits']['total'];
     }
     $data = [];
     foreach ($searchResults['hits']['hits'] as $searchResult) {
         $data[$searchResult['_source']['entry']][] = $searchResult['_source']['input'];
     }
     return $data;
 }
Esempio n. 2
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $this->bench->start();
     // Setting a brand new index name
     $entityIndexName = config('elasticquent.default_index') . '_dns';
     $versionedIndex = $entityIndexName . '_' . time();
     Config::set('elasticquent.default_index', $versionedIndex);
     // create new index
     DNSRecord::createIndex(8);
     $currentCount = 0;
     $idCounter = 0;
     $filePath = 'final_output.csv';
     $dnsModel = new DNSRecord();
     $name = $dnsModel->getTable();
     $params = ['body' => []];
     $client = $dnsModel->getElasticSearchClient();
     $rrTypes = DNSRecord::$rrTypes;
     $fp = fopen($filePath, 'r');
     if ($fp) {
         while (($line = fgets($fp)) !== false) {
             $line = trim($line);
             if (empty($line) === true) {
                 continue;
             }
             $parts = explode(',', $line, 3);
             if (count($parts) !== 3) {
                 $this->warn('====================================');
                 $this->error('Error processing the following line:');
                 dump($line);
                 $this->warn('====================================');
                 continue;
             }
             // Make sure the type is something we know/have
             $parts[1] = strtoupper($parts[1]);
             if (isset($rrTypes[$parts[1]]) !== true) {
                 $this->warn('====================================');
                 $this->error('Unkonw rrType: ' . $parts[1]);
                 dump($line);
                 $this->warn('====================================');
                 continue;
             }
             $data = ['input' => $parts[0], 'type' => $rrTypes[$parts[1]], 'entry' => $parts[2]];
             if ($parts[1] == 'A' || $parts[1] == 'AAAA') {
                 $data['ip_dec'] = $this->ipUtils->ip2dec($data['entry']);
             } else {
                 $data['ip_dec'] = null;
             }
             $params['body'][] = ['index' => ['_index' => $versionedIndex, '_type' => $name, '_id' => $idCounter++]];
             $params['body'][] = $data;
             $currentCount++;
             if ($currentCount > $this->batchAmount) {
                 // Get our document body data.
                 $client->bulk($params);
                 $this->info('Inserted ' . number_format($this->batchAmount) . ' DNS records');
                 // Reset the batching
                 $currentCount = 0;
                 $params['body'] = [];
             }
         }
         fclose($fp);
     }
     // Insert the remaining entries
     if (count($params['body']) > 0) {
         $client->bulk($params);
         $this->info('Inserted the remaining ' . count($params['body']) . ' records');
     }
     $this->hotSwapIndices($versionedIndex, $entityIndexName);
     $this->output->newLine(1);
     $this->bench->end();
     $this->info(sprintf('Time: %s, Memory: %s', $this->bench->getTime(), $this->bench->getMemoryPeak()));
 }