diff --git a/.htaccess b/.htaccess index 8db6885..94bc389 100644 --- a/.htaccess +++ b/.htaccess @@ -1,5 +1,10 @@ RewriteEngine On RewriteBase / + RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d -RewriteRule ^(.*)$ /index.php?m=$1 +RewriteRule ^([^/]+)/?$ /index.php?m=$1 [L] + +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule ^([^/]+)/([^/]+)/?$ index.php?m=$1&t=$2 [L] diff --git a/README.md b/README.md index 91db1b7..d0d4b46 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ A simple PHP script to tell a visitor's IP address and more information. This fi ## Plain return +Example: [/][https://ip.nandus.net] + When the script is properly installed, one can visit/curl/wget the website to just receive one's public IP address. This is very useful in combination with other scripts (check out my snap-dyndns packages for example). ``` @@ -13,6 +15,8 @@ max@laptop ~> wget -qO - ip.nandus.net ## Detailed return +Example: [/myself](https://ip4.nandus.net/myself) + Given the location of the script is `ip.example.com`, you can also receive more information about the HTTP headers or the IP's reverse lookup: ``` @@ -30,6 +34,17 @@ HOSTNAME: p5B3E4401.dip0.t-ipconnect.de IPv4 REVERSE LOOKUP: 91.62.68.1 ``` +Additionally you will be given a whois output of your IP. + +## Custom DNS and IP lookup + +Example DNS: [/dns/example.com](https://ip.nandus.net/dns/example.com) +Example IP: [/ip/8.8.8.8](https://ip.nandus.net/ip/8.8.8.8) + +You can also freely look up any hostname or IP. For domains, this will return a useful list of different DNS records (A, AAAA, NS, MX, TXT, SOA etc). + +You will also be given a whois output of the domain or IP. + ## Ideal domain setup In order to use IPv4- and IPv6-only domains, you'll have to have control over the DNS nameserver records of your own domain. diff --git a/index.php b/index.php index b345778..4a07908 100644 --- a/index.php +++ b/index.php @@ -2,16 +2,52 @@ $config = include('config.php'); +$m = isset($_GET['m']) ? $_GET['m'] : false; +$t = isset($_GET['t']) ? $_GET['t'] : false; + // Define method by get parameter if(empty($_GET)) { $method = "simple"; +} elseif($m === "dns" && ! empty($t)) { + $method = "dns"; +} elseif($m === "ip" && ! empty($t)) { + $method = "ip"; +} elseif($m === "myself" && empty($t)) { + $method = "myself"; } else { - $method = $_GET['m']; + $method = "unknown"; } if($method === "simple") { // simple - echo $_SERVER['REMOTE_ADDR'] . "\n"; -} elseif($method === "detailed" ) { // detailed + $ip = $_SERVER['REMOTE_ADDR']; + echo $ip . "\n"; + exit; +} elseif($method === "myself" ) { // detailed info about own IP + $ip = $_SERVER['REMOTE_ADDR']; + show_http_headers(); + show_hostname($ip); + show_reverse($hostname); + show_whois($ip); +} elseif($method === "ip" ) { // detailed info an IP + $ip = $t; + show_hostname($ip); + show_reverse($hostname); + show_whois($ip); +} elseif($method === "dns" ) { // detailed info a host name + $hostname = $t; + show_reverse($hostname); + show_dnsrecords($hostname); + show_whois($hostname); +} else if($method === "unknown") { // wrong URL + header("HTTP/1.1 301 Moved Permanently"); + header("Location:/".$config->detailed_uri.""); + exit; +} +footer(); + +// HTTP HEADERS +function show_http_headers() { + echo "

YOUR HTTP HEADERS

"; readheader("REMOTE_ADDR"); readheader("HTTP_X_FORWARDED_FOR"); readheader("HTTP_USER_AGENT"); @@ -21,33 +57,129 @@ if($method === "simple") { // simple readheader("HTTP_FORWARDED"); readheader("HTTP_HOST"); readheader("HTTP_URI"); - $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); echo "
"; - echo "HOSTNAME: " . $hostname; - echo "
"; - echo "IPv4 REVERSE LOOKUP: " . implode(', ', gethostbynamel($hostname)); - echo "

WHOIS:"; - $whois = shell_exec("whois " . $_SERVER['REMOTE_ADDR']); +} +// DNS LOOKUP +function show_hostname($ip) { + global $hostname; + $hostname = ip2hostname($ip); + echo "HOSTNAME: " . link_target($hostname); + echo "
\n"; +} +// REVERSE IP4 LOOKUP +function show_reverse($hostname) { + global $hostname; + $ip = hostname2ip($hostname); + echo "IPv4 REVERSE LOOKUP: " . link_target($ip); + echo "
\n"; +} +// WHOIS +function show_whois($target) { + echo "

WHOIS

\n"; + if(preg_match('/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/', $target)) { + // if domain, skip subdomains + preg_match('/[a-zA-Z\d]+\.[a-zA-Z\d]+$/', $target, $m); + $target = $m[0]; + } + $whois = shell_exec("whois " . $target ); echo "
"; print_r($whois); echo "
"; - echo "
"; - - echo "


"; - echo 'Plain IP address: IPv4 | IPv6'; - echo "
"; - echo 'Detailed header info: IPv4 | IPv6'; -} else { // wrong URL - header("HTTP/1.1 301 Moved Permanently"); - header("Location:/".$config->detailed_uri.""); - exit; + echo "
\n"; +} +// DNSRECORDS +function show_dnsrecords($hostname) { + echo "

DNS Records

\n"; + // A + $dnsrecords = dnsrecords($hostname, "DNS_A"); + foreach($dnsrecords as $key=>$value) { + echo "" . $value['type'] . ": "; + echo link_target($value['ip']) . "
\n"; + } + // AAAA + $dnsrecords = dnsrecords($hostname, "DNS_AAAA"); + foreach($dnsrecords as $key=>$value) { + echo "" . $value['type'] . ": "; + echo link_target($value['ipv6']) . "
\n"; + } + // MX + $dnsrecords = dnsrecords($hostname, "DNS_MX"); + foreach($dnsrecords as $key=>$value) { + echo "" . $value['type'] . ": "; + echo link_target($value['target']) . " (" . $value['pri'] . ")
\n"; + } + // NS + $dnsrecords = dnsrecords($hostname, "DNS_NS"); + foreach($dnsrecords as $key=>$value) { + echo "" . $value['type'] . ": "; + echo link_target($value['target']) . "
\n"; + } + // TXT + $dnsrecords = dnsrecords($hostname, "DNS_TXT"); + foreach($dnsrecords as $key=>$value) { + echo "" . $value['type'] . ": "; + $i = 0; + foreach($value['entries'] as $entry) { + if($i !== 0){echo " | ";} + echo $entry; + } + echo "
\n"; + } + // SOA + $dnsrecords = dnsrecords($hostname, "DNS_SOA"); + foreach($dnsrecords as $key=>$value) { + echo "" . $value['type'] . ": "; + echo "" . $value['mname'] . " (mname), "; + echo "" . $value['rname'] . " (rname), "; + echo "" . $value['serial'] . " (serial)
\n"; + } + + // OTHER + $dnsrecords = dnsrecords($hostname, "OTHER"); + if(!empty($dnsrecords)) { + echo "Other DNS records:
\n "; + echo "
"; print_r($dnsrecords); echo "
"; + echo "
\n"; + } +} +// PRINT FOOTER +function footer() { + global $config; + echo "


\n"; + echo 'Your plain IP address: IPv4 | IPv6'; + echo "
\n"; + echo 'Your detailed IP info: IPv4 | IPv6'; } -function readheader($value) -{ +// HELPER FUNCTIONS +function readheader($value) { echo "" . $value . ": "; // show desired value if(isset($_SERVER[$value])) { // only if desired header has been delivered echo $_SERVER[$value]; } - echo "
"; + echo "
\n"; +} +function ip2hostname($ip) { + $hostname = gethostbyaddr($ip); + return $hostname; +} +function hostname2ip($hostname) { + $ip = implode(', ', gethostbynamel($hostname)); + return $ip; +} +function dnsrecords($hostname, $record = "DNS_ALL") { + if($record === "OTHER") { + $dnsrecords = dns_get_record($hostname, DNS_ALL - DNS_A - DNS_AAAA - DNS_MX - DNS_NS - DNS_TXT - DNS_SOA); + } else { + $dnsrecords = dns_get_record($hostname, constant($record)); + } + return $dnsrecords; +} +function link_target($target) { + if(filter_var($target, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) || filter_var($target, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + // target is an IP + return "" . $target . ""; + } elseif(preg_match('/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/', $target)) { + return "" . $target . ""; + } } ?>