├── .github └── workflows │ ├── codeql-analysis.yml │ ├── release.yml │ └── tests.yml ├── .gitignore ├── LICENSE ├── README.md ├── cmd └── main │ ├── Makefile │ └── main.go ├── go.mod ├── go.sum ├── img └── live-view.png └── pkg ├── api ├── allQueries.go ├── enableDisable.go ├── summary.go ├── summary_test.go └── topItems.go ├── auth ├── auth.go └── auth_test.go ├── cli ├── cli.go ├── configCmd.go ├── databaseCmd.go ├── enableDisableCmd.go ├── init.go ├── runCmd.go └── setupCmd.go ├── data ├── data.go └── settings.go ├── database ├── clientSummary.go ├── databaseUtilities.go └── topQueries.go ├── network ├── httpClient.go └── utilities.go └── ui └── ui.go /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # 12 | name: "CodeQL" 13 | 14 | on: 15 | push: 16 | branches: [ master ] 17 | pull_request: 18 | # The branches below must be a subset of the branches above 19 | branches: [ master ] 20 | schedule: 21 | - cron: '24 16 * * 3' 22 | 23 | jobs: 24 | analyze: 25 | name: Analyze 26 | runs-on: ubuntu-latest 27 | 28 | strategy: 29 | fail-fast: false 30 | matrix: 31 | language: [ 'go' ] 32 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 33 | # Learn more: 34 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed 35 | 36 | steps: 37 | - name: Checkout repository 38 | uses: actions/checkout@v3 39 | 40 | # Initializes the CodeQL tools for scanning. 41 | - name: Initialize CodeQL 42 | uses: github/codeql-action/init@v2 43 | with: 44 | languages: ${{ matrix.language }} 45 | # If you wish to specify custom queries, you can do so here or in a config file. 46 | # By default, queries listed here will override any specified in a config file. 47 | # Prefix the list here with "+" to use these queries and those in the config file. 48 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 49 | 50 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 51 | # If this step fails, then you should remove it and run the build manually (see below) 52 | - name: Autobuild 53 | uses: github/codeql-action/autobuild@v2 54 | 55 | # ℹ️ Command-line programs to run using the OS shell. 56 | # 📚 https://git.io/JvXDl 57 | 58 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 59 | # and modify them (or add more) to build your code if your project 60 | # uses a compiled language 61 | 62 | #- run: | 63 | # make bootstrap 64 | # make release 65 | 66 | - name: Perform CodeQL Analysis 67 | uses: github/codeql-action/analyze@v2 68 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Create tag and draft release 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - master 8 | 9 | jobs: 10 | release: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Checkout code 14 | uses: actions/checkout@v4 15 | 16 | - name: Unlock keyring 17 | uses: t1m0thyj/unlock-keyring@v1 18 | 19 | - name: Set up Go 20 | uses: actions/setup-go@v5 21 | with: 22 | go-version: 1.18 23 | 24 | - name: Go mod tidy 25 | run: go mod tidy 26 | 27 | - name: Set up MinGW 28 | uses: egor-tensin/setup-mingw@v2 29 | 30 | - name: Make windows 31 | run: make -C cmd/main win 32 | 33 | - name: Make mac 34 | run: make -C cmd/main mac 35 | 36 | - name: Make linux 37 | run: make -C cmd/main linux 38 | 39 | - name: Set up Git 40 | run: | 41 | git config --local user.email "action@github.com" 42 | git config --local user.name "GitHub Action" 43 | 44 | - name: Create Tag 45 | id: create_tag 46 | run: | 47 | TAG_DATE=$(date +'%Y%m%d-%H%M%S') 48 | TAG_NAME="v${TAG_DATE}" 49 | echo "Creating tag ${TAG_NAME}" 50 | git tag $TAG_NAME 51 | git push origin $TAG_NAME 52 | echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT 53 | 54 | - name: Create Draft Release 55 | uses: softprops/action-gh-release@v1 56 | env: 57 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 58 | with: 59 | body: | 60 | Note: this was an autogenerated release based on a workflow. 61 | tag_name: ${{ steps.create_tag.outputs.tag_name }} 62 | name: Release ${{ steps.create_tag.outputs.tag_name }} 63 | draft: true 64 | generate_release_notes: true 65 | files: | 66 | cmd/main/picli-mac 67 | cmd/main/picli-linux 68 | cmd/main/picli.exe 69 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: Build and test Go 5 | 6 | on: 7 | push: 8 | branches: [ "**" ] 9 | pull_request: 10 | branches: [ "**" ] 11 | 12 | jobs: 13 | 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: t1m0thyj/unlock-keyring@v1 19 | 20 | - name: Set up Go 21 | uses: actions/setup-go@v5 22 | with: 23 | go-version: 1.18 24 | 25 | - name: Build 26 | run: go build -v ./... 27 | 28 | - name: Test 29 | run: go test -v ./... 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | 17 | # database file used for testing Pi-CLI's database analytics functions 18 | /cmd/main/*.db 19 | 20 | # ignore binaries to prevent accidentally committing 21 | cmd/main/picli-mac 22 | cmd/main/picli-linux 23 | cmd/main/picli.exe 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Pi-CLI

2 | 3 |

4 | 5 | 6 | 7 | 8 | 9 | 10 |

11 | 12 |

Pi-CLI is a command line program used to view data from a Pi-Hole instance directly in your terminal.

