1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
// SPDX-License-Identifier: GPL-3.0-or-later
package pihole
import (
"bufio"
"errors"
"fmt"
"net/http"
"os"
"strings"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
)
func (p *Pihole) validateConfig() error {
if p.URL == "" {
return errors.New("url not set")
}
return nil
}
func (p *Pihole) initHTTPClient() (*http.Client, error) {
return web.NewHTTPClient(p.Client)
}
func (p *Pihole) getWebPassword() string {
// do no read setupVarsPath is password is set in the configuration file
if p.Password != "" {
return p.Password
}
if !isLocalHost(p.URL) {
p.Info("abort web password auto detection, host is not localhost")
return ""
}
p.Infof("starting web password auto detection, reading : %s", p.SetupVarsPath)
pass, err := getWebPassword(p.SetupVarsPath)
if err != nil {
p.Warningf("error during reading '%s' : %v", p.SetupVarsPath, err)
}
return pass
}
func getWebPassword(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer func() { _ = f.Close() }()
s := bufio.NewScanner(f)
var password string
for s.Scan() && password == "" {
if strings.HasPrefix(s.Text(), "WEBPASSWORD") {
parts := strings.Split(s.Text(), "=")
if len(parts) != 2 {
return "", fmt.Errorf("unparsable line : %s", s.Text())
}
password = parts[1]
}
}
return password, nil
}
func isLocalHost(u string) bool {
if strings.Contains(u, "127.0.0.1") {
return true
}
if strings.Contains(u, "localhost") {
return true
}
return false
}
|