From 72a0a337f638afa9577a5c6efe5e2edcfcf8d2cb Mon Sep 17 00:00:00 2001 From: mfiscus Date: Sun, 26 Nov 2023 13:50:16 -0700 Subject: [PATCH] json api Ported (go/gin) json api from gomrefdash (https://github.com/kc1awv/gomrefdash) to (php/apache) urfd dashboard. This provides consistency between mrefd and urfd reflector dashboards and is intended to accelerate integration with dvref.com for an official URFD reflector registry. Updated dashboard version to 2.5.0 to help reflector admins determine compatibility with eventual dvref.com integration (>= 2.5.0). It has been extensively tested and is currently running on https://urf847.kk7mnz.com/json/reflector dvref.com integration has been tested at https://dvref.com/mrefd/m17-847 --- dashboard/json/.htaccess | 6 +++ dashboard/json/index.php | 63 ++++++++++++++++++++++++ dashboard/json/views/links.php | 24 +++++++++ dashboard/json/views/metadata.php | 23 +++++++++ dashboard/json/views/modulesinuse.php | 30 ++++++++++++ dashboard/json/views/peers.php | 22 +++++++++ dashboard/json/views/reflector.php | 70 +++++++++++++++++++++++++++ dashboard/json/views/stations.php | 36 ++++++++++++++ dashboard/json/views/status.php | 18 +++++++ dashboard/pgs/config.inc.php | 2 +- 10 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 dashboard/json/.htaccess create mode 100644 dashboard/json/index.php create mode 100644 dashboard/json/views/links.php create mode 100644 dashboard/json/views/metadata.php create mode 100644 dashboard/json/views/modulesinuse.php create mode 100644 dashboard/json/views/peers.php create mode 100644 dashboard/json/views/reflector.php create mode 100644 dashboard/json/views/stations.php create mode 100644 dashboard/json/views/status.php diff --git a/dashboard/json/.htaccess b/dashboard/json/.htaccess new file mode 100644 index 0000000..46623a5 --- /dev/null +++ b/dashboard/json/.htaccess @@ -0,0 +1,6 @@ +RewriteEngine On +RewriteBase /json/ +RewriteRule ^index\\.php$ - [L] +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule . /json/index.php [L] \ No newline at end of file diff --git a/dashboard/json/index.php b/dashboard/json/index.php new file mode 100644 index 0000000..90872a5 --- /dev/null +++ b/dashboard/json/index.php @@ -0,0 +1,63 @@ +SetXMLFile($Service['XMLFile']); +$Reflector->SetPIDFile($Service['PIDFile']); +$Reflector->LoadXML(); +$Reflector->SetFlagFile($_SERVER['DOCUMENT_ROOT'] . "/pgs/country.csv"); +$Reflector->LoadFlags(); + +$Request = $_SERVER['REQUEST_URI']; +$ViewDir = '/views/'; + +switch ($Request) { + case '/json/links': + require __DIR__ . $ViewDir . 'links.php'; + break; + + case '/json/metadata': + require __DIR__ . $ViewDir . 'metadata.php'; + break; + + case '/json/modulesinuse': + require __DIR__ . $ViewDir . 'modulesinuse.php'; + break; + + case '/json/peers': + require __DIR__ . $ViewDir . 'peers.php'; + break; + + case '/json/reflector': + require __DIR__ . $ViewDir . 'reflector.php'; + break; + + case '/json/stations': + require __DIR__ . $ViewDir . 'stations.php'; + break; + + case '/json/status': + require __DIR__ . $ViewDir . 'status.php'; + break; + + default: + header('Content-Type: text/plain'); + http_response_code(404); + echo('404 page not found'); + +} + +?> \ No newline at end of file diff --git a/dashboard/json/views/links.php b/dashboard/json/views/links.php new file mode 100644 index 0000000..ef34d51 --- /dev/null +++ b/dashboard/json/views/links.php @@ -0,0 +1,24 @@ +NodeCount();$i++) { + + // craft payload array + $payload[$i] = array( + 'callsign' => $Reflector->Nodes[$i]->GetCallSign() . ' ' . $Reflector->Nodes[$i]->GetSuffix(), + 'ip' => $Reflector->Nodes[$i]->GetIP(), + 'linkedmodule' => $Reflector->Nodes[$i]->GetLinkedModule(), + 'protocol' => $Reflector->Nodes[$i]->GetProtocol(), + 'connecttime' => date('Y-m-d\TH:i:sp', $Reflector->Nodes[$i]->GetConnectTime()), + 'lastheardtime' => date('Y-m-d\TH:i:sp', $Reflector->Nodes[$i]->GetLastHeardTime()) + ); + +} + + +// json encode payload array +$records = json_encode($payload); + +echo $records; + +?> \ No newline at end of file diff --git a/dashboard/json/views/metadata.php b/dashboard/json/views/metadata.php new file mode 100644 index 0000000..fad5ed8 --- /dev/null +++ b/dashboard/json/views/metadata.php @@ -0,0 +1,23 @@ + $PageOptions['DashboardVersion'], + 'ipV4' => @$Net4[0]['ip'], + 'ipV6' => @$Net6[0]['ipv6'], + 'reflector_callsign' => str_replace("XLX", "URF", $Reflector->GetReflectorName()), + 'reflector_version' => $Reflector->GetVersion(), + 'sysop_email' => $PageOptions['ContactEmail'] +); + + +// json encode payload array +$records = json_encode($payload); + +echo $records; + +?> \ No newline at end of file diff --git a/dashboard/json/views/modulesinuse.php b/dashboard/json/views/modulesinuse.php new file mode 100644 index 0000000..85ba9c4 --- /dev/null +++ b/dashboard/json/views/modulesinuse.php @@ -0,0 +1,30 @@ +GetModules(); +sort($Modules, SORT_STRING); + + +/*** Add modules to payload ***/ +for ($i=0;$i $Modules[$i] + ); + + $Users = $Reflector->GetNodesInModulesByID($Modules[$i]); + + for ($j=0;$jGetCallsignAndSuffixByID($Users[$j]); + + } + +} + + +// json encode payload array +$records = json_encode($payload); + +echo $records; + +?> \ No newline at end of file diff --git a/dashboard/json/views/peers.php b/dashboard/json/views/peers.php new file mode 100644 index 0000000..d427c93 --- /dev/null +++ b/dashboard/json/views/peers.php @@ -0,0 +1,22 @@ +PeerCount();$i++) { + + $payload[$i] = array( + 'callsign' => $Reflector->Peers[$i]->GetCallSign(), + 'ip' => $Reflector->Peers[$i]->GetIP(), + 'linkedmodule' => $Reflector->Peers[$i]->GetLinkedModule(), + 'connecttime' => date('Y-m-d\TH:i:sp', $Reflector->Peers[$i]->GetConnectTime()), + 'lastheardtime' => date('Y-m-d\TH:i:sp', $Reflector->Peers[$i]->GetLastHeardTime()) + ); + +} + + +// json encode payload array +$records = json_encode($payload); + +echo $records; + +?> \ No newline at end of file diff --git a/dashboard/json/views/reflector.php b/dashboard/json/views/reflector.php new file mode 100644 index 0000000..97c4b1f --- /dev/null +++ b/dashboard/json/views/reflector.php @@ -0,0 +1,70 @@ + date('Y-m-d\TH:i:sp', time()), + 'status' => $Status, + 'uptime' => $Reflector->GetServiceUptime() +); + + +/*** add data to payload ***/ +$payload['data'] = array( + 'filetime' => date('Y-m-d\TH:i:sp', filemtime($Service['XMLFile'])), + 'callsign' => str_replace("XLX", "URF", $Reflector->GetReflectorName()), + 'version' => $Reflector->GetVersion() +); + + +/*** Add peers to payload ***/ +for ($i=0;$i<$Reflector->PeerCount();$i++) { + + $payload['data']['peers'][] = array( + 'callsign' => $Reflector->Peers[$i]->GetCallSign(), + 'ip' => $Reflector->Peers[$i]->GetIP(), + 'linkedmodule' => $Reflector->Peers[$i]->GetLinkedModule(), + 'connecttime' => date('Y-m-d\TH:i:sp', $Reflector->Peers[$i]->GetConnectTime()), + 'lastheardtime' => date('Y-m-d\TH:i:sp', $Reflector->Peers[$i]->GetLastHeardTime()) + ); + +} + + +/*** Add nodes to payload ***/ +for ($i=0;$i<$Reflector->NodeCount();$i++) { + + // craft payload array + $payload['data']['nodes'][] = array( + 'callsign' => $Reflector->Nodes[$i]->GetCallSign() . ' ' . $Reflector->Nodes[$i]->GetSuffix(), + 'ip' => $Reflector->Nodes[$i]->GetIP(), + 'linkedmodule' => $Reflector->Nodes[$i]->GetLinkedModule(), + 'protocol' => $Reflector->Nodes[$i]->GetProtocol(), + 'connecttime' => date('Y-m-d\TH:i:sp', $Reflector->Nodes[$i]->GetConnectTime()), + 'lastheardtime' => date('Y-m-d\TH:i:sp', $Reflector->Nodes[$i]->GetLastHeardTime()) + ); + +} + + +/*** Add stations to payload ***/ +for ($i=0;$i<$Reflector->StationCount();$i++) { + + // craft payload array + $payload['data']['stations'][] = array( + 'callsign' => $Reflector->Stations[$i]->GetCallSign(), + 'vianode' => $Reflector->Stations[$i]->GetVia(), + 'onmodule' => $Reflector->Stations[$i]->GetModule(), + 'viapeer' => $Reflector->Stations[$i]->GetPeer(), + 'lastheardtime' => date('Y-m-d\TH:i:sp', $Reflector->Stations[$i]->GetLastHeardTime()) + ); + +} + + +// json encode payload array +$records = json_encode($payload); + +echo $records; + +?> \ No newline at end of file diff --git a/dashboard/json/views/stations.php b/dashboard/json/views/stations.php new file mode 100644 index 0000000..fac3dae --- /dev/null +++ b/dashboard/json/views/stations.php @@ -0,0 +1,36 @@ +StationCount();$i++) { + + $tmp = preg_split('/\s+/', $Reflector->Stations[$i]->GetCallSign(), -1, PREG_SPLIT_NO_EMPTY); + $Callsign = $tmp[0]; + + $tmp = preg_split('/\s+/', $Reflector->Stations[$i]->GetVia(), -1, PREG_SPLIT_NO_EMPTY); + $CallsignSuffix = $tmp[1]; + + // craft payload array + $payload['stations'][$i] = array( + 'callsign' => $Callsign, + 'callsignsuffix' => $CallsignSuffix, + 'vianode' => $Reflector->Stations[$i]->GetVia(), + 'onmodule' => $Reflector->Stations[$i]->GetModule(), + 'lastheard' => date('Y-m-d\TH:i:sp', $Reflector->Stations[$i]->GetLastHeardTime()) + ); + + list ($CountryCode, $Country) = $Reflector->GetFlag($Reflector->Stations[$i]->GetCallSign()); + + $payload['stations'][$i]['country'] = array ( + 'country' => $Country, + 'countrycode' => $CountryCode + ); + +} + + +// json encode payload array +$records = json_encode($payload); + +echo $records; + +?> \ No newline at end of file diff --git a/dashboard/json/views/status.php b/dashboard/json/views/status.php new file mode 100644 index 0000000..947b450 --- /dev/null +++ b/dashboard/json/views/status.php @@ -0,0 +1,18 @@ + date('U', time()), + 'lasturfdupdate' => date('U', filemtime($Service['XMLFile'])), + 'reflectorstatus' => $ReflectorStatus, + 'reflectoruptimeseconds' => $Reflector->GetServiceUptime() +); + + +// json encode payload array +$records = json_encode($payload); + +echo $records; + +?> diff --git a/dashboard/pgs/config.inc.php b/dashboard/pgs/config.inc.php index ee027e0..bfd71d6 100644 --- a/dashboard/pgs/config.inc.php +++ b/dashboard/pgs/config.inc.php @@ -16,7 +16,7 @@ $PageOptions = array(); $PageOptions['ContactEmail'] = 'your_email'; // Support E-Mail address -$PageOptions['DashboardVersion'] = '2.4.1'; // Dashboard Version +$PageOptions['DashboardVersion'] = '2.5.0'; // Dashboard Version $PageOptions['PageRefreshActive'] = true; // Activate automatic refresh $PageOptions['PageRefreshDelay'] = '10000'; // Page refresh time in miliseconds