13 | 14 |
15 | 16 | ![terminal](./img/live-view.png) 17 | 18 | # Features 19 | 20 | - Live view 21 | - As shown above, Pi-CLI can generate a live updating view of your Pi-Hole data 22 | - Updates down to a minimum of 1s, providing essentially live query data. Support for smaller intervals may come in the future. 23 | - Update parameters including the number of logged queries in the 'latest queries' table and watch the UI automatically update and pull in the correct data for you. Use your arrow keys to scroll and navigate the table. 24 | - One off commands 25 | - Don't want a live view? Use one of the subcommands of Pi-CLI to tell it exactly what data you want, and it will give it to you. No fancy UI needed. 26 | - Database analysis 27 | - Pi-CLI has the newly added ability to be able to analyse the `pihole-FTL.db` database used a long term data store 28 | for a Pi-Hole. It can extract and analyse all time data, including client, query and ad domain data. 29 | - Quickly configure and forget 30 | - Run one setup command, and Pi-CLI will store and remember all of your details for next time. 31 | - Lightweight 32 | - Even when logging 1000's of DNS queries, memory and CPU usage remains minimal. 33 | - Secure 34 | - Pi-CLI uses cross-platform OS keyring libraries to make sure your Pi-Hole API key is both securely stored and easy to retrieve in the future. Your API key is never stored in plaintext unless you explicitly tell Pi-CLI to not use your keyring. 35 | 36 | # Usage 37 | 38 | ### `~$ picli [global options] command [command options] [arguments...]` 39 | 40 |
41 | 42 | For help, run `~$ picli h` 43 | 44 | For command help, run `~$ picli h` 45 | 46 | For subcommand help, run `~$ picli -h` 47 | 48 | 49 |
50 | 51 | # Commands 52 | 53 | ``` 54 | setup, s Configure Pi-CLI 55 | config, c Interact with stored configuration settings 56 | run, r Run a one off command without booting the live view 57 | database, d Analytics options to run on a Pi-Hole's FTL database 58 | help, h Shows a list of commands or help for one command 59 | ``` 60 | 61 | ### The `config` command 62 | 63 | _Manage stored config data_ 64 | 65 | ``` 66 | delete, d Delete stored config data (config file and API key) 67 | view, v View config stored config data (config file and API key) 68 | help, h Shows a list of commands or help for one command 69 | ``` 70 | 71 | ### The `run` command 72 | 73 | _Run a single command without the live view_ 74 | 75 | ``` 76 | summary, s Extract a basic summary of data from the Pi-Hole 77 | top-forwarded, tf Extract the current top 10 forwarded DNS queries 78 | top-blocked, tb Extract the current top 10 blocked DNS queries 79 | latest-queries, lq Extract the latest queries 80 | enable, e Enable the Pi-Hole 81 | disable, d Disable the Pi-Hole 82 | help, h Shows a list of commands or help for one command 83 | ``` 84 | 85 | ### The `database` command 86 | 87 | _These commands are ran against a Pi-Hole's FTL database file and provide **all time** data metrics_ 88 | 89 | ``` 90 | client-summary, cs Summary of all Pi-Hole clients 91 | top-queries, tq Returns the top (all time) queries 92 | help, h Shows a list of commands or help for one command 93 | ``` 94 | 95 | # FAQ 96 | 97 | - Where do I get my API key? 98 | - Navigate to your Pi-Hole's web interface, then settings. Click on the API/Web interface tab and press 99 | 'Show API token'. 100 | - Pre-Compiled binaries? 101 | - See [releases](https://github.com/Reeceeboii/Pi-CLI/releases) 102 | - How do I compile myself? 103 | 104 | - With [make](https://www.gnu.org/software/make/)! There is a `Makefile` in the 105 | [cmd/main](https://github.com/Reeceeboii/Pi-CLI/tree/master/cmd/main) directory that can be used for compilation 106 | on Windows, Mac and Linux. 107 | 108 | Compilation targets are: `win`, `mac` and `linux` 109 | --- 110 | 111 | If you find Pi-CLI useful, please consider [donating to the Pi-Hole project](https://pi-hole.net/donate/) 112 | 113 | Or, feel free to submit code to make Pi-CLI even more useful! 114 | -------------------------------------------------------------------------------- /cmd/main/Makefile: -------------------------------------------------------------------------------- 1 | # REQUIREMENTS 2 | # Make 3 | # go toolchain 4 | # LINUX SPECIFIC 5 | # gcc 6 | # gcc-mingw-w64-x86-64 7 | # WINDOWS SPECIFIC 8 | # MinGW 9 | 10 | # Parameters 11 | GO-CMD=go 12 | GO-BUILD=$(GO-CMD) build 13 | 14 | # Binary names 15 | WINDOWS-BINARY=picli.exe 16 | LINUX-BINARY=picli-linux 17 | MAC-BINARY=picli-mac 18 | 19 | # Linker flags 20 | LINKER_FLAGS=-ldflags "-s -w" 21 | 22 | # Compiling for Windows from Linux requires the MinGW-w64 x86 cross compiler 23 | CGO_ENABLED=CGO_ENABLED=1 24 | MINGW_CROSS_COMPILER=CC=x86_64-w64-mingw32-gcc 25 | 26 | # Cross-compilation compiler operating system flags 27 | GOOS-WINDOWS=GOOS=windows 28 | GOOS-MAC=GOOS=darwin 29 | GOOS-LINUX=GOOS=linux 30 | 31 | # Compiling Pi-CLI for Windows 32 | win: $(objects) 33 | @echo "Compiling for Windows..." 34 | ifeq ($(OS), Windows_NT) 35 | $(GO-BUILD) $(LINKER_FLAGS) -o $(WINDOWS-BINARY) 36 | else 37 | $(GOOS-WINDOWS) $(MINGW_CROSS_COMPILER) $(CGO_ENABLED) $(GO-BUILD) $(LINKER_FLAGS) -o $(WINDOWS-BINARY) 38 | endif 39 | 40 | # Compiling Pi-CLI for Mac 41 | mac: 42 | @echo "Compiling for Mac..." 43 | $(GOOS-MAC) $(GO-BUILD) $(LINKER_FLAGS) -o $(MAC-BINARY) 44 | 45 | # Compiling Pi-CLI for Linux 46 | linux: 47 | @echo "Compiling for Linux..." 48 | $(GOOS-LINUX) $(GO-BUILD) $(LINKER_FLAGS) -o $(LINUX-BINARY) 49 | -------------------------------------------------------------------------------- /cmd/main/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/Reeceeboii/Pi-CLI/pkg/cli" 5 | "log" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | if err := cli.App.Run(os.Args); err != nil { 11 | log.Fatal(err) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Reeceeboii/Pi-CLI 2 | 3 | go 1.18 4 | 5 | require ( 6 | github.com/buger/jsonparser v1.1.1 7 | github.com/fatih/color v1.15.0 8 | github.com/gizak/termui/v3 v3.1.0 9 | github.com/mattn/go-sqlite3 v1.14.17 10 | github.com/urfave/cli/v2 v2.25.7 11 | github.com/zalando/go-keyring v0.2.3 12 | golang.org/x/text v0.10.0 13 | ) 14 | 15 | require ( 16 | github.com/alessio/shellescape v1.4.1 // indirect 17 | github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect 18 | github.com/danieljoos/wincred v1.2.0 // indirect 19 | github.com/godbus/dbus/v5 v5.1.0 // indirect 20 | github.com/mattn/go-colorable v0.1.13 // indirect 21 | github.com/mattn/go-isatty v0.0.19 // indirect 22 | github.com/mattn/go-runewidth v0.0.14 // indirect 23 | github.com/mitchellh/go-wordwrap v1.0.1 // indirect 24 | github.com/nsf/termbox-go v1.1.1 // indirect 25 | github.com/rivo/uniseg v0.4.4 // indirect 26 | github.com/russross/blackfriday/v2 v2.1.0 // indirect 27 | github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect 28 | golang.org/x/sys v0.9.0 // indirect 29 | ) 30 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0= 2 | github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30= 3 | github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= 4 | github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= 5 | github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= 6 | github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 7 | github.com/danieljoos/wincred v1.2.0 h1:ozqKHaLK0W/ii4KVbbvluM91W2H3Sh0BncbUNPS7jLE= 8 | github.com/danieljoos/wincred v1.2.0/go.mod h1:FzQLLMKBFdvu+osBrnFODiv32YGwCfx0SkRa/eYHgec= 9 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 10 | github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= 11 | github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= 12 | github.com/gizak/termui/v3 v3.1.0 h1:ZZmVDgwHl7gR7elfKf1xc4IudXZ5qqfDh4wExk4Iajc= 13 | github.com/gizak/termui/v3 v3.1.0/go.mod h1:bXQEBkJpzxUAKf0+xq9MSWAvWZlE7c+aidmyFlkYTrY= 14 | github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= 15 | github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= 16 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 17 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 18 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 19 | github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= 20 | github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 21 | github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= 22 | github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= 23 | github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= 24 | github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 25 | github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM= 26 | github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= 27 | github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= 28 | github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= 29 | github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= 30 | github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ= 31 | github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY= 32 | github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo= 33 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 34 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 35 | github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= 36 | github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= 37 | github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= 38 | github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 39 | github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= 40 | github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= 41 | github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs= 42 | github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= 43 | github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= 44 | github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= 45 | github.com/zalando/go-keyring v0.2.3 h1:v9CUu9phlABObO4LPWycf+zwMG7nlbb3t/B5wa97yms= 46 | github.com/zalando/go-keyring v0.2.3/go.mod h1:HL4k+OXQfJUWaMnqyuSOc0drfGPX2b51Du6K+MRgZMk= 47 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 48 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 49 | golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= 50 | golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 51 | golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= 52 | golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= 53 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 54 | -------------------------------------------------------------------------------- /img/live-view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reeceeboii/Pi-CLI/9efaf8304f897b5e2e68f0d25d1300188a23f0a7/img/live-view.png -------------------------------------------------------------------------------- /pkg/api/allQueries.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "fmt" 5 | "github.com/Reeceeboii/Pi-CLI/pkg/data" 6 | "github.com/Reeceeboii/Pi-CLI/pkg/network" 7 | "github.com/buger/jsonparser" 8 | "io/ioutil" 9 | "log" 10 | "net/http" 11 | "strconv" 12 | "sync" 13 | "time" 14 | ) 15 | 16 | // instance of AllQueries used at runtime 17 | var LiveAllQueries = NewAllQueries() 18 | 19 | const ( 20 | AllQueryDataKey = "data" 21 | // The starting setting for the number of queries that are included in the live log 22 | DefaultAmountOfQueries = 10 23 | ) 24 | 25 | // Holds information about a single query logged by Pi-Hole 26 | type Query struct { 27 | // UNIX timestamp of when the query was logged 28 | UnixTime string 29 | // The type of query 30 | QueryType string 31 | // The domain the query was sent to 32 | Domain string 33 | // The client that sent the query 34 | OriginClient string 35 | // Where the query was forwarded to 36 | ForwardedTo string 37 | } 38 | 39 | // Holds a slice of query structs 40 | type AllQueries struct { 41 | // Slice of Query structs 42 | Queries []Query 43 | // The amount of queries being stored in the log 44 | AmountOfQueriesInLog int 45 | // The queries stored in a format able to be displayed as a table 46 | Table []string 47 | } 48 | 49 | // Make a new AllQueries instance 50 | func NewAllQueries() *AllQueries { 51 | return &AllQueries{ 52 | Queries: make([]Query, DefaultAmountOfQueries), 53 | AmountOfQueriesInLog: DefaultAmountOfQueries, 54 | Table: []string{}, 55 | } 56 | } 57 | 58 | // Updates the all queries list with up to date information from the Pi-Hole 59 | func (allQueries *AllQueries) Update(wg *sync.WaitGroup) { 60 | if wg != nil { 61 | wg.Add(1) 62 | defer wg.Done() 63 | } 64 | 65 | queryAmount := strconv.Itoa(allQueries.AmountOfQueriesInLog) 66 | url := data.LivePiCLIData.FormattedAPIAddress + 67 | "?getAllQueries=" + 68 | queryAmount + 69 | "&auth=" + 70 | data.LivePiCLIData.APIKey 71 | 72 | req, err := http.NewRequest("GET", url, nil) 73 | if err != nil { 74 | log.Fatal(err) 75 | } 76 | 77 | res, err := network.HttpClient.Do(req) 78 | if err != nil { 79 | log.Fatal(err) 80 | } 81 | defer res.Body.Close() 82 | 83 | parsedBody, _ := ioutil.ReadAll(res.Body) 84 | 85 | /* 86 | For every index in the parsed body's data array, pull out the required fields. 87 | I tried to use ArrayEach here but couldn't seem to get it to work the way I wanted. 88 | There has to be a nicer way to do this. This approach is absolute garbage. 89 | */ 90 | for iter := 0; iter < allQueries.AmountOfQueriesInLog; iter++ { 91 | queryArray, _, _, _ := jsonparser.Get(parsedBody, AllQueryDataKey, fmt.Sprintf("[%d]", iter)) 92 | unixTime, _ := jsonparser.GetString(queryArray, "[0]") 93 | queryType, _ := jsonparser.GetString(queryArray, "[1]") 94 | domain, _ := jsonparser.GetString(queryArray, "[2]") 95 | originClient, _ := jsonparser.GetString(queryArray, "[3]") 96 | forwardedTo, _ := jsonparser.GetString(queryArray, "[10]") 97 | allQueries.Queries[iter] = Query{ 98 | UnixTime: unixTime, 99 | QueryType: queryType, 100 | Domain: domain, 101 | OriginClient: originClient, 102 | ForwardedTo: forwardedTo, 103 | } 104 | } 105 | allQueries.convertToTable() 106 | } 107 | 108 | // Convert slice of queries to a formatted multidimensional slice 109 | func (allQueries *AllQueries) convertToTable() { 110 | table := make([]string, allQueries.AmountOfQueriesInLog) 111 | 112 | for i, q := range allQueries.Queries { 113 | iTime, _ := strconv.ParseInt(q.UnixTime, 10, 64) 114 | parsedTime := time.Unix(iTime, 0) 115 | entry := fmt.Sprintf("%d [%s] Query type %s from %s to %s forwarded to %s", 116 | (allQueries.AmountOfQueriesInLog)-i, 117 | parsedTime.Format("15:04:05"), 118 | q.QueryType, 119 | q.OriginClient, 120 | q.Domain, 121 | q.ForwardedTo, 122 | ) 123 | table[(allQueries.AmountOfQueriesInLog-1)-i] = entry 124 | } 125 | allQueries.Table = table 126 | } 127 | -------------------------------------------------------------------------------- /pkg/api/enableDisable.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "fmt" 5 | "github.com/Reeceeboii/Pi-CLI/pkg/data" 6 | "github.com/Reeceeboii/Pi-CLI/pkg/network" 7 | "log" 8 | "net/http" 9 | ) 10 | 11 | // Enable the Pi-Hole 12 | func EnablePiHole() { 13 | url := data.LivePiCLIData.FormattedAPIAddress + "?enable" + "&auth=" + data.LivePiCLIData.APIKey 14 | req, err := http.NewRequest("GET", url, nil) 15 | if err != nil { 16 | log.Fatal(err) 17 | } 18 | 19 | _, err = network.HttpClient.Do(req) 20 | if err != nil { 21 | log.Fatal(err) 22 | } 23 | } 24 | 25 | // Disable the Pi-Hole 26 | func DisablePiHole(timeout bool, time int64) { 27 | disable := "?disable" 28 | if timeout { 29 | disable += fmt.Sprintf("=%d", time) 30 | } 31 | url := data.LivePiCLIData.FormattedAPIAddress + disable + "&auth=" + data.LivePiCLIData.APIKey 32 | req, err := http.NewRequest("GET", url, nil) 33 | if err != nil { 34 | log.Fatal(err) 35 | } 36 | 37 | _, err = network.HttpClient.Do(req) 38 | if err != nil { 39 | log.Fatal(err) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /pkg/api/summary.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "github.com/Reeceeboii/Pi-CLI/pkg/network" 5 | "github.com/buger/jsonparser" 6 | "io/ioutil" 7 | "log" 8 | "net/http" 9 | "sync" 10 | ) 11 | 12 | var LiveSummary = NewSummary() 13 | 14 | // Keys that can be used to index JSON responses from the Pi-Hole's API 15 | const ( 16 | DNSQueriesTodayKey = "dns_queries_today" 17 | AdsBlockedTodayKey = "ads_blocked_today" 18 | PercentBlockedTodayKey = "ads_percentage_today" 19 | DomainsOnBlockListKey = "domains_being_blocked" 20 | StatusKey = "status" 21 | PrivacyLevelKey = "privacy_level" 22 | TotalClientsSeenKey = "clients_ever_seen" 23 | ) 24 | 25 | // Summary holds things that do not require authentication to retrieve 26 | type Summary struct { 27 | // Total number of queries logged today 28 | QueriesToday string 29 | // Total number of queries blocked today 30 | BlockedToday string 31 | // Percentage of today's queries that have been blocked 32 | PercentBlockedToday string 33 | // How large is Pi-Hole's active blocklist? 34 | DomainsOnBlocklist string 35 | // Enabled vs. disabled 36 | Status string 37 | // Pi-Hole's current data privacy level 38 | PrivacyLevel string 39 | // Mapping between privacy level numbers and their meanings 40 | PrivacyLevelNumberMapping map[string]string 41 | // The total number of clients that the Pi-Hole has seen 42 | TotalClientsSeen string 43 | } 44 | 45 | /* 46 | Returns a new Summary instance with default values for all fields 47 | */ 48 | func NewSummary() *Summary { 49 | return &Summary{ 50 | QueriesToday: "", 51 | BlockedToday: "", 52 | PercentBlockedToday: "", 53 | DomainsOnBlocklist: "", 54 | Status: "", 55 | PrivacyLevel: "", 56 | PrivacyLevelNumberMapping: map[string]string{ 57 | "0": "Show Everything", 58 | "1": "Hide Domains", 59 | "2": "Hide Domains and Clients", 60 | "3": "Anonymous", 61 | }, 62 | TotalClientsSeen: "", 63 | } 64 | } 65 | 66 | // Updates a Summary struct with up to date information 67 | func (summary *Summary) Update(url string, key string, wg *sync.WaitGroup) { 68 | if wg != nil { 69 | wg.Add(1) 70 | defer wg.Done() 71 | } 72 | // create the URL for the summary data and send a request to it 73 | url += "?summary" 74 | if len(key) > 0 { 75 | url += "&auth=" + key 76 | } 77 | 78 | req, err := http.NewRequest("GET", url, nil) 79 | if err != nil { 80 | log.Fatal(err) 81 | } 82 | 83 | res, err := network.HttpClient.Do(req) 84 | if err != nil { 85 | log.Fatal(err) 86 | } 87 | defer res.Body.Close() 88 | 89 | parsedBody, _ := ioutil.ReadAll(res.Body) 90 | // yoink out all the data from the response 91 | // pack it into the struct 92 | summary.QueriesToday, _ = jsonparser.GetString(parsedBody, DNSQueriesTodayKey) 93 | summary.BlockedToday, _ = jsonparser.GetString(parsedBody, AdsBlockedTodayKey) 94 | summary.PercentBlockedToday, _ = jsonparser.GetString(parsedBody, PercentBlockedTodayKey) 95 | summary.DomainsOnBlocklist, _ = jsonparser.GetString(parsedBody, DomainsOnBlockListKey) 96 | summary.Status, _ = jsonparser.GetString(parsedBody, StatusKey) 97 | summary.PrivacyLevel, _ = jsonparser.GetString(parsedBody, PrivacyLevelKey) 98 | summary.TotalClientsSeen, _ = jsonparser.GetString(parsedBody, TotalClientsSeenKey) 99 | } 100 | -------------------------------------------------------------------------------- /pkg/api/summary_test.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "net/http" 5 | "net/http/httptest" 6 | "strings" 7 | "testing" 8 | ) 9 | 10 | const ( 11 | // Sample API key for test case usage. 12 | testKey = "c808f484a4e88cc32a9a8bfcce19169c77bcd9c5eec18d859e1bb4b318bf42bf" 13 | ) 14 | 15 | // Tests for api.Summary.Update() with an API key 16 | func TestUpdateWithApiKey(t *testing.T) { 17 | summary := NewSummary() 18 | mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 19 | // Ensure URL is formatted with the correct query string. 20 | if !strings.Contains(r.URL.RequestURI(), "/api.php?summary&auth="+testKey) { 21 | t.Error("@TestUpdateWithApiKey: api.Summary.Update() did not request the expected Pi Hole api endpoint with expected API key.") 22 | } 23 | })) 24 | defer mockServer.Close() 25 | url := mockServer.URL + "/api.php" 26 | 27 | summary.Update(url, testKey, nil) 28 | } 29 | 30 | // Tests for api.Summary.Update() without an API key 31 | func TestUpdateWithoutAPIKey(t *testing.T) { 32 | summary := NewSummary() 33 | mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 34 | // Ensure URL does not contain &auth as part of its query string. 35 | if strings.Contains(r.URL.RequestURI(), "/api.php?summary&auth=") { 36 | t.Error("@TestUpdateWithoutAPIKey: api.Summary.Update() did not send the expected query string when calling with an API key: /api.php?summary&auth=") 37 | } 38 | // Ensure URL is formatted with the correct query string. 39 | if !strings.Contains(r.URL.RequestURI(), "/api.php?summary") { 40 | t.Error("@TestUpdateWithoutAPIKey: api.Summary.Update() did not send the expected query string when calling with an empty API key: /api.php?summary") 41 | } 42 | })) 43 | defer mockServer.Close() 44 | url := mockServer.URL + "/api.php" 45 | summary.Update(url, "", nil) 46 | } 47 | -------------------------------------------------------------------------------- /pkg/api/topItems.go: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import ( 4 | "fmt" 5 | "github.com/Reeceeboii/Pi-CLI/pkg/data" 6 | "github.com/Reeceeboii/Pi-CLI/pkg/network" 7 | "github.com/buger/jsonparser" 8 | "io/ioutil" 9 | "log" 10 | "net/http" 11 | "sort" 12 | "strconv" 13 | "sync" 14 | ) 15 | 16 | // Instance of TopItems used at runtime 17 | var LiveTopItems = NewTopItems() 18 | 19 | // Keys that can be used to index JSON responses from the Pi-Hole's API 20 | const ( 21 | TopQueriesTodayKey = "top_queries" 22 | TopAdsTodayKey = "top_ads" 23 | ) 24 | 25 | // TopItems stores top permitted domains and top blocked domains (requires authentication to retrieve) 26 | type TopItems struct { 27 | // Mapping of top DNS queried domains and their occurrences 28 | TopQueries map[string]int 29 | // Mapping of top blocked DNS domains (ads and/or tracking) and their occurrences 30 | TopAds map[string]int 31 | // Pretty list version of TopQueries 32 | PrettyTopQueries []string 33 | // Pretty list version of TopAds 34 | PrettyTopAds []string 35 | } 36 | 37 | // A single domain and the number of times it occurs 38 | type domainOccurrencePair struct { 39 | // The domain 40 | domain string 41 | // The number of times it has occurred 42 | occurrence int 43 | } 44 | 45 | // Create a new TopItems instance 46 | func NewTopItems() *TopItems { 47 | return &TopItems{ 48 | TopQueries: map[string]int{}, 49 | TopAds: map[string]int{}, 50 | PrettyTopQueries: []string{}, 51 | PrettyTopAds: []string{}, 52 | } 53 | } 54 | 55 | // Updates a TopItems struct with up to date information 56 | func (topItems *TopItems) Update(wg *sync.WaitGroup) { 57 | if wg != nil { 58 | wg.Add(1) 59 | defer wg.Done() 60 | } 61 | 62 | url := data.LivePiCLIData.FormattedAPIAddress + "?topItems" + "&auth=" + data.LivePiCLIData.APIKey 63 | req, err := http.NewRequest("GET", url, nil) 64 | if err != nil { 65 | log.Fatal(err) 66 | } 67 | 68 | res, err := network.HttpClient.Do(req) 69 | if err != nil { 70 | log.Fatal(err) 71 | } 72 | defer res.Body.Close() 73 | 74 | parsedBody, _ := ioutil.ReadAll(res.Body) 75 | 76 | // parse the top queries response 77 | _ = jsonparser.ObjectEach(parsedBody, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error { 78 | topItems.TopQueries[string(key)], _ = strconv.Atoi(string(value)) 79 | return nil 80 | }, TopQueriesTodayKey) 81 | 82 | // and the same for the top ad networks 83 | _ = jsonparser.ObjectEach(parsedBody, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error { 84 | topItems.TopAds[string(key)], _ = strconv.Atoi(string(value)) 85 | return nil 86 | }, TopAdsTodayKey) 87 | 88 | topItems.prettyConvert() 89 | } 90 | 91 | // Convert maps of domain:hits to nice lists that can be displayed 92 | func (topItems *TopItems) prettyConvert() { 93 | var sortedTopQueries []domainOccurrencePair 94 | var sortedTopAds []domainOccurrencePair 95 | topItems.PrettyTopQueries = []string{} 96 | topItems.PrettyTopAds = []string{} 97 | 98 | for key, value := range topItems.TopQueries { 99 | sortedTopQueries = append(sortedTopQueries, domainOccurrencePair{ 100 | domain: key, 101 | occurrence: value, 102 | }) 103 | } 104 | 105 | for key, value := range topItems.TopAds { 106 | sortedTopAds = append(sortedTopAds, domainOccurrencePair{ 107 | domain: key, 108 | occurrence: value, 109 | }) 110 | } 111 | 112 | // sort ads and domains by occurrence 113 | sort.SliceStable(sortedTopQueries[:], func(i, j int) bool { 114 | return sortedTopQueries[i].occurrence > sortedTopQueries[j].occurrence 115 | }) 116 | sort.SliceStable(sortedTopAds[:], func(i, j int) bool { 117 | return sortedTopAds[i].occurrence > sortedTopAds[j].occurrence 118 | }) 119 | 120 | for _, domain := range sortedTopQueries { 121 | listEntry := fmt.Sprintf("%d hits | %s", domain.occurrence, domain.domain) 122 | topItems.PrettyTopQueries = append(topItems.PrettyTopQueries, listEntry) 123 | } 124 | for _, domain := range sortedTopAds { 125 | listEntry := fmt.Sprintf("%d hits | %s", domain.occurrence, domain.domain) 126 | topItems.PrettyTopAds = append(topItems.PrettyTopAds, listEntry) 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /pkg/auth/auth.go: -------------------------------------------------------------------------------- 1 | package auth 2 | 3 | import ( 4 | "io/ioutil" 5 | "log" 6 | "net/http" 7 | 8 | "github.com/Reeceeboii/Pi-CLI/pkg/network" 9 | "github.com/buger/jsonparser" 10 | "github.com/zalando/go-keyring" 11 | ) 12 | 13 | // Keyring Service: Required for use in authentication and API key management 14 | var KeyringService = "PiCLI" 15 | 16 | // Keyring User: Required for use in authentication and API key management 17 | var KeyringUsr = "api-key" 18 | 19 | // Retrieve the API key from the system keyring 20 | func RetrieveAPIKeyFromKeyring() string { 21 | APIKey, err := keyring.Get(KeyringService, KeyringUsr) 22 | if err != nil { 23 | log.Fatal(err) 24 | } 25 | return APIKey 26 | } 27 | 28 | /* 29 | Store the API key in the system keyring. Returns an error if this action failed. 30 | */ 31 | func StoreAPIKeyInKeyring(key string) error { 32 | if err := keyring.Set(KeyringService, KeyringUsr, key); err != nil { 33 | return err 34 | } 35 | return nil 36 | } 37 | 38 | // Delete the stored API key if it exists 39 | func DeleteAPIKeyFromKeyring() bool { 40 | if err := keyring.Delete(KeyringService, KeyringUsr); err != nil { 41 | return false 42 | } 43 | return true 44 | } 45 | 46 | // Is there an entry for the API key in the system keyring? 47 | func APIKeyIsInKeyring() bool { 48 | if _, err := keyring.Get(KeyringService, KeyringUsr); err != nil { 49 | return false 50 | } 51 | return true 52 | } 53 | 54 | // Does an key allow authentication? I.e., is is valid? 55 | func ValidateAPIKey(url string, key string) bool { 56 | /* 57 | To test the validity of the API key, we can attempt to enable the Pi-Hole. 58 | 59 | The response for a correct key: 60 | { 61 | "status": "enabled" 62 | } 63 | 64 | And the response for an incorrect key: 65 | [] 66 | 67 | Therefore we can simply perform a lookup for that "status" key. If it's there, the key is valid. 68 | 69 | */ 70 | 71 | queryString := url + "?enable" + "&auth=" + key 72 | req, err := http.NewRequest("GET", queryString, nil) 73 | if err != nil { 74 | log.Fatal(err) 75 | } 76 | 77 | res, err := network.HttpClient.Do(req) 78 | if err != nil { 79 | log.Fatal(err) 80 | } 81 | defer res.Body.Close() 82 | parsedBody, _ := ioutil.ReadAll(res.Body) 83 | 84 | if _, err := jsonparser.GetString(parsedBody, "status"); err != nil { 85 | return false 86 | } 87 | return true 88 | } 89 | -------------------------------------------------------------------------------- /pkg/auth/auth_test.go: -------------------------------------------------------------------------------- 1 | package auth 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "net/http/httptest" 7 | "strings" 8 | "testing" 9 | ) 10 | 11 | const ( 12 | // Sample API key for test case usage. 13 | testKey = "c808f484a4e88cc32a9a8bfcce19169c77bcd9c5eec18d859e1bb4b318bf42bf" 14 | ) 15 | 16 | // Calling init() in order to overwrite global variables for test purposes. 17 | func init() { 18 | KeyringService = "test-service" // Overwrite KeyringService for test cases 19 | KeyringUsr = "test-key" // Overwrite KeyringUsr for test cases 20 | } 21 | 22 | /* 23 | NOTE: 24 | Each test case is self-contained, for example: A key is stored at the beginning of each case and deleted before it ends. 25 | We do this because we cannot rely on Go to run its tests sequentially every time. 26 | */ 27 | 28 | // Tests for auth.APIKeyIsInKeyring() 29 | func TestAPIKeyIsInKeyring(t *testing.T) { 30 | // Ensuring StoreAPIKeyInKeyring() can successfully store a key in the keyring. 31 | err := StoreAPIKeyInKeyring(testKey) 32 | if err != nil { 33 | t.Errorf("@TestAPIKeyIsInKeyring: auth.StoreAPIKeyInKeyring() failed to store API key: %s", err) 34 | } 35 | 36 | // Ensuring APIKeyIsInKeyring() can successfully find the stored key. 37 | if !APIKeyIsInKeyring() { 38 | t.Error("@TestAPIKeyIsInKeyring: auth.APIKeyIsInKeyring() failed to find key in keyring.") 39 | } 40 | 41 | // Ensuring DeleteAPIKeyFromKeyring() is able to successfully find and delete key 42 | if !DeleteAPIKeyFromKeyring() { 43 | t.Error("@TestRetrieveAPIKeyFromKeyring: auth.DeleteAPIKeyFromKeyring() did not find/delete key in keyring.") 44 | } 45 | 46 | // Ensuring APIKeyIsInKeyring() cannot find a key that should not exist. 47 | if APIKeyIsInKeyring() { 48 | t.Error("@TestAPIKeyIsInKeyring: auth.APIKeyIsInKeyring() found key in keyring after it should have been deleted.") 49 | } 50 | } 51 | 52 | // Tests for auth.RetrieveAPIKeyFromKeyring() 53 | func TestRetrieveAPIKeyFromKeyring(t *testing.T) { 54 | // Ensuring StoreAPIKeyInKeyring() can successfully store a key in the keyring. 55 | err := StoreAPIKeyInKeyring(testKey) 56 | if err != nil { 57 | t.Errorf("@TestRetrieveAPIKeyFromKeyring: auth.StoreAPIKeyInKeyring() failed to store API key: %s", err) 58 | } 59 | 60 | // Ensuring RetrieveAPIKeyFromKeyring() can successfully find the right key in keyring. 61 | key := RetrieveAPIKeyFromKeyring() 62 | if key != testKey { 63 | t.Error("@TestRetrieveAPIKeyFromKeyring: auth.RetrieveAPIKeyFromKeyring() did not match provided test key.") 64 | } 65 | 66 | // Ensuring DeleteAPIKeyFromKeyring() is able to successfully find and delete key 67 | if !DeleteAPIKeyFromKeyring() { 68 | t.Error("@TestRetrieveAPIKeyFromKeyring: auth.DeleteAPIKeyFromKeyring() did not find/delete key in keyring.") 69 | } 70 | } 71 | 72 | // Tests for auth.DeleteAPIKeyFromKeyring() 73 | func TestDeleteAPIKeyFromKeyring(t *testing.T) { 74 | // Ensuring StoreAPIKeyInKeyring() can successfully store a key in the keyring. 75 | err := StoreAPIKeyInKeyring(testKey) 76 | if err != nil { 77 | t.Errorf("@TestDeleteAPIKeyFromKeyring: auth.StoreAPIKeyInKeyring() failed to store API key: %s", err) 78 | } 79 | 80 | // Ensuring DeleteAPIKeyFromKeyring() is able to successfully find and delete key 81 | if !DeleteAPIKeyFromKeyring() { 82 | t.Error("@TestDeleteAPIKeyFromKeyring: auth.DeleteAPIKeyFromKeyring() did not find/delete key in keyring.") 83 | } 84 | 85 | // Ensuring DeleteAPIKeyFromKeyring() does not find or delete a key as expected when the key does not exist. 86 | if DeleteAPIKeyFromKeyring() { 87 | t.Error("@TestDeleteAPIKeyFromKeyring: auth.DeleteAPIKeyFromKeyring() found/deleted key from keyring when one should not exist, it should have been deleted in the previous assertion.") 88 | } 89 | } 90 | 91 | // Tests for auth.TestValidateAPIKey() 92 | func TestValidateAPIKey(t *testing.T) { 93 | mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 94 | // Ensure URL is formatted with the correct query string. 95 | if !strings.Contains(r.URL.RequestURI(), "/api.php?enable&auth=") { 96 | fmt.Println(r.URL.RequestURI()) 97 | t.Error("@TestValidateAPIKey: auth.ValidateAPIKey() did not request the expected Pi Hole auth endpoint.") 98 | } 99 | if r.URL.Query().Get("auth") != testKey { 100 | w.Write([]byte(`{}`)) 101 | return 102 | } 103 | w.Write([]byte(`{"status": "enabled"}`)) 104 | })) 105 | 106 | defer mockServer.Close() 107 | url := mockServer.URL + "/api.php" 108 | // Requests should succeed with the correct API key 109 | if !ValidateAPIKey(url, testKey) { 110 | t.Error("@TestValidateAPIKey: auth.ValidateAPIKey() should have received a successful response from the server, but it did not.") 111 | } 112 | 113 | // Request should return an empty response with the wrong API key 114 | if ValidateAPIKey(url, "test") { 115 | t.Error("@TestValidateAPIKey: auth.ValidateAPIKey() should have received an empty response from the server as it is looking for the wrong API key.") 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /pkg/cli/cli.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "fmt" 5 | "github.com/Reeceeboii/Pi-CLI/pkg/database" 6 | "github.com/Reeceeboii/Pi-CLI/pkg/ui" 7 | "github.com/urfave/cli/v2" 8 | "time" 9 | ) 10 | 11 | /* 12 | This is the main CLI app, it contains all the various commands and subcommands 13 | that Pi-CLI is capable of responding to, and manages all of their corresponding flags 14 | */ 15 | var App = cli.App{ 16 | Name: "Pi-CLI", 17 | Usage: "Third party program to retrieve and display Pi-Hole data right from your terminal", 18 | Description: "Pi-Hole data right from your terminal. Live updating view, query history extraction and more!", 19 | Copyright: fmt.Sprintf("Copyright (c) %d Reece Mercer", time.Now().Year()), 20 | Authors: []*cli.Author{ 21 | { 22 | Name: "Reece Mercer", 23 | Email: "reecemercer981@gmail.com", 24 | }, 25 | }, 26 | Commands: []*cli.Command{ 27 | { 28 | Name: "setup", 29 | Aliases: []string{"s"}, 30 | Usage: "Configure Pi-CLI", 31 | Action: SetupCommand, 32 | }, 33 | { 34 | Name: "config", 35 | Aliases: []string{"c"}, 36 | Usage: "Interact with stored configuration settings", 37 | Subcommands: []*cli.Command{ 38 | { 39 | Name: "delete", 40 | Aliases: []string{"d"}, 41 | Usage: "Delete stored config data (config file and API key)", 42 | Action: ConfigDeleteCommand, 43 | }, 44 | { 45 | Name: "view", 46 | Aliases: []string{"v"}, 47 | Usage: "View config stored config data (config file and API key)", 48 | Action: ConfigViewCommand, 49 | }, 50 | }, 51 | }, 52 | { 53 | Name: "run", 54 | Aliases: []string{"r"}, 55 | Usage: "Run a one off command without booting the live view", 56 | Subcommands: []*cli.Command{ 57 | { 58 | Name: "summary", 59 | Aliases: []string{"s"}, 60 | Usage: "Extract a basic summary of data from the Pi-Hole", 61 | Action: RunSummaryCommand, 62 | }, 63 | { 64 | Name: "top-forwarded", 65 | Aliases: []string{"tf"}, 66 | Usage: "Extract the current top 10 forwarded DNS queries", 67 | Action: RunTopTenForwardedCommand, 68 | }, 69 | { 70 | Name: "top-blocked", 71 | Aliases: []string{"tb"}, 72 | Usage: "Extract the current top 10 blocked DNS queries", 73 | Action: RunTopTenBlockedCommand, 74 | }, 75 | { 76 | Name: "latest-queries", 77 | Aliases: []string{"lq"}, 78 | Usage: "Extract the latest queries", 79 | Flags: []cli.Flag{ 80 | &cli.Int64Flag{ 81 | Name: "limit", 82 | Aliases: []string{"l"}, 83 | Usage: "The limit on the number of queries to extract", 84 | DefaultText: "10", 85 | }, 86 | }, 87 | Action: RunLatestQueriesCommand, 88 | }, 89 | { 90 | Name: "enable", 91 | Aliases: []string{"e"}, 92 | Usage: "Enable the Pi-Hole", 93 | Action: RunEnablePiHoleCommand, 94 | }, 95 | { 96 | Name: "disable", 97 | Aliases: []string{"d"}, 98 | Usage: "Disable the Pi-Hole", 99 | Flags: []cli.Flag{ 100 | &cli.Int64Flag{ 101 | Name: "timeout", 102 | Aliases: []string{"t"}, 103 | Usage: "A timeout in seconds. Pi-Hole will re-enable after this time has elapsed.", 104 | DefaultText: "permanent", 105 | }, 106 | }, 107 | Action: RunDisablePiHoleCommand, 108 | }, 109 | }, 110 | }, 111 | { 112 | Name: "database", 113 | Aliases: []string{"d"}, 114 | Usage: "Analytics options to run on a Pi-Hole's FTL database", 115 | Subcommands: []*cli.Command{ 116 | { 117 | Name: "client-summary", 118 | Aliases: []string{"cs"}, 119 | Usage: "Summary of all Pi-Hole clients", 120 | Flags: []cli.Flag{ 121 | &cli.StringFlag{ 122 | Name: "path", 123 | Aliases: []string{"p"}, 124 | Usage: "Path to a Pi-Hole FTL database file", 125 | DefaultText: database.DefaultDatabaseFileLocation, 126 | }, 127 | }, 128 | Action: RunDatabaseClientSummaryCommand, 129 | }, 130 | { 131 | Name: "top-queries", 132 | Aliases: []string{"tq"}, 133 | Usage: "Returns the top (all time) queries", 134 | Flags: []cli.Flag{ 135 | &cli.StringFlag{ 136 | Name: "path", 137 | Aliases: []string{"p"}, 138 | Usage: "Path to the Pi-Hole FTL database file", 139 | DefaultText: database.DefaultDatabaseFileLocation, 140 | }, 141 | &cli.Int64Flag{ 142 | Name: "limit", 143 | Aliases: []string{"l"}, 144 | Usage: "The limit on the number of queries to extract", 145 | DefaultText: "10", 146 | }, 147 | &cli.StringFlag{ 148 | Name: "filter", 149 | Aliases: []string{"f"}, 150 | Usage: "Filter by domain or word. (e.g. 'google.com', 'spotify', 'facebook' etc...)", 151 | DefaultText: "No filter", 152 | }, 153 | }, 154 | Action: RunDatabaseTopQueriesCommand, 155 | }, 156 | }, 157 | }, 158 | }, 159 | 160 | Action: func(context *cli.Context) error { 161 | InitialisePICLI() 162 | ui.StartUI() 163 | return nil 164 | }, 165 | } 166 | -------------------------------------------------------------------------------- /pkg/cli/configCmd.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "fmt" 5 | "github.com/Reeceeboii/Pi-CLI/pkg/auth" 6 | "github.com/Reeceeboii/Pi-CLI/pkg/data" 7 | "github.com/fatih/color" 8 | "github.com/urfave/cli/v2" 9 | "os/exec" 10 | ) 11 | 12 | /* 13 | Searches for and deletes: 14 | - the API key from the system keyring (if exists) 15 | - the config file from the user's home directory (if exists) 16 | */ 17 | func ConfigDeleteCommand(*cli.Context) error { 18 | if auth.DeleteAPIKeyFromKeyring() { 19 | color.Green("System keyring API entry has been deleted!") 20 | } else { 21 | color.Yellow("Pi-CLI did not find a keyring entry to delete") 22 | } 23 | if data.DeleteConfigFile() { 24 | color.Green("Stored config file has been deleted!") 25 | } else { 26 | color.Yellow("Pi-CLI did not find a config file to delete") 27 | } 28 | return nil 29 | } 30 | 31 | /* 32 | Displays any saved configuration data to the user. 33 | If a config file is present, that can be loaded and displayed, 34 | otherwise, the user can be prompted to create one. 35 | */ 36 | func ConfigViewCommand(*cli.Context) error { 37 | /* 38 | - Pi-Hole IP address 39 | - Pi-Hole port 40 | - Data refresh rate 41 | */ 42 | if data.ConfigFileExists() { 43 | // Display the location of the config file in the filesystem 44 | color.Green("Config location: %s\n", data.GetConfigFileLocation()) 45 | 46 | // Open the config file so we can extract data from it 47 | data.PICLISettings.LoadFromFile() 48 | fmt.Printf("Pi-Hole address: %s\n", data.PICLISettings.PiHoleAddress) 49 | fmt.Printf("Pi-Hole port: %d\n", data.PICLISettings.PiHolePort) 50 | fmt.Printf("Refresh rate: %ds\n", data.PICLISettings.RefreshS) 51 | } else { 52 | color.Yellow("No config file is present - run the setup command to create one") 53 | } 54 | 55 | // and the same with the API key 56 | if auth.APIKeyIsInKeyring() { 57 | fmt.Printf("API key (keyring): %s\n", auth.RetrieveAPIKeyFromKeyring()) 58 | } else if data.PICLISettings.APIKeyIsInFile() { 59 | fmt.Printf("API key (config file): %s\n", data.PICLISettings.APIKey) 60 | } else { 61 | color.Yellow("No API key has been provided - run the setup command to enter it") 62 | } 63 | 64 | _ = exec.Command(data.GetConfigFileLocation()).Run() 65 | 66 | return nil 67 | } 68 | -------------------------------------------------------------------------------- /pkg/cli/databaseCmd.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "github.com/Reeceeboii/Pi-CLI/pkg/database" 5 | "github.com/urfave/cli/v2" 6 | ) 7 | 8 | /* 9 | FOR ALL DATABASE COMMANDS: 10 | If no path is provided by the user, Pi-CLI will assume that the database file's 11 | name hasn't been changed from it's default name, and that is has been placed in the 12 | same working directory that it is being executed from. This saves some command typing. 13 | */ 14 | 15 | /* 16 | Extracts a summary of data regarding the Pi-Hole's clients 17 | */ 18 | func RunDatabaseClientSummaryCommand(c *cli.Context) error { 19 | path := c.String("path") 20 | if path == "" { 21 | path = database.DefaultDatabaseFileLocation 22 | } 23 | 24 | conn := database.Connect(path) 25 | database.ClientSummary(conn) 26 | 27 | return nil 28 | } 29 | 30 | /* 31 | Extracts all time top query data from the database file. 32 | */ 33 | func RunDatabaseTopQueriesCommand(c *cli.Context) error { 34 | path := c.String("path") 35 | if path == "" { 36 | path = database.DefaultDatabaseFileLocation 37 | } 38 | 39 | conn := database.Connect(path) 40 | database.TopQueries(conn, c.Int64("limit"), c.String("filter")) 41 | 42 | return nil 43 | } 44 | -------------------------------------------------------------------------------- /pkg/cli/enableDisableCmd.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "github.com/Reeceeboii/Pi-CLI/pkg/api" 5 | "github.com/Reeceeboii/Pi-CLI/pkg/data" 6 | "github.com/fatih/color" 7 | "github.com/urfave/cli/v2" 8 | ) 9 | 10 | /* 11 | Enable the Pi-Hole if it is not already enabled, 12 | */ 13 | func RunEnablePiHoleCommand(*cli.Context) error { 14 | InitialisePICLI() 15 | api.LiveSummary.Update(data.LivePiCLIData.FormattedAPIAddress, data.LivePiCLIData.APIKey, nil) 16 | 17 | if api.LiveSummary.Status == "enabled" { 18 | color.Yellow("Pi-Hole is already enabled!") 19 | } else { 20 | api.EnablePiHole() 21 | color.Green("Pi-Hole enabled") 22 | } 23 | 24 | return nil 25 | } 26 | 27 | /* 28 | Disable the Pi-Hole. This command also takes an optional timeout parameter in seconds. 29 | If given and within constraints, the Pi-Hole will automatically re-enable after this 30 | time period has elapsed 31 | */ 32 | func RunDisablePiHoleCommand(c *cli.Context) error { 33 | InitialisePICLI() 34 | api.LiveSummary.Update(data.LivePiCLIData.FormattedAPIAddress, data.LivePiCLIData.APIKey, nil) 35 | 36 | if api.LiveSummary.Status == "disabled" { 37 | color.Yellow("Pi-Hole is already disabled!") 38 | } else { 39 | timeout := c.Int64("timeout") 40 | if timeout == 0 { 41 | api.DisablePiHole(false, 0) 42 | color.Green("Pi-Hole disabled until explicitly re-enabled") 43 | } else { 44 | api.DisablePiHole(true, timeout) 45 | color.Green("Pi-Hole disabled. Will re-enable in %d seconds\n", timeout) 46 | } 47 | } 48 | 49 | return nil 50 | } 51 | -------------------------------------------------------------------------------- /pkg/cli/init.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "github.com/Reeceeboii/Pi-CLI/pkg/auth" 5 | "github.com/Reeceeboii/Pi-CLI/pkg/data" 6 | "github.com/Reeceeboii/Pi-CLI/pkg/network" 7 | "github.com/fatih/color" 8 | "os" 9 | ) 10 | 11 | /* 12 | Validate that the config file and API key are in place. 13 | Load the required settings into memory 14 | */ 15 | func InitialisePICLI() { 16 | // firstly, has a config file been created? 17 | if !data.ConfigFileExists() { 18 | color.Red("Please configure Pi-CLI via the 'setup' command") 19 | os.Exit(1) 20 | } 21 | 22 | data.PICLISettings.LoadFromFile() 23 | 24 | // retrieve the API key depending upon its storage location 25 | if !data.PICLISettings.APIKeyIsInFile() && !auth.APIKeyIsInKeyring() { 26 | color.Red("Please configure Pi-CLI via the 'setup' command") 27 | os.Exit(1) 28 | } else { 29 | if data.PICLISettings.APIKeyIsInFile() { 30 | data.LivePiCLIData.APIKey = data.PICLISettings.APIKey 31 | } else { 32 | data.LivePiCLIData.APIKey = auth.RetrieveAPIKeyFromKeyring() 33 | } 34 | } 35 | 36 | data.LivePiCLIData.Settings = data.PICLISettings 37 | data.LivePiCLIData.FormattedAPIAddress = network.GenerateAPIAddress( 38 | data.PICLISettings.PiHoleAddress, 39 | data.PICLISettings.PiHolePort) 40 | } 41 | -------------------------------------------------------------------------------- /pkg/cli/runCmd.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "time" 7 | 8 | "github.com/Reeceeboii/Pi-CLI/pkg/api" 9 | "github.com/Reeceeboii/Pi-CLI/pkg/data" 10 | "github.com/fatih/color" 11 | "github.com/urfave/cli/v2" 12 | ) 13 | 14 | /* 15 | This file stores commands that can be ran 'one-off', i.e. without needing to boot the live UI 16 | */ 17 | 18 | /* 19 | Extracts a quick summary of the previous 24/hr of data from the Pi-Hole. 20 | */ 21 | func RunSummaryCommand(*cli.Context) error { 22 | InitialisePICLI() 23 | api.LiveSummary.Update(data.LivePiCLIData.FormattedAPIAddress, data.LivePiCLIData.APIKey, nil) 24 | fmt.Printf("Summary @ %s\n", time.Now().Format(time.Stamp)) 25 | fmt.Println() 26 | 27 | if api.LiveSummary.Status == "enabled" { 28 | fmt.Printf("Pi-Hole status: %s\n", color.GreenString(strings.Title(api.LiveSummary.Status))) 29 | } else { 30 | fmt.Printf("Pi-Hole status: %s\n", color.RedString(strings.Title(api.LiveSummary.Status))) 31 | } 32 | 33 | fmt.Println() 34 | fmt.Printf("Queries /24hr: %s\n", api.LiveSummary.QueriesToday) 35 | fmt.Printf("Blocked /24hr: %s\n", api.LiveSummary.BlockedToday) 36 | fmt.Printf("Percent blocked: %s%%\n", api.LiveSummary.PercentBlockedToday) 37 | fmt.Printf("Domains on blocklist: %s\n", api.LiveSummary.DomainsOnBlocklist) 38 | fmt.Printf("Privacy level: %s - %s\n", 39 | api.LiveSummary.PrivacyLevel, 40 | api.LiveSummary.PrivacyLevelNumberMapping[api.LiveSummary.PrivacyLevel], 41 | ) 42 | fmt.Printf("Total clients seen: %s\n", api.LiveSummary.TotalClientsSeen) 43 | fmt.Println() 44 | return nil 45 | } 46 | 47 | /* 48 | Extract the current top 10 permitted domains that have been forwarded to the upstream DNS resolver 49 | */ 50 | func RunTopTenForwardedCommand(*cli.Context) error { 51 | InitialisePICLI() 52 | 53 | api.LiveTopItems.Update(nil) 54 | fmt.Printf("Top queries as of @ %s\n\n", time.Now().Format(time.Stamp)) 55 | for _, q := range api.LiveTopItems.PrettyTopQueries { 56 | fmt.Println(q) 57 | } 58 | 59 | return nil 60 | } 61 | 62 | /* 63 | Extract the current top 10 blocked domains that the FTL has filtered out and not forwarded 64 | to the upstream DNS resolver 65 | */ 66 | func RunTopTenBlockedCommand(*cli.Context) error { 67 | InitialisePICLI() 68 | 69 | api.LiveTopItems.Update(nil) 70 | fmt.Printf("Top blocked domains as of @ %s\n\n", time.Now().Format(time.Stamp)) 71 | for _, q := range api.LiveTopItems.PrettyTopAds { 72 | fmt.Println(q) 73 | } 74 | 75 | return nil 76 | } 77 | 78 | func RunLatestQueriesCommand(c *cli.Context) error { 79 | queryAmount := c.Int("limit") 80 | if queryAmount == 0 { 81 | queryAmount = 10 82 | } 83 | 84 | if queryAmount < 1 { 85 | fmt.Println("Please enter a number of queries >= 1") 86 | return nil 87 | } 88 | 89 | InitialisePICLI() 90 | 91 | api.LiveAllQueries.AmountOfQueriesInLog = queryAmount 92 | api.LiveAllQueries.Queries = make([]api.Query, api.LiveAllQueries.AmountOfQueriesInLog) 93 | api.LiveAllQueries.Update(nil) 94 | 95 | for _, query := range api.LiveAllQueries.Table { 96 | fmt.Println(query) 97 | } 98 | 99 | return nil 100 | } 101 | -------------------------------------------------------------------------------- /pkg/cli/setupCmd.go: -------------------------------------------------------------------------------- 1 | package cli 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "github.com/Reeceeboii/Pi-CLI/pkg/auth" 7 | "github.com/Reeceeboii/Pi-CLI/pkg/data" 8 | "github.com/Reeceeboii/Pi-CLI/pkg/network" 9 | "github.com/fatih/color" 10 | "github.com/urfave/cli/v2" 11 | "log" 12 | "net" 13 | "net/http" 14 | "os" 15 | "strconv" 16 | "strings" 17 | ) 18 | 19 | /* 20 | Reads in data from the user and uses it to construct a config file that Pi-CLI can use 21 | in the future. 22 | 23 | The setup commands takes: 24 | - The IP address of the target Pi-Hole instance 25 | - The port exposing the Pi-Hole's web interface 26 | - A data refresh rate in seconds 27 | - User's Pi-Hole API key (used for authentication) 28 | 29 | It will then ask them if they wish to store the API key in their system keyring or the config 30 | file itself. 31 | */ 32 | func SetupCommand(c *cli.Context) error { 33 | reader := bufio.NewReader(os.Stdin) 34 | 35 | for { 36 | // read in the IP address and check that it is valid 37 | fmt.Print(" > Please enter the IP address of your Pi-Hole: ") 38 | piHoleAddress, _ := reader.ReadString('\n') 39 | ip := net.ParseIP(strings.TrimSpace(piHoleAddress)) 40 | if ip == nil { 41 | color.Yellow("Please enter a valid IP address") 42 | continue 43 | } 44 | data.PICLISettings.PiHoleAddress = ip.String() 45 | break 46 | } 47 | 48 | for { 49 | // read in the port 50 | fmt.Print(" > Please enter the port that exposes the web interface (default 80): ") 51 | piHolePort, _ := reader.ReadString('\n') 52 | trimmed := strings.TrimSpace(piHolePort) 53 | // if the user entered something, validate and apply. Else, revert to the default 54 | if len(trimmed) > 0 { 55 | intPiHolePort, err := strconv.Atoi(trimmed) 56 | if err != nil { 57 | color.Yellow("Please enter a number") 58 | continue 59 | } 60 | if intPiHolePort < 1 || intPiHolePort > 65535 { 61 | color.Yellow("Port must be between 1 and 65535") 62 | continue 63 | } 64 | testAddressWithPort := network.GenerateAPIAddress(data.PICLISettings.PiHoleAddress, intPiHolePort) 65 | if network.IsAlive(testAddressWithPort) { 66 | data.PICLISettings.PiHolePort = intPiHolePort 67 | } else { 68 | continue 69 | } 70 | } else { 71 | testAddressWithPort := network.GenerateAPIAddress(data.PICLISettings.PiHoleAddress, data.DefaultPort) 72 | if network.IsAlive(testAddressWithPort) { 73 | data.PICLISettings.PiHolePort = data.DefaultPort 74 | } else { 75 | continue 76 | } 77 | } 78 | 79 | // send a request to the PiHole to validate that the IP and port actually point to it 80 | tempURL := fmt.Sprintf( 81 | "http://%s:%d/admin/api.php", 82 | data.PICLISettings.PiHoleAddress, 83 | data.PICLISettings.PiHolePort) 84 | req, err := http.NewRequest("GET", tempURL, nil) 85 | if err != nil { 86 | log.Fatal(err) 87 | } 88 | res, err := network.HttpClient.Do(req) 89 | 90 | // if the details are valid and the request didn't time out... 91 | // lazy evaluation saves us from deref errors here and saves a check 92 | if err == nil && network.ValidatePiHoleDetails(res) { 93 | break 94 | } else { 95 | color.Yellow("Pi-Hole doesn't seem to be alive, check your details and try again!") 96 | fmt.Println() 97 | } 98 | } 99 | 100 | color.Green( 101 | "Pi-Hole reachable at %s:%d!\n", 102 | data.PICLISettings.PiHoleAddress, 103 | data.PICLISettings.PiHolePort) 104 | 105 | // read in the data refresh rate 106 | for { 107 | fmt.Print(" > Please enter your preferred data refresh rate in seconds (default 1s): ") 108 | refreshS, _ := reader.ReadString('\n') 109 | trimmed := strings.TrimSpace(refreshS) 110 | if len(trimmed) > 0 { 111 | intRefreshS, err := strconv.Atoi(trimmed) 112 | if err != nil { 113 | color.Yellow("Please enter a number") 114 | continue 115 | } 116 | if intRefreshS < 1 { 117 | color.Yellow("Refresh time cannot be less than 1 second") 118 | continue 119 | } 120 | data.PICLISettings.RefreshS = intRefreshS 121 | break 122 | } else { 123 | break 124 | } 125 | } 126 | 127 | // read in the API key and work out where the user wants to store it (keyring or config file) 128 | for { 129 | fmt.Print(" > Please enter your Pi-Hole API key: ") 130 | apiKey, _ := reader.ReadString('\n') 131 | apiKey = strings.TrimSpace(apiKey) 132 | if len(apiKey) < 1 { 133 | color.Yellow("Please provide your API key for authentication") 134 | continue 135 | } 136 | 137 | data.PICLISettings.APIKey = apiKey 138 | 139 | // before we store the API token (keyring or config file), we should check that it's valid 140 | // the address + port have been validated by this point so we're safe to shoot requests at it 141 | data.LivePiCLIData.Settings = data.PICLISettings 142 | data.LivePiCLIData.FormattedAPIAddress = network.GenerateAPIAddress( 143 | data.PICLISettings.PiHoleAddress, 144 | data.PICLISettings.PiHolePort) 145 | 146 | if !auth.ValidateAPIKey(data.LivePiCLIData.FormattedAPIAddress, data.PICLISettings.APIKey) { 147 | color.Yellow("That API token doesn't seem to be correct, check it and try again!") 148 | } else { 149 | break 150 | } 151 | } 152 | 153 | color.Green("Authenticated with API key!\n") 154 | 155 | fmt.Print(" > Do you wish to store the API key in your system keyring? (y/n - default y): ") 156 | storageChoice, _ := reader.ReadString('\n') 157 | storageChoice = strings.ToLower(strings.TrimSpace(storageChoice)) 158 | 159 | // if they wish to use their system's keyring... 160 | if storageChoice == "y" || len(storageChoice) == 0 { 161 | err := auth.StoreAPIKeyInKeyring(data.PICLISettings.APIKey) 162 | 163 | if err == nil { 164 | color.Green("Your API token has been securely stored in your system keyring") 165 | /* 166 | After the API key has been saved to the keyring, there is no longer a need to save it 167 | to the config file, so the stored copy of it can be removed from the in-memory settings 168 | instance before it gets serialised to disk 169 | */ 170 | data.PICLISettings.APIKey = "" 171 | } else { 172 | color.Yellow("System keyring call failed, falling back to config file") 173 | } 174 | } 175 | 176 | // write config file to disk 177 | // all fields in the settings struct would have been set by this point 178 | if err := data.PICLISettings.SaveToFile(); err != nil { 179 | color.Red("Failed to save settings") 180 | log.Fatal(err.Error()) 181 | } 182 | 183 | color.Green("\nConfiguration successfully saved to %s", data.GetConfigFileLocation()) 184 | return nil 185 | } 186 | -------------------------------------------------------------------------------- /pkg/data/data.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import ( 4 | "time" 5 | ) 6 | 7 | // live updating config data used at runtime 8 | var LivePiCLIData = NewPiCLIData() 9 | 10 | // Stores the data needed by Pi-CLI during runtime 11 | type PiCLIData struct { 12 | // An instance of settings.Settings 13 | Settings *Settings 14 | // Remote address of the Pi-Hole 15 | FormattedAPIAddress string 16 | // The API key used to authenticate with the Pi-Hole 17 | APIKey string 18 | // The time that the last data poll was sent out to the Pi-Hole 19 | LastUpdated time.Time 20 | // If the keybinds screen is being shown or not 21 | ShowKeybindsScreen bool 22 | // String used to display the keybindings 23 | Keybinds []string 24 | } 25 | 26 | func NewPiCLIData() *PiCLIData { 27 | return &PiCLIData{ 28 | Keybinds: []string{ 29 | "", 30 | "---------- Query Log ----------", 31 | "", 32 | " [E/D] Increase/decrease number of queries in query log by 1", 33 | " [R/F] Increase/decrease number of queries in query log by 10 ", 34 | "[UP/DOWN ARROW] Scroll up/down query log by 1", 35 | " [PAGE UP/DOWN] Scroll up/down query log by 10", 36 | "", 37 | "---------- Misc. ----------", 38 | "", 39 | "[P] Enable/Disable Pi-Hole", 40 | "[Q] Quit Pi-CLI", 41 | }, 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /pkg/data/settings.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import ( 4 | "encoding/json" 5 | "io/ioutil" 6 | "log" 7 | "os" 8 | "os/user" 9 | "path" 10 | "runtime" 11 | "strings" 12 | ) 13 | 14 | // Store PiCLI settings 15 | var PICLISettings = NewSettings() 16 | 17 | // Constant values required by Pi-CLI 18 | const ( 19 | // Port that the Pi-Hole API is defaulted to 20 | DefaultPort = 80 21 | // The default refresh rate of the data in seconds 22 | DefaultRefreshS = 1 23 | // The name of the configuration file 24 | ConfigFileName = "picli-config.json" 25 | ) 26 | 27 | // Settings contains the current configuration options being used by Pi-CLI 28 | type Settings struct { 29 | // The Pi-Hole's address 30 | PiHoleAddress string `json:"pi_hole_address"` 31 | // The port the Pi-Hole exposes that can be used for HTTP/S traffic 32 | PiHolePort int `json:"pi_hole_port"` 33 | // The number of seconds to wait between each data refresh 34 | RefreshS int `json:"refresh_s"` 35 | // API key used to authenticate with the Pi-Hole instance 36 | APIKey string `json:"api_key"` 37 | } 38 | 39 | // Generate the location of the config file (or at least where it should be) 40 | var configFileLocation = GetConfigFileLocation() 41 | 42 | // Checks for the existence of a config file 43 | func ConfigFileExists() bool { 44 | _, err := os.Stat(configFileLocation) 45 | return !os.IsNotExist(err) 46 | } 47 | 48 | // Return a new Settings instance 49 | func NewSettings() *Settings { 50 | return &Settings{ 51 | PiHoleAddress: "", 52 | PiHolePort: DefaultPort, 53 | RefreshS: DefaultRefreshS, 54 | APIKey: "", 55 | } 56 | } 57 | 58 | // Attempts to create a settings instance from a config file 59 | func (settings *Settings) LoadFromFile() { 60 | if byteArr, err := ioutil.ReadFile(configFileLocation); err != nil { 61 | log.Fatal(err) 62 | } else { 63 | if err := json.Unmarshal(byteArr, settings); err != nil { 64 | log.Fatal(err) 65 | } 66 | } 67 | } 68 | 69 | // Saves the current settings to a config file 70 | func (settings *Settings) SaveToFile() error { 71 | byteArr, err := json.MarshalIndent(settings, "", "\t") 72 | if err != nil { 73 | return err 74 | } 75 | if err = ioutil.WriteFile(configFileLocation, byteArr, 0644); err != nil { 76 | return err 77 | } 78 | return nil 79 | } 80 | 81 | // Is API key stored in the config file? If not, off to the system keyring you go! 82 | func (settings *Settings) APIKeyIsInFile() bool { 83 | return settings.APIKey != "" 84 | } 85 | 86 | // Delete the config file if it exists 87 | func DeleteConfigFile() bool { 88 | // first, check if the file actually exists 89 | if !ConfigFileExists() { 90 | return false 91 | } 92 | if err := os.Remove(configFileLocation); err != nil { 93 | return false 94 | } 95 | return true 96 | } 97 | 98 | // Return the path to the config file 99 | func GetConfigFileLocation() string { 100 | usr, err := user.Current() 101 | if err != nil { 102 | log.Fatal(err) 103 | } 104 | 105 | /* 106 | Return user's home directory plus the config file name. If on Windows, make sure path is returned 107 | with backslashes as the directory separators rather than forward slashes 108 | */ 109 | if runtime.GOOS == "windows" { 110 | return strings.ReplaceAll(path.Join(usr.HomeDir, ConfigFileName), "/", "\\") 111 | } 112 | return path.Join(usr.HomeDir, ConfigFileName) 113 | } 114 | -------------------------------------------------------------------------------- /pkg/database/clientSummary.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "github.com/fatih/color" 7 | "golang.org/x/text/language" 8 | "golang.org/x/text/message" 9 | "log" 10 | "strings" 11 | ) 12 | 13 | /* 14 | Extracts a summary of all of the clients that have been served by the Pi-Hole instance. 15 | 16 | NOTE: 17 | This will include duplicate DNS entries if the same client has been seen multiple times 18 | (i.e. a phone being seen as a LAN client both locally and via a VPN - the client itself 19 | is the same but has separate query counts and different local addresses and as such will 20 | be listed as many times as it has appeared in different contexts). 21 | 22 | This database dump includes: 23 | - The client's address: (IP or mac addr) 24 | - The date that the client was first seen 25 | - The date that the last query from the client was received 26 | - The total number of queries received from the client 27 | - The client's DNS name 28 | */ 29 | func ClientSummary(db *sql.DB) { 30 | rows, err := db.Query(` 31 | SELECT DISTINCT n.hwaddr, n.firstSeen, n.lastQuery, n.numQueries, na.name 32 | FROM network n 33 | INNER JOIN network_addresses na on n.id = na.network_id 34 | WHERE n.numQueries != 0 35 | ORDER BY numQueries DESC 36 | `) 37 | 38 | if err != nil { 39 | log.Fatalf("Error in database client summary query: %s", err.Error()) 40 | } 41 | 42 | var address string 43 | var firstSeen int 44 | var lastQuery int 45 | var numQueries int 46 | var name string 47 | 48 | tabWriter := NewConfiguredTabWriter(1) 49 | localisedNumberWriter := message.NewPrinter(language.English) 50 | 51 | // insert column headers 52 | _, _ = fmt.Fprintln( 53 | tabWriter, 54 | "#\t", 55 | "Address\t", 56 | "First seen\t", 57 | "Last query\t", 58 | "No. queries\t", 59 | "DNS\t") 60 | 61 | // insert blank line separator 62 | _, _ = fmt.Fprintln(tabWriter, "\t", "\t", "\t", "\t", "\t", "\t") 63 | 64 | row := 1 65 | 66 | // print out each row from the query results 67 | for rows.Next() { 68 | _ = rows.Scan(&address, &firstSeen, &lastQuery, &numQueries, &name) 69 | 70 | // if the string is denoting an IP, we can chop off the IP identifier from the row entry 71 | if strings.Contains(address, "ip-") { 72 | address = strings.Split(address, "ip-")[1] 73 | } 74 | 75 | _, _ = fmt.Fprintln( 76 | tabWriter, 77 | fmt.Sprintf("%d\t", row), 78 | fmt.Sprintf("%s\t", address), 79 | fmt.Sprintf("%s\t", FormattedDBUnixTimestamp(firstSeen)), 80 | fmt.Sprintf("%s\t", FormattedDBUnixTimestamp(lastQuery)), 81 | fmt.Sprintf("%s\t", localisedNumberWriter.Sprintf("%d", numQueries)), 82 | fmt.Sprintf("%s\t", name)) 83 | row++ 84 | } 85 | 86 | // if the row counter has never been incremented, the database query returned zero results 87 | if row == 1 { 88 | color.Red("0 results in database") 89 | } 90 | 91 | if err := tabWriter.Flush(); err != nil { 92 | return 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /pkg/database/databaseUtilities.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "database/sql" 5 | "errors" 6 | "fmt" 7 | _ "github.com/mattn/go-sqlite3" 8 | "log" 9 | "os" 10 | "text/tabwriter" 11 | "time" 12 | ) 13 | 14 | // Database constants 15 | const ( 16 | // The name of the database driver to use 17 | DBDriverName = "sqlite3" 18 | // The default limit on the number of queries returned from some database queries 19 | DefaultQueryTableLimit = 10 20 | /* 21 | The default location and name used by database commands to look for the Pi-Hole's 22 | FTL database file 23 | */ 24 | DefaultDatabaseFileLocation = "./pihole-FTL.db" 25 | ) 26 | 27 | /* 28 | Attempts to connect to a database, returns an sql.DB handle 29 | if this connection succeeds 30 | */ 31 | func Connect(pathToPotentialDB string) *sql.DB { 32 | conn := &sql.DB{} 33 | if validateDatabase(pathToPotentialDB) { 34 | conn, _ = sql.Open(DBDriverName, pathToPotentialDB) 35 | } 36 | return conn 37 | } 38 | 39 | /* 40 | Returns a RFC822 formatted version of a given Unix time integer retrieved 41 | from a database row. For example, given the Unix time of 1612548060, the function 42 | will return the string "05 Feb 21 18:01 GMT" 43 | */ 44 | func FormattedDBUnixTimestamp(stamp int) string { 45 | return time.Unix(int64(stamp), 0).Format(time.RFC822) 46 | } 47 | 48 | /* 49 | Returns a newly configured tabwriter.Writer, with a parameterised padding, 50 | allowing optional changes to the padding between an element and the edge of 51 | its cell 52 | */ 53 | func NewConfiguredTabWriter(padding int) *tabwriter.Writer { 54 | return tabwriter.NewWriter( 55 | os.Stdout, 56 | 0, 57 | 0, 58 | padding, 59 | ' ', 60 | tabwriter.Debug) 61 | } 62 | 63 | // Checks if the filepath to a database is valid and that a connection can be opened 64 | func validateDatabase(pathToPotentialDB string) bool { 65 | if err := doesDatabaseFileExist(pathToPotentialDB); err != nil { 66 | log.Fatal(err.Error()) 67 | } 68 | 69 | if err := canOpenConnectionToDB(pathToPotentialDB); err != nil { 70 | log.Fatal(err.Error()) 71 | } 72 | 73 | return true 74 | } 75 | 76 | // Does the database file exist? 77 | func doesDatabaseFileExist(pathToPotentialDB string) error { 78 | if _, err := os.Stat(pathToPotentialDB); os.IsNotExist(err) { 79 | return errors.New(fmt.Sprintf("'%s' does not exist", pathToPotentialDB)) 80 | } 81 | return nil 82 | } 83 | 84 | // Can a connection be opened with the DB file? 85 | func canOpenConnectionToDB(pathToPotentialDB string) error { 86 | // attempt to open a connection to the database 87 | conn, err := sql.Open(DBDriverName, pathToPotentialDB) 88 | 89 | /* 90 | If the connection failed, return an error. If we get to this point, the file is valid 91 | and is present in the local filesystem. However, either the file is not an SQLite database, 92 | or it is somehow unreadable. 93 | */ 94 | if err != nil { 95 | return errors.New( 96 | fmt.Sprintf( 97 | "Failed to connect. Check that the path is correct & points to a valid file: %s", err.Error())) 98 | } 99 | 100 | if err := conn.Close(); err != nil { 101 | return errors.New(fmt.Sprintf("Failed to close connection: %s", err.Error())) 102 | } 103 | 104 | return nil 105 | } 106 | -------------------------------------------------------------------------------- /pkg/database/topQueries.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "github.com/fatih/color" 7 | "golang.org/x/text/language" 8 | "golang.org/x/text/message" 9 | "log" 10 | "math" 11 | ) 12 | 13 | /* 14 | Extracts the the top queries of all time. This will include both blocked and non 15 | blocked queries. The only factor in ordering/appearance is the number of times that 16 | a query for that domain has hit the Pi-Hole. 17 | 18 | An optional filter parameter can be provided that can filter down returned results to 19 | those belonging to a certain domain, or those that contain a certain word. 20 | 21 | This query is also parameterised on a limit, the user can choose how many top queries 22 | they want returned (i.e. top 10, top 20 etc...). 23 | 24 | This database dump includes: 25 | - The domain 26 | - The number of queries that have been sent for that domain 27 | - A total sum of all of the occurrences 28 | */ 29 | func TopQueries(db *sql.DB, limit int64, domainFilter string) { 30 | var rows *sql.Rows 31 | var err error 32 | 33 | /* 34 | If any <0 integer is given, default to the max int64 value to essentially remove the limit. 35 | If zero is provided, revert to the default of 10, else we can go with the user's provided limit 36 | */ 37 | if limit < 0 { 38 | limit = math.MaxInt64 39 | color.Yellow("Limit: unlimited") 40 | } else if limit == 0 { 41 | limit = DefaultQueryTableLimit 42 | color.Yellow("Limit: %d", DefaultQueryTableLimit) 43 | } else { 44 | color.Yellow("Limit: %d", limit) 45 | } 46 | 47 | color.Yellow("Filter: '%s' \n\n", domainFilter) 48 | 49 | // if filter has been provided, we want to plug it into the SQL query 50 | if domainFilter == "" { 51 | rows, err = db.Query(` 52 | SELECT domain, COUNT(domain) 53 | FROM queries 54 | GROUP BY domain 55 | ORDER BY COUNT(domain) DESC 56 | LIMIT ? 57 | `, limit) 58 | } else { 59 | sqlFilter := "%" + domainFilter + "%" 60 | 61 | rows, err = db.Query(` 62 | SELECT domain, COUNT(domain) 63 | FROM queries 64 | WHERE queries.domain LIKE ? 65 | GROUP BY domain 66 | ORDER BY COUNT(domain) DESC 67 | LIMIT ? 68 | `, sqlFilter, limit) 69 | } 70 | 71 | if err != nil { 72 | log.Fatalf("Error in database top queries query: %s", err.Error()) 73 | } 74 | 75 | var domain string 76 | var occurrence int 77 | 78 | var occurrenceSum uint64 79 | 80 | tabWriter := NewConfiguredTabWriter(1) 81 | localisedNumberWriter := message.NewPrinter(language.English) 82 | 83 | // insert column headers 84 | _, _ = fmt.Fprintln(tabWriter, "#\t", "Domain\t", "Occurrences\t") 85 | // insert blank line separator 86 | _, _ = fmt.Fprintln(tabWriter, "\t", "\t", "\t") 87 | 88 | // used to count the rows as they're outputted 89 | var row int64 = 1 90 | 91 | for rows.Next() { 92 | _ = rows.Scan(&domain, &occurrence) 93 | 94 | occurrenceSum = occurrenceSum + uint64(occurrence) 95 | 96 | _, _ = fmt.Fprintln( 97 | tabWriter, 98 | fmt.Sprintf("%d\t", row), 99 | fmt.Sprintf("%s\t", domain), 100 | localisedNumberWriter.Sprintf("%d\t", occurrence), 101 | ) 102 | row++ 103 | } 104 | 105 | // insert blank line separator 106 | _, _ = fmt.Fprintln(tabWriter, "\t", "\t", "\t") 107 | // insert column headers 108 | _, _ = fmt.Fprintln(tabWriter, "\t", "\t", "Total\t") 109 | 110 | // insert the total of the occurrences 111 | _, _ = fmt.Fprintln( 112 | tabWriter, 113 | "\t", 114 | "\t", 115 | fmt.Sprintf("%s\t", localisedNumberWriter.Sprintf("%d", occurrenceSum))) 116 | 117 | // if the row counter has never been incremented, the database query returned zero results 118 | if row == 1 { 119 | color.Red("0 results in database") 120 | } 121 | 122 | if err := tabWriter.Flush(); err != nil { 123 | return 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /pkg/network/httpClient.go: -------------------------------------------------------------------------------- 1 | package network 2 | 3 | import ( 4 | "net/http" 5 | "time" 6 | ) 7 | 8 | // Construct a http.Client with a 3 second timeout for use in API requests 9 | var HttpClient = NewHTTPClient(time.Second * 3) 10 | 11 | // Create a new http.Client with a given timeout duration 12 | func NewHTTPClient(timeout time.Duration) *http.Client { 13 | return &http.Client{ 14 | Timeout: timeout, 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /pkg/network/utilities.go: -------------------------------------------------------------------------------- 1 | package network 2 | 3 | import ( 4 | "fmt" 5 | "github.com/fatih/color" 6 | "log" 7 | "net/http" 8 | ) 9 | 10 | // Plug the Pi-Hole address and port together to get a full URL 11 | func GenerateAPIAddress(address string, port int) string { 12 | return fmt.Sprintf("http://%s:%d/admin/api.php", address, port) 13 | } 14 | 15 | // IsAlive will, given an IP and port, return true or false denoting if the address is alive 16 | func IsAlive(address string) bool { 17 | color.Yellow("Validating " + address) 18 | req, err := http.NewRequest("GET", address, nil) 19 | 20 | if err != nil { 21 | color.Red("Failed to generate HTTP GET in IsAlive()") 22 | log.Fatal(err) 23 | } 24 | 25 | _, err = HttpClient.Do(req) 26 | if err != nil { 27 | color.Red("Address not reachable!") 28 | return false 29 | } 30 | return true 31 | } 32 | 33 | /* 34 | Do the provided address & port actually point to a live Pi-Hole? 35 | Issue #16 @https://github.com/Reeceeboii/Pi-CLI/issues/16 36 | */ 37 | func ValidatePiHoleDetails(res *http.Response) bool { 38 | return res.StatusCode == http.StatusOK 39 | } 40 | -------------------------------------------------------------------------------- /pkg/ui/ui.go: -------------------------------------------------------------------------------- 1 | package ui 2 | 3 | import ( 4 | "fmt" 5 | "github.com/Reeceeboii/Pi-CLI/pkg/api" 6 | "github.com/Reeceeboii/Pi-CLI/pkg/data" 7 | ui "github.com/gizak/termui/v3" 8 | "github.com/gizak/termui/v3/widgets" 9 | "log" 10 | "strings" 11 | "sync" 12 | "time" 13 | ) 14 | 15 | var wg sync.WaitGroup 16 | 17 | /* 18 | Update data so it can be displayed. 19 | This function makes calls to the Pi-Hole's API 20 | */ 21 | func updateData() { 22 | go api.LiveSummary.Update(data.LivePiCLIData.FormattedAPIAddress, data.LivePiCLIData.APIKey, &wg) 23 | go api.LiveTopItems.Update(&wg) 24 | go api.LiveAllQueries.Update(&wg) 25 | wg.Wait() 26 | data.LivePiCLIData.LastUpdated = time.Now() 27 | } 28 | 29 | /* 30 | Given a value representing the current privacy level, return the level name. 31 | https://docs.pi-hole.net/ftldns/privacylevels/ 32 | */ 33 | func getPrivacyLevel(level *string) string { 34 | return api.LiveSummary.PrivacyLevelNumberMapping[*level] 35 | } 36 | 37 | /* 38 | Is the UI free to draw to? Currently this only takes into account the fact 39 | that the keybinds view may be showing. Adding more conditions for halting live 40 | UI redraws is as simple as ANDing them here 41 | */ 42 | func uiCanDraw() bool { 43 | return !data.LivePiCLIData.ShowKeybindsScreen 44 | } 45 | 46 | // Create the UI and start rendering 47 | func StartUI() { 48 | if err := ui.Init(); err != nil { 49 | log.Fatalf("failed to initialize termui: %v", err) 50 | } 51 | defer ui.Close() 52 | 53 | piHoleInfo := widgets.NewList() 54 | piHoleInfo.Border = false 55 | 56 | totalQueries := widgets.NewParagraph() 57 | totalQueries.Title = "Queries /24hr" 58 | totalQueries.TitleStyle.Fg = ui.ColorGreen 59 | totalQueries.BorderStyle.Fg = ui.ColorGreen 60 | 61 | queriesBlocked := widgets.NewParagraph() 62 | queriesBlocked.Title = "Blocked /24hr" 63 | queriesBlocked.TitleStyle.Fg = ui.ColorBlue 64 | queriesBlocked.BorderStyle.Fg = ui.ColorBlue 65 | 66 | percentBlocked := widgets.NewParagraph() 67 | percentBlocked.Title = "Percent Blocked" 68 | percentBlocked.TitleStyle.Fg = ui.ColorYellow 69 | percentBlocked.BorderStyle.Fg = ui.ColorYellow 70 | 71 | domainsOnBlocklist := widgets.NewParagraph() 72 | domainsOnBlocklist.Title = "Blocklist Size" 73 | domainsOnBlocklist.TitleStyle.Fg = ui.ColorRed 74 | domainsOnBlocklist.BorderStyle.Fg = ui.ColorRed 75 | 76 | topQueries := widgets.NewList() 77 | topQueries.Title = "Top 10 Permitted Domains" 78 | topQueries.Rows = api.LiveTopItems.PrettyTopQueries 79 | 80 | topAds := widgets.NewList() 81 | topAds.Title = "Top 10 Blocked Domains" 82 | topAds.Rows = api.LiveTopItems.PrettyTopAds 83 | 84 | queryLog := widgets.NewList() 85 | queryLog.Title = fmt.Sprintf("Latest %d queries", api.LiveAllQueries.AmountOfQueriesInLog) 86 | queryLog.Rows = api.LiveAllQueries.Table 87 | 88 | keybindsPrompt := widgets.NewParagraph() 89 | keybindsPrompt.Text = "Press F1 at any time to view keybinds..." 90 | keybindsPrompt.Border = false 91 | 92 | grid := ui.NewGrid() 93 | w, h := ui.TerminalDimensions() 94 | grid.SetRect(0, 0, w, h) 95 | 96 | grid.Set( 97 | ui.NewRow(.2, 98 | ui.NewCol(.2, 99 | ui.NewRow(.5, totalQueries), 100 | ui.NewRow(.5, percentBlocked), 101 | ), 102 | ui.NewCol(.2, 103 | ui.NewRow(.5, queriesBlocked), 104 | ui.NewRow(.5, domainsOnBlocklist), 105 | ), 106 | ui.NewCol(.6, 107 | ui.NewRow(1, piHoleInfo), 108 | ), 109 | ), 110 | ui.NewRow(.35, 111 | ui.NewCol(.5, topQueries), 112 | ui.NewCol(.5, topAds), 113 | ), 114 | ui.NewRow(.35, 115 | ui.NewCol(1, queryLog), 116 | ), 117 | ui.NewRow(.1, 118 | ui.NewCol(1, keybindsPrompt), 119 | ), 120 | ) 121 | 122 | keybindsList := widgets.NewList() 123 | keybindsList.Title = "Pi-CLI keybinds" 124 | keybindsList.Rows = data.LivePiCLIData.Keybinds 125 | 126 | returnHomePrompt := widgets.NewParagraph() 127 | returnHomePrompt.Text = "Press F1 at any time to return home..." 128 | returnHomePrompt.Border = false 129 | 130 | keybindsGrid := ui.NewGrid() 131 | w, h = ui.TerminalDimensions() 132 | keybindsGrid.SetRect(0, 0, w, h) 133 | keybindsGrid.Set( 134 | ui.NewRow(.9, 135 | ui.NewCol(1, keybindsList), 136 | ), 137 | ui.NewRow(.1, 138 | ui.NewCol(1, returnHomePrompt), 139 | ), 140 | ) 141 | 142 | draw := func() { 143 | if uiCanDraw() { 144 | // 4 top summary boxes 145 | totalQueries.Text = api.LiveSummary.QueriesToday 146 | queriesBlocked.Text = api.LiveSummary.BlockedToday 147 | percentBlocked.Text = api.LiveSummary.PercentBlockedToday + "%" 148 | domainsOnBlocklist.Text = api.LiveSummary.DomainsOnBlocklist 149 | 150 | // domain lists 151 | topQueries.Rows = api.LiveTopItems.PrettyTopQueries 152 | topAds.Rows = api.LiveTopItems.PrettyTopAds 153 | 154 | // query log 155 | queryLog.Rows = api.LiveAllQueries.Table 156 | queryLog.Title = fmt.Sprintf("Latest %d queries", api.LiveAllQueries.AmountOfQueriesInLog) 157 | 158 | // timestamp of the last data grab 159 | formattedTime := data.LivePiCLIData.LastUpdated.Format("15:04:05") 160 | 161 | piHoleInfo.Rows = []string{ 162 | fmt.Sprintf("Pi-Hole Status: %s", strings.Title(api.LiveSummary.Status)), 163 | fmt.Sprintf( 164 | "Data last updated: %s (update every %ds)", 165 | formattedTime, 166 | data.LivePiCLIData.Settings.RefreshS), 167 | fmt.Sprintf("Privacy Level: %s", getPrivacyLevel(&api.LiveSummary.PrivacyLevel)), 168 | fmt.Sprintf("Total Clients Seen: %s", api.LiveSummary.TotalClientsSeen), 169 | } 170 | 171 | // render the grid 172 | ui.Render(grid) 173 | } else { 174 | ui.Render(keybindsGrid) 175 | } 176 | } 177 | 178 | uiEvents := ui.PollEvents() 179 | 180 | // channel used to capture ticker events to time data update events 181 | tickerDuration := time.Duration(data.LivePiCLIData.Settings.RefreshS) 182 | dataUpdateTicker := time.NewTicker(time.Second * tickerDuration).C 183 | 184 | // channel used to capture ticker events to time redraws (30fps) 185 | drawTicker := time.NewTicker(time.Second / 30).C 186 | 187 | updateData() 188 | draw() 189 | for { 190 | select { 191 | case e := <-uiEvents: 192 | switch e.ID { 193 | 194 | // quit 195 | case "q", "": 196 | return 197 | 198 | // respond to terminal resize events 199 | case "": 200 | payload := e.Payload.(ui.Resize) 201 | if uiCanDraw() { 202 | grid.SetRect(0, 0, payload.Width, payload.Height) 203 | ui.Render(grid) 204 | break 205 | } 206 | keybindsGrid.SetRect(0, 0, payload.Width, payload.Height) 207 | ui.Clear() 208 | ui.Render(keybindsGrid) 209 | break 210 | 211 | // increase (by 1) the number of queries in the query log 212 | case "e": 213 | if uiCanDraw() { 214 | api.LiveAllQueries.AmountOfQueriesInLog++ 215 | api.LiveAllQueries.Queries = append(api.LiveAllQueries.Queries, api.Query{}) 216 | } 217 | break 218 | 219 | // increase (by 10) the number of queries in the query log 220 | case "r": 221 | if uiCanDraw() { 222 | api.LiveAllQueries.AmountOfQueriesInLog += 10 223 | api.LiveAllQueries.Queries = append(api.LiveAllQueries.Queries, make([]api.Query, 10)...) 224 | } 225 | break 226 | 227 | // decrease (by 1) the number of queries in the query log 228 | case "d": 229 | if uiCanDraw() && api.LiveAllQueries.AmountOfQueriesInLog > 1 { 230 | api.LiveAllQueries.AmountOfQueriesInLog-- 231 | api.LiveAllQueries.Queries = api.LiveAllQueries.Queries[:len(api.LiveAllQueries.Queries)-1] 232 | } 233 | break 234 | 235 | // decrease (by 10) the number of queries in the query log 236 | case "f": 237 | if uiCanDraw() { 238 | if api.LiveAllQueries.AmountOfQueriesInLog-10 <= 0 { 239 | api.LiveAllQueries.AmountOfQueriesInLog = 1 240 | api.LiveAllQueries.Queries = 241 | api.LiveAllQueries.Queries[:len(api.LiveAllQueries.Queries)-(len(api.LiveAllQueries.Queries)-1)] 242 | } else { 243 | api.LiveAllQueries.AmountOfQueriesInLog -= 10 244 | api.LiveAllQueries.Queries = api.LiveAllQueries.Queries[:len(api.LiveAllQueries.Queries)-10] 245 | } 246 | } 247 | break 248 | 249 | // scroll down (by 1) in the query log list 250 | case "": 251 | if uiCanDraw() { 252 | queryLog.ScrollDown() 253 | } 254 | break 255 | 256 | // scroll down (by 10) in the query log list 257 | case "": 258 | if uiCanDraw() { 259 | queryLog.ScrollAmount(10) 260 | } 261 | break 262 | 263 | // scroll up (by 1) in the query log list 264 | case "": 265 | if uiCanDraw() { 266 | queryLog.ScrollUp() 267 | } 268 | break 269 | 270 | // scroll up (by 10) in the query log list 271 | case "": 272 | if uiCanDraw() { 273 | queryLog.ScrollAmount(-10) 274 | } 275 | break 276 | 277 | // enable or disable the Pi-Hole 278 | case "p": 279 | if uiCanDraw() { 280 | if api.LiveSummary.Status == "enabled" { 281 | api.DisablePiHole(false, 0) 282 | } else { 283 | api.EnablePiHole() 284 | } 285 | } 286 | break 287 | 288 | // switch grids between the keybinds view and the main screen 289 | case "": 290 | ui.Clear() 291 | data.LivePiCLIData.ShowKeybindsScreen = !data.LivePiCLIData.ShowKeybindsScreen 292 | break 293 | } 294 | 295 | /* 296 | Capturing 2 separate ticker channels like this allows the update of the data and the update of the 297 | UI to occur independently. Key presses will still be visually responded to and the program itself will 298 | *feel* quick and responsive even if the user has set a much longer data refresh rate. 299 | */ 300 | 301 | // refresh event used to time API polls for up to date data 302 | case <-dataUpdateTicker: 303 | // there's only a need to make API calls when the keybinds screen isn't being shown 304 | if uiCanDraw() { 305 | updateData() 306 | } 307 | break 308 | 309 | // draw event used to time UI redraws 310 | case <-drawTicker: 311 | draw() 312 | break 313 | } 314 | } 315 | } 316 | --------------------------------------------------------------------------------