From 374fb846e501039007bc5ebf7dff7132aa612cdc Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 7 Feb 2021 00:58:41 +1100 Subject: [PATCH] Added get ASN feature Get the ASN name and number for the servers ip adress. This is able to be turned off with the CONST GET_ASN_INFO. Thanks to ripe.net for this service --- class.php | 76 ++++++++++++++++++++++++++++++++++++++++------ my_idlers.sql | 3 +- update1.2to1.3.sql | 2 -- update1.3to1.4.sql | 2 ++ 4 files changed, 70 insertions(+), 13 deletions(-) delete mode 100644 update1.2to1.3.sql create mode 100644 update1.3to1.4.sql diff --git a/class.php b/class.php index 0aebf28..2fa7384 100644 --- a/class.php +++ b/class.php @@ -21,6 +21,8 @@ class idlersConfig const DEFAULT_VIEW = 'CARDS';//CARDS or TABLE const SAVE_YABS_OUTPUT = true;//true or false + + const GET_ASN_INFO = true;//Get ANS name and number } class elementHelpers extends idlersConfig @@ -3099,11 +3101,19 @@ class idlers extends helperFunctions public function getIpForDomain(string $domain, string $type = 'A'): string {//Gets IP from A record for a domain - $data = json_decode(file_get_contents("https://whatsmydns.net/api/details?server=428&type=$type&query=$domain"), true); - if (isset($data['data'][0]['response'][0])) { - if (strlen($data['data'][0]['response'][0]) > 6) { - return $data['data'][0]['response'][0]; - } + switch ($type) { + case "A": + $data = dns_get_record($domain, DNS_A); + if (isset($data['0']['ip'])) { + return $data['0']['ip']; + } + break; + case "AAAA": + $data = dns_get_record($domain, DNS_AAAA); + if (isset($data['0']['ipv6'])) { + return $data['0']['ipv6']; + } + break; } return "";//Doesnt exist/null/empty/invalid } @@ -3119,6 +3129,25 @@ class idlers extends helperFunctions return $result; } + public function getAsnInfo(string $ip) + {//Thanks ripe.net + $data = json_decode(@file_get_contents("https://stat.ripe.net/data/whois/data.json?resource=$ip"), true); + if (isset($data) && $data['status_code'] == 200) { + $origin = $desc = ''; + foreach ($data['data']['irr_records'][0] as $rec) { + if ($rec['key'] == 'descr') { + $desc = $rec['value']; + } + if ($rec['key'] == 'origin') { + $origin .= $rec['value']; + } + } + return "$desc, AS$origin"; + } else { + return ""; + } + } + protected function viewSwitcherIcon() { $this->rowColOpen('row text-center', 'col-12'); @@ -3383,7 +3412,6 @@ class idlers extends helperFunctions $this->outputString(''); } - } class itemInsert extends idlers @@ -3407,10 +3435,24 @@ class itemInsert extends idlers (empty($data['ipv4'])) ? $ipv4 = null : $ipv4 = $data['ipv4']; (empty($data['ipv6'])) ? $ipv6 = null : $ipv6 = $data['ipv6']; (empty($data['label'])) ? $label = null : $label = $data['label']; + if (self::GET_ASN_INFO) { + if (!is_null($ipv4)) { + $asn = $this->getAsnInfo($ipv4); + } elseif (!is_null($ipv6)) { + $asn = $this->getAsnInfo($ipv6); + } else { + $asn = null; + } + if (empty($asn)) { + $asn = null; + } + } else { + $asn = null; + } $location_id = $this->handleLocation($data['location']); $provider_id = $this->handleProvider($data['provider']); - $insert = $this->dbConnect()->prepare('INSERT IGNORE INTO `servers` (id, hostname, location, provider, ipv4,ipv6, owned_since, os, is_cpu_dedicated, is_dedicated, was_special, bandwidth, virt, has_yabs, ns1, ns2, ssh_port, label) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'); - $insert->execute([$item_id, $data['hostname'], $location_id, $provider_id, $ipv4, $ipv6, $data['owned_since'], $data['os'], $dedi_cpu, $dedi, $offer, $data['bandwidth'], $data['virt'], $data['has_yabs'], $data['ns1'], $data['ns2'], $data['ssh_port'], $label]); + $insert = $this->dbConnect()->prepare('INSERT IGNORE INTO `servers` (id, hostname, location, provider, ipv4,ipv6, owned_since, os, is_cpu_dedicated, is_dedicated, was_special, bandwidth, virt, has_yabs, ns1, ns2, ssh_port, label, asn) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'); + $insert->execute([$item_id, $data['hostname'], $location_id, $provider_id, $ipv4, $ipv6, $data['owned_since'], $data['os'], $dedi_cpu, $dedi, $offer, $data['bandwidth'], $data['virt'], $data['has_yabs'], $data['ns1'], $data['ns2'], $data['ssh_port'], $label, $asn]); $this->insertPrice($data['price'], $data['currency'], $data['term'], $data['next_due_date']); return $item_id; } @@ -3425,13 +3467,27 @@ class itemInsert extends idlers (empty($data['ipv4'])) ? $ipv4 = null : $ipv4 = $data['ipv4']; (empty($data['ipv6'])) ? $ipv6 = null : $ipv6 = $data['ipv6']; (empty($data['label'])) ? $label = null : $label = $data['label']; + if (self::GET_ASN_INFO) { + if (!is_null($ipv4)) { + $asn = $this->getAsnInfo($ipv4); + } elseif (!is_null($ipv6)) { + $asn = $this->getAsnInfo($ipv6); + } else { + $asn = null; + } + if (empty($asn)) { + $asn = null; + } + } else { + $asn = null; + } ($data['ram_type'] == 'GB') ? $ram_mb = $this->GBtoMB($data['ram']) : $ram_mb = $data['ram']; ($data['swap_type'] == 'GB') ? $swap_mb = $this->GBtoMB($data['swap']) : $swap_mb = $data['swap']; ($data['disk_type'] == 'TB') ? $disk_gb = $this->TBtoGB($data['disk']) : $disk_gb = $data['disk']; $location_id = $this->handleLocation($data['location']); $provider_id = $this->handleProvider($data['provider']); - $insert = $this->dbConnect()->prepare('INSERT IGNORE INTO `servers` (id, hostname, location, provider, ipv4,ipv6, owned_since, os, is_cpu_dedicated, is_dedicated, was_special, bandwidth, virt, cpu, cpu_freq, ram, ram_type, swap, swap_type, disk, disk_type, ram_mb, swap_mb, disk_gb, ns1, ns2, ssh_port, label) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'); - $insert->execute([$item_id, $data['hostname'], $location_id, $provider_id, $ipv4, $ipv6, $data['owned_since'], $data['os'], $dedi_cpu, $dedi, $offer, $data['bandwidth'], $data['virt'], $data['cpu_amount'], $data['cpu_speed'], $data['ram'], $data['ram_type'], $data['swap'], $data['swap_type'], $data['disk'], $data['disk_type'], $ram_mb, $swap_mb, $disk_gb, $data['ns1'], $data['ns2'], $data['ssh_port'], $label]); + $insert = $this->dbConnect()->prepare('INSERT IGNORE INTO `servers` (id, hostname, location, provider, ipv4,ipv6, owned_since, os, is_cpu_dedicated, is_dedicated, was_special, bandwidth, virt, cpu, cpu_freq, ram, ram_type, swap, swap_type, disk, disk_type, ram_mb, swap_mb, disk_gb, ns1, ns2, ssh_port, label, asn) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'); + $insert->execute([$item_id, $data['hostname'], $location_id, $provider_id, $ipv4, $ipv6, $data['owned_since'], $data['os'], $dedi_cpu, $dedi, $offer, $data['bandwidth'], $data['virt'], $data['cpu_amount'], $data['cpu_speed'], $data['ram'], $data['ram_type'], $data['swap'], $data['swap_type'], $data['disk'], $data['disk_type'], $ram_mb, $swap_mb, $disk_gb, $data['ns1'], $data['ns2'], $data['ssh_port'], $label, $asn]); $this->insertPrice($data['price'], $data['currency'], $data['term'], $data['next_due_date']); return $item_id; } diff --git a/my_idlers.sql b/my_idlers.sql index 2003731..d78008d 100644 --- a/my_idlers.sql +++ b/my_idlers.sql @@ -292,7 +292,7 @@ CREATE TABLE IF NOT EXISTS `servers` ( `id` char(8) NOT NULL, `hostname` varchar(124) DEFAULT NULL, - `label` varchar(24) DEFAULT NULL, + `label` varchar(24) DEFAULT NULL, `location` int(11) DEFAULT NULL, `provider` int(11) DEFAULT NULL, `ipv4` varchar(124) DEFAULT NULL, @@ -328,6 +328,7 @@ CREATE TABLE IF NOT EXISTS `servers` `owned_since` date DEFAULT NULL, `tags` varchar(255) DEFAULT NULL, `notes` varchar(255) DEFAULT NULL, + `asn` varchar(124) DEFAULT NULL, `has_yabs` tinyint(1) DEFAULT 0, `has_st` tinyint(1) DEFAULT 0, PRIMARY KEY (`id`), diff --git a/update1.2to1.3.sql b/update1.2to1.3.sql deleted file mode 100644 index 4f2d849..0000000 --- a/update1.2to1.3.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE `servers` - ADD COLUMN `label` VARCHAR(24) NULL DEFAULT NULL AFTER `hostname`; \ No newline at end of file diff --git a/update1.3to1.4.sql b/update1.3to1.4.sql new file mode 100644 index 0000000..58ae896 --- /dev/null +++ b/update1.3to1.4.sql @@ -0,0 +1,2 @@ +ALTER TABLE `servers` + ADD COLUMN `asn` VARCHAR(124) NULL DEFAULT NULL AFTER `notes`; \ No newline at end of file