Steven Smith revised this gist . Go to revision
1 file changed, 56 insertions
portopen.php(file created)
| @@ -0,0 +1,56 @@ | |||
| 1 | + | <?php | |
| 2 | + | // A script to check if a given port is open on the connecting host. | |
| 3 | + | // IP ranges included are for cloudflare, replace with an array containing your proxy server IP ranges | |
| 4 | + | // https://b303.me/portopen.php?port=80 | |
| 5 | + | header("Content-Type: application/json"); | |
| 6 | + | header("Access-Control-Allow-Origin: *"); | |
| 7 | + | function cidr_match($ip, $ranges) { | |
| 8 | + | $out = array(); | |
| 9 | + | foreach ($ranges as $range) { | |
| 10 | + | list ($subnet, $bits) = explode('/', $range); | |
| 11 | + | $ip = ip2long($ip); | |
| 12 | + | $subnet = ip2long($subnet); | |
| 13 | + | $mask = -1 << (32 - $bits); | |
| 14 | + | $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned | |
| 15 | + | array_push($out, ($ip & $mask) == $subnet); | |
| 16 | + | } | |
| 17 | + | return $out; | |
| 18 | + | } | |
| 19 | + | function get_ip() { | |
| 20 | + | $ranges = array("103.21.244.0/22","103.22.200.0/22","103.31.4.0/22", | |
| 21 | + | "104.16.0.0/12","108.162.192.0/18","131.0.72.0/22", | |
| 22 | + | "141.101.64.0/18","162.158.0.0/15","172.64.0.0/13", | |
| 23 | + | "173.245.48.0/20","188.114.96.0/20","190.93.240.0/20", | |
| 24 | + | "197.234.240.0/22","198.41.128.0/17","199.27.128.0/21", | |
| 25 | + | "127.0.0.0/8"); | |
| 26 | + | if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && count(array_unique(cidr_match($_SERVER["HTTP_X_FORWARDED_FOR"], $ranges))) === 1 ) { | |
| 27 | + | $ip = $_SERVER["HTTP_X_FORWARDED_FOR"]; | |
| 28 | + | } else { | |
| 29 | + | $ip = $_SERVER["REMOTE_ADDR"]; | |
| 30 | + | } | |
| 31 | + | return $ip; | |
| 32 | + | } | |
| 33 | + | function check_port($ip, $port) { | |
| 34 | + | $connection = @fsockopen($ip, $port, $errno, $errstr, 1); | |
| 35 | + | if (is_resource($connection)) { | |
| 36 | + | fclose($connection); | |
| 37 | + | return true; | |
| 38 | + | } else { | |
| 39 | + | return false; | |
| 40 | + | } | |
| 41 | + | } | |
| 42 | + | if (!isset($_GET["port"])) { | |
| 43 | + | die(json_encode(array("error" => "Specify port parameter"), JSON_PRETTY_PRINT)); | |
| 44 | + | } | |
| 45 | + | if (!is_numeric($_GET["port"])) { | |
| 46 | + | die(json_encode(array("error" => "Numbers only please"), JSON_PRETTY_PRINT)); | |
| 47 | + | } | |
| 48 | + | $ip = get_ip(); | |
| 49 | + | echo json_encode( | |
| 50 | + | array( | |
| 51 | + | "host" => $ip, | |
| 52 | + | "port" => intval($_GET["port"]), | |
| 53 | + | "open" => check_port(get_ip(), intval($_GET["port"])) | |
| 54 | + | ), | |
| 55 | + | JSON_PRETTY_PRINT | |
| 56 | + | ); | |
Newer
Older