Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the information source to "pi-hole status" in the API #2059

Merged
merged 1 commit into from
Jan 8, 2022
Merged

Changing the information source to "pi-hole status" in the API #2059

merged 1 commit into from
Jan 8, 2022

Conversation

rdwebdesign
Copy link
Member

By submitting this pull request, I confirm the following:

  • I have read and understood the contributors guide, as well as this entire template.
  • I have made only one major change in my proposed changes.
  • I have commented my proposed changes within the code.
  • I have tested my proposed changes.
  • I am willing to help maintain this change if there are issues with it later.
  • I give this submission freely and claim no ownership.
  • It is compatible with the EUPL 1.2 license
  • I have squashed any insignificant commits. (git rebase)
  • I have Signed Off all commits. (git commit --signoff)

What does this PR aim to accomplish?:

Fix an API intermittent error, where the status info received by api.php or api.php?summary is sometimes wrongly reported.
Cause:
api_FTL.php uses FTL response to determine if pi-hole is enabled or disabled.
FTL only updates this value when is internally needed, but it doesn't update this value before answering the API call.

How does this PR accomplish the above?:

Replacing the status received from FTL with the status currently stored in setupVars.conf.

What documentation changes (if any) are needed to support this PR?:

none

@rdwebdesign rdwebdesign requested a review from DL6ER January 8, 2022 04:50
@yubiuser
Copy link
Member

yubiuser commented Jan 8, 2022

Idea: Re-use

https://github.com/pi-hole/AdminLTE/blob/33755f1775db31bc5cd5de0f8d82fa10a000bd42/api.php#L23-L41

@yubiuser yubiuser linked an issue Jan 8, 2022 that may be closed by this pull request
@DL6ER
Copy link
Member

DL6ER commented Jan 8, 2022

Idea: Re-use

https://github.com/pi-hole/AdminLTE/blob/33755f1775db31bc5cd5de0f8d82fa10a000bd42/api.php#L23-L41

This is a good idea. Refactor this into its own function and use the same functions in both. Prevents this from happening again in the future.

@rdwebdesign
Copy link
Member Author

rdwebdesign commented Jan 8, 2022

I think I will reuse my new function:

This:

if (isset($_GET['status'])) {
    // Receive the return of "pihole status web"
    $pistatus = pihole_execute('status web');

    if (isset($pistatus[0])) {
        $pistatus = intval($pistatus[0]);
    } else {
        // If no response, status="Unknown" (-2)
        $pistatus = -2;
    }

    switch ($pistatus) {
        case -2: // Unkown
        case -1: // DNS service not running"
        case 0: // Offline
            $data = array_merge($data, array("status" => "disabled"));
            break;

        default:
            // DNS service on port $returncode
            $data = array_merge($data, array("status" => "enabled"));
    }

} elseif ...

Will become this:

if (isset($_GET['status'])) {
    // read setupVars and return the status
    $data = array_merge($data, array("status" => piholeStatus() ));

} elseif ... 

What do you think?

@yubiuser
Copy link
Member

yubiuser commented Jan 8, 2022

Just reading /etc/pihole/setupVars.conf is not enough. When blocking is enabled there, but FTL is not running, we should not return status=enabled We actually need to check if FTL is running before determining the blocking status,

@DL6ER
Copy link
Member

DL6ER commented Jan 8, 2022

So the question is pihole status web or reading setupVars.conf using parse_ini_file()

What happens if there are comments in setupVars.conf ?

@rdwebdesign
Copy link
Member Author

Just reading /etc/pihole/setupVars.conf is not enough. When blocking is enabled there, but FTL is not running, we should not return status=enabled We actually need to check if FTL is running before determining the blocking status,

I didn't know it was possible to have status=enabled in setupVars.conf while FTL is not running

@yubiuser
Copy link
Member

yubiuser commented Jan 8, 2022

It could just crash...

@rdwebdesign
Copy link
Member Author

What happens if there are comments in setupVars.conf ?

PHP strips the comments.

Using "pihole status web" info

Signed-off-by: rdwebdesign <github@rdwebdesign.com.br>
@rdwebdesign rdwebdesign requested a review from yubiuser January 8, 2022 17:17
Copy link
Member

@yubiuser yubiuser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works both for http://pi.hole/admin/api.php and http://pi.hole/admin/api.php?status

@yubiuser yubiuser merged commit ab6b7ae into pi-hole:devel Jan 8, 2022
@rdwebdesign rdwebdesign deleted the change_apiFTL branch January 8, 2022 19:31
@yubiuser yubiuser mentioned this pull request Jan 8, 2022
9 tasks
yubiuser pushed a commit that referenced this pull request Jan 11, 2022
Using "pihole status web" info

Signed-off-by: rdwebdesign <github@rdwebdesign.com.br>
@pralor-bot
Copy link

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

https://discourse.pi-hole.net/t/pi-hole-ftl-v5-14-web-v5-11-and-core-v5-9-released/53529/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API reporting status: enabled while blocking is disabled
4 participants