FTL icon indicating copy to clipboard operation
FTL copied to clipboard

Add HTTP server and JSON API

Open DL6ER opened this issue 5 years ago • 41 comments

By submitting this pull request, I confirm the following (please check boxes, eg [X]) Failure to fill the template will close your PR:

Please submit all pull requests against the development branch. Failure to do so will delay or deny your request

  • [X] I have read and understood the contributors guide.
  • [X] I have checked that another pull request for this purpose does not exist.
  • [X] I have considered, and confirmed that this submission will be valuable to others.
  • [X] I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  • [X] I give this submission freely, and claim no ownership to its content.

How familiar are you with the codebase?:

10


This PR adds a fully-fledged HTTP server and JSON API to FTL. It will replace lighttpd and serve both the API and the next-gen web interface.

More detail is to be added here once this feature advances further.

DL6ER avatar Nov 21 '19 12:11 DL6ER

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/make-some-advance-api/26786/2

pralor-bot avatar Jan 10 '20 19:01 pralor-bot

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/black-whitelisting-does-not-work-on-v5-api/33119/1

pralor-bot avatar May 22 '20 17:05 pralor-bot

We're still following this. I just rebased it onto the latest development branch.

DL6ER avatar May 26 '20 22:05 DL6ER

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/php-webscripts/31016/28

pralor-bot avatar May 27 '20 09:05 pralor-bot

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/php-webscripts/31016/32

pralor-bot avatar May 27 '20 12:05 pralor-bot

This branch can now host the web interface. Many things will not work as we removed the rusty telnet interface and added a React-based API in this version of FTL. This will have to be adapted for the current API. Eventually, we will be able to get rid of api.php and siblings and have all JS scripts directly talking to FTL.

However, pages not directly depending on the interactive API do already work.

For instance:

Screenshot at 2020-05-29 00-14-58 Screenshot at 2020-05-29 00-20-25

DL6ER avatar May 28 '20 22:05 DL6ER

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/php-webscripts/31016/35

pralor-bot avatar May 28 '20 22:05 pralor-bot

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/how-to-update-dhcp-with-api/34773/1

pralor-bot avatar Jun 23 '20 07:06 pralor-bot

Is there a rough date when this is going to merge?

davidhessler avatar Nov 16 '20 15:11 davidhessler

To add to that: will the old API still exist?

jojost1 avatar Nov 16 '20 15:11 jojost1

Is there a rough date when this is going to merge?

When it's ready.

To add to that: will the old API still exist?

Which API are you referring to? There are a few different things that people call an API.

dschaper avatar Nov 16 '20 17:11 dschaper

Which API are you referring to? There are a few different things that people call an API.

The current /api.php? summary, lists, topItems, ... endpoints.

jojost1 avatar Nov 17 '20 17:11 jojost1

/api.php? summary, lists, topItems, ... endpoints.

The goal is to have as little disruption as possible and to open even more opportunity for API use in a true API. The URL endpoint may change, but there will be plenty of notification and discussion prior to that, if that even needs to happen.

dschaper avatar Nov 17 '20 22:11 dschaper

The PHP-based API (which is what /api.php?summary, etc. are based on) will be replaced by a proper, more uniform and clear JSON interface. Documentation for this is being written whenever we find the time for it. As @dschaper mentioned, this will by no means be a sudden transition. We will have an extended beta-period preceding the release of the embedded system. However, it brings an enormous amount of benefits and this clearly outweighs the disadvantage of breaking a few scripts. Also, because this allows us to have the API be much more flexible in the future to extend new functionality.

DL6ER avatar Nov 17 '20 23:11 DL6ER

Hi @DL6ER, as a Go developer I would like to start writing a Go SDK for this new API. I succeeded to install Pi-Hole on a development instance with this branch with the file /etc/pihole/ftlbranch. image

I also have the latest Pi-Hole documentation with the API part your are working on. But, request the /api endpoint always try to redirect me to the "old" /admin and for now, I don't know how to "enable" this new API.

Can you help me to set up Pi-Hole in order to have this API and try to starting playing with ?

Thanks.

SkYNewZ avatar Dec 08 '20 20:12 SkYNewZ

