API to test TCP connections

This commit is contained in:
Bofh 2022-11-22 11:53:23 +01:00
parent 75d4fa1081
commit a8128d1a00
1 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1,21 @@
<?php chdir('../../../../') ?>
<?php require 'base.php' ?>
<?php
$uri = trim($_GET['uri']);
if (!preg_match('/^\d+\.\d+\.\d+\.\d+:\d+$/', $uri)) {
http_response_code(400);
apiresult(['error' => 'The given URI must match IPV4_HOST:PORT scheme']);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://$uri");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$error_msg = curl_error($ch);
curl_close($ch);
if (strpos(strtolower($error_msg), 'connection refused'))
apiresult(['error' => 'Could not connect to host: '.$uri]);
apiresult(['status' => 'ok']);