Skip to content

Commit

Permalink
support librespeed (#89)
Browse files Browse the repository at this point in the history
* extra checks

* typos

* support librespeed

* dont filter

* redirect

* words
  • Loading branch information
ipitio authored Feb 28, 2024
1 parent 4ed34ae commit ca5a063
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
10 changes: 9 additions & 1 deletion api_speedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$setupVars = parse_ini_file('/etc/pihole/setupVars.conf');

$cmdLog = '[[ -f /tmp/pimod.log ]] && cat /tmp/pimod.log || { [[ -f /var/log/pihole/mod.log ]] && cat /var/log/pihole/mod.log || echo ""; }';
$cmdServers = 'speedtest -h | grep -q official && sudo speedtest -L || speedtest --secure --list';
$cmdServers = '/usr/bin/speedtest -h | grep -q official && sudo /usr/bin/speedtest -L || /usr/bin/speedtest --secure --list 2>&1';
$cmdRun = '[[ -f /tmp/speedtest.log ]] && cat /tmp/speedtest.log || { [[ -f /etc/pihole/speedtest.log ]] && cat /etc/pihole/speedtest.log || echo ""; }';
$cmdServersCurl = "curl 'https://c.speedtest.net/speedtest-servers-static.php' --compressed -H 'Upgrade-Insecure-Requests: 1' -H 'DNT: 1' -H 'Sec-GPC: 1'";
$cmdServersJSON = "curl 'https://www.speedtest.net/api/js/servers' --compressed -H 'Upgrade-Insecure-Requests: 1' -H 'DNT: 1' -H 'Sec-GPC: 1'";
Expand Down Expand Up @@ -54,6 +54,9 @@
if (isset($_GET['getNumberOfDaysInDB'])) {
$data = array_merge($data, getNumberOfDaysInDB($dbSpeedtest));
}
if (isset($_GET['isLibrespeed'])) {
$data = array_merge($data, array('data' => isLibrespeed()));
}
}

function hasSpeedTestBackup($dbSpeedtestOld)
Expand Down Expand Up @@ -335,3 +338,8 @@ function getStatusCmd()

return $cmdStatus;
}

function isLibrespeed()
{
return strpos(speedtestExecute('/usr/bin/speedtest --version')['data'], 'LibreSpeed') !== false;
}
44 changes: 27 additions & 17 deletions scripts/pi-hole/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,25 +739,35 @@ $(function () {
};

if (!cmds || cmds.length === 0) {
cmds = ["JSONClosestServers", "getClosestServers", "curlClosestServers"];
}

$.ajax({
url: `api.php?${cmds[0]}`,
dataType: "json",
})
.done(function (data) {
const serversInfo = data?.data;
if (serversInfo) {
speedtestServerCtr.find("p").remove();
codeBlock(speedtestServerCtr, serversInfo, speedtestServerBtn, "servers");
$.ajax({
url: `api.php?isLibrespeed`,
dataType: "json",
}).done(function (data) {
const librespeed = data?.data;
if (librespeed) {
closestServers(["getClosestServers"]);
} else {
tryNextCmd();
closestServers(["JSONClosestServers", "getClosestServers", "curlClosestServers"]);
}
})
.fail(function () {
tryNextCmd();
});
} else {
$.ajax({
url: `api.php?${cmds[0]}`,
dataType: "json",
})
.done(function (data) {
const serversInfo = data?.data;
if (serversInfo) {
speedtestServerCtr.find("p").remove();
codeBlock(speedtestServerCtr, serversInfo, speedtestServerBtn, "servers");
} else {
tryNextCmd();
}
})
.fail(function () {
tryNextCmd();
});
}
};

const hasBackup = callback => {
Expand Down Expand Up @@ -893,7 +903,7 @@ $(function () {
const closestServersList = speedtestServerCtr.find("pre");
if (closestServersList.length > 0) {
closestServersList.remove();
speedtestServerBtn.text("Show closest servers");
speedtestServerBtn.text("Show available servers");
} else {
speedtestServerBtn.text("Retrieving servers...");
closestServers();
Expand Down
11 changes: 9 additions & 2 deletions scripts/pi-hole/js/speedtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ var serverPing = [];
function createChart() {
var gridColor = getCSSval("graphs-grid", "background-color");
var ticksColor = getCSSval("graphs-ticks", "color");
var speedChartctx = document.getElementById("speedOverTimeChart")?.getContext("2d");
const chartElement = document.getElementById("speedOverTimeChart");
if (chartElement === null || chartElement === undefined) return;
var speedChartctx = chartElement.getContext("2d");
if (speedChartctx === null || speedChartctx === undefined) return;
speedChart = new Chart(speedChartctx, {
type: getGraphType(1),
Expand Down Expand Up @@ -187,7 +189,12 @@ function updateSpeedTestData() {
createChart();
}

if (speedChart && speedChart.data.labels !== speedlabels) {
if (
speedChart &&
speedChart !== null &&
speedChart !== undefined &&
speedChart.data.labels !== speedlabels
) {
speedChart.data.labels = speedlabels;
speedChart.data.datasets[0].data = downloadspeed;
speedChart.data.datasets[1].data = uploadspeed;
Expand Down
4 changes: 2 additions & 2 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ class="form-control"

<div class="form-group col-md-6">
<label for="speedtestserver">Set specific server</label>
<p>If you know better</p>
<p>To be tried first</p>
<div class="input-group">
<div class="input-group-addon">id</div>
<input
Expand All @@ -1653,7 +1653,7 @@ class="form-control"
>
</div>
<p id="closestServers" style="margin-top: 1vw;">
<button class="btn btn-default" id="closestServersBtn" type="button">Show closest servers</button>
<button class="btn btn-default" id="closestServersBtn" type="button">Show available servers</button>
</p>
</div>
</div>
Expand Down

0 comments on commit ca5a063

Please sign in to comment.