Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified PiHoleShell/PiHoleShell.psd1
Binary file not shown.
2 changes: 1 addition & 1 deletion PiHoleShell/PiHoleShell.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Export-ModuleMember -Function @(
'Get-PiHoleList', 'Search-PiHoleListDomain', 'Add-PiHoleList', 'Remove-PiHoleList', 'Update-PiHoleList', `
#FTLInformation
'Get-PiHoleInfoMessage', 'Get-PiHoleInfoHost', 'Get-PiHoleInfoClient', 'Get-PiHoleInfoLogin', 'Get-PiHoleInfoSystem', 'Get-PiHoleInfoFtl', `
'Get-PiHoleInfoSensors', 'Get-PiHoleInfoDatabase', 'Get-PiHoleInfoVersion', 'Get-PiHoleInfoMetrics', 'Get-PiHoleInfoMessageCount', 'Remove-PiHoleInfoMessage', 'Get-PiHoleLogWebserver', 'Get-PiHoleLogDnsmasq', 'Get-PiHoleLogFtl', `
'Get-PiHoleInfoSensors', 'Get-PiHoleInfoDatabase', 'Get-PiHoleInfoVersion', 'Get-PiHoleInfoMetrics', 'Get-PiHoleInfoMessageCount', 'Remove-PiHoleInfoMessage', 'Get-PiHoleLogWebserver', 'Get-PiHoleLogDnsmasq', 'Get-PiHoleLogFtl', 'Get-PiHoleApiEndpoint', `
#History
'Get-PiHoleHistory', 'Get-PiHoleHistoryDatabase', 'Get-PiHoleHistoryClient', 'Get-PiHoleHistoryDatabaseClient', `
#Teleporter
Expand Down
38 changes: 38 additions & 0 deletions PiHoleShell/Private/Invoke-PiHoleApiRequest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function Invoke-PiHoleApiRequest {
#INTERNAL FUNCTION
#
# Logs in, calls one API endpoint and always logs out again - the request/session boilerplate
# every public function otherwise repeats. Errors are left to propagate, so callers keep their
# own catch block (and its ConvertTo-PiHoleFriendlyErrorMessage call) unchanged.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")]
param (
[Parameter(Mandatory = $true)]
[System.URI]$PiHoleServer,
[Parameter(Mandatory = $true)]
[string]$Password,
[Parameter(Mandatory = $true)]
[string]$Endpoint,
[string]$Method = "Get",
[bool]$IgnoreSsl = $false
)

try {
$Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl

$Params = @{
Headers = @{sid = $($Sid) }
Uri = "$($PiHoleServer.OriginalString)/api/$($Endpoint.TrimStart('/'))"
Method = $Method
SkipCertificateCheck = $IgnoreSsl
ContentType = "application/json"
}

Invoke-RestMethod @Params -ErrorAction Stop
}

finally {
if ($Sid) {
Remove-PiHoleCurrentAuthSession -PiHoleServer $PiHoleServer -Sid $Sid -IgnoreSsl $IgnoreSsl
}
}
}
18 changes: 1 addition & 17 deletions PiHoleShell/Public/Authentication/Get-PiHoleAuthApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,7 @@ Generates a new app password and makes it the active one. From then on, use $App
[bool]$RawOutput = $false
)
try {
$Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl

$Params = @{
Headers = @{sid = $($Sid) }
Uri = "$($PiHoleServer.OriginalString)/api/auth/app"
Method = "Get"
SkipCertificateCheck = $IgnoreSsl
ContentType = "application/json"
}

$Response = Invoke-RestMethod @Params
$Response = Invoke-PiHoleApiRequest -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl -Endpoint 'auth/app'

if ($RawOutput) {
Write-Output $Response
Expand All @@ -71,10 +61,4 @@ Generates a new app password and makes it the active one. From then on, use $App
catch {
Write-Error -Message (ConvertTo-PiHoleFriendlyErrorMessage -ErrorRecord $_)
}

finally {
if ($Sid) {
Remove-PiHoleCurrentAuthSession -PiHoleServer $PiHoleServer -Sid $Sid -IgnoreSsl $IgnoreSsl
}
}
}
64 changes: 64 additions & 0 deletions PiHoleShell/Public/FTLInformation/Get-PiHoleApiEndpoint.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function Get-PiHoleApiEndpoint {
<#
.SYNOPSIS
Get list of available API endpoints

.DESCRIPTION
Returns every endpoint the Pi-hole API exposes, one object per HTTP method and URI. Parameters
holds any path parameters the endpoint accepts (e.g. "/{client}"), or is empty if it takes none.

.PARAMETER PiHoleServer
The URL to the PiHole Server, for example "http://pihole.domain.com:8080", or "http://192.168.1.100"

.PARAMETER Password
The API Password you generated from your PiHole server

.PARAMETER IgnoreSsl
Set to $true to skip SSL certificate validation

.PARAMETER RawOutput
This will dump the response instead of the formatted object

.EXAMPLE
Get-PiHoleApiEndpoint -PiHoleServer "http://pihole.domain.com:8080" -Password "your-app-password"

.EXAMPLE
Get-PiHoleApiEndpoint -PiHoleServer "http://pihole.domain.com:8080" -Password "your-app-password" | Where-Object Method -eq 'DELETE'

Lists only the endpoints that delete something.
#>
[CmdletBinding(HelpUri = 'https://ftl.pi-hole.net/master/docs/#get-/endpoints')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")]
param (
[Parameter(Mandatory = $true)]
[System.URI]$PiHoleServer,
[Parameter(Mandatory = $true)]
[string]$Password,
[bool]$IgnoreSsl = $false,
[bool]$RawOutput = $false
)

try {
$Response = Invoke-PiHoleApiRequest -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl -Endpoint 'endpoints'

if ($RawOutput) {
Write-Output $Response
}
else {
foreach ($MethodGroup in $Response.endpoints.PSObject.Properties) {
foreach ($Endpoint in $MethodGroup.Value) {
$Object = [PSCustomObject]@{
Method = $MethodGroup.Name.ToUpper()
Uri = $Endpoint.uri
Parameters = $Endpoint.parameters
}
Write-Output $Object
}
}
}
}

catch {
Write-Error -Message (ConvertTo-PiHoleFriendlyErrorMessage -ErrorRecord $_)
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ See [docs/EXAMPLES.md](docs/EXAMPLES.md) for real, captured output from every fu
| Function | Description |
|---|---|
| `Add-PiHoleConfigArrayItem` | Add config array item |
| `Get-PiHoleApiEndpoint` | Get list of available API endpoints |
| `Get-PiHoleConfig` | Get current configuration of Pi-hole |
| `Get-PiHoleConfigProperty` | Get special properties of your Pi-hole configuration |
| `Get-PiHoleDhcpLease` | Get currently active DHCP leases |
Expand Down
Loading
Loading