From b21ce47e23d841e9d3d72b6b6775299596e698c7 Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Mon, 16 Jun 2025 22:24:16 -0500 Subject: [PATCH 1/7] debugging --- PiHoleShell/PiHoleShell.psd1 | Bin 2974 -> 3106 bytes PiHoleShell/PiHoleShell.psm1 | 2 +- PiHoleShell/Private/Misc.ps1 | 25 ++++++++ PiHoleShell/Public/Authentication.ps1 | 26 -------- PiHoleShell/Public/DnsControl.ps1 | 16 +++-- PiHoleShell/Public/GroupManagement.ps1 | 5 +- PiHoleShell/Public/ListManagement.ps1 | 82 +++++++++++++++++++++++++ tests/Set-PiHoleDnsBlocking.Tests.ps1 | 16 ++++- 8 files changed, 133 insertions(+), 39 deletions(-) create mode 100644 PiHoleShell/Public/ListManagement.ps1 diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index 63feccc6e0d94756d7f5dd14126e5883d9967379..a8bcf62ca4f4f2fe964cd6be830765d36390e245 100644 GIT binary patch delta 36 lcmbOyzDQyN3&&&u78Q0MhD?THhLXv)EdDU|W=4*=%mAlL2)qCQ delta 12 TcmZ1^F;9F03&-XTj# + #Work In Progress + [CmdletBinding()] + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] + param ( + [Parameter(Mandatory = $true)] + [System.Uri]$PiHoleServer, + [Parameter(Mandatory = $true)] + [string]$Password, + [System.Uri]$List = $null, + [bool]$IgnoreSsl = $false, + [bool]$RawOutput = $false + ) + try { + $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl + + $Groups = Get-PiHoleGroup -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl + + $Params = @{ + Headers = @{sid = $($Sid) } + Uri = "$($PiHoleServer.OriginalString)/api/lists/$List" + Method = "Get" + SkipCertificateCheck = $IgnoreSsl + ContentType = "application/json" + } + + $Response = Invoke-RestMethod @Params + + if ($RawOutput) { + Write-Output $Response + } + + else { + $ObjectFinal = @() + foreach ($Item in $Response.Lists) { + $GroupNames = [System.Collections.ArrayList]@() + foreach ($Group in $Item.Groups) { + $GroupNames += ($Groups | Where-Object { $_.Id -eq $Group }).Name + } + + $Object = $null + $Object = [PSCustomObject]@{ + Address = $Item.address + comment = $Item.comment + Groups = $GroupNames + Enabled = $Item.enabled + Id = $Item.id + date_added = $Item.date_added + date_modified = $Item.date_modified + type = $Item.type + date_updated = $Item.date_updated + number = $Item.number + invalid_domains = $Item.invalid_domains + abp_entries = $Item.abp_entries + status = $Item.status + + } + $ObjectFinal += $Object + } + + else { + Write-Output $ObjectFinal + } + } + } + + catch { + Write-Error -Message $_.Exception.Message + break + } + + finally { + if ($Sid) { + Remove-PiHoleCurrentAuthSession -PiHoleServer $PiHoleServer -Sid $Sid -IgnoreSsl $IgnoreSsl + } + } +} \ No newline at end of file diff --git a/tests/Set-PiHoleDnsBlocking.Tests.ps1 b/tests/Set-PiHoleDnsBlocking.Tests.ps1 index 4f31a2f..a855059 100644 --- a/tests/Set-PiHoleDnsBlocking.Tests.ps1 +++ b/tests/Set-PiHoleDnsBlocking.Tests.ps1 @@ -1,13 +1,27 @@ + # Requires -Module Pester Describe 'Set-PiHoleDnsBlocking' { BeforeAll { + Import-Module .\PiHoleShell\PiHoleShell.psm1 + + Mock -CommandName Request-PiHoleAuth -MockWith { return 'mock-sid' } + Mock -CommandName Invoke-RestMethod -MockWith { + return @{ + blocking = 'false' + timer = 60 + } + } + Mock -CommandName Remove-PiHoleCurrentAuthSession + Mock -CommandName Format-PiHoleSecond -MockWith { + return @{ TimeInSeconds = 60 } + } # Sample input values $server = [uri]'http://pihole.local' $password = 'mock-password' $sid = 'mock-session-id' # Mock external functions - Mock -CommandName Request-PiHoleAuth -MockWith { return $sid } + Mock -CommandName Request-PiHoleAuth -MockWith { 'mock-sid' } Mock -CommandName Remove-PiHoleCurrentAuthSession Mock -CommandName Format-PiHoleSecond -MockWith { return @{ TimeInSeconds = 60 } From f1106b070d7d5387d42fe1e0067bab62d279a8da Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Mon, 16 Jun 2025 22:33:02 -0500 Subject: [PATCH 2/7] debugging --- PiHoleShell/PiHoleShell.psm1 | 2 +- PiHoleShell/Private/Misc.ps1 | 6 +++--- PiHoleShell/Public/GroupManagement.ps1 | 2 +- PiHoleShell/Public/ListManagement.ps1 | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/PiHoleShell/PiHoleShell.psm1 b/PiHoleShell/PiHoleShell.psm1 index e44ed39..20495c5 100644 --- a/PiHoleShell/PiHoleShell.psm1 +++ b/PiHoleShell/PiHoleShell.psm1 @@ -5,7 +5,7 @@ if ($PSVersionTable.PSEdition -ne 'Core') { # Get all .ps1 files in the 'Public' directory and dot-source them $PublicFunctions = Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') -Filter '*.ps1' -File $PrivateFunctions = Get-ChildItem -Path (Join-Path $PSScriptRoot 'Private') -Filter '*.ps1' -File -$PSScriptRoot + foreach ($File in $PublicFunctions) { . $File.FullName } diff --git a/PiHoleShell/Private/Misc.ps1 b/PiHoleShell/Private/Misc.ps1 index ae58905..b75175c 100644 --- a/PiHoleShell/Private/Misc.ps1 +++ b/PiHoleShell/Private/Misc.ps1 @@ -51,9 +51,9 @@ function Remove-PiHoleCurrentAuthSession { [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Justification = "It removes sessions from PiHole only")] [CmdletBinding()] param ( - $PiHoleServer, - $Sid, - $IgnoreSsl = $false + [System.URI]$PiHoleServer, + [string]$Sid, + [bool]$IgnoreSsl = $false ) $Params = @{ Headers = @{sid = $($Sid) } diff --git a/PiHoleShell/Public/GroupManagement.ps1 b/PiHoleShell/Public/GroupManagement.ps1 index e61d314..a874595 100644 --- a/PiHoleShell/Public/GroupManagement.ps1 +++ b/PiHoleShell/Public/GroupManagement.ps1 @@ -9,7 +9,7 @@ https://TODO [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.Uri]$PiHoleServer, + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] $Password, $GroupName = $null, diff --git a/PiHoleShell/Public/ListManagement.ps1 b/PiHoleShell/Public/ListManagement.ps1 index a9d13ba..51fbb18 100644 --- a/PiHoleShell/Public/ListManagement.ps1 +++ b/PiHoleShell/Public/ListManagement.ps1 @@ -9,10 +9,10 @@ https://TODO [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.Uri]$PiHoleServer, + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, - [System.Uri]$List = $null, + [System.URI]$List = $null, [bool]$IgnoreSsl = $false, [bool]$RawOutput = $false ) From c080eb2e5bb6d8f121610b055c26795c05da8c73 Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Mon, 16 Jun 2025 22:34:43 -0500 Subject: [PATCH 3/7] debugging --- PiHoleShell/Public/ListManagement.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/PiHoleShell/Public/ListManagement.ps1 b/PiHoleShell/Public/ListManagement.ps1 index 51fbb18..98170c5 100644 --- a/PiHoleShell/Public/ListManagement.ps1 +++ b/PiHoleShell/Public/ListManagement.ps1 @@ -18,7 +18,6 @@ https://TODO ) try { $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl - $Groups = Get-PiHoleGroup -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl $Params = @{ @@ -37,7 +36,7 @@ https://TODO else { $ObjectFinal = @() - foreach ($Item in $Response.Lists) { + foreach ($Item in $Response.lists) { $GroupNames = [System.Collections.ArrayList]@() foreach ($Group in $Item.Groups) { $GroupNames += ($Groups | Where-Object { $_.Id -eq $Group }).Name From ae12183ada873c1082a457a1a124aadbc5913a2b Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Mon, 16 Jun 2025 22:54:33 -0500 Subject: [PATCH 4/7] debugging --- PiHoleShell/PiHoleShell.psd1 | Bin 3106 -> 3276 bytes PiHoleShell/Public/FTLInformation.ps1 | 135 ++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 PiHoleShell/Public/FTLInformation.ps1 diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index a8bcf62ca4f4f2fe964cd6be830765d36390e245..12c97c92eab9d15d3d0bcd8ee6e1eb29d8d8d9a9 100644 GIT binary patch delta 88 zcmZ1^aYk}O2M43hWP4^&VPyt4h7blH22X}OhBStJh9ZXC$-2y%le0KPBw=E{45jxkzF|2ghb3&RNU=Alw9N diff --git a/PiHoleShell/Public/FTLInformation.ps1 b/PiHoleShell/Public/FTLInformation.ps1 new file mode 100644 index 0000000..98beadf --- /dev/null +++ b/PiHoleShell/Public/FTLInformation.ps1 @@ -0,0 +1,135 @@ +function Get-PiHoleInfoMessage { + <# +.SYNOPSIS +Get Pi-hole diagnosis messages +Request Pi-hole diagnosis messages + #> + [CmdletBinding()] + [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 { + $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl + + $Params = @{ + Headers = @{sid = $($Sid) } + Uri = "$($PiHoleServer.OriginalString)/api/info/messages" + Method = "Get" + SkipCertificateCheck = $IgnoreSsl + ContentType = "application/json" + } + + $Response = Invoke-RestMethod @Params + + if ($RawOutput) { + Write-Output $Response + } + + else { + $ObjectFinal = @() + foreach ($Item in $Response.messages) { + $Object = $null + $Object = [PSCustomObject]@{ + Id = $Item.id + Timestamp = (Convert-PiHoleUnixTimeToLocalTime -UnixTime $Item.timestamp).LocalTime + Type = $Item.type + Plain = $Item.plain + Html = $Item.html + + } + + Write-Verbose -Message "Name - $($Object.Id)" + Write-Verbose -Message "Timestamp - $($Object.Timestamp)" + Write-Verbose -Message "Type - $($Object.Type)" + Write-Verbose -Message "Plain - $($Object.Plain)" + Write-Verbose -Message "Html - $($Object.Html)" + $ObjectFinal += $Object + } + + Write-Output $ObjectFinal + } + } + + catch { + Write-Error -Message $_.Exception.Message + break + } + + finally { + if ($Sid) { + Remove-PiHoleCurrentAuthSession -PiHoleServer $PiHoleServer -Sid $Sid -IgnoreSsl $IgnoreSsl + } + } +} + +function Get-PiHoleInfoHost { + <# +.SYNOPSIS +Get info about various host parameters +This API hook returns a collection of host infos. + + #> + #Work In Progress + [CmdletBinding()] + [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 { + $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl + + $Params = @{ + Headers = @{sid = $($Sid) } + Uri = "$($PiHoleServer.OriginalString)/api/info/host" + Method = "Get" + SkipCertificateCheck = $IgnoreSsl + ContentType = "application/json" + } + + $Response = Invoke-RestMethod @Params + + if ($RawOutput) { + Write-Output $Response + } + + else { + $ObjectFinal = @() + foreach ($Item in $Response.host) { + $Object = $null + $Object = [PSCustomObject]@{ + DomainName = $Item.uname.domainname + Machine = $Item.uname.machine + NodeName = $Item.uname.nodename + Release = $Item.uname.release + SysName = $Item.uname.sysname + Version = $Item.uname.version + + } + $ObjectFinal += $Object + Write-Output $ObjectFinal + } + } + } + + catch { + Write-Error -Message $_.Exception.Message + break + } + + finally { + if ($Sid) { + Remove-PiHoleCurrentAuthSession -PiHoleServer $PiHoleServer -Sid $Sid -IgnoreSsl $IgnoreSsl + } + } +} \ No newline at end of file From 82646c829a5c6093ee468975dacdf2eef5a39750 Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Thu, 19 Jun 2025 22:18:00 -0500 Subject: [PATCH 5/7] fixing list mgmt func --- PiHoleShell/Public/ListManagement.ps1 | 39 +++++++++++++++------------ 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/PiHoleShell/Public/ListManagement.ps1 b/PiHoleShell/Public/ListManagement.ps1 index 98170c5..8367b8b 100644 --- a/PiHoleShell/Public/ListManagement.ps1 +++ b/PiHoleShell/Public/ListManagement.ps1 @@ -43,28 +43,33 @@ https://TODO } $Object = $null + if ($Item.date_updated -eq 0) { + $DateUpdated = $null + } + else { + $DateUpdated = (Convert-PiHoleUnixTimeToLocalTime -UnixTime $Item.date_modified).LocalTime + } $Object = [PSCustomObject]@{ - Address = $Item.address - comment = $Item.comment - Groups = $GroupNames - Enabled = $Item.enabled - Id = $Item.id - date_added = $Item.date_added - date_modified = $Item.date_modified - type = $Item.type - date_updated = $Item.date_updated - number = $Item.number - invalid_domains = $Item.invalid_domains - abp_entries = $Item.abp_entries - status = $Item.status - + Address = $Item.address + Comment = $Item.comment + Groups = $GroupNames + Enabled = $Item.enabled + Id = $Item.id + DateAdded = (Convert-PiHoleUnixTimeToLocalTime -UnixTime $Item.date_added).LocalTime + DateModified = (Convert-PiHoleUnixTimeToLocalTime -UnixTime $Item.date_modified).LocalTime + Type = $Item.type + DateUpdated = $DateUpdated + Number = $Item.number + InvalidDomains = $Item.invalid_domains + AbpEntries = $Item.abp_entries + Status = $Item.status } + $ObjectFinal += $Object } - else { - Write-Output $ObjectFinal - } + Write-Output $ObjectFinal + } } From 298ab9a6bf6947f79f1ba73ca8a6be5140fc79a0 Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Fri, 20 Jun 2025 00:07:31 -0500 Subject: [PATCH 6/7] debugging --- PiHoleShell/Public/ListManagement.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PiHoleShell/Public/ListManagement.ps1 b/PiHoleShell/Public/ListManagement.ps1 index 8367b8b..0462d2c 100644 --- a/PiHoleShell/Public/ListManagement.ps1 +++ b/PiHoleShell/Public/ListManagement.ps1 @@ -18,6 +18,7 @@ https://TODO ) try { $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl + $Groups = Get-PiHoleGroup -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl $Params = @{ @@ -38,7 +39,7 @@ https://TODO $ObjectFinal = @() foreach ($Item in $Response.lists) { $GroupNames = [System.Collections.ArrayList]@() - foreach ($Group in $Item.Groups) { + foreach ($Group in $Item.groups) { $GroupNames += ($Groups | Where-Object { $_.Id -eq $Group }).Name } @@ -64,7 +65,7 @@ https://TODO AbpEntries = $Item.abp_entries Status = $Item.status } - + $ObjectFinal += $Object } From 84c830e7466de8adf8ed838bc79d5be2cbdad161 Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Fri, 20 Jun 2025 00:23:21 -0500 Subject: [PATCH 7/7] whitespace --- PiHoleShell/Public/ListManagement.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PiHoleShell/Public/ListManagement.ps1 b/PiHoleShell/Public/ListManagement.ps1 index 0462d2c..7366e3d 100644 --- a/PiHoleShell/Public/ListManagement.ps1 +++ b/PiHoleShell/Public/ListManagement.ps1 @@ -42,14 +42,14 @@ https://TODO foreach ($Group in $Item.groups) { $GroupNames += ($Groups | Where-Object { $_.Id -eq $Group }).Name } - + $Object = $null if ($Item.date_updated -eq 0) { $DateUpdated = $null } else { $DateUpdated = (Convert-PiHoleUnixTimeToLocalTime -UnixTime $Item.date_modified).LocalTime - } + } $Object = [PSCustomObject]@{ Address = $Item.address Comment = $Item.comment @@ -70,7 +70,7 @@ https://TODO } Write-Output $ObjectFinal - + } }