I should mention that this API is very much work-in-progress. Even when we made a plan how the API should look like, we do not prevent it from maturing on the way. If we realize only later that a specific path should be changes because this just feels more natural (or similar), we take the liberty to change it. While we're documenting this by updating the documentation itself, it would be too much work to write change logs so tracing our changes may be some substantial amount of work (read as: may or may not, no guarantees in any direction). As such, I have the feeling that it may be too early for even staring an SDK, but I will also not stop you :-)

The redirection to /admin was added only (rather) recently and there is no exception for /api. I'm also not sure if we should use /api when the entire rest of the system is inside /admin so what we currently have is probably even correct. Users may be hosting different stuff at the root-level of the domain and I'm not sure if we should enforce an immutable endpoint on them.

Everything is still open for discussion in this branch. More eyes can often help so any comments are appreciated.

DL6ER avatar Dec 08 '20 21:12 DL6ER

If we realize only later that a specific path should be changes because this just feels more natural (or similar), we take the liberty to change it

I truly understand that your job is work in progress. Let's say I don't have a personal project at the moment, and it could be one :)

So all of your current work is available on /admin instead of /api for now ?

SkYNewZ avatar Dec 08 '20 22:12 SkYNewZ

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/consider-switching-to-using-nginx-unit-instead-of-lighttpd-php-fpm/27379/3

pralor-bot avatar Jan 28 '21 12:01 pralor-bot

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/support-for-choice-of-resolver-and-or-web-server/222/15

pralor-bot avatar Jan 28 '21 12:01 pralor-bot

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/pihole-blacklist-api-no-netflix-until-your-fitbit-says-you-can/2196/5

pralor-bot avatar Jan 29 '21 13:01 pralor-bot

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/domain-regex-enable-disable-command-or-pihole-b-command-with-group-option/43744/4

pralor-bot avatar Jan 29 '21 18:01 pralor-bot

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/client-management-only-shows-mac/46236/7

pralor-bot avatar Apr 14 '21 18:04 pralor-bot

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/add-api-integration-to-perform-crud-operations-on-local-dns-records/46350/3

pralor-bot avatar Apr 18 '21 09:04 pralor-bot

