including some more dashboard components
This commit is contained in:
184
stacks/dashboard/config/www/SupportedApps/Pihole/Pihole.php
Normal file
184
stacks/dashboard/config/www/SupportedApps/Pihole/Pihole.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace App\SupportedApps\Pihole;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Pihole extends \App\SupportedApps implements \App\EnhancedApps
|
||||
{
|
||||
public $config;
|
||||
|
||||
//protected $login_first = true; // Uncomment if api requests need to be authed first
|
||||
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->jar = new \GuzzleHttp\Cookie\CookieJar(); // Uncomment if cookies need to be set
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
$version = $this->config->version;
|
||||
|
||||
if ($version == 5) {
|
||||
$test = parent::appTest($this->url("api.php?summaryRaw"));
|
||||
echo $test->status;
|
||||
}
|
||||
if ($version == 6) {
|
||||
$test = $this->getInfo();
|
||||
if ($test["valid"]) {
|
||||
echo "Successfully communicated with the API";
|
||||
} else {
|
||||
echo "Error while communicating with the API: " . $test["message"];
|
||||
}
|
||||
}
|
||||
}
|
||||
public function livestats()
|
||||
{
|
||||
$data = [];
|
||||
$status = "inactive";
|
||||
$version = $this->config->version;
|
||||
|
||||
if ($version == 5) {
|
||||
$res = parent::execute($this->url("api.php?summaryRaw"));
|
||||
$details = json_decode($res->getBody());
|
||||
if ($details) {
|
||||
$data["ads_blocked"] = number_format(
|
||||
$details->ads_blocked_today
|
||||
);
|
||||
$data["ads_percentage"] = number_format(
|
||||
$details->ads_percentage_today,
|
||||
1
|
||||
);
|
||||
|
||||
$status = "active";
|
||||
}
|
||||
}
|
||||
|
||||
if ($version == 6) {
|
||||
$results = $this->getInfo();
|
||||
|
||||
if ($results["valid"]) {
|
||||
$data["ads_blocked"] = $results["queries"];
|
||||
$data["ads_percentage"] = $results["percent"];
|
||||
|
||||
$status = "active";
|
||||
}
|
||||
}
|
||||
return parent::getLiveStats($status, $data);
|
||||
}
|
||||
|
||||
public function url($endpoint)
|
||||
{
|
||||
$version = $this->config->version;
|
||||
if ($version == 5) {
|
||||
$apikey = $this->config->apikey;
|
||||
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
|
||||
|
||||
if ($apikey) {
|
||||
$api_url .= "&auth=" . $apikey;
|
||||
}
|
||||
}
|
||||
if ($version == 6) {
|
||||
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
|
||||
}
|
||||
return $api_url;
|
||||
}
|
||||
|
||||
public function getInfo()
|
||||
{
|
||||
$ignoreTls = $this->getConfigValue("ignore_tls", false);
|
||||
if ($ignoreTls) {
|
||||
$attrs = [
|
||||
"body" => json_encode(['password' => $this->config->apikey]),
|
||||
"cookies" => $this->jar,
|
||||
"verify" => false,
|
||||
"headers" => [
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "application/json",
|
||||
],
|
||||
];
|
||||
$attrsid["verify"] = false;
|
||||
} else {
|
||||
$attrs = [
|
||||
"body" => json_encode(['password' => $this->config->apikey]),
|
||||
"cookies" => $this->jar,
|
||||
"headers" => [
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "application/json",
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
// Create session and retrieve data
|
||||
$response = parent::execute($this->url("api/auth"), $attrs, null, "POST");
|
||||
$auth = json_decode($response->getBody());
|
||||
|
||||
if (!$auth->session->valid) {
|
||||
$data = [
|
||||
'valid' => false,
|
||||
'validity' => -1,
|
||||
'message' => $auth->session->message,
|
||||
'queries' => 0,
|
||||
'percent' => 0
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ($ignoreTls) {
|
||||
$attrsid = [
|
||||
"body" => json_encode(['sid' => $auth->session->sid]),
|
||||
"cookies" => $this->jar,
|
||||
"verify" => false,
|
||||
"headers" => [
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "application/json",
|
||||
],
|
||||
];
|
||||
} else {
|
||||
$attrsid = [
|
||||
"body" => json_encode(['sid' => $auth->session->sid]),
|
||||
"cookies" => $this->jar,
|
||||
"headers" => [
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "application/json",
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
// Get queries data
|
||||
$responsesummary = parent::execute($this->url("api/stats/summary"), $attrsid, null, "GET");
|
||||
$datasummary = json_decode($responsesummary->getBody());
|
||||
|
||||
// After retrieving the data the session is closed to declutter
|
||||
parent::execute($this->url("api/auth"), $attrsid, null, "DELETE");
|
||||
|
||||
// Extract data from the response
|
||||
$valid = $auth->session->valid;
|
||||
$validity = $auth->session->validity;
|
||||
$message = $auth->session->message;
|
||||
|
||||
if (!$auth->session->valid) {
|
||||
$queriesblocked = 0;
|
||||
$percentblocked = 0;
|
||||
} else {
|
||||
$queriesblocked = $datasummary->queries->blocked;
|
||||
$percentblocked = round($datasummary->queries->percent_blocked, 2);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'valid' => $valid,
|
||||
'validity' => $validity,
|
||||
'message' => $message,
|
||||
'queries' => $queriesblocked,
|
||||
'percent' => $percentblocked
|
||||
];
|
||||
return $data;
|
||||
}
|
||||
public function getConfigValue($key, $default = null)
|
||||
{
|
||||
return isset($this->config) && isset($this->config->$key)
|
||||
? $this->config->$key
|
||||
: $default;
|
||||
}
|
||||
}
|
||||
10
stacks/dashboard/config/www/SupportedApps/Pihole/app.json
Normal file
10
stacks/dashboard/config/www/SupportedApps/Pihole/app.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"appid": "b89920409bdce40e08ba1023480b0546061cd577",
|
||||
"name": "Pi-hole",
|
||||
"website": "https://pi-hole.net",
|
||||
"license": "European Union Public License 1.2",
|
||||
"description": "Pi-hole is a Linux network-level advertisement and internet tracker blocking application which acts as a DNS sinkhole, intended for use on a private network.",
|
||||
"enhanced": true,
|
||||
"tile_background": "dark",
|
||||
"icon": "pihole.svg"
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
|
||||
<div class="items">
|
||||
<input type="hidden" data-config="dataonly" class="config-item" name="config[dataonly]" value="1" />
|
||||
<div class="input">
|
||||
<label>{{ strtoupper(__('app.url')) }}</label>
|
||||
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
|
||||
</div>
|
||||
<div class="input">
|
||||
<label>Api Key(v5)/App Password(v6)</label>
|
||||
{!! Form::text('config[apikey]', isset($item) && property_exists($item->getconfig(), 'apikey') ? $item->getconfig()->apikey : null, ['placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item']) !!}
|
||||
</div>
|
||||
<div class="items" style="max-width: 100px;">
|
||||
<div class="input">
|
||||
<label>Version</label>
|
||||
{!! Form::select(
|
||||
'config[version]',
|
||||
['5' => 'v5', '6' => 'v6'],
|
||||
isset($item) ? $item->getconfig()->version : null,
|
||||
['data-config' => 'version', 'class' => 'form-control config-item'],
|
||||
) !!}
|
||||
</div>
|
||||
</div>
|
||||
<div class="input" style="max-width: 150px;">
|
||||
<label>Skip TLS verification</label>
|
||||
<div class="toggleinput" style="margin-top: 26px; padding-left: 15px;">
|
||||
{!! Form::hidden('config[ignore_tls]', 0, ['class' => 'config-item', 'data-config' => 'ignore_tls']) !!}
|
||||
<label class="switch">
|
||||
<?php
|
||||
$checked = false;
|
||||
if (isset($item) && !empty($item) && isset($item->getconfig()->ignore_tls)) {
|
||||
$checked = $item->getconfig()->ignore_tls;
|
||||
}
|
||||
$set_checked = $checked ? ' checked="checked"' : '';
|
||||
?>
|
||||
<input type="checkbox" class="config-item" data-config="ignore_tls" name="config[ignore_tls]" value="1" <?php echo $set_checked; ?> />
|
||||
<span class="slider round"></span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="input">
|
||||
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,10 @@
|
||||
<ul class="livestats">
|
||||
<li>
|
||||
<span class="title">Queries<br />Blocked</span>
|
||||
<strong>{!! $ads_blocked !!}</strong>
|
||||
</li>
|
||||
<li>
|
||||
<span class="title">Percent<br /> Blocked</span>
|
||||
<strong>{!! $ads_percentage !!}</strong>
|
||||
</li>
|
||||
</ul>
|
||||
Reference in New Issue
Block a user