Skip to content

Commit

Permalink
v2.9 - Expose (Most of) the Mod Script in Settings (#99)
Browse files Browse the repository at this point in the history
* expose new opts

* check if on untagged commit for footer
  • Loading branch information
ipitio authored May 2, 2024
1 parent c24fb4b commit 90c66d6
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 47 deletions.
2 changes: 1 addition & 1 deletion api_speedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function whichSpeedtest()
return 'sivel\'s';
}

return 'LibreSpeed';
return 'librespeed';
}

return 'no';
Expand Down
Binary file modified img/st-chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/st-pref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 35 additions & 7 deletions scripts/pi-hole/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,17 @@ $(function () {
const speedtestChartPreview = $("#speedtestchartpreview");
const speedtestChartPreviewBtn = $("#speedtestchartpreviewBtn");

const speedtestUpdate = $("#speedtestupdate");
const speedtestUninstall = $("#speedtestuninstall");
const speedtestDelete = $("#speedtestdelete");
const speedtestDeleteLabel = speedtestDelete.parent().children("label");

const speedtestLog = $("#latestLog");
const speedtestLogBtn = $("#latestLogBtn");

const speedtestCLIOfficial = $("#speedtestcliOfficial");
const speedtestCLISivel = $("#speedtestcliSivel");
const speedtestCLILibre = $("#speedtestcliLibre");

const speedtestSubmit = $("#st-submit");
const defaultClass = "btn-primary";
const colorClasses = ["btn-success", "btn-warning", "btn-danger"];
Expand All @@ -502,6 +505,8 @@ $(function () {
speedtestChartType.prop("checked", type === "bar");
localStorage.setItem("speedtest_chart_type", type);

var speedtestCLISelected = false;

const preCode = content => {
const pre = document.createElement("pre");
const code = document.createElement("code");
Expand Down Expand Up @@ -557,7 +562,7 @@ $(function () {
const serviceStatus = () => {
whichSpeedtest();
const speedtestVersion = localStorage.getItem("speedtest") || "unknown";
let cliText = "Will use official CLI";
let cliText = "Will install CLI before next run";
if (speedtestVersion !== "no") {
cliText = `Using ${speedtestVersion} CLI`;
}
Expand Down Expand Up @@ -855,7 +860,6 @@ $(function () {
document.addEventListener("DOMContentLoaded", function () {
speedtestDays.attr("value", speedtestDays.val());
speedtestChartTypeSave.attr("value", null);
speedtestUpdate.attr("value", null);
speedtestUninstall.attr("value", null);
speedtestDelete.attr("value", null);
speedtestTest.attr("value", null);
Expand Down Expand Up @@ -888,10 +892,6 @@ $(function () {
previewChart(speedtestChartPreview.find("div").length === 0);
});

speedtestUpdate.on("click", function () {
speedtestUpdate.attr("value", speedtestUpdate.attr("value") ? null : "up");
});

speedtestTest.on("click", function () {
speedtestTest.attr("value", speedtestTest.attr("value") ? null : "yes");
const status = speedtestStatus.find("pre");
Expand Down Expand Up @@ -948,6 +948,18 @@ $(function () {
canRestore();
});

speedtestCLIOfficial.on("click", function () {
speedtestCLISelected = true;
});

speedtestCLISivel.on("click", function () {
speedtestCLISelected = true;
});

speedtestCLILibre.on("click", function () {
speedtestCLISelected = true;
});

setInterval(() => {
if (speedtestStatus.find("pre").length > 0) {
serviceStatus();
Expand All @@ -972,6 +984,22 @@ $(function () {
closestServers();
}

if (!speedtestCLISelected) {
whichSpeedtest();
const currentCLI = localStorage.getItem("speedtest");
if (currentCLI === "official") {
speedtestCLIOfficial.prop("checked", true);
}

if (currentCLI === "sivel's") {
speedtestCLISivel.prop("checked", true);
}

if (currentCLI === "librespeed") {
speedtestCLILibre.prop("checked", true);
}
}

canRestore();
}, 1000);
});
35 changes: 35 additions & 0 deletions scripts/pi-hole/php/header_authenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,41 @@ function getNumberOfDaysInDB($dbSpeedtest)
$speedtestcharttype = $setupVars['SPEEDTEST_CHART_TYPE'];
}

function speedtestExecute($command)
{
$output = array();
$return_status = -1;
exec('/bin/bash -c \''.$command.'\'', $output, $return_status);

if ($return_status !== 0) {
trigger_error("Executing {$command} failed.", E_USER_WARNING);
}

return array('data' => implode("\n", $output));
}

function whichSpeedtest()
{
if (file_exists('/usr/bin/speedtest')) {
$officialInstalled = speedtestExecute('. /opt/pihole/speedtestmod/lib.sh ; notInstalled speedtest && echo "false" || echo "true"')['data'];

if ($officialInstalled === 'true') {
return 'official';
}

$version = speedtestExecute('/usr/bin/speedtest -h')['data'];

if (strpos($version, 'sivel') !== false) {
return 'sivel\'s';
}

return 'librespeed';
}

return 'no';
}

$speedtestcli = whichSpeedtest();
$piholeFTLConf = piholeFTLConfig();

require 'header.php';
Expand Down
86 changes: 58 additions & 28 deletions scripts/pi-hole/php/savesettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,43 +618,73 @@ function addStaticDHCPLease($mac, $ip, $hostname)
}
pihole_execute('-a -st '.trim($charttype));

$success .= 'The Speedtest settings have been updated';
$success .= 'Speedtest Settings have been updated';

if (isset($_POST['speedtesttest'])) {
$success .= ' and a speedtest has been started';
pihole_execute('-a -sn', !isset($_POST['speedtestuninstall']));
}

$run_mod_script = false;
$opt_mod_script = '';

if (isset($_POST['speedtestbackup'])) {
$run_mod_script = true;
$opt_mod_script .= ' -b';
}

if (isset($_POST['speedtestupgrade'])) {
$run_mod_script = true;
$opt_mod_script .= ' -u';
}

if (isset($_POST['speedtestupdate'])) {
$success .= ' and Pi-hole will be updated';
if (isset($_POST['speedtestuninstall'])) {
$success .= ', but the Mod will be uninstalled';
if (isset($_POST['speedtestdelete'])) {
$success .= ' and its history will be deleted';
pihole_execute('-a -up un db', true);
} else {
pihole_execute('-a -up un', true);
}
} else {
if (isset($_POST['speedtestdelete'])) {
$success .= ' and its history will be modified';
pihole_execute('-a -up db', true);
} else {
pihole_execute('-a -up', true);
}
}
} elseif (isset($_POST['speedtestuninstall'])) {
$success .= ' and the Mod will be uninstalled';
if (isset($_POST['speedtestdelete'])) {
$success .= ' and its history will be deleted';
pihole_execute('-a -un db', true);
} else {
pihole_execute('-a -un', true);
$run_mod_script = true;
}

if (isset($_POST['speedtestonline'])) {
$run_mod_script = true;
$opt_mod_script .= ' -o';
}

if (isset($_POST['speedtestreinstall'])) {
$run_mod_script = true;
$opt_mod_script .= ' -r';
}

if (isset($_POST['speedtestuninstall'])) {
$run_mod_script = true;
$opt_mod_script .= ' -n';
}

if (isset($_POST['speedtestdelete'])) {
$run_mod_script = true;
$opt_mod_script .= ' -d';
}

if (isset($_POST['speedtestverbose'])) {
$run_mod_script = true;
$opt_mod_script .= ' -x';
}

if (isset($_POST['speedtestcli']) && whichSpeedtest() != $_POST['speedtestcli']) {
$run_mod_script = true;
$chosen_cli = '';

if ($_POST['speedtestcli'] == 'sivel\'s') {
$chosen_cli = 'sivel';
} elseif ($_POST['speedtestcli'] == 'librespeed') {
$chosen_cli = 'libre';
}
} elseif (isset($_POST['speedtestdelete'])) {
$success .= ' and its history will be modified';
pihole_execute('-a -db', true);

$opt_mod_script .= ' -s'.$chosen_cli;
}

if ($run_mod_script) {
$success .= ' and the Mod Script has been executed';
pihole_execute('-a -sm'.$opt_mod_script, true);
}

break;
default:
// Option not found
Expand Down
11 changes: 7 additions & 4 deletions scripts/pi-hole/php/update_checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ function checkUpdate($currentVersion, $latestVersion)
$versions = parse_ini_file($versionsfile);

// Get Pi-hole core branch / version / commit
// Check if on a dev branch
// Check if on a dev branch or untagged commit
$core_branch = $versions['CORE_BRANCH'];
if ($core_branch !== 'master') {
$core_version = $versions['CORE_VERSION'];
if ($core_branch !== 'master' || !strpos($core_version, '.')) {
$core_current = 'vDev';
$core_commit = $versions['CORE_VERSION'];
} else {
Expand All @@ -46,7 +47,8 @@ function checkUpdate($currentVersion, $latestVersion)

// Get Pi-hole web branch / version / commit
$web_branch = $versions['WEB_BRANCH'];
if ($web_branch !== 'master') {
$web_version = $versions['WEB_VERSION'];
if ($web_branch !== 'master' || !strpos($web_version, '.')) {
$web_current = 'vDev';
$web_commit = $versions['WEB_VERSION'];
} else {
Expand All @@ -55,7 +57,8 @@ function checkUpdate($currentVersion, $latestVersion)

// Get Speedtest Mod branch / version / commit
$speedtest_branch = $versions['SPEEDTEST_BRANCH'];
if ($speedtest_branch !== 'master') {
$speedtest_version = $versions['SPEEDTEST_VERSION'];
if ($speedtest_branch !== 'master' || !strpos($speedtest_version, '.')) {
$speedtest_current = 'vDev';
$speedtest_commit = $versions['SPEEDTEST_VERSION'];
} else {
Expand Down
56 changes: 49 additions & 7 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1615,29 +1615,71 @@ class="form-control"
<div class="box-body">
<div class="row">
<div class="col-md-12">

<div class="form-group col-md-6">
<strong>Mod the Mod</strong>
<p>With the Script</p>
<div class="form-group col-md-6">
<strong>Restoration options</strong>
<p>For when something's the matter</p>
<div>
<input type="checkbox" name="speedtestupdate" id="speedtestupdate">
<label for="speedtestupdate">(Re)install Latest</label>
<input type="checkbox" name="speedtestbackup" id="speedtestbackup">
<label for="speedtestbackup">Create Backup</label>
</div>
<div>
<input type="checkbox" name="speedtestonline" id="speedtestonline">
<label for="speedtestonline">Restore Online</label>
</div>
<div>
<input type="checkbox" name="speedtestreinstall" id="speedtestreinstall">
<label for="speedtestreinstall">Repair Mod</label>
</div>
<div>
<input type="checkbox" name="speedtestuninstall" id="speedtestuninstall">
<label for="speedtestuninstall">Uninstall Mod</label>
</div>
</div>

<div class="form-group col-md-6">
<strong>Speedtest CLI</strong>
<p>To be tried first</p>
<div>
<input type="radio" name="speedtestcli" id="speedtestcliOfficial" value="official" <?php if ($speedtestcli === 'official' || $speedtestcli === 'no') { ?>checked<?php } ?>>
<label for="speedtestcliOfficial"><a href="https://www.speedtest.net/apps/cli" target="_blank">official</a></label>
</div>
<div>
<input type="radio" name="speedtestcli" id="speedtestcliSivel" value="sivel's" <?php if ($speedtestcli === "sivel's") { ?>checked<?php } ?>>
<label for="speedtestcliSivel"><a href="https://github.com/sivel/speedtest-cli" target="_blank">sivel's</a></label>
</div>
<div>
<input type="radio" name="speedtestcli" id="speedtestcliLibre" value="librespeed" <?php if ($speedtestcli === 'librespeed') { ?>checked<?php } ?>>
<label for="speedtestcliLibre"><a href="https://github.com/librespeed/speedtest-cli" target="_blank">librespeed</a></label>
</div>
<p>If you just confirmed a new CLI, please wait a moment for the selection to update.</p>
</div>

<div class="form-group col-md-6">
<strong>Main options</strong>
<p>For when nothing's (hopefully) the matter</p>
<div>
<input type="checkbox" name="speedtestupgrade" id="speedtestupgrade">
<label for="speedtestupgrade">Update Pi-hole</label>
</div>
<div>
<input type="checkbox" name="speedtestupdate" id="speedtestupdate">
<label for="speedtestupdate">Update Mod</label>
</div>
<div>
<input type="checkbox" name="speedtestdelete" id="speedtestdelete">
<label for="speedtestdelete">Clear History</label>
</div>
<div>
<input type="checkbox" name="speedtestverbose" id="speedtestverbose">
<label for="speedtestverbose">Verbose Logging</label>
</div>
<p id="latestLog" style="margin-top: 1vw;">
<button class="btn btn-default" id="latestLogBtn" type="button">Show latest log</button>
</p>
</div>

<div class="form-group col-md-6">
<label for="speedtestserver">Set specific server</label>
<label for="speedtestserver">Speedtest server</label>
<p>To be tried first</p>
<div class="input-group">
<div class="input-group-addon">id</div>
Expand Down

0 comments on commit 90c66d6

Please sign in to comment.