Skip to content

Commit

Permalink
feat: new usage system
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaser committed Aug 22, 2024
1 parent 5fa62e6 commit a077cf5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/usr/local/emhttp/plugins/tailscale/daily.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';

require_once "{$docroot}/plugins/tailscale/include/common.php";
require_once "{$docroot}/plugins/tailscale/include/tailscale-status.php";

foreach (glob("{$docroot}/plugins/tailscale/include/daily/*.php") as $file) {
require_once $file;
Expand Down
57 changes: 46 additions & 11 deletions src/usr/local/emhttp/plugins/tailscale/include/daily/ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

$endpoint = "https://plugin-usage.edacerton.win/";

function send_usage($url)
function send_usage($url, $content)
{
$body = json_encode($content);
$token = file_get_contents($url . '?connect');

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);

$headers = [
'Content-Type: application/json',
'Authorization: Bearer ' . $token
];

curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $body);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
curl_setopt($c, CURLOPT_USERAGENT, 'plugin-metrics/1.0.0');

curl_exec($c);
if ( ! curl_errno($c)) {
$info = curl_getinfo($c);
Expand All @@ -20,28 +34,49 @@ function send_usage($url)
}

$version = parse_ini_file('/var/local/emhttp/plugins/tailscale/tailscale.ini');
$query = array(

$prefs = getTailscalePrefs();

$exit = false;
$subnet = false;
foreach($prefs->AdvertiseRoutes as $net)
{
switch ($net) {
case "0.0.0.0/0":
case "::/0":
$exit = true;
break;
default:
$subnet = true;
break;
}
}

$content = array(
'clientId' => hash("crc32b", $var['flashGUID']),
'plugin' => 'tailscale',
'version' => $version['VERSION'],
'branch' => $version['BRANCH'],
'unraid' => $var['version']
'plugin_version' => $version['VERSION'],
'plugin_branch' => $version['BRANCH'],
'unraid_version' => $var['version'],
'bool1' => boolval($tailscale_config['ACCEPT_DNS']),
'bool2' => boolval($tailscale_config['ACCEPT_ROUTES']),
'bool3' => boolval($tailscale_config['INCLUDE_INTERFACE']),
'bool4' => $subnet,
'bool5' => $exit
);

$queryString = http_build_query($query);

logmsg("Sending usage data: {$queryString}");
logmsg("Sending usage data");
$attempts = 0;
$delay = 0;
do {
sleep(rand($delay, $delay + 60));
$delay = 300;

$attempts++;
$result = send_usage("{$endpoint}?{$queryString}");
} while (($result != '201') && ($attempts < 3));
$result = send_usage($endpoint, $content);
} while (($result != '200') && ($attempts < 3));

if ($result != '201') {
if ($result != '200') {
logmsg("Error occurred while transmitting usage data.");
}
} else {
Expand Down

0 comments on commit a077cf5

Please sign in to comment.