mirror of
https://github.com/eko/pihole-exporter.git
synced 2024-12-21 15:47:59 +01:00
Merge pull request #211 from csmarchbanks/avoid-auth-panic
Avoid Panic During Authentication
This commit is contained in:
commit
085b3bbbec
@ -162,12 +162,12 @@ func (c *Client) setMetrics(stats *Stats) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getPHPSessionID() (sessionID string) {
|
func (c *Client) getPHPSessionID() (string, error) {
|
||||||
values := url.Values{"pw": []string{c.config.PIHolePassword}}
|
values := url.Values{"pw": []string{c.config.PIHolePassword}}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", c.config.PIHoleLoginURL(), strings.NewReader(values.Encode()))
|
req, err := http.NewRequest("POST", c.config.PIHoleLoginURL(), strings.NewReader(values.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("An error has occured when creating HTTP statistics request", err)
|
return "", fmt.Errorf("creating HTTP statistics request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
@ -175,17 +175,16 @@ func (c *Client) getPHPSessionID() (sessionID string) {
|
|||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("An error has occured during login to Pi-hole: %v", err)
|
return "", fmt.Errorf("loging in to Pi-hole: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cookie := range resp.Cookies() {
|
for _, cookie := range resp.Cookies() {
|
||||||
if cookie.Name == "PHPSESSID" {
|
if cookie.Name == "PHPSESSID" {
|
||||||
sessionID = cookie.Value
|
return cookie.Value, nil
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return "", fmt.Errorf("no PHPSESSID cookie found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getStatistics() (*Stats, error) {
|
func (c *Client) getStatistics() (*Stats, error) {
|
||||||
@ -203,7 +202,10 @@ func (c *Client) getStatistics() (*Stats, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if c.isUsingPassword() {
|
if c.isUsingPassword() {
|
||||||
c.authenticateRequest(req)
|
err := c.authenticateRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("an error has occurred authenticating the request: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
@ -233,7 +235,12 @@ func (c *Client) isUsingApiToken() bool {
|
|||||||
return len(c.config.PIHoleApiToken) > 0
|
return len(c.config.PIHoleApiToken) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) authenticateRequest(req *http.Request) {
|
func (c *Client) authenticateRequest(req *http.Request) error {
|
||||||
cookie := http.Cookie{Name: "PHPSESSID", Value: c.getPHPSessionID()}
|
sessionID, err := c.getPHPSessionID()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cookie := http.Cookie{Name: "PHPSESSID", Value: sessionID}
|
||||||
req.AddCookie(&cookie)
|
req.AddCookie(&cookie)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user