├── .gitattributes ├── .gitignore ├── LICENSE ├── PiHoleApiClient.Tests ├── Data │ └── Api │ │ ├── getAllQueries.json │ │ ├── getForwardDestinations.json │ │ ├── getQueryTypes.json │ │ ├── overTimeData10mins.json │ │ ├── summary.json │ │ ├── summaryRaw.json │ │ ├── topClients.json │ │ └── topItems.json ├── PiHoleApiClient.Tests.csproj └── PiHoleApiClientTests.cs ├── PiHoleApiClient.sln ├── PiHoleApiClient ├── IPiHoleApiClient.cs ├── Mappers │ └── QueryMapper.cs ├── Models │ ├── ApiVersion.cs │ ├── BackendType.cs │ ├── ForwardDestinations.cs │ ├── OverTimeData10mins.cs │ ├── PiStatus.cs │ ├── Query.cs │ ├── Querytypes.cs │ ├── Summary.cs │ ├── TopClients.cs │ └── TopItems.cs ├── PiHoleApiClient.cs └── PiHoleApiClient.csproj └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb 341 | /PiHoleApiClient.Local 342 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Sandeep Parekh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/Data/Api/getAllQueries.json: -------------------------------------------------------------------------------- 1 | { 2 | "data": [ 3 | [ 4 | "1596348004", 5 | "AAAA", 6 | "mytestsite.net", 7 | "127.0.0.1", 8 | "2", 9 | "0", 10 | "4", 11 | "26", 12 | "N/A", 13 | "-1" 14 | ], 15 | [ 16 | "1596348004", 17 | "A", 18 | "mytestsite.net", 19 | "127.0.0.1", 20 | "3", 21 | "0", 22 | "1", 23 | "1", 24 | "N/A", 25 | "-1" 26 | ], 27 | [ 28 | "1596348004", 29 | "A", 30 | "mytestsite.net.reddog.microsoft.com", 31 | "127.0.0.1", 32 | "3", 33 | "0", 34 | "2", 35 | "1", 36 | "N/A", 37 | "-1" 38 | ], 39 | [ 40 | "1596348033", 41 | "AAAA", 42 | "mytestsite.net", 43 | "10.0.0.1", 44 | "3", 45 | "0", 46 | "4", 47 | "1", 48 | "N/A", 49 | "-1" 50 | ], 51 | [ 52 | "1596348034", 53 | "A", 54 | "mytestsite.net", 55 | "10.0.0.1", 56 | "3", 57 | "0", 58 | "1", 59 | "1", 60 | "N/A", 61 | "-1" 62 | ], 63 | [ 64 | "1596348034", 65 | "A", 66 | "mytestsite.net.reddog.microsoft.com", 67 | "10.0.0.1", 68 | "3", 69 | "0", 70 | "2", 71 | "2", 72 | "N/A", 73 | "-1" 74 | ], 75 | [ 76 | "1596348077", 77 | "AAAA", 78 | "www.msftncsi.com", 79 | "127.0.0.1", 80 | "2", 81 | "0", 82 | "3", 83 | "18", 84 | "N/A", 85 | "-1" 86 | ], 87 | [ 88 | "1596348077", 89 | "A", 90 | "www.msftncsi.com", 91 | "127.0.0.1", 92 | "2", 93 | "0", 94 | "3", 95 | "17", 96 | "N/A", 97 | "-1" 98 | ], 99 | [ 100 | "1596348119", 101 | "AAAA", 102 | "test-network.net", 103 | "127.0.0.1", 104 | "3", 105 | "0", 106 | "4", 107 | "1", 108 | "N/A", 109 | "-1" 110 | ], 111 | [ 112 | "1596348119", 113 | "A", 114 | "test-network.net", 115 | "127.0.0.1", 116 | "2", 117 | "0", 118 | "1", 119 | "2798", 120 | "N/A", 121 | "-1" 122 | ], 123 | [ 124 | "1596348119", 125 | "A", 126 | "test-network.net.reddog.microsoft.com", 127 | "127.0.0.1", 128 | "2", 129 | "0", 130 | "2", 131 | "2377", 132 | "N/A", 133 | "-1" 134 | ] 135 | ] 136 | } -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/Data/Api/getForwardDestinations.json: -------------------------------------------------------------------------------- 1 | { 2 | "forward_destinations": { 3 | "blocklist|blocklist": 4.22, 4 | "cache|cache": 61.69, 5 | "one.one.one.one|1.1.1.1": 21.7, 6 | "one.one.one.one|1.0.0.1": 18.1 7 | } 8 | } -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/Data/Api/getQueryTypes.json: -------------------------------------------------------------------------------- 1 | { 2 | "querytypes": { 3 | "A (IPv4)": 73.4, 4 | "AAAA (IPv6)": 25.27, 5 | "ANY": 0, 6 | "SRV": 0.49, 7 | "SOA": 0, 8 | "PTR": 0.83, 9 | "TXT": 0, 10 | "NAPTR": 0 11 | } 12 | } -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/Data/Api/overTimeData10mins.json: -------------------------------------------------------------------------------- 1 | { 2 | "domains_over_time": { 3 | "1596438300": 66, 4 | "1596438900": 64, 5 | "1596439500": 69, 6 | "1596440100": 84, 7 | "1596440700": 72, 8 | "1596441300": 159, 9 | "1596441900": 108, 10 | "1596442500": 111, 11 | "1596443100": 67, 12 | "1596443700": 78, 13 | "1596444300": 70, 14 | "1596444900": 60, 15 | "1596445500": 43, 16 | "1596446100": 206, 17 | "1596446700": 54, 18 | "1596447300": 46, 19 | "1596447900": 55, 20 | "1596448500": 53, 21 | "1596449100": 56, 22 | "1596449700": 42, 23 | "1596450300": 55, 24 | "1596450900": 46, 25 | "1596451500": 56, 26 | "1596452100": 52, 27 | "1596452700": 49, 28 | "1596453300": 48, 29 | "1596453900": 43, 30 | "1596454500": 56, 31 | "1596455100": 53, 32 | "1596455700": 50, 33 | "1596456300": 41, 34 | "1596456900": 56, 35 | "1596457500": 47, 36 | "1596458100": 40, 37 | "1596458700": 53, 38 | "1596459300": 46, 39 | "1596459900": 49, 40 | "1596460500": 46, 41 | "1596461100": 58, 42 | "1596461700": 39, 43 | "1596462300": 49, 44 | "1596462900": 41, 45 | "1596463500": 55, 46 | "1596464100": 56, 47 | "1596464700": 46, 48 | "1596465300": 41, 49 | "1596465900": 63, 50 | "1596466500": 59, 51 | "1596467100": 48, 52 | "1596467700": 49, 53 | "1596468300": 50, 54 | "1596468900": 51, 55 | "1596469500": 40, 56 | "1596470100": 49, 57 | "1596470700": 49, 58 | "1596471300": 53, 59 | "1596471900": 45, 60 | "1596472500": 51, 61 | "1596473100": 43, 62 | "1596473700": 49, 63 | "1596474300": 55, 64 | "1596474900": 50, 65 | "1596475500": 44, 66 | "1596476100": 52, 67 | "1596476700": 52, 68 | "1596477300": 55, 69 | "1596477900": 43, 70 | "1596478500": 50, 71 | "1596479100": 49, 72 | "1596479700": 53, 73 | "1596480300": 38, 74 | "1596480900": 54, 75 | "1596481500": 46, 76 | "1596482100": 44, 77 | "1596482700": 44, 78 | "1596483300": 54, 79 | "1596483900": 57, 80 | "1596484500": 43, 81 | "1596485100": 50, 82 | "1596485700": 52, 83 | "1596486300": 48, 84 | "1596486900": 42, 85 | "1596487500": 48, 86 | "1596488100": 49, 87 | "1596488700": 48, 88 | "1596489300": 43, 89 | "1596489900": 55, 90 | "1596490500": 48, 91 | "1596491100": 37, 92 | "1596491700": 291, 93 | "1596492300": 262, 94 | "1596492900": 60, 95 | "1596493500": 16, 96 | "1596494100": 21, 97 | "1596494700": 24, 98 | "1596495300": 37, 99 | "1596495900": 25, 100 | "1596496500": 17, 101 | "1596497100": 15, 102 | "1596497700": 25, 103 | "1596498300": 15, 104 | "1596498900": 30, 105 | "1596499500": 30, 106 | "1596500100": 35, 107 | "1596500700": 17, 108 | "1596501300": 15, 109 | "1596501900": 65, 110 | "1596502500": 32, 111 | "1596503100": 18, 112 | "1596503700": 18, 113 | "1596504300": 21, 114 | "1596504900": 12, 115 | "1596505500": 24, 116 | "1596506100": 34, 117 | "1596506700": 20, 118 | "1596507300": 18, 119 | "1596507900": 16, 120 | "1596508500": 17, 121 | "1596509100": 61, 122 | "1596509700": 29, 123 | "1596510300": 15, 124 | "1596510900": 20, 125 | "1596511500": 15, 126 | "1596512100": 19, 127 | "1596512700": 21, 128 | "1596513300": 26, 129 | "1596513900": 18, 130 | "1596514500": 21, 131 | "1596515100": 11, 132 | "1596515700": 18, 133 | "1596516300": 24, 134 | "1596516900": 22, 135 | "1596517500": 19, 136 | "1596518100": 73, 137 | "1596518700": 23, 138 | "1596519300": 17, 139 | "1596519900": 19, 140 | "1596520500": 31, 141 | "1596521100": 13 142 | }, 143 | "ads_over_time": { 144 | "1596438300": 8, 145 | "1596438900": 0, 146 | "1596439500": 0, 147 | "1596440100": 0, 148 | "1596440700": 1, 149 | "1596441300": 16, 150 | "1596441900": 30, 151 | "1596442500": 37, 152 | "1596443100": 0, 153 | "1596443700": 2, 154 | "1596444300": 1, 155 | "1596444900": 0, 156 | "1596445500": 0, 157 | "1596446100": 12, 158 | "1596446700": 0, 159 | "1596447300": 0, 160 | "1596447900": 0, 161 | "1596448500": 0, 162 | "1596449100": 0, 163 | "1596449700": 0, 164 | "1596450300": 0, 165 | "1596450900": 0, 166 | "1596451500": 0, 167 | "1596452100": 0, 168 | "1596452700": 0, 169 | "1596453300": 0, 170 | "1596453900": 0, 171 | "1596454500": 0, 172 | "1596455100": 0, 173 | "1596455700": 0, 174 | "1596456300": 0, 175 | "1596456900": 0, 176 | "1596457500": 0, 177 | "1596458100": 0, 178 | "1596458700": 0, 179 | "1596459300": 0, 180 | "1596459900": 0, 181 | "1596460500": 0, 182 | "1596461100": 0, 183 | "1596461700": 0, 184 | "1596462300": 0, 185 | "1596462900": 0, 186 | "1596463500": 0, 187 | "1596464100": 0, 188 | "1596464700": 0, 189 | "1596465300": 0, 190 | "1596465900": 0, 191 | "1596466500": 0, 192 | "1596467100": 0, 193 | "1596467700": 0, 194 | "1596468300": 0, 195 | "1596468900": 0, 196 | "1596469500": 0, 197 | "1596470100": 0, 198 | "1596470700": 0, 199 | "1596471300": 0, 200 | "1596471900": 0, 201 | "1596472500": 0, 202 | "1596473100": 0, 203 | "1596473700": 0, 204 | "1596474300": 0, 205 | "1596474900": 0, 206 | "1596475500": 0, 207 | "1596476100": 0, 208 | "1596476700": 0, 209 | "1596477300": 0, 210 | "1596477900": 0, 211 | "1596478500": 0, 212 | "1596479100": 0, 213 | "1596479700": 0, 214 | "1596480300": 0, 215 | "1596480900": 0, 216 | "1596481500": 0, 217 | "1596482100": 0, 218 | "1596482700": 0, 219 | "1596483300": 0, 220 | "1596483900": 0, 221 | "1596484500": 0, 222 | "1596485100": 0, 223 | "1596485700": 0, 224 | "1596486300": 0, 225 | "1596486900": 0, 226 | "1596487500": 0, 227 | "1596488100": 0, 228 | "1596488700": 0, 229 | "1596489300": 0, 230 | "1596489900": 0, 231 | "1596490500": 0, 232 | "1596491100": 0, 233 | "1596491700": 43, 234 | "1596492300": 87, 235 | "1596492900": 22, 236 | "1596493500": 1, 237 | "1596494100": 0, 238 | "1596494700": 0, 239 | "1596495300": 1, 240 | "1596495900": 0, 241 | "1596496500": 0, 242 | "1596497100": 0, 243 | "1596497700": 1, 244 | "1596498300": 0, 245 | "1596498900": 0, 246 | "1596499500": 1, 247 | "1596500100": 1, 248 | "1596500700": 0, 249 | "1596501300": 0, 250 | "1596501900": 5, 251 | "1596502500": 2, 252 | "1596503100": 1, 253 | "1596503700": 0, 254 | "1596504300": 1, 255 | "1596504900": 0, 256 | "1596505500": 0, 257 | "1596506100": 0, 258 | "1596506700": 0, 259 | "1596507300": 1, 260 | "1596507900": 0, 261 | "1596508500": 0, 262 | "1596509100": 10, 263 | "1596509700": 0, 264 | "1596510300": 0, 265 | "1596510900": 1, 266 | "1596511500": 0, 267 | "1596512100": 0, 268 | "1596512700": 0, 269 | "1596513300": 0, 270 | "1596513900": 0, 271 | "1596514500": 1, 272 | "1596515100": 0, 273 | "1596515700": 1, 274 | "1596516300": 0, 275 | "1596516900": 0, 276 | "1596517500": 0, 277 | "1596518100": 19, 278 | "1596518700": 2, 279 | "1596519300": 0, 280 | "1596519900": 0, 281 | "1596520500": 0, 282 | "1596521100": 0 283 | } 284 | } -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/Data/Api/summary.json: -------------------------------------------------------------------------------- 1 | { 2 | "domains_being_blocked": "85,674", 3 | "dns_queries_today": "6,787", 4 | "ads_blocked_today": "326", 5 | "ads_percentage_today": "4.8", 6 | "unique_domains": "2,262", 7 | "queries_forwarded": "2,379", 8 | "queries_cached": "4,064", 9 | "clients_ever_seen": "4", 10 | "unique_clients": "3", 11 | "dns_queries_all_types": "6,787", 12 | "reply_NODATA": "1,827", 13 | "reply_NXDOMAIN": "1,464", 14 | "reply_CNAME": "1,432", 15 | "reply_IP": "2,669", 16 | "privacy_level": "0", 17 | "status": "enabled", 18 | "gravity_last_updated": { 19 | "file_exists": true, 20 | "absolute": 1596340568, 21 | "relative": { 22 | "days": 2, 23 | "hours": 2, 24 | "minutes": 48 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/Data/Api/summaryRaw.json: -------------------------------------------------------------------------------- 1 | { 2 | "domains_being_blocked": 85674, 3 | "dns_queries_today": 1700, 4 | "ads_blocked_today": 304, 5 | "ads_percentage_today": 17.882353, 6 | "unique_domains": 2407, 7 | "queries_forwarded": 981, 8 | "queries_cached": 403, 9 | "clients_ever_seen": 4, 10 | "unique_clients": 1, 11 | "dns_queries_all_types": 1700, 12 | "reply_NODATA": 355, 13 | "reply_NXDOMAIN": 105, 14 | "reply_CNAME": 1173, 15 | "reply_IP": 924, 16 | "privacy_level": 0, 17 | "status": "enabled", 18 | "gravity_last_updated": { 19 | "file_exists": true, 20 | "absolute": 1596340568, 21 | "relative": { 22 | "days": 2, 23 | "hours": 21, 24 | "minutes": 20 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/Data/Api/topClients.json: -------------------------------------------------------------------------------- 1 | { 2 | "top_sources": { 3 | "win10.internal.cloudapp.net|10.0.2.4": 1409 4 | } 5 | } -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/Data/Api/topItems.json: -------------------------------------------------------------------------------- 1 | { 2 | "top_queries": { 3 | "polling.bbc.co.uk": 92, 4 | "wpad.reddog.microsoft.com": 80, 5 | "www.bing.com": 60, 6 | "www.msftncsi.com": 31, 7 | }, 8 | "top_ads": { 9 | "g.msn.com": 534, 10 | "g.live.com": 64, 11 | "vortex.data.microsoft.com": 20, 12 | "c.msn.com": 16, 13 | "watson.telemetry.microsoft.com": 8, 14 | "web.vortex.data.microsoft.com": 4, 15 | "otf.msn.com": 2, 16 | "www.googletagservices.com": 2, 17 | "mybbc-analytics.files.bbci.co.uk": 2 18 | } 19 | } -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/PiHoleApiClient.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | PreserveNewest 24 | 25 | 26 | PreserveNewest 27 | 28 | 29 | PreserveNewest 30 | 31 | 32 | PreserveNewest 33 | 34 | 35 | PreserveNewest 36 | 37 | 38 | PreserveNewest 39 | 40 | 41 | PreserveNewest 42 | 43 | 44 | PreserveNewest 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /PiHoleApiClient.Tests/PiHoleApiClientTests.cs: -------------------------------------------------------------------------------- 1 | using Moq; 2 | using Moq.Protected; 3 | using System.IO; 4 | using System.Net; 5 | using System.Net.Http; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using Xunit; 9 | 10 | namespace PiHoleApiClient.Tests 11 | { 12 | public class PiHoleApiClientTests 13 | { 14 | private Mock GetMockHttpMsgHandler(string response) 15 | { 16 | var handlerMock = new Mock(MockBehavior.Strict); 17 | handlerMock.Protected() 18 | // Setup the PROTECTED method to mock 19 | .Setup>( 20 | "SendAsync", 21 | ItExpr.IsAny(), 22 | ItExpr.IsAny() 23 | ) 24 | // prepare the expected response of the mocked http call 25 | .ReturnsAsync(new HttpResponseMessage() 26 | { 27 | StatusCode = HttpStatusCode.OK, 28 | Content = new StringContent(response), 29 | }) 30 | .Verifiable(); 31 | return handlerMock; 32 | } 33 | 34 | [Fact] 35 | public async void PiHoleDisable_Success() 36 | { 37 | string successResponse = "{\"status\": \"disabled\"}"; 38 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 39 | 40 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 41 | var status = await piholeClient.Disable(10); 42 | 43 | Assert.NotNull(status); 44 | Assert.Equal("disabled", status.Status); 45 | } 46 | 47 | [Fact] 48 | public async void PiHoleEnable_Success() 49 | { 50 | string successResponse = "{\"status\": \"enabled\"}"; 51 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 52 | 53 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 54 | var status = await piholeClient.Enable(); 55 | 56 | Assert.NotNull(status); 57 | Assert.Equal("enabled", status.Status); 58 | } 59 | 60 | [Fact] 61 | public async void GetAllQueries_Success() 62 | { 63 | string successResponse = File.ReadAllText("Data/Api/getAllQueries.json"); 64 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 65 | 66 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 67 | var queries = await piholeClient.GetAllQueriesAsync(); 68 | 69 | Assert.NotNull(queries); 70 | Assert.True(queries.Count > 0); 71 | Assert.Equal("1596348004", queries[0].Time); 72 | Assert.Equal("AAAA", queries[0].Type); 73 | Assert.Equal("mytestsite.net", queries[0].Domain); 74 | Assert.Equal("127.0.0.1", queries[0].Client); 75 | Assert.Equal("OK (forwarded)", queries[0].Status); 76 | } 77 | 78 | [Fact] 79 | public async void GetApiBackendType_Success() 80 | { 81 | string successResponse = "{\"type\": \"FTL\"}"; 82 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 83 | 84 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 85 | var typeObj = await piholeClient.GetApiBackendTypeAsync(); 86 | 87 | Assert.NotNull(typeObj); 88 | Assert.Equal("FTL", typeObj.Type); 89 | } 90 | 91 | 92 | [Fact] 93 | public async void GetApiVersion_Success() 94 | { 95 | string successResponse = "{\"version\": 3}"; 96 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 97 | 98 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", ""); 99 | var versionObj = await piholeClient.GetApiVersionAsync(); 100 | 101 | Assert.NotNull(versionObj); 102 | Assert.Equal("3", versionObj.Version); 103 | } 104 | 105 | [Fact] 106 | public async void GetForwardDestinationsAsStringc_Success() 107 | { 108 | string successResponse = File.ReadAllText("Data/Api/getForwardDestinations.json"); 109 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 110 | 111 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 112 | var fwdsObj = await piholeClient.GetForwardDestinationsAsync(); 113 | 114 | Assert.NotNull(fwdsObj); 115 | Assert.True(fwdsObj.FwdDestinations.Count > 0); 116 | Assert.Equal("4.22", fwdsObj.FwdDestinations["blocklist|blocklist"]); 117 | Assert.Equal("61.69", fwdsObj.FwdDestinations["cache|cache"]); 118 | Assert.Equal("21.7", fwdsObj.FwdDestinations["one.one.one.one|1.1.1.1"]); 119 | Assert.Equal("18.1", fwdsObj.FwdDestinations["one.one.one.one|1.0.0.1"]); 120 | } 121 | 122 | [Fact] 123 | public async void GetOverTimeData10minsAsync_Success() 124 | { 125 | string successResponse = File.ReadAllText("Data/Api/overTimeData10mins.json"); 126 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 127 | 128 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 129 | var ovrTimeObj = await piholeClient.GetOverTimeData10minsAsync(); 130 | 131 | Assert.NotNull(ovrTimeObj); 132 | Assert.True(ovrTimeObj.AdsOverTime.Count > 0); 133 | Assert.True(ovrTimeObj.DomainsOverTime.Count > 0); 134 | Assert.Equal(66, ovrTimeObj.DomainsOverTime["1596438300"]); 135 | Assert.Equal(8, ovrTimeObj.AdsOverTime["1596438300"]); 136 | } 137 | 138 | [Fact] 139 | public async void GetQueryTypesAsync_Success() 140 | { 141 | string successResponse = File.ReadAllText("Data/Api/getQueryTypes.json"); 142 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 143 | 144 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 145 | var qTypeObj = await piholeClient.GetQueryTypesAsync(); 146 | 147 | Assert.NotNull(qTypeObj); 148 | Assert.True(qTypeObj.Types.Count > 0); 149 | Assert.Equal(73.4, qTypeObj.Types["A (IPv4)"]); 150 | Assert.Equal(25.27, qTypeObj.Types["AAAA (IPv6)"]); 151 | } 152 | 153 | [Fact] 154 | public async void GetSummaryAsync_Success() 155 | { 156 | string successResponse = File.ReadAllText("Data/Api/summary.json"); 157 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 158 | 159 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 160 | var summaryObj = await piholeClient.GetSummaryAsync(); 161 | 162 | Assert.NotNull(summaryObj); 163 | Assert.NotNull(summaryObj.GravityLastUpdated); 164 | Assert.NotNull(summaryObj.GravityLastUpdated.Relative); 165 | Assert.Equal("85,674", summaryObj.DomainsBeingBlocked); 166 | Assert.Equal("6,787", summaryObj.DnsQueriesToday); 167 | Assert.True(summaryObj.GravityLastUpdated.FileExists); 168 | Assert.Equal("1596340568", summaryObj.GravityLastUpdated.Absolute); 169 | Assert.Equal(2, summaryObj.GravityLastUpdated.Relative.Days); 170 | } 171 | 172 | [Fact] 173 | public async void GetSummaryRawAsync_Success() 174 | { 175 | string successResponse = File.ReadAllText("Data/Api/summaryRaw.json"); 176 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 177 | 178 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 179 | var summaryObj = await piholeClient.GetSummaryAsync(); 180 | 181 | Assert.NotNull(summaryObj); 182 | Assert.NotNull(summaryObj.GravityLastUpdated); 183 | Assert.NotNull(summaryObj.GravityLastUpdated.Relative); 184 | Assert.Equal("85674", summaryObj.DomainsBeingBlocked); 185 | Assert.Equal("1700", summaryObj.DnsQueriesToday); 186 | Assert.True(summaryObj.GravityLastUpdated.FileExists); 187 | Assert.Equal("1596340568", summaryObj.GravityLastUpdated.Absolute); 188 | Assert.Equal(2, summaryObj.GravityLastUpdated.Relative.Days); 189 | } 190 | 191 | [Fact] 192 | public async void GetTopClientsAsync_Success() 193 | { 194 | string successResponse = File.ReadAllText("Data/Api/topClients.json"); 195 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 196 | 197 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 198 | var tClientsObj = await piholeClient.GetTopClientsAsync(); 199 | 200 | Assert.NotNull(tClientsObj); 201 | Assert.Equal(1409, tClientsObj.TopSources["win10.internal.cloudapp.net|10.0.2.4"]); 202 | } 203 | 204 | [Fact] 205 | public async void GetTopItemssAsync_Success() 206 | { 207 | string successResponse = File.ReadAllText("Data/Api/topItems.json"); 208 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 209 | 210 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 211 | var tItemsObj = await piholeClient.GetTopItemsAsync(); 212 | 213 | Assert.NotNull(tItemsObj); 214 | Assert.Equal(92, tItemsObj.TopQueries["polling.bbc.co.uk"]); 215 | Assert.Equal(534, tItemsObj.TopAds["g.msn.com"]); 216 | } 217 | 218 | [Fact] 219 | public async void GetRecentlyBlocked_Success() 220 | { 221 | string successResponse = "vortex.data.microsoft.com"; 222 | var httpClient = new HttpClient(GetMockHttpMsgHandler(successResponse).Object); 223 | 224 | var piholeClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 225 | var blocked = await piholeClient.RecentlyBlockedAsync(); 226 | 227 | Assert.NotNull(blocked); 228 | Assert.Equal("vortex.data.microsoft.com", blocked); 229 | } 230 | 231 | 232 | } 233 | } -------------------------------------------------------------------------------- /PiHoleApiClient.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30320.27 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiHoleApiClient", "PiHoleApiClient\PiHoleApiClient.csproj", "{96BC8AB4-2D99-4E8C-9E5A-525F20F2AB00}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PiHoleApiClient.Tests", "PiHoleApiClient.Tests\PiHoleApiClient.Tests.csproj", "{CDB2D080-B87B-4355-A119-AB398D41460A}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {96BC8AB4-2D99-4E8C-9E5A-525F20F2AB00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {96BC8AB4-2D99-4E8C-9E5A-525F20F2AB00}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {96BC8AB4-2D99-4E8C-9E5A-525F20F2AB00}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {96BC8AB4-2D99-4E8C-9E5A-525F20F2AB00}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {CDB2D080-B87B-4355-A119-AB398D41460A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {CDB2D080-B87B-4355-A119-AB398D41460A}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {CDB2D080-B87B-4355-A119-AB398D41460A}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {CDB2D080-B87B-4355-A119-AB398D41460A}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {654B80C3-68E4-48C5-AF05-CE02ACC01BB8} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /PiHoleApiClient/IPiHoleApiClient.cs: -------------------------------------------------------------------------------- 1 | using PiHoleApiClient.Models; 2 | using System.Collections.Generic; 3 | using System.Threading.Tasks; 4 | 5 | namespace PiHoleApiClient 6 | { 7 | /// 8 | /// Pi Hole API client. more information at : https://discourse.pi-hole.net/t/pi-hole-api/1863 9 | /// 10 | public interface IPiHoleApiClient 11 | { 12 | Task GetApiVersionAsync(); 13 | 14 | Task GetApiBackendTypeAsync(); 15 | 16 | Task GetSummaryRawAsync(); 17 | 18 | Task GetSummaryAsync(); 19 | 20 | Task GetOverTimeData10minsAsync(); 21 | 22 | Task GetTopItemsAsync(); 23 | 24 | Task GetTopClientsAsync(); 25 | 26 | 27 | Task GetForwardDestinationsAsync(); 28 | 29 | Task GetQueryTypesAsync(); 30 | 31 | 32 | Task> GetAllQueriesAsync(); 33 | 34 | Task Enable(); 35 | 36 | Task Disable(long seconds = 0); 37 | 38 | Task RecentlyBlockedAsync(); 39 | } 40 | } -------------------------------------------------------------------------------- /PiHoleApiClient/Mappers/QueryMapper.cs: -------------------------------------------------------------------------------- 1 | using PiHoleApiClient.Models; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace PiHoleApiClient.Mappers 6 | { 7 | public static class QueryMapper 8 | { 9 | public static List MapQueries(this PreQuery preQuery) 10 | { 11 | if (preQuery == null) return new List(); 12 | return preQuery?.Data?.Select(q => new Query(q[0], q[1], q[2], q[3], q[4])).ToList(); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /PiHoleApiClient/Models/ApiVersion.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace PiHoleApiClient.Models 4 | { 5 | public class ApiVersion 6 | { 7 | public string Version { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /PiHoleApiClient/Models/BackendType.cs: -------------------------------------------------------------------------------- 1 | namespace PiHoleApiClient.Models 2 | { 3 | public class BackendType 4 | { 5 | public string Type { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /PiHoleApiClient/Models/ForwardDestinations.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System.Collections.Generic; 3 | 4 | namespace PiHoleApiClient.Models 5 | { 6 | public class ForwardDestinations 7 | { 8 | [JsonProperty("forward_destinations")] 9 | public Dictionary FwdDestinations { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /PiHoleApiClient/Models/OverTimeData10mins.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System.Collections.Generic; 3 | 4 | namespace PiHoleApiClient.Models 5 | { 6 | public class OverTimeData10mins 7 | { 8 | [JsonProperty("domains_over_time")] 9 | public Dictionary DomainsOverTime { get; set; } 10 | 11 | [JsonProperty("ads_over_time")] 12 | public Dictionary AdsOverTime { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /PiHoleApiClient/Models/PiStatus.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace PiHoleApiClient.Models 4 | { 5 | public class PiStatus 6 | { 7 | [JsonProperty("status")] 8 | public string Status { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /PiHoleApiClient/Models/Query.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace PiHoleApiClient.Models 4 | { 5 | public class PreQuery 6 | { 7 | public List> Data { get; set; } 8 | } 9 | 10 | public class Query 11 | { 12 | public Query(string time, string type, string domain, string client, string status) 13 | { 14 | Time = time; 15 | Type = type; 16 | Domain = domain; 17 | Client = client; 18 | switch (status) 19 | { 20 | case "1": 21 | Status = "Blocked (gravity)"; 22 | break; 23 | 24 | case "2": 25 | Status = "OK (forwarded)"; 26 | break; 27 | 28 | case "3": 29 | Status = "OK (cached)"; 30 | break; 31 | 32 | case "4": 33 | Status = "Blocked (regex blacklist)"; 34 | break; 35 | 36 | case "5": 37 | Status = "Blocked (exact blacklist)"; 38 | break; 39 | 40 | case "6": 41 | Status = "Blocked (external, IP)"; 42 | break; 43 | 44 | case "7": 45 | Status = "Blocked (external, NULL)"; 46 | break; 47 | 48 | case "8": 49 | Status = "Blocked (external, NXRA)"; 50 | break; 51 | 52 | case "9": 53 | Status = "Blocked (gravity, CNAME)"; 54 | break; 55 | 56 | case "10": 57 | Status = "Blocked (regex blacklist, CNAME)"; 58 | break; 59 | 60 | case "11": 61 | Status = "Blocked (exact blacklist, CNAME)"; 62 | break; 63 | 64 | default: 65 | Status = "Unknown"; 66 | break; 67 | } 68 | } 69 | 70 | public string Time { get; private set; } 71 | public string Type { get; private set; } 72 | public string Domain { get; private set; } 73 | public string Client { get; private set; } 74 | public string Status { get; private set; } 75 | } 76 | } -------------------------------------------------------------------------------- /PiHoleApiClient/Models/Querytypes.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System.Collections.Generic; 3 | 4 | namespace PiHoleApiClient.Models 5 | { 6 | public class Querytypes 7 | { 8 | [JsonProperty("querytypes")] 9 | public Dictionary Types { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /PiHoleApiClient/Models/Summary.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | 3 | namespace PiHoleApiClient.Models 4 | { 5 | public class Summary 6 | { 7 | [JsonProperty("domains_being_blocked")] 8 | public string DomainsBeingBlocked { get; set; } 9 | 10 | [JsonProperty("dns_queries_today")] 11 | public string DnsQueriesToday { get; set; } 12 | 13 | [JsonProperty("ads_blocked_today")] 14 | public string AdsBlockedToday { get; set; } 15 | 16 | [JsonProperty("ads_percentage_today")] 17 | public string AdsPercentageToday { get; set; } 18 | 19 | [JsonProperty("unique_domains")] 20 | public string UniqueDomains { get; set; } 21 | 22 | [JsonProperty("queries_forwarded")] 23 | public string QueriesForwarded { get; set; } 24 | 25 | [JsonProperty("queries_cached")] 26 | public string QueriesCached { get; set; } 27 | 28 | [JsonProperty("clients_ever_seen")] 29 | public string ClientsEverSeen { get; set; } 30 | 31 | [JsonProperty("unique_clients")] 32 | public string UniqueClients { get; set; } 33 | 34 | [JsonProperty("dns_queries_all_types")] 35 | public string DnsQueriesAllTypes { get; set; } 36 | 37 | [JsonProperty("reply_NODATA")] 38 | public string ReplyNoData { get; set; } 39 | 40 | [JsonProperty("reply_NXDOMAIN")] 41 | public string ReplyNxDomain { get; set; } 42 | 43 | [JsonProperty("reply_CNAME")] 44 | public string ReplyCname { get; set; } 45 | 46 | [JsonProperty("reply_IP")] 47 | public string ReplyIp { get; set; } 48 | 49 | [JsonProperty("privacy_level")] 50 | public string PrivacyLevel { get; set; } 51 | 52 | [JsonProperty("status")] 53 | public string Status { get; set; } 54 | 55 | [JsonProperty("gravity_last_updated")] 56 | public GravityLastUpdated GravityLastUpdated { get; set; } 57 | 58 | 59 | } 60 | public class Relative 61 | { 62 | [JsonProperty("days")] 63 | public int Days { get; set; } 64 | 65 | [JsonProperty("hours")] 66 | public int Hours { get; set; } 67 | 68 | [JsonProperty("minutes")] 69 | public int Minutes { get; set; } 70 | } 71 | 72 | public class GravityLastUpdated 73 | { 74 | [JsonProperty("file_exists")] 75 | public bool FileExists { get; set; } 76 | 77 | [JsonProperty("absolute")] 78 | public string Absolute { get; set; } 79 | 80 | [JsonProperty("relative")] 81 | public Relative Relative { get; set; } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /PiHoleApiClient/Models/TopClients.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System.Collections.Generic; 3 | 4 | namespace PiHoleApiClient.Models 5 | { 6 | public class TopClients 7 | { 8 | [JsonProperty("top_sources")] 9 | public Dictionary TopSources { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /PiHoleApiClient/Models/TopItems.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using System.Collections.Generic; 3 | 4 | namespace PiHoleApiClient.Models 5 | { 6 | public class TopItems 7 | { 8 | [JsonProperty("top_queries")] 9 | public Dictionary TopQueries { get; set; } 10 | 11 | [JsonProperty("top_ads")] 12 | public Dictionary TopAds { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /PiHoleApiClient/PiHoleApiClient.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using PiHoleApiClient.Mappers; 3 | using PiHoleApiClient.Models; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Net.Http; 7 | using System.Threading.Tasks; 8 | 9 | namespace PiHoleApiClient 10 | { 11 | public class PiHoleApiClient : IPiHoleApiClient 12 | { 13 | private readonly string _baseUrl; 14 | private readonly string _token; 15 | private HttpClient _httpClient; 16 | private readonly string _disableEndpoint = "disable"; 17 | private readonly string _enabledEndpoint = "enable"; 18 | private readonly string _getAllQueriesEndpoint = "getAllQueries"; 19 | private readonly string _getApiBackendTypeEndpoint = "type"; 20 | private readonly string _getApiVersionEndpoint = "version"; 21 | private readonly string _getForwardDestinationsEndpoint = "getForwardDestinations"; 22 | private readonly string _overTimeData10minsEndpoint = "overTimeData10mins"; 23 | private readonly string _getQueryTypesEndpoint = "getQueryTypes"; 24 | private readonly string _summaryEndpoint = "summary"; 25 | private readonly string _summaryRawEndpoint = "summaryRaw"; 26 | private readonly string _topClientsEndpoint = "topClients"; 27 | private readonly string _topItemsEndpoint = "topItems"; 28 | private readonly string _recentBlockedEndpoint = "recentBlocked"; 29 | 30 | public PiHoleApiClient(HttpClient httpClient, string baseUrl, string token = "") 31 | { 32 | _httpClient = httpClient; 33 | _baseUrl = baseUrl; 34 | _token = token; 35 | } 36 | 37 | private async Task GetResultAsString(string url) 38 | { 39 | var r = await _httpClient.GetAsync(url); 40 | return await r.Content.ReadAsStringAsync(); 41 | } 42 | 43 | public async Task Disable(long seconds = 0) 44 | { 45 | var s = seconds > 0 ? $"{_disableEndpoint}={seconds}" : _disableEndpoint; 46 | var resultString = await GetResultAsString($"{_baseUrl}?{s}&auth={_token}"); 47 | return JsonConvert.DeserializeObject(resultString); 48 | } 49 | 50 | public async Task Enable() 51 | { 52 | return JsonConvert.DeserializeObject 53 | (await GetResultAsString($"{_baseUrl}?{_enabledEndpoint}&auth={_token}")); 54 | } 55 | 56 | public async Task> GetAllQueriesAsync() 57 | { 58 | var result = await GetResultAsString($"{_baseUrl}?{_getAllQueriesEndpoint}&auth={_token}"); 59 | return JsonConvert.DeserializeObject(result).MapQueries(); 60 | } 61 | 62 | public async Task GetApiBackendTypeAsync() 63 | { 64 | var resultString = await GetResultAsString($"{_baseUrl}?{_getApiBackendTypeEndpoint}"); 65 | return JsonConvert.DeserializeObject(resultString); 66 | } 67 | 68 | public async Task GetApiVersionAsync() 69 | { 70 | var resultString = await GetResultAsString($"{_baseUrl}?{_getApiVersionEndpoint}"); 71 | return JsonConvert.DeserializeObject(resultString); 72 | } 73 | 74 | public async Task GetForwardDestinationsAsync() 75 | { 76 | var resultString = await GetResultAsString($"{_baseUrl}?{_getForwardDestinationsEndpoint}&auth={_token}"); 77 | return JsonConvert.DeserializeObject(resultString); 78 | } 79 | 80 | public async Task GetOverTimeData10minsAsync() 81 | { 82 | var resultString = await GetResultAsString($"{_baseUrl}?{_overTimeData10minsEndpoint}"); 83 | return JsonConvert.DeserializeObject(resultString); 84 | } 85 | 86 | public async Task GetQueryTypesAsync() 87 | { 88 | var resultString = await GetResultAsString($"{_baseUrl}?{_getQueryTypesEndpoint}&auth={_token}"); 89 | return JsonConvert.DeserializeObject(resultString); 90 | } 91 | 92 | public async Task GetSummaryAsync() 93 | { 94 | var resultString = await GetResultAsString($"{_baseUrl}?{_summaryEndpoint}"); 95 | return JsonConvert.DeserializeObject(resultString); 96 | } 97 | 98 | public async Task GetSummaryRawAsync() 99 | { 100 | var resultString = await GetResultAsString($"{_baseUrl}?{_summaryRawEndpoint}"); 101 | return JsonConvert.DeserializeObject(resultString); 102 | } 103 | 104 | public async Task GetTopClientsAsync() 105 | { 106 | var resultString = await GetResultAsString($"{_baseUrl}?{_topClientsEndpoint}&auth={_token}"); 107 | return JsonConvert.DeserializeObject(resultString); 108 | } 109 | 110 | public async Task GetTopItemsAsync() 111 | { 112 | var resultString = await GetResultAsString($"{_baseUrl}?{_topItemsEndpoint}&auth={_token}"); 113 | return JsonConvert.DeserializeObject(resultString); 114 | } 115 | 116 | public async Task RecentlyBlockedAsync() 117 | { 118 | return await GetResultAsString($"{_baseUrl}?{_recentBlockedEndpoint}&auth={_token}"); 119 | 120 | } 121 | } 122 | } -------------------------------------------------------------------------------- /PiHoleApiClient/PiHoleApiClient.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | Sandeep Parekh 6 | 7 | This is a cross platform client for communicating with Pi-Hole API, a network wide Ads blocking DNS Sink. 8 | https://github.com/spar/PiHoleApiClient 9 | https://github.com/spar/PiHoleApiClient 10 | 11 | LICENSE 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | True 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://dev.azure.com/sandeepparekh/PiHoleApiClient/_apis/build/status/PiHoleApiClient-CI?branchName=master)](https://dev.azure.com/sandeepparekh/PiHoleApiClient/_build/latest?definitionId=11&branchName=master) 2 | # Introduction 3 | This is a cross platform client for communicating with [Pi-Hole](https://pi-hole.net/) API, a network wide Ads blocking DNS Sink. 4 | 5 | ## Pi-Hole Endpoints 6 | Available endpoints 7 | 8 | ### **version** 9 | This endpoint return Pi-hole API's version. 10 | 11 | ```csharp 12 | var httpClient = new HttpClient(); 13 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 14 | var versionObj = await apiClient.GetApiVersionAsync(); 15 | Console.WriteLine(versionObj.Version); 16 | //3 17 | ``` 18 | 19 | 20 | ### **type** 21 | Return Pi-hole API's backend Type 22 | 23 | ```csharp 24 | var httpClient = new HttpClient(); 25 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 26 | var typeObj = await apiClient.GetApiBackendTypeAsync(); 27 | Console.WriteLine(typeObj.Type); 28 | //FTL 29 | ``` 30 | 31 | ### **summaryRaw** 32 | Returns Statistics in Raw format 33 | 34 | ```csharp 35 | var httpClient = new HttpClient(); 36 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 37 | var summaryRaw = await apiClient.GetSummaryRawAsync(); 38 | Console.WriteLine(JsonConvert.SerializeObject(summaryRaw)); 39 | ``` 40 | Output 41 | ```json 42 | { 43 | "domains_being_blocked": 85674, 44 | "dns_queries_today": 1700, 45 | "ads_blocked_today": 304, 46 | "ads_percentage_today": 17.882353, 47 | "unique_domains": 2407, 48 | "queries_forwarded": 981, 49 | "queries_cached": 403, 50 | "clients_ever_seen": 4, 51 | "unique_clients": 1, 52 | "dns_queries_all_types": 1700, 53 | "reply_NODATA": 355, 54 | "reply_NXDOMAIN": 105, 55 | "reply_CNAME": 1173, 56 | "reply_IP": 924, 57 | "privacy_level": 0, 58 | "status": "enabled", 59 | "gravity_last_updated": { 60 | "file_exists": true, 61 | "absolute": 1596340568, 62 | "relative": { 63 | "days": 2, 64 | "hours": 21, 65 | "minutes": 20 66 | } 67 | } 68 | } 69 | ``` 70 | 71 | ### **summary** 72 | Returns Statistics formatted style 73 | 74 | ```csharp 75 | var httpClient = new HttpClient(); 76 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 77 | var summary = await apiClient.GetSummaryAsync(); 78 | Console.WriteLine(JsonConvert.SerializeObject(summary)); 79 | ``` 80 | Output 81 | ```json 82 | { 83 | "domains_being_blocked": "85,674", 84 | "dns_queries_today": "6,787", 85 | "ads_blocked_today": "326", 86 | "ads_percentage_today": "4.8", 87 | "unique_domains": "2,262", 88 | "queries_forwarded": "2,379", 89 | "queries_cached": "4,064", 90 | "clients_ever_seen": "4", 91 | "unique_clients": "3", 92 | "dns_queries_all_types": "6,787", 93 | "reply_NODATA": "1,827", 94 | "reply_NXDOMAIN": "1,464", 95 | "reply_CNAME": "1,432", 96 | "reply_IP": "2,669", 97 | "privacy_level": "0", 98 | "status": "enabled", 99 | "gravity_last_updated": { 100 | "file_exists": true, 101 | "absolute": 1596340568, 102 | "relative": { 103 | "days": 2, 104 | "hours": 2, 105 | "minutes": 48 106 | } 107 | } 108 | } 109 | ``` 110 | 111 | ### **overTimeData10mins** 112 | Returns domains and ads data over 10 min. Useful for showing graphs 113 | 114 | ```csharp 115 | var httpClient = new HttpClient(); 116 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 117 | var data = await apiClient.GetOverTimeData10minsAsync(); 118 | Console.WriteLine(JsonConvert.SerializeObject(data)); 119 | ``` 120 | Output 121 | ```json 122 | { 123 | "domains_over_time": { 124 | "1596438300": 66, 125 | "1596438900": 64, 126 | "1596439500": 69, 127 | ... 128 | "1596521100": 13 129 | }, 130 | "ads_over_time": { 131 | "1596438300": 8, 132 | "1596438900": 0, 133 | ... 134 | "1596521100": 0 135 | } 136 | } 137 | ``` 138 | 139 | ### **topItems** 140 | Returns tops domains and top ads data 141 | 142 | ```csharp 143 | var httpClient = new HttpClient(); 144 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 145 | var data = await apiClient.GetTopItemsAsync(); 146 | Console.WriteLine(JsonConvert.SerializeObject(data)); 147 | ``` 148 | Output 149 | ```json 150 | { 151 | "top_queries": { 152 | "polling.bbc.co.uk": 92, 153 | "wpad.reddog.microsoft.com": 80, 154 | "www.bing.com": 60, 155 | "www.msftncsi.com": 31, 156 | }, 157 | "top_ads": { 158 | "g.msn.com": 534, 159 | "g.live.com": 64, 160 | "vortex.data.microsoft.com": 20, 161 | "c.msn.com": 16, 162 | "watson.telemetry.microsoft.com": 8, 163 | "web.vortex.data.microsoft.com": 4, 164 | "otf.msn.com": 2, 165 | "www.googletagservices.com": 2, 166 | "mybbc-analytics.files.bbci.co.uk": 2 167 | } 168 | } 169 | ``` 170 | 171 | ### **topClients** 172 | Returns tops clients list 173 | 174 | ```csharp 175 | var httpClient = new HttpClient(); 176 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 177 | var data = await apiClient.GetTopClientsAsync(); 178 | Console.WriteLine(JsonConvert.SerializeObject(data)); 179 | ``` 180 | Output 181 | ```json 182 | { 183 | "top_sources": { 184 | "win10|10.0.1.1": 1409 185 | } 186 | } 187 | ``` 188 | 189 | ### **getForwardDestinations** 190 | Shows number of queries that have been forwarded and the target 191 | 192 | ```csharp 193 | var httpClient = new HttpClient(); 194 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 195 | var data = await apiClient.GetForwardDestinationsAsync(); 196 | Console.WriteLine(JsonConvert.SerializeObject(data)); 197 | ``` 198 | Output 199 | ```json 200 | { 201 | "forward_destinations": { 202 | "blocklist|blocklist": 4.22, 203 | "cache|cache": 61.69, 204 | "one.one.one.one|1.1.1.1": 21.7, 205 | "one.one.one.one|1.0.0.1": 18.1 206 | } 207 | } 208 | ``` 209 | 210 | ### **getQueryTypes** 211 | Get queries types processed by pi-hole 212 | 213 | ```csharp 214 | var httpClient = new HttpClient(); 215 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 216 | var data = await apiClient.GetQueryTypesAsync(); 217 | Console.WriteLine(JsonConvert.SerializeObject(data)); 218 | ``` 219 | Output 220 | ```json 221 | { 222 | "querytypes": { 223 | "A (IPv4)": 73.4, 224 | "AAAA (IPv6)": 25.27, 225 | "ANY": 0, 226 | "SRV": 0.49, 227 | "SOA": 0, 228 | "PTR": 0.83, 229 | "TXT": 0, 230 | "NAPTR": 0 231 | } 232 | } 233 | ``` 234 | 235 | ### **getAllQueries** 236 | returns all DNS queries 237 | 238 | ```csharp 239 | var httpClient = new HttpClient(); 240 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 241 | var data = await apiClient.GetAllQueriesAsync(); 242 | Console.WriteLine(JsonConvert.SerializeObject(data)); 243 | ``` 244 | Output 245 | ```json 246 | { 247 | "data": [ 248 | [ 249 | "1596348004", 250 | "AAAA", 251 | "mytestsite.net", 252 | "127.0.0.1", 253 | "2", 254 | "0", 255 | "4", 256 | "26", 257 | "N/A", 258 | "-1" 259 | ], 260 | ... 261 | [ 262 | "1596348004", 263 | "A", 264 | "mytestsite.net", 265 | "127.0.0.1", 266 | "3", 267 | "0", 268 | "1", 269 | "1", 270 | "N/A", 271 | "-1" 272 | ] 273 | ] 274 | } 275 | ``` 276 | ### **enable** 277 | enables pi-hole 278 | 279 | ```csharp 280 | var httpClient = new HttpClient(); 281 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 282 | var data = await apiClient.Enable(); 283 | Console.WriteLine(JsonConvert.SerializeObject(data.Status)); 284 | //enabled 285 | ``` 286 | 287 | ### **disable** 288 | disables pi-hole 289 | 290 | ```csharp 291 | var httpClient = new HttpClient(); 292 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 293 | var data = await apiClient.Disable(10); // disables for 10 seconds, leave empty indefinitely disable it 294 | Console.WriteLine(JsonConvert.SerializeObject(data.Status)); 295 | //diabled 296 | ``` 297 | 298 | 299 | ### **recentBlocked** 300 | returns most recently blocked domain 301 | 302 | ```csharp 303 | var httpClient = new HttpClient(); 304 | var apiClient = new PiHoleApiClient(httpClient, "http://pi.hole/admin/api.php", "token"); 305 | var domain = await apiClient.RecentlyBlockedAsync(); 306 | Console.WriteLine(JsonConvert.SerializeObject(domain)); 307 | //someAdDomain.com 308 | ``` 309 | 310 | --------------------------------------------------------------------------------