Skip to content

Instantly share code, notes, and snippets.

@irfanhussain22
Last active January 10, 2022 04:19
Show Gist options
  • Save irfanhussain22/a774b204334e12bbbc7ce20b7ad6d303 to your computer and use it in GitHub Desktop.
Save irfanhussain22/a774b204334e12bbbc7ce20b7ad6d303 to your computer and use it in GitHub Desktop.
<?php
sleep(1); // This is to make sure that the python script have finished committing the records in the table.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Connection checking:
$con = mysqli_connect('', '', '', '');
if (!$con)
{
echo "<h2>MySQL Error!</h2>";
exit;
}
$source_ip = $_SERVER['REMOTE_ADDR'];
$source_port = $_SERVER['REMOTE_PORT'];
$queries = array();
parse_str($_SERVER['QUERY_STRING'], $queries);
$sql_query = "SELECT * FROM client_info where source_ip='{$source_ip}' and source_port={$source_port} order by updated_time desc limit 1;";
// Perform query
if ($q = mysqli_query($con, $sql_query))
{
$ttl = 0;
for ($c = 0;$c < mysqli_num_rows($q);$c++)
{
$f = mysqli_fetch_array($q);
$ttl = $f[2];
}
mysqli_free_result($q);
$isp_name = "UNKNOWN";
$isp_connection_type = "UNKNOWN";
$ip_row_num = "";
$sql_query = "SELECT * FROM provider_info where INET_ATON('{$source_ip}') BETWEEN INET_ATON(from_ip) AND INET_ATON(to_ip);";
if ($q = mysqli_query($con, $sql_query))
{
$rowcount = mysqli_num_rows($q);
if ($rowcount > 0)
{
$row = $q->fetch_row();
$id = $row[0];
$isp_name = $row[5];
$isp_connection_type = $row[4];
$ip_row_num = $row[0];
$isp_ttl = $row[6];
}
else
{
mysqli_free_result($q);
$sql_query = "INSERT INTO `provider_info` (`id`, `ip_block`, `conn_type`, `isp_name`, `ttl_value`) VALUES (NULL, '{$source_ip}/32', 'UNKNOWN', 'UNKNOWN', '{$ttl}');";
if (!mysqli_query($con, $sql_query))
{
echo "Error: " . $sql_query . "<br>" . mysqli_error($con);
exit;
}
$isp_ttl = $ttl;
}
echo "Your ISP -> {$isp_name} -> {$isp_connection_type}<br>";
if ($ttl && $ttl < $isp_ttl)
{
echo "You are most likely TETHERING.<br>";
}
else
{
echo "NO TETHERING Detected.<br>";
}
if ($queries && $queries['diag'])
{
echo "<br>-----Diagnostics Info------<br>";
echo "Source IP {$source_ip} :: Source port {$source_port} :: TTL {$ttl}";
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment