A simple PHP script to tell a visitor's IP address and more information. Similar to ipecho.net/plain, but better :)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.4 KiB

<?php
$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 = "unknown";
}
if($method === "simple") { // simple
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip . "\n";
exit;
} elseif($method === "myself" ) { // detailed info about own IP
$ip = $_SERVER['REMOTE_ADDR'];
print_header();
show_http_headers();
show_hostname($ip);
show_reverse($hostname);
show_ipinfo($ip);
show_whois($ip);
} elseif($method === "ip" ) { // detailed info an IP
$ip = $t;
print_header();
show_hostname($ip);
show_reverse($hostname);
show_ipinfo($ip);
show_whois($ip);
} elseif($method === "dns" ) { // detailed info a host name
$hostname = $t;
print_header();
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;
}
print_footer();
// HTTP HEADERS
function show_http_headers() {
echo "<h2>YOUR HTTP HEADERS</h2>";
readheader("REMOTE_ADDR");
readheader("HTTP_X_FORWARDED_FOR");
readheader("HTTP_USER_AGENT");
readheader("HTTP_CLIENT_IP");
readheader("HTTP_X_FORWARDED");
readheader("HTTP_FORWARDED_FOR");
readheader("HTTP_FORWARDED");
readheader("HTTP_HOST");
readheader("HTTP_URI");
echo "<br />";
}
// DNS LOOKUP
function show_hostname($ip) {
global $hostname;
$hostname = ip2hostname($ip);
echo "<strong>HOSTNAME</strong>: " . link_target($hostname);
echo "<br />\n";
}
// REVERSE IP4 LOOKUP
function show_reverse($hostname) {
global $hostname;
$ip = hostname2ip($hostname);
echo "<strong>IPv4 REVERSE LOOKUP</strong>: " . link_target($ip);
echo "<br />\n";
}
// IPINFO
function show_ipinfo($ip) {
echo "<h2>IPINFO</h2>\n";
$json = file_get_contents("http://ipinfo.io/" . $ip);
$data = json_decode($json, TRUE);
echo "<strong>LOCATION</strong>: " . $data['country'] .", ". $data['region'] .", ". $data['city'] . "<br />\n";
echo "<strong>MAP</strong>: <a href='https://www.openstreetmap.org/search?query=" . $data['loc'] . "#map=10/0/0'>" . $data['loc'] . "</a><br />\n";
echo "<strong>AS</strong>: " . $data['org'] . "<br />\n";
}
// WHOIS
function show_whois($target) {
echo "<h2>WHOIS</h2>\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("timeout 5 whois " . $target );
echo "<pre>"; print_r($whois); echo "</pre>";
echo "<br />\n";
}
// DNSRECORDS
function show_dnsrecords($hostname) {
echo "<h2>DNS Records</h2>\n";
// A
$dnsrecords = dnsrecords($hostname, "DNS_A");
foreach($dnsrecords as $key=>$value) {
echo "<strong>" . $value['type'] . "</strong>: ";
echo link_target($value['ip']) . "<br />\n";
}
// AAAA
$dnsrecords = dnsrecords($hostname, "DNS_AAAA");
foreach($dnsrecords as $key=>$value) {
echo "<strong>" . $value['type'] . "</strong>: ";
echo link_target($value['ipv6']) . "<br />\n";
}
// MX
$dnsrecords = dnsrecords($hostname, "DNS_MX");
foreach($dnsrecords as $key=>$value) {
echo "<strong>" . $value['type'] . "</strong>: ";
echo link_target($value['target']) . " (" . $value['pri'] . ")<br />\n";
}
// NS
$dnsrecords = dnsrecords($hostname, "DNS_NS");
foreach($dnsrecords as $key=>$value) {
echo "<strong>" . $value['type'] . "</strong>: ";
echo link_target($value['target']) . "<br />\n";
}
// TXT
$dnsrecords = dnsrecords($hostname, "DNS_TXT");
foreach($dnsrecords as $key=>$value) {
echo "<strong>" . $value['type'] . "</strong>: ";
$i = 0;
foreach($value['entries'] as $entry) {
if($i !== 0){echo " | ";}
echo $entry;
}
echo "<br />\n";
}
// SOA
$dnsrecords = dnsrecords($hostname, "DNS_SOA");
foreach($dnsrecords as $key=>$value) {
echo "<strong>" . $value['type'] . "</strong>: ";
echo "" . $value['mname'] . " (mname), ";
echo "" . $value['rname'] . " (rname), ";
echo "" . $value['serial'] . " (serial)<br />\n";
}
// OTHER
$dnsrecords = dnsrecords($hostname, "OTHER");
if(!empty($dnsrecords)) {
echo "<strong>Other DNS records</strong>:<br/>\n ";
echo "<pre>"; print_r($dnsrecords); echo "</pre>";
echo "<br />\n";
}
}
// PRINT HEADER
function print_header() {
print '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>IP and DNS information</title>
<link rel="icon" type="image/png" href="/favicon.png">
</head>
<body>
';
}
// PRINT FOOTER
function print_footer() {
global $config;
echo "<br /><hr /><br />\n";
echo 'Your plain IP address: <a href="//'.$config->ip4domain.'">IPv4</a> | <a href="//'.$config->ip6domain.'">IPv6</a>';
echo "<br />\n";
echo 'Your detailed IP info: <a href="//'.$config->ip4domain.'/'.$config->detailed_uri.'">IPv4</a> | <a href="//'.$config->ip6domain.'/'.$config->detailed_uri.'">IPv6</a>';
echo '</body></html>';
}
// HELPER FUNCTIONS
function readheader($value) {
echo "<strong>" . $value . "</strong>: "; // show desired value
if(isset($_SERVER[$value])) { // only if desired header has been delivered
echo $_SERVER[$value];
}
echo "<br />\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 "<a href='/ip/" . $target . "/'>" . $target . "</a>";
} elseif(preg_match('/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/', $target)) {
return "<a href='/dns/" . $target . "/'>" . $target . "</a>";
}
}
?>