Not sure what scope this PR is and where to request new API features so I'll place a comment here. I'd love API functionality for the following:

  • Enable/Disable Black/Whitelist entries
  • Detect and start an update
  • Get instance temp/cpu/mem (same that's displayed on the web interface)
  • Manage groups, adlists, clients
  • Manage settings (DNS, DHCP)
  • Manage local DNS records

Cheers!

jojost1 avatar Apr 25 '21 14:04 jojost1

Enable/Disable Black/Whitelist entries Get instance temp/cpu/mem (same that's displayed on the web interface) Manage groups, adlists, clients

This is already implemented

yubiuser avatar Apr 25 '21 14:04 yubiuser

@jojost1 The will include everything about Pi-hole that needs to be addressed from the web interface. Because we have a very tight security concept, a few things will not be possible (like restarting the system or running updates) because the FTL daemon never runs itself as root so it cannot do certain things. I do believe that such a thought-through security concept is an essential thing and more other project should follow this. But, as it makes things considerably more difficult in some locations, I don't really expect them to do similar things.

You may say that updating gravity or restarting the system is working now, however, this is only possible because of some dirty privilege tricks which we'll throw out to sandbox the Pi-hole daemon without the overhead of a "real" sandbox.

DL6ER avatar Apr 26 '21 05:04 DL6ER

Isn't this going to increase the database size a lot?

Yes, experiments I ran over the last few days suggest an increase of 30-40% (the majority is storing the client host name).

It is advantageous as it simplifies the user interface because now it doesn't matter if you query on-disk (long-term) or in-memory (fast 24 hours). The database structures are now identical and both loading and storing becomes are easy as

INSERT INTO queries SELECT * FROM disk.queries WHERE timestamp >= ?;
INSERT INTO disk.queries SELECT * FROM queries WHERE id > ? AND timestamp < ?;

Furthermore, the entire content is restored after an FTL restart and there are no artifacts like DNSSEC status always being UNKNOWN after a re-import. This is also what regex_id wants to cover - full restorability.

I did experiment with not storing TTL and REPLY_TIME but these two are only a few bytes (if stored at all, not all queries have TTL and reply time (e.g. no reply arrived at all)) and the logic for excluding them and handling this properly increases the complicity beyond what the gain is.

I currently plan to reduce the duration of the database we typically keep from one year to maybe one month. One could even argue to reduce it to one week or even 24 hours (I'm not planing to be that drastic right now). This will give us enough room to possibly add more stuff in the future. Users can always decide that they still want a specific number of days using MAXDBDAYS. We'd only change the default value.

DL6ER avatar May 26 '21 10:05 DL6ER

I agree with reducing MAXDBDAYS to less than a year. I would go no lower than 30 days as the default. Consider 60 or 90.

jfb-pihole avatar May 26 '21 12:05 jfb-pihole

I consider 90 days a good range.

Yes, experiments I ran over the last few days suggest an increase of 30-40% (the majority is storing the client host name).

Could this be a place for normalization? DNSSEC and REPLY will be fairly constant, and even client hostname could benefit from a separate table. This could save some space (although space is cheap...). Or would the trade-off, more complex queries, be to high?

yubiuser avatar May 26 '21 18:05 yubiuser

Could this be a place for normalization?

DNSSEC and REPLY will be fairly constant

Yes, we only store an ID (like we do already for type, status, etc.) so it will be one byte per entry. A linked table could possibly need more storage, but not less.

client hostname

Yes, for this it would work. However, I kept the IP address in client for backwards compatibility. Because we replicate the table in multiple places (two times in-memory and once on-disk), the additional complexity introduced by a linking table would actually be large. Even when we put the client linking-table only in the on-disk database, the storing/restoring SQL statements would be much more complex than what I shows here: https://github.com/pi-hole/FTL/pull/659#issuecomment-848672429

I'd say "storage is cheap" is a clear winner here.

DL6ER avatar May 26 '21 18:05 DL6ER

what's the status on this?

reesericci avatar Oct 24 '21 22:10 reesericci

It will be ready soon™

PromoFaux avatar Oct 25 '21 01:10 PromoFaux

We've been focusing on the v5.x code lately. The next big step is merging the changes for v5.x into this branch which will happen as early as possible.

DL6ER avatar Oct 25 '21 03:10 DL6ER

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/secure-web-interface-and-updates/51150/7

pralor-bot avatar Nov 20 '21 12:11 pralor-bot

Amazing work here! Is the API stable enough to build on top of? I have plans on building an external-dns provider for dynamically modifying DNS records and the legacy API does not support it.

adyanth avatar Jan 18 '22 09:01 adyanth

Thanks @adyanth! After spending significant time on the current release branch (v5.x), focus only recently (end of last week) moved back to the development on this branch. We are currently exploring several independent options for the web interface (AdminLTE, Vue-based, etc.) and reverse the right to tweak the individual API endpoint as we see fit. Reason for this is that we designed the REST API implementation by looking at what v5.x has and transforming this to REST best practices. However, it may turn out that it may be more natural to change a few things (like putting objects in an array rather than individual items in an array) as we develop the frontend.

DL6ER avatar Jan 18 '22 10:01 DL6ER

Understood @DL6ER , thanks! This is the place I can follow for updates right? Or is there a discource/any other place where I can keep myself updated on the progress of this?

adyanth avatar Jan 18 '22 10:01 adyanth

We're posting noteworthy updates here: https://discourse.pi-hole.net/t/pi-hole-v6-0-development-is-well-on-its-way/43314

DL6ER avatar Jan 18 '22 10:01 DL6ER

This pull request has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/support-recursive-resolution-for-non-authoritative-cname-entries/52747/3

pralor-bot avatar Jan 18 '22 12:01 pralor-bot

We're posting noteworthy updates here: https://discourse.pi-hole.net/t/pi-hole-v6-0-development-is-well-on-its-way/43314

Looks like I either do not have access to it or the link is broken. Will wait for v6.0, thanks!

adyanth avatar Jan 18 '22 12:01 adyanth