Skip to content

Commit e57ec3a

Browse files
Added read services enpoint /services/index.php to return a list of available services and their current status
1 parent e35be47 commit e57ec3a

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

pfSense-pkg-API/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ do-install:
185185
${STAGEDIR}${PREFIX}/www/api/v1/firewall/rules/delete
186186

187187
# SERVICE API ENDPOINTS----------------------------------------
188+
# Services base
188189
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services
190+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/index.php \
191+
${STAGEDIR}${PREFIX}/www/api/v1/services
189192
# Unbound base
190193
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/unbound
191194
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/unbound/index.php \
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
// IMPORTS--------------------------------------------------------------------------------------------------------------
4+
require_once("config.inc");
5+
require_once("auth.inc");
6+
require_once("functions.inc");
7+
require_once("api.inc");
8+
9+
// HEADERS--------------------------------------------------------------------------------------------------------------
10+
api_runtime_allowed(); // Check that our configuration allows this API call to run first
11+
header('Content-Type: application/json');
12+
header("Referer: no-referrer");
13+
14+
// VARIABLES------------------------------------------------------------------------------------------------------------
15+
global $config, $api_resp, $client_params;
16+
$read_only_action = true; // Set whether this action requires read only access
17+
$req_privs = array("page-all", "page-status-services"); // Array of privs allowed
18+
$http_method = $_SERVER['REQUEST_METHOD']; // Save our HTTP method
19+
20+
// RUN TIME-------------------------------------------------------------------------------------------------------------
21+
// Check that client is authenticated and authorized
22+
if (api_authorized($req_privs, $read_only_action)) {
23+
// Check that our HTTP method is POST (READ)
24+
if ($http_method === 'GET') {
25+
if (isset($client_params['name'])) {
26+
$name = $client_params['name'];
27+
}
28+
$service_list = get_services(); // Stop our service
29+
// Loop through our service list and add our service status
30+
foreach ($service_list as $key => $srvc) {
31+
$s_status = get_service_status($srvc);
32+
// Check if service is started
33+
if ($s_status === true) {
34+
$service_list[$key]["status"] = "running";
35+
} elseif ($s_status === false) {
36+
$service_list[$key]["status"] = "stopped";
37+
} else {
38+
$service_list[$key]["status"] = "unknown";
39+
}
40+
// Check if user requested only one service, if so remove unmatched services
41+
if (isset($name) and $name !== $srvc["name"]) {
42+
unset($service_list[$key]);
43+
}
44+
}
45+
// Print our JSON response
46+
$api_resp = array("status" => "ok", "code" => 200, "return" => 0, "message" => "", "data" => $service_list);
47+
http_response_code(200);
48+
echo json_encode($api_resp) . PHP_EOL;
49+
die();
50+
} else {
51+
$api_resp = array("status" => "bad request", "code" => 400, "return" => 2, "message" => "invalid http method");
52+
http_response_code(400);
53+
echo json_encode($api_resp) . PHP_EOL;
54+
die();
55+
}
56+
} else {
57+
http_response_code(401);
58+
echo json_encode($api_resp) . PHP_EOL;
59+
die();
60+
}

pfSense-pkg-API/pkg-plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
/usr/local/www/api/v1/firewall/rules/add/index.php
8888
/usr/local/www/api/v1/firewall/rules/delete/index.php
8989

90+
@dir /usr/local/www/api/v1/services
9091
@dir /usr/local/www/api/v1/services/unbound
9192
@dir /usr/local/www/api/v1/services/unbound/add
9293
@dir /usr/local/www/api/v1/services/unbound/delete
@@ -97,6 +98,7 @@
9798
@dir /usr/local/www/api/v1/services/unbound/add/hosts
9899
@dir /usr/local/www/api/v1/services/unbound/delete/hosts
99100
@dir /usr/local/www/api/v1/services/unbound/modify/hosts
101+
/usr/local/www/api/v1/services/index.php
100102
/usr/local/www/api/v1/services/unbound/index.php
101103
/usr/local/www/api/v1/services/unbound/start/index.php
102104
/usr/local/www/api/v1/services/unbound/stop/index.php

0 commit comments

Comments
 (0)