Fixed a bug when PI-Hole is not protected by a password (fixes #1)
This commit is contained in:
parent
cc6acaf60c
commit
4298f919ab
1 changed files with 21 additions and 14 deletions
|
@ -22,10 +22,11 @@ var (
|
||||||
|
|
||||||
// Client struct is a PI-Hole client to request an instance of a PI-Hole ad blocker.
|
// Client struct is a PI-Hole client to request an instance of a PI-Hole ad blocker.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
httpClient http.Client
|
||||||
|
interval time.Duration
|
||||||
hostname string
|
hostname string
|
||||||
password string
|
password string
|
||||||
interval time.Duration
|
sessionID string
|
||||||
httpClient http.Client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient method initializes a new PI-Hole client.
|
// NewClient method initializes a new PI-Hole client.
|
||||||
|
@ -46,13 +47,11 @@ func NewClient(hostname, password string, interval time.Duration) *Client {
|
||||||
// and then pass them as Prometheus metrics.
|
// and then pass them as Prometheus metrics.
|
||||||
func (c *Client) Scrape() {
|
func (c *Client) Scrape() {
|
||||||
for range time.Tick(c.interval) {
|
for range time.Tick(c.interval) {
|
||||||
sessionID := c.getPHPSessionID()
|
if c.isAuthenticated() {
|
||||||
if sessionID == nil {
|
c.sessionID = c.getPHPSessionID()
|
||||||
log.Println("Unable to retrieve session identifier")
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stats := c.getStatistics(*sessionID)
|
stats := c.getStatistics()
|
||||||
c.setMetrics(stats)
|
c.setMetrics(stats)
|
||||||
|
|
||||||
log.Printf("New tick of statistics: %s", stats.ToString())
|
log.Printf("New tick of statistics: %s", stats.ToString())
|
||||||
|
@ -103,9 +102,7 @@ func (c *Client) setMetrics(stats *Stats) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getPHPSessionID() *string {
|
func (c *Client) getPHPSessionID() (sessionID string) {
|
||||||
var sessionID string
|
|
||||||
|
|
||||||
loginURL := fmt.Sprintf(loginURLPattern, c.hostname)
|
loginURL := fmt.Sprintf(loginURLPattern, c.hostname)
|
||||||
values := url.Values{"pw": []string{c.password}}
|
values := url.Values{"pw": []string{c.password}}
|
||||||
|
|
||||||
|
@ -134,10 +131,10 @@ func (c *Client) getPHPSessionID() *string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &sessionID
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getStatistics(sessionID string) *Stats {
|
func (c *Client) getStatistics() *Stats {
|
||||||
var stats Stats
|
var stats Stats
|
||||||
|
|
||||||
statsURL := fmt.Sprintf(statsURLPattern, c.hostname)
|
statsURL := fmt.Sprintf(statsURLPattern, c.hostname)
|
||||||
|
@ -147,8 +144,9 @@ func (c *Client) getStatistics(sessionID string) *Stats {
|
||||||
log.Fatal("An error has occured when creating HTTP statistics request", err)
|
log.Fatal("An error has occured when creating HTTP statistics request", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cookie := http.Cookie{Name: "PHPSESSID", Value: sessionID}
|
if c.isAuthenticated() {
|
||||||
req.AddCookie(&cookie)
|
c.authenticateRequest(req)
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -167,3 +165,12 @@ func (c *Client) getStatistics(sessionID string) *Stats {
|
||||||
|
|
||||||
return &stats
|
return &stats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) isAuthenticated() bool {
|
||||||
|
return len(c.password) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) authenticateRequest(req *http.Request) {
|
||||||
|
cookie := http.Cookie{Name: "PHPSESSID", Value: c.sessionID}
|
||||||
|
req.AddCookie(&cookie)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue