Skip to content

Commit 877d5f8

Browse files
authored
Allow path prefix multiplexing the dashboard and API (#3269)
2 parents f45ef29 + 6a6a391 commit 877d5f8

28 files changed

Lines changed: 119 additions & 117 deletions

login.lp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
mg.include('scripts/lua/header.lp','r')
1111
?>
12-
<body class="hold-transition layout-boxed login-page">
12+
<body class="hold-transition layout-boxed login-page" data-apiurl="<?=pihole.api_url()?>">
1313
<div class="box login-box" id="login-box">
1414
<section style="padding: 15px;">
1515
<div class="login-logo">
1616
<div class="text-center">
17-
<img src="<?=pihole.webhome()?>img/logo.svg" alt="Pi-hole logo" class="loginpage-logo" width="140" height="202">
17+
<img src="<?=webhome?>img/logo.svg" alt="Pi-hole logo" class="loginpage-logo" width="140" height="202">
1818
</div>
1919
<div class="panel-title text-center"><span class="logo-lg" style="font-size: 25px;">Pi-<b>hole</b></span></div>
2020
</div>
@@ -117,7 +117,7 @@ mg.include('scripts/lua/header.lp','r')
117117
<strong><a href="https://pi-hole.net/donate/" rel="noopener" target="_blank"><i class="fa fa-heart text-red"></i> Donate</a></strong> if you found this useful.
118118
</div>
119119
</div>
120-
<script src="<?=pihole.fileversion('scripts/js/login.js')?>"></script>
121120
<script src="<?=pihole.fileversion('scripts/js/footer.js')?>"></script>
121+
<script src="<?=pihole.fileversion('scripts/js/login.js')?>"></script>
122122
</body>
123123
</html>

scripts/js/footer.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
/* global utils:false, moment:false */
99

10-
//The following functions allow us to display time until pi-hole is enabled after disabling.
11-
//Works between all pages
10+
var _isLoginPage = false;
11+
const apiUrl = document.body.dataset.apiurl;
1212

1313
const REFRESH_INTERVAL = {
1414
logs: 500, // 0.5 sec (logs page)
@@ -115,7 +115,7 @@ function checkBlocking() {
115115
}
116116

117117
$.ajax({
118-
url: "/api/dns/blocking",
118+
url: apiUrl + "/dns/blocking",
119119
method: "GET",
120120
})
121121
.done(function (data) {
@@ -144,7 +144,7 @@ function piholeChange(action, duration) {
144144

145145
btnStatus.html("<i class='fa fa-spinner fa-spin'> </i>");
146146
$.ajax({
147-
url: "/api/dns/blocking",
147+
url: apiUrl + "/dns/blocking",
148148
method: "POST",
149149
dataType: "json",
150150
processData: false,
@@ -267,7 +267,7 @@ function updateQueryFrequency(intl, frequency) {
267267
var ftlinfoTimer = null;
268268
function updateFtlInfo() {
269269
$.ajax({
270-
url: "/api/info/ftl",
270+
url: apiUrl + "/info/ftl",
271271
})
272272
.done(function (data) {
273273
var ftl = data.ftl;
@@ -324,7 +324,7 @@ function updateFtlInfo() {
324324

325325
function updateSystemInfo() {
326326
$.ajax({
327-
url: "/api/info/system",
327+
url: apiUrl + "/info/system",
328328
})
329329
.done(function (data) {
330330
var system = data.system;
@@ -484,7 +484,7 @@ function versionCompare(v1, v2) {
484484

485485
function updateVersionInfo() {
486486
$.ajax({
487-
url: "/api/info/version",
487+
url: apiUrl + "/info/version",
488488
}).done(function (data) {
489489
var version = data.version;
490490
var updateAvailable = false;
@@ -619,7 +619,7 @@ function updateVersionInfo() {
619619
}
620620

621621
$(function () {
622-
if (globalThis.location.pathname !== "/admin/login") updateInfo();
622+
if (!_isLoginPage) updateInfo();
623623
var enaT = $("#enableTimer");
624624
var target = new Date(parseInt(enaT.html(), 10));
625625
var seconds = Math.round((target.getTime() - Date.now()) / 1000);
@@ -634,7 +634,7 @@ $(function () {
634634
// Apply per-browser styling settings
635635
initCheckboxRadioStyle();
636636

637-
if (globalThis.location.pathname !== "/admin/login") {
637+
if (!_isLoginPage) {
638638
// Run check immediately after page loading ...
639639
utils.checkMessages();
640640
// ... and then periodically

scripts/js/gravity.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* This file is copyright under the latest version of the EUPL.
66
* Please see LICENSE file for your rights under this license. */
7+
/* global apiUrl: false */
78

89
function eventsource() {
910
var alInfo = $("#alInfo");
@@ -15,7 +16,7 @@ function eventsource() {
1516
alInfo.show();
1617
alSuccess.hide();
1718

18-
fetch("/api/action/gravity", {
19+
fetch(apiUrl + "/action/gravity", {
1920
method: "POST",
2021
headers: { "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content") },
2122
})

scripts/js/groups-clients.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
* This file is copyright under the latest version of the EUPL.
66
* Please see LICENSE file for your rights under this license. */
77

8-
/* global utils:false, groups:false,, apiFailure:false, updateFtlInfo:false, getGroups:false, processGroupResult:false, delGroupItems:false */
8+
/* global utils:false, apiUrl:false, groups:false,, apiFailure:false, updateFtlInfo:false, getGroups:false, processGroupResult:false, delGroupItems:false */
99
/* exported initTable */
1010

1111
var table;
1212

1313
function reloadClientSuggestions() {
1414
$.ajax({
15-
url: "/api/clients/_suggestions",
15+
url: apiUrl + "/clients/_suggestions",
1616
type: "GET",
1717
dataType: "json",
1818
success: function (data) {
@@ -96,7 +96,7 @@ function initTable() {
9696
table = $("#clientsTable").DataTable({
9797
processing: true,
9898
ajax: {
99-
url: "/api/clients",
99+
url: apiUrl + "/clients",
100100
dataSrc: "clients",
101101
type: "GET",
102102
},
@@ -402,7 +402,7 @@ function addClient() {
402402
}
403403

404404
$.ajax({
405-
url: "/api/clients",
405+
url: apiUrl + "/clients",
406406
method: "post",
407407
dataType: "json",
408408
processData: false,
@@ -459,7 +459,7 @@ function editClient() {
459459
const clientDecoded = utils.hexDecode(client);
460460
utils.showAlert("info", "", "Editing client...", clientDecoded);
461461
$.ajax({
462-
url: "/api/clients/" + encodeURIComponent(clientDecoded),
462+
url: apiUrl + "/clients/" + encodeURIComponent(clientDecoded),
463463
method: "put",
464464
dataType: "json",
465465
processData: false,

scripts/js/groups-common.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* This file is copyright under the latest version of the EUPL.
66
* Please see LICENSE file for your rights under this license. */
77

8-
/* global apiFailure:false, utils:false, initTable:false, updateFtlInfo:false */
8+
/* global apiFailure:false, utils:false, apiUrl:false, initTable:false, updateFtlInfo:false */
99

1010
var groups = [];
1111

@@ -39,7 +39,7 @@ function populateGroupSelect(selectEl) {
3939
// eslint-disable-next-line no-unused-vars
4040
function getGroups(groupSelector) {
4141
$.ajax({
42-
url: "/api/groups",
42+
url: apiUrl + "/groups",
4343
type: "GET",
4444
dataType: "json",
4545
success: function (data) {
@@ -79,7 +79,7 @@ function delGroupItems(type, ids, table, listType = undefined) {
7979
// Check input validity
8080
if (!Array.isArray(ids)) return;
8181

82-
const url = "/api/" + type + "s:batchDelete";
82+
const url = apiUrl + "/" + type + "s:batchDelete";
8383

8484
// use utils.hexDecode() to decode all clients
8585
let idstring = "";

scripts/js/groups-domains.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* This file is copyright under the latest version of the EUPL.
66
* Please see LICENSE file for your rights under this license. */
77

8-
/* global utils:false, groups:false,, getGroups:false, updateFtlInfo:false, apiFailure:false, processGroupResult:false, delGroupItems:false */
8+
/* global utils:false, apiUrl:false, groups:false,, getGroups:false, updateFtlInfo:false, apiFailure:false, processGroupResult:false, delGroupItems:false */
99
/* exported initTable */
1010

1111
var table;
@@ -91,7 +91,7 @@ function initTable() {
9191
table = $("#domainsTable").DataTable({
9292
processing: true,
9393
ajax: {
94-
url: "/api/domains",
94+
url: apiUrl + "/domains",
9595
dataSrc: "domains",
9696
type: "GET",
9797
},
@@ -507,7 +507,7 @@ function addDomain() {
507507
const type = action === "add_deny" ? "deny" : "allow";
508508

509509
$.ajax({
510-
url: "/api/domains/" + type + "/" + kind,
510+
url: apiUrl + "/domains/" + type + "/" + kind,
511511
method: "post",
512512
dataType: "json",
513513
processData: false,
@@ -596,7 +596,7 @@ function editDomain() {
596596
const domainDecoded = utils.hexDecode(domain.split("_")[0]);
597597
utils.showAlert("info", "", "Editing domain...", domainDecoded);
598598
$.ajax({
599-
url: "/api/domains/" + newTypestr + "/" + encodeURIComponent(domainDecoded),
599+
url: apiUrl + "/domains/" + newTypestr + "/" + encodeURIComponent(domainDecoded),
600600
method: "put",
601601
dataType: "json",
602602
processData: false,

scripts/js/groups-lists.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* This file is copyright under the latest version of the EUPL.
66
* Please see LICENSE file for your rights under this license. */
77

8-
/* global utils:false, groups:false, apiFailure:false, updateFtlInfo:false, getGroups:false, processGroupResult:false, delGroupItems:false */
8+
/* global utils:false, apiUrl:false, groups:false, apiFailure:false, updateFtlInfo:false, getGroups:false, processGroupResult:false, delGroupItems:false */
99
/* exported initTable */
1010

1111
var table;
@@ -170,7 +170,7 @@ function initTable() {
170170
table = $("#listsTable").DataTable({
171171
processing: true,
172172
ajax: {
173-
url: "/api/lists",
173+
url: apiUrl + "/lists",
174174
dataSrc: "lists",
175175
type: "GET",
176176
},
@@ -519,7 +519,7 @@ function addList(event) {
519519
}
520520

521521
$.ajax({
522-
url: "/api/lists",
522+
url: apiUrl + "/lists",
523523
method: "post",
524524
dataType: "json",
525525
processData: false,
@@ -588,7 +588,7 @@ function editList() {
588588
utils.disableAll();
589589
utils.showAlert("info", "", "Editing address...", address);
590590
$.ajax({
591-
url: "/api/lists/" + encodeURIComponent(address) + "?type=" + type,
591+
url: apiUrl + "/lists/" + encodeURIComponent(address) + "?type=" + type,
592592
method: "put",
593593
dataType: "json",
594594
processData: false,

scripts/js/groups.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* This file is copyright under the latest version of the EUPL.
66
* Please see LICENSE file for your rights under this license. */
77

8-
/* global utils:false, apiFailure:false, updateFtlInfo:false, processGroupResult:false, delGroupItems:false */
8+
/* global utils:false, apiUrl:false, apiFailure:false, updateFtlInfo:false, processGroupResult:false, delGroupItems:false */
99

1010
var table;
1111

@@ -26,7 +26,7 @@ $(function () {
2626
table = $("#groupsTable").DataTable({
2727
processing: true,
2828
ajax: {
29-
url: "/api/groups",
29+
url: apiUrl + "/groups",
3030
error: handleAjaxError,
3131
dataSrc: "groups",
3232
type: "GET",
@@ -261,7 +261,7 @@ function addGroup() {
261261
}
262262

263263
$.ajax({
264-
url: "/api/groups",
264+
url: apiUrl + "/groups",
265265
method: "post",
266266
dataType: "json",
267267
processData: false,
@@ -329,7 +329,7 @@ function editGroup() {
329329
utils.disableAll();
330330
utils.showAlert("info", "", "Editing group...", oldName);
331331
$.ajax({
332-
url: "/api/groups/" + oldName,
332+
url: apiUrl + "/groups/" + oldName,
333333
method: "put",
334334
dataType: "json",
335335
processData: false,

scripts/js/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* This file is copyright under the latest version of the EUPL.
66
* Please see LICENSE file for your rights under this license. */
77

8-
/* global utils:false, Chart:false, apiFailure:false, THEME_COLORS:false, customTooltips:false, htmlLegendPlugin:false,doughnutTooltip:false, ChartDeferred:false, REFRESH_INTERVAL: false, updateQueryFrequency: false */
8+
/* global utils:false, apiUrl:false, Chart:false, apiFailure:false, THEME_COLORS:false, customTooltips:false, htmlLegendPlugin:false,doughnutTooltip:false, ChartDeferred:false, REFRESH_INTERVAL: false, updateQueryFrequency: false */
99

1010
// Define global variables
1111
var timeLineChart, clientsChart;
@@ -22,7 +22,7 @@ Chart.defaults.set("plugins.deferred", {
2222

2323
var failures = 0;
2424
function updateQueriesOverTime() {
25-
$.getJSON("/api/history", function (data) {
25+
$.getJSON(apiUrl + "/history", function (data) {
2626
// Remove graph if there are no results (e.g. new
2727
// installation or privacy mode enabled)
2828
if (jQuery.isEmptyObject(data.history)) {
@@ -92,7 +92,7 @@ function updateQueriesOverTime() {
9292
}
9393

9494
function updateQueryTypesPie() {
95-
$.getJSON("/api/stats/query_types", function (data) {
95+
$.getJSON(apiUrl + "/stats/query_types", function (data) {
9696
var v = [],
9797
c = [],
9898
k = [],
@@ -134,7 +134,7 @@ function updateQueryTypesPie() {
134134
}
135135

136136
function updateClientsOverTime() {
137-
$.getJSON("/api/history/clients", function (data) {
137+
$.getJSON(apiUrl + "/history/clients", function (data) {
138138
// Remove graph if there are no results (e.g. new
139139
// installation or privacy mode enabled)
140140
if (jQuery.isEmptyObject(data.history)) {
@@ -212,7 +212,7 @@ function updateClientsOverTime() {
212212

213213
var upstreams = {};
214214
function updateForwardDestinationsPie() {
215-
$.getJSON("/api/stats/upstreams", function (data) {
215+
$.getJSON(apiUrl + "/stats/upstreams", function (data) {
216216
var v = [],
217217
c = [],
218218
k = [],
@@ -273,13 +273,13 @@ function updateForwardDestinationsPie() {
273273
function updateTopClientsTable(blocked) {
274274
let api, style, tablecontent, overlay, clienttable;
275275
if (blocked) {
276-
api = "/api/stats/top_clients?blocked=true";
276+
api = apiUrl + "/stats/top_clients?blocked=true";
277277
style = "queries-blocked";
278278
tablecontent = $("#client-frequency-blocked td").parent();
279279
overlay = $("#client-frequency-blocked .overlay");
280280
clienttable = $("#client-frequency-blocked").find("tbody:last");
281281
} else {
282-
api = "/api/stats/top_clients";
282+
api = apiUrl + "/stats/top_clients";
283283
style = "queries-permitted";
284284
tablecontent = $("#client-frequency td").parent();
285285
overlay = $("#client-frequency .overlay");
@@ -333,13 +333,13 @@ function updateTopClientsTable(blocked) {
333333
function updateTopDomainsTable(blocked) {
334334
let api, style, tablecontent, overlay, domaintable;
335335
if (blocked) {
336-
api = "/api/stats/top_domains?blocked=true";
336+
api = apiUrl + "/stats/top_domains?blocked=true";
337337
style = "queries-blocked";
338338
tablecontent = $("#ad-frequency td").parent();
339339
overlay = $("#ad-frequency .overlay");
340340
domaintable = $("#ad-frequency").find("tbody:last");
341341
} else {
342-
api = "/api/stats/top_domains";
342+
api = apiUrl + "/stats/top_domains";
343343
style = "queries-permitted";
344344
tablecontent = $("#domain-frequency td").parent();
345345
overlay = $("#domain-frequency .overlay");
@@ -408,7 +408,7 @@ function updateTopLists() {
408408
var previousCount = 0;
409409
var firstSummaryUpdate = true;
410410
function updateSummaryData(runOnce = false) {
411-
$.getJSON("/api/stats/summary", function (data) {
411+
$.getJSON(apiUrl + "/stats/summary", function (data) {
412412
var intl = new Intl.NumberFormat();
413413
const newCount = parseInt(data.queries.total, 10);
414414

scripts/js/interfaces.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* This file is copyright under the latest version of the EUPL.
66
* Please see LICENSE file for your rights under this license. */
77

8-
/* global utils */
8+
/* global utils: false, apiUrl: false */
99

1010
$(function () {
1111
$.ajax({
12-
url: "/api/network/gateway",
12+
url: apiUrl + "/network/gateway",
1313
data: { detailed: true },
1414
}).done(function (data) {
1515
var intl = new Intl.NumberFormat();

0 commit comments

Comments
 (0)