pihole-api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +58 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +110 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +10 -0
- data/bin/console +11 -0
- data/bin/make +3 -0
- data/bin/setup +8 -0
- data/lib/pihole-api/authorised_endpoints.rb +82 -0
- data/lib/pihole-api/client.rb +118 -0
- data/lib/pihole-api/constants.rb +5 -0
- data/lib/pihole-api/unauthorised_endpoints.rb +16 -0
- data/lib/pihole-api/version.rb +3 -0
- data/lib/pihole-api.rb +15 -0
- data/pihole-api.gemspec +38 -0
- metadata +217 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f0caf0f5f8ec1a96e09e1078ae0ab54e7c7bc6d4f1726250eb17627e146d162e
|
4
|
+
data.tar.gz: a979aca92108d4149e964bc203b16eb185a3a3b1afa7e4df2eb01037836f60d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4368a1a0310ba474d4cf8d6b44ba42ef6456d46c17f4c944dabb7a93ff3c66d1882235d946954cf8bf91ba5d96ddf931e1053aa68ac40facb0b8a41f33429e16
|
7
|
+
data.tar.gz: 2e12802fcf1837cd8f19a190311d5f55b3fd0a96dc62bbec965c21020673ed2a0cb2d27ea83e9a8d56eb8f4df97a7e8246ae4fcffd6787b964d7f14a6276dc8d
|
data/.gitignore
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
# Ignore Byebug command history file.
|
17
|
+
.byebug_history
|
18
|
+
|
19
|
+
## Specific to RubyMotion:
|
20
|
+
.dat*
|
21
|
+
.repl_history
|
22
|
+
build/
|
23
|
+
*.bridgesupport
|
24
|
+
build-iPhoneOS/
|
25
|
+
build-iPhoneSimulator/
|
26
|
+
|
27
|
+
## Specific to RubyMotion (use of CocoaPods):
|
28
|
+
#
|
29
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
30
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
31
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
32
|
+
#
|
33
|
+
# vendor/Pods/
|
34
|
+
|
35
|
+
## Documentation cache and generated files:
|
36
|
+
/.yardoc/
|
37
|
+
/_yardoc/
|
38
|
+
/doc/
|
39
|
+
/rdoc/
|
40
|
+
|
41
|
+
## Environment normalization:
|
42
|
+
/.bundle/
|
43
|
+
/vendor/bundle
|
44
|
+
/lib/bundler/man/
|
45
|
+
|
46
|
+
# for a library or gem, you might want to ignore these files since the code is
|
47
|
+
# intended to run in multiple environments; otherwise, check them in:
|
48
|
+
# Gemfile.lock
|
49
|
+
# .ruby-version
|
50
|
+
# .ruby-gemset
|
51
|
+
|
52
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
53
|
+
.rvmrc
|
54
|
+
|
55
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
56
|
+
# .rubocop-https?--*
|
57
|
+
|
58
|
+
.DS_Store
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.2.0
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behaviour that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behaviour by participants include:
|
24
|
+
|
25
|
+
* The use of sexualised language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behaviour and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behaviour.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviours that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behaviour may be
|
58
|
+
reported by contacting the project team at contact@jasonchalom.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pihole-api (0.0.1)
|
5
|
+
active_attr (~> 0.15.4)
|
6
|
+
httparty (~> 0.21.0)
|
7
|
+
nokogiri (~> 1.14.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
actionpack (7.0.4.1)
|
13
|
+
actionview (= 7.0.4.1)
|
14
|
+
activesupport (= 7.0.4.1)
|
15
|
+
rack (~> 2.0, >= 2.2.0)
|
16
|
+
rack-test (>= 0.6.3)
|
17
|
+
rails-dom-testing (~> 2.0)
|
18
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
19
|
+
actionview (7.0.4.1)
|
20
|
+
activesupport (= 7.0.4.1)
|
21
|
+
builder (~> 3.1)
|
22
|
+
erubi (~> 1.4)
|
23
|
+
rails-dom-testing (~> 2.0)
|
24
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
25
|
+
active_attr (0.15.4)
|
26
|
+
actionpack (>= 3.0.2, < 7.1)
|
27
|
+
activemodel (>= 3.0.2, < 7.1)
|
28
|
+
activesupport (>= 3.0.2, < 7.1)
|
29
|
+
activemodel (7.0.4.1)
|
30
|
+
activesupport (= 7.0.4.1)
|
31
|
+
activesupport (7.0.4.1)
|
32
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
33
|
+
i18n (>= 1.6, < 2)
|
34
|
+
minitest (>= 5.1)
|
35
|
+
tzinfo (~> 2.0)
|
36
|
+
addressable (2.8.1)
|
37
|
+
public_suffix (>= 2.0.2, < 6.0)
|
38
|
+
ansi (1.5.0)
|
39
|
+
builder (3.2.4)
|
40
|
+
coderay (1.1.3)
|
41
|
+
concurrent-ruby (1.1.10)
|
42
|
+
crack (0.4.5)
|
43
|
+
rexml
|
44
|
+
crass (1.0.6)
|
45
|
+
erubi (1.12.0)
|
46
|
+
hashdiff (1.0.1)
|
47
|
+
httparty (0.21.0)
|
48
|
+
mini_mime (>= 1.0.0)
|
49
|
+
multi_xml (>= 0.5.2)
|
50
|
+
i18n (1.12.0)
|
51
|
+
concurrent-ruby (~> 1.0)
|
52
|
+
loofah (2.19.1)
|
53
|
+
crass (~> 1.0.2)
|
54
|
+
nokogiri (>= 1.5.9)
|
55
|
+
method_source (1.0.0)
|
56
|
+
mini_mime (1.1.2)
|
57
|
+
minitest (5.17.0)
|
58
|
+
minitest-focus (1.3.1)
|
59
|
+
minitest (>= 4, < 6)
|
60
|
+
minitest-reporters (1.5.0)
|
61
|
+
ansi
|
62
|
+
builder
|
63
|
+
minitest (>= 5.0)
|
64
|
+
ruby-progressbar
|
65
|
+
mocha (2.0.2)
|
66
|
+
ruby2_keywords (>= 0.0.5)
|
67
|
+
multi_xml (0.6.0)
|
68
|
+
nokogiri (1.14.0-arm64-darwin)
|
69
|
+
racc (~> 1.4)
|
70
|
+
pry (0.14.2)
|
71
|
+
coderay (~> 1.1)
|
72
|
+
method_source (~> 1.0)
|
73
|
+
public_suffix (5.0.1)
|
74
|
+
racc (1.6.2)
|
75
|
+
rack (2.2.6.2)
|
76
|
+
rack-test (2.0.2)
|
77
|
+
rack (>= 1.3)
|
78
|
+
rails-dom-testing (2.0.3)
|
79
|
+
activesupport (>= 4.2.0)
|
80
|
+
nokogiri (>= 1.6)
|
81
|
+
rails-html-sanitizer (1.5.0)
|
82
|
+
loofah (~> 2.19, >= 2.19.1)
|
83
|
+
rake (13.0.6)
|
84
|
+
rexml (3.2.5)
|
85
|
+
ruby-progressbar (1.11.0)
|
86
|
+
ruby2_keywords (0.0.5)
|
87
|
+
timecop (0.9.6)
|
88
|
+
tzinfo (2.0.5)
|
89
|
+
concurrent-ruby (~> 1.0)
|
90
|
+
webmock (3.18.1)
|
91
|
+
addressable (>= 2.8.0)
|
92
|
+
crack (>= 0.3.2)
|
93
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
94
|
+
|
95
|
+
PLATFORMS
|
96
|
+
arm64-darwin-20
|
97
|
+
|
98
|
+
DEPENDENCIES
|
99
|
+
minitest (~> 5.17.0)
|
100
|
+
minitest-focus (~> 1.3.1)
|
101
|
+
minitest-reporters (~> 1.5.0)
|
102
|
+
mocha (~> 2.0.2)
|
103
|
+
pihole-api!
|
104
|
+
pry (~> 0.14.2)
|
105
|
+
rake (~> 13.0.6)
|
106
|
+
timecop (~> 0.9.6)
|
107
|
+
webmock (~> 3.18.1)
|
108
|
+
|
109
|
+
BUNDLED WITH
|
110
|
+
2.3.16
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Jason Chalom
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 trex22
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# PiholeApiClient
|
2
|
+
A client for the PiholeApi API.
|
3
|
+
|
4
|
+
See:
|
5
|
+
- https://discourse.pi-hole.net/t/pi-hole-api/1863
|
6
|
+
- https://www.reddit.com/r/pihole/comments/br5tla/how_to_get_info_from_the_new_restful_api/
|
7
|
+
- https://pi-hole.net/blog/2022/11/17/upcoming-changes-authentication-for-more-api-endpoints-required/
|
8
|
+
- https://pi-hole.net/blog/2022/12/21/pi-hole-ftl-v5-20-and-web-v5-18-released/
|
9
|
+
- https://github.com/pi-hole/AdminLTE/issues/2470
|
10
|
+
- https://github.com/pi-hole/AdminLTE/issues/2326
|
11
|
+
- https://discourse.pi-hole.net/t/using-the-api/976/6
|
12
|
+
|
13
|
+
This is an unofficial project and still a work in progress (WIP) ... more to come soon.
|
14
|
+
|
15
|
+
## TODO
|
16
|
+
- Login and Auth token generation: `admin/scripts/pi-hole/php/api_token.php`
|
17
|
+
- Undocumented endpointsw
|
18
|
+
- Undocumented filters for getAllQueries
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'pihole-api'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install pihole-api
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'pihole-api'
|
40
|
+
client = PiholeApi::Client.new(base_path: 'http://localhost:80/', api_token: '', port: 80)
|
41
|
+
|
42
|
+
# Some example calls
|
43
|
+
client.type
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
### Endpoints
|
48
|
+
#### Authorised
|
49
|
+
- `summary_raw`
|
50
|
+
- `summary(params: {})`
|
51
|
+
- `over_time_data_10_mins`
|
52
|
+
- `top_items(number_of_items=10)`
|
53
|
+
- `get_query_sources(number_of_items=10)`
|
54
|
+
- `top_clients(number_of_items=10)`
|
55
|
+
- `get_forward_destinations`
|
56
|
+
- `get_query_types`
|
57
|
+
`get_all_queries(params: {}, from_time: nil, until_time: nil, latest_number_of_items: nil)`
|
58
|
+
- `recent_blocked`
|
59
|
+
- `enable`
|
60
|
+
- `disable`
|
61
|
+
|
62
|
+
#### Unauthorised
|
63
|
+
- `type`
|
64
|
+
- `version`
|
65
|
+
|
66
|
+
### Constants
|
67
|
+
Constants
|
68
|
+
|
69
|
+
## Development
|
70
|
+
|
71
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
72
|
+
|
73
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
74
|
+
|
75
|
+
### Tests
|
76
|
+
To run tests execute:
|
77
|
+
|
78
|
+
$ rake test
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/trex22/pihole-api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
87
|
+
|
88
|
+
## Code of Conduct
|
89
|
+
|
90
|
+
Everyone interacting in the pihole-api: project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/trex22/pihole-api/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pihole-api'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require 'pry'
|
11
|
+
Pry.start
|
data/bin/make
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
module PiholeApi
|
2
|
+
module AuthorisedEndpoints
|
3
|
+
# These endpoints need to be authorised
|
4
|
+
# https://discourse.pi-hole.net/t/pi-hole-api/1863
|
5
|
+
|
6
|
+
def summary_raw
|
7
|
+
command = "summaryRaw&auth=#{password}"
|
8
|
+
authorise_and_send(http_method: :get, command: command)
|
9
|
+
end
|
10
|
+
|
11
|
+
def summary(params: {})
|
12
|
+
command = 'summary'
|
13
|
+
authorise_and_send(http_method: :get, command: command, params: params)
|
14
|
+
end
|
15
|
+
|
16
|
+
def over_time_data_10_mins
|
17
|
+
command = 'overTimeData10mins'
|
18
|
+
authorise_and_send(http_method: :get, command: command)
|
19
|
+
end
|
20
|
+
|
21
|
+
def top_items(number_of_items=10)
|
22
|
+
command = "topItems=#{number_of_items}"
|
23
|
+
authorise_and_send(http_method: :get, command: command)
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_query_sources(number_of_items=10)
|
27
|
+
command = "getQuerySources=#{number_of_items}"
|
28
|
+
authorise_and_send(http_method: :get, command: command)
|
29
|
+
end
|
30
|
+
|
31
|
+
def top_clients(number_of_items=10)
|
32
|
+
command = "topClients=#{number_of_items}"
|
33
|
+
authorise_and_send(http_method: :get, command: command)
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_forward_destinations
|
37
|
+
command = 'getForwardDestinations'
|
38
|
+
authorise_and_send(http_method: :get, command: command)
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_query_types
|
42
|
+
command = 'getQueryTypes'
|
43
|
+
authorise_and_send(http_method: :get, command: command)
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_all_queries(params: {}, from_time: nil, until_time: nil, latest_number_of_items: nil)
|
47
|
+
# API version 3:
|
48
|
+
# First column: Timestamp of query
|
49
|
+
# Second column: Type of query (IPv4 or IPv6)
|
50
|
+
# Third column: Requested domain name
|
51
|
+
# Fourth column: Requesting client
|
52
|
+
# Fifth column: Answer type (1 = blocked by gravity.list, 2 = forwarded to upstream server, 3 = answered by local cache, 4 = blocked by wildcard blocking)
|
53
|
+
|
54
|
+
if latest_number_of_items
|
55
|
+
command = "getAllQueries=#{latest_number_of_items}"
|
56
|
+
else
|
57
|
+
command = 'getAllQueries'
|
58
|
+
end
|
59
|
+
|
60
|
+
if from_time
|
61
|
+
command = "#{command}&from=#{from_time.to_i}&until=#{until_time.to_i}"
|
62
|
+
end
|
63
|
+
|
64
|
+
authorise_and_send(http_method: :get, command: command, params: params)
|
65
|
+
end
|
66
|
+
|
67
|
+
def recent_blocked
|
68
|
+
command = 'recentBlocked'
|
69
|
+
authorise_and_send(http_method: :get, command: command)
|
70
|
+
end
|
71
|
+
|
72
|
+
def enable
|
73
|
+
command = 'enable'
|
74
|
+
authorise_and_send(http_method: :get, command: command)
|
75
|
+
end
|
76
|
+
|
77
|
+
def disable
|
78
|
+
command = 'disable'
|
79
|
+
authorise_and_send(http_method: :get, command: command)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
module PiholeApi
|
2
|
+
class Client
|
3
|
+
include ::PiholeApi::Constants
|
4
|
+
|
5
|
+
# Endpoints
|
6
|
+
include ::PiholeApi::UnauthorisedEndpoints
|
7
|
+
include ::PiholeApi::AuthorisedEndpoints
|
8
|
+
|
9
|
+
attr_reader :api_token, :base_path, :port
|
10
|
+
|
11
|
+
def initialize(base_path:, api_token:, port: 80)
|
12
|
+
@api_token = api_token
|
13
|
+
@base_path = base_path
|
14
|
+
@port = port
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.compatible_api_version
|
18
|
+
'v3'
|
19
|
+
end
|
20
|
+
|
21
|
+
# This is the version of the API docs this client was built off-of
|
22
|
+
def self.api_version
|
23
|
+
'v2 2023-01-21'
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def authorise_and_send(http_method:, command:, payload: {}, params: {})
|
29
|
+
start_time = micro_second_time_now
|
30
|
+
|
31
|
+
if params.nil? || params.empty?
|
32
|
+
params = {}
|
33
|
+
end
|
34
|
+
|
35
|
+
response = HTTParty.send(
|
36
|
+
http_method.to_sym,
|
37
|
+
construct_base_path(command, params),
|
38
|
+
body: payload,
|
39
|
+
headers: {
|
40
|
+
'Access-Token': @access_token,
|
41
|
+
'Content-Type': 'application/json',
|
42
|
+
"Accept": 'application/json',
|
43
|
+
"X-Pi-hole-Authenticate": api_token
|
44
|
+
},
|
45
|
+
port: port,
|
46
|
+
format: :json
|
47
|
+
)
|
48
|
+
|
49
|
+
end_time = micro_second_time_now
|
50
|
+
construct_response_object(response, command, start_time, end_time)
|
51
|
+
end
|
52
|
+
|
53
|
+
def construct_response_object(response, path, start_time, end_time)
|
54
|
+
{
|
55
|
+
'body' => parse_body(response, path),
|
56
|
+
'headers' => response.headers,
|
57
|
+
'metadata' => construct_metadata(response, start_time, end_time)
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
def construct_metadata(response, start_time, end_time)
|
62
|
+
total_time = end_time - start_time
|
63
|
+
|
64
|
+
{
|
65
|
+
'start_time' => start_time,
|
66
|
+
'end_time' => end_time,
|
67
|
+
'total_time' => total_time
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
def body_is_present?(response)
|
72
|
+
!body_is_missing?(response)
|
73
|
+
end
|
74
|
+
|
75
|
+
def body_is_missing?(response)
|
76
|
+
response.body.nil? || response.body.empty?
|
77
|
+
end
|
78
|
+
|
79
|
+
def parse_body(response, path)
|
80
|
+
return [] if response.body == "[]"
|
81
|
+
parse_json(response) # Purposely not using HTTParty
|
82
|
+
end
|
83
|
+
|
84
|
+
def parse_json(response)
|
85
|
+
begin
|
86
|
+
JSON.parse(response.body)
|
87
|
+
rescue => _e
|
88
|
+
response.body
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def micro_second_time_now
|
93
|
+
(Time.now.to_f * 1_000_000).to_i
|
94
|
+
end
|
95
|
+
|
96
|
+
def construct_base_path(command, params)
|
97
|
+
constructed_path = "#{base_path}/#{API_PATH}?#{command}&auth=#{api_token}"
|
98
|
+
|
99
|
+
if params == {}
|
100
|
+
constructed_path
|
101
|
+
else
|
102
|
+
"#{constructed_path}&#{process_params(params)}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def process_params(params)
|
107
|
+
params.keys.map { |key| "#{key}=#{params[key]}" }.join('&')
|
108
|
+
end
|
109
|
+
|
110
|
+
def process_cursor(cursor, params: {})
|
111
|
+
unless cursor.nil? || cursor.empty?
|
112
|
+
params['cursor'] = cursor
|
113
|
+
end
|
114
|
+
|
115
|
+
params
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module PiholeApi
|
2
|
+
module UnauthorisedEndpoints
|
3
|
+
# These endpoints dont need to be authorised
|
4
|
+
# https://discourse.pi-hole.net/t/pi-hole-api/1863
|
5
|
+
|
6
|
+
def type
|
7
|
+
command = 'type'
|
8
|
+
authorise_and_send(http_method: :get, command: command)
|
9
|
+
end
|
10
|
+
|
11
|
+
def version
|
12
|
+
command = 'version'
|
13
|
+
authorise_and_send(http_method: :get, command: command)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/pihole-api.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
require 'pihole-api/version'
|
5
|
+
require 'pihole-api/constants'
|
6
|
+
|
7
|
+
# Endpoints
|
8
|
+
require 'pihole-api/unauthorised_endpoints'
|
9
|
+
require 'pihole-api/authorised_endpoints'
|
10
|
+
|
11
|
+
require 'pihole-api/client'
|
12
|
+
|
13
|
+
module PiholeApi
|
14
|
+
class Error < StandardError; end
|
15
|
+
end
|
data/pihole-api.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'pihole-api/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'pihole-api'
|
7
|
+
spec.version = PiholeApi::VERSION
|
8
|
+
spec.authors = ['trex22']
|
9
|
+
spec.email = ['contact@jasonchalom.com']
|
10
|
+
|
11
|
+
spec.summary = 'A client for using the PiholeApi API in Ruby.'
|
12
|
+
spec.description = 'A client for using the PiholeApi API in Ruby. Built form their api documentation. https://discourse.pi-hole.net/t/using-the-api/976/6. This is an unofficial project.'
|
13
|
+
spec.homepage = 'https://github.com/TRex22/pihole-api'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_dependency "httparty", "~> 0.21.0"
|
26
|
+
spec.add_dependency "active_attr", "~> 0.15.4"
|
27
|
+
spec.add_dependency "nokogiri", "~> 1.14.0"
|
28
|
+
|
29
|
+
# Development dependancies
|
30
|
+
spec.add_development_dependency "rake", "~> 13.0.6"
|
31
|
+
spec.add_development_dependency "minitest", "~> 5.17.0"
|
32
|
+
spec.add_development_dependency "minitest-focus", "~> 1.3.1"
|
33
|
+
spec.add_development_dependency "minitest-reporters", "~> 1.5.0"
|
34
|
+
spec.add_development_dependency "timecop", "~> 0.9.6"
|
35
|
+
spec.add_development_dependency "mocha", "~> 2.0.2"
|
36
|
+
spec.add_development_dependency "pry", "~> 0.14.2"
|
37
|
+
spec.add_development_dependency "webmock", "~> 3.18.1"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,217 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pihole-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- trex22
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.21.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.21.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: active_attr
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.15.4
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.15.4
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: nokogiri
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.14.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.14.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 13.0.6
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 13.0.6
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 5.17.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 5.17.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest-focus
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.3.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.3.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: minitest-reporters
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.5.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.5.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: timecop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.9.6
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.9.6
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: mocha
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 2.0.2
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 2.0.2
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: pry
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 0.14.2
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 0.14.2
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: webmock
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 3.18.1
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 3.18.1
|
167
|
+
description: A client for using the PiholeApi API in Ruby. Built form their api documentation.
|
168
|
+
https://discourse.pi-hole.net/t/using-the-api/976/6. This is an unofficial project.
|
169
|
+
email:
|
170
|
+
- contact@jasonchalom.com
|
171
|
+
executables: []
|
172
|
+
extensions: []
|
173
|
+
extra_rdoc_files: []
|
174
|
+
files:
|
175
|
+
- ".gitignore"
|
176
|
+
- ".ruby-version"
|
177
|
+
- CODE_OF_CONDUCT.md
|
178
|
+
- Gemfile
|
179
|
+
- Gemfile.lock
|
180
|
+
- LICENSE
|
181
|
+
- LICENSE.txt
|
182
|
+
- README.md
|
183
|
+
- Rakefile
|
184
|
+
- bin/console
|
185
|
+
- bin/make
|
186
|
+
- bin/setup
|
187
|
+
- lib/pihole-api.rb
|
188
|
+
- lib/pihole-api/authorised_endpoints.rb
|
189
|
+
- lib/pihole-api/client.rb
|
190
|
+
- lib/pihole-api/constants.rb
|
191
|
+
- lib/pihole-api/unauthorised_endpoints.rb
|
192
|
+
- lib/pihole-api/version.rb
|
193
|
+
- pihole-api.gemspec
|
194
|
+
homepage: https://github.com/TRex22/pihole-api
|
195
|
+
licenses:
|
196
|
+
- MIT
|
197
|
+
metadata: {}
|
198
|
+
post_install_message:
|
199
|
+
rdoc_options: []
|
200
|
+
require_paths:
|
201
|
+
- lib
|
202
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
203
|
+
requirements:
|
204
|
+
- - ">="
|
205
|
+
- !ruby/object:Gem::Version
|
206
|
+
version: '0'
|
207
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
208
|
+
requirements:
|
209
|
+
- - ">="
|
210
|
+
- !ruby/object:Gem::Version
|
211
|
+
version: '0'
|
212
|
+
requirements: []
|
213
|
+
rubygems_version: 3.3.4
|
214
|
+
signing_key:
|
215
|
+
specification_version: 4
|
216
|
+
summary: A client for using the PiholeApi API in Ruby.
|
217
|
+
test_files: []
|