allows user to specify a custom admin port

This commit is contained in:
Hugo Tavares Reis 2020-07-27 15:03:58 +02:00
parent a9316b28cc
commit 5e40a146ed
4 changed files with 14 additions and 8 deletions

View file

@ -17,6 +17,7 @@ import (
type Config struct { type Config struct {
PIHoleProtocol string `config:"pihole_protocol"` PIHoleProtocol string `config:"pihole_protocol"`
PIHoleHostname string `config:"pihole_hostname"` PIHoleHostname string `config:"pihole_hostname"`
PIHolePort uint16 `config:"pihole_port"`
PIHolePassword string `config:"pihole_password"` PIHolePassword string `config:"pihole_password"`
PIHoleApiToken string `config:"pihole_api_token"` PIHoleApiToken string `config:"pihole_api_token"`
Port string `config:"port"` Port string `config:"port"`
@ -27,6 +28,7 @@ func getDefaultConfig() *Config {
return &Config{ return &Config{
PIHoleProtocol: "http", PIHoleProtocol: "http",
PIHoleHostname: "127.0.0.1", PIHoleHostname: "127.0.0.1",
PIHolePort: 80,
PIHolePassword: "", PIHolePassword: "",
PIHoleApiToken: "", PIHoleApiToken: "",
Port: "9617", Port: "9617",

2
go.sum
View file

@ -70,6 +70,7 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
@ -262,6 +263,7 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw= golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

View file

@ -16,8 +16,8 @@ import (
) )
var ( var (
loginURLPattern = "%s://%s/admin/index.php?login" loginURLPattern = "%s://%s:%d/admin/index.php?login"
statsURLPattern = "%s://%s/admin/api.php?summaryRaw&overTimeData&topItems&recentItems&getQueryTypes&getForwardDestinations&getQuerySources&jsonForceObject" statsURLPattern = "%s://%s:%d/admin/api.php?summaryRaw&overTimeData&topItems&recentItems&getQueryTypes&getForwardDestinations&getQuerySources&jsonForceObject"
) )
// 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.
@ -26,13 +26,14 @@ type Client struct {
interval time.Duration interval time.Duration
protocol string protocol string
hostname string hostname string
port uint16
password string password string
sessionID string sessionID string
apiToken string apiToken string
} }
// NewClient method initializes a new PI-Hole client. // NewClient method initializes a new PI-Hole client.
func NewClient(protocol, hostname, password, apiToken string, interval time.Duration) *Client { func NewClient(protocol, hostname string, port uint16, password, apiToken string, interval time.Duration) *Client {
if protocol != "http" && protocol != "https" { if protocol != "http" && protocol != "https" {
log.Printf("protocol %s is invalid. Must be http or https.", protocol) log.Printf("protocol %s is invalid. Must be http or https.", protocol)
os.Exit(1) os.Exit(1)
@ -41,6 +42,7 @@ func NewClient(protocol, hostname, password, apiToken string, interval time.Dura
return &Client{ return &Client{
protocol: protocol, protocol: protocol,
hostname: hostname, hostname: hostname,
port: port,
password: password, password: password,
apiToken: apiToken, apiToken: apiToken,
interval: interval, interval: interval,
@ -109,7 +111,7 @@ func (c *Client) setMetrics(stats *Stats) {
} }
func (c *Client) getPHPSessionID() (sessionID string) { func (c *Client) getPHPSessionID() (sessionID string) {
loginURL := fmt.Sprintf(loginURLPattern, c.protocol, c.hostname) loginURL := fmt.Sprintf(loginURLPattern, c.protocol, c.hostname, c.port)
values := url.Values{"pw": []string{c.password}} values := url.Values{"pw": []string{c.password}}
req, err := http.NewRequest("POST", loginURL, strings.NewReader(values.Encode())) req, err := http.NewRequest("POST", loginURL, strings.NewReader(values.Encode()))
@ -138,7 +140,7 @@ func (c *Client) getPHPSessionID() (sessionID string) {
func (c *Client) getStatistics() *Stats { func (c *Client) getStatistics() *Stats {
var stats Stats var stats Stats
statsURL := fmt.Sprintf(statsURLPattern, c.protocol, c.hostname) statsURL := fmt.Sprintf(statsURLPattern, c.protocol, c.hostname, c.port)
if c.isUsingApiToken() { if c.isUsingApiToken() {
statsURL = fmt.Sprintf("%s&auth=%s", statsURL, c.apiToken) statsURL = fmt.Sprintf("%s&auth=%s", statsURL, c.apiToken)

View file

@ -26,14 +26,14 @@ func main() {
metrics.Init() metrics.Init()
initPiHoleClient(conf.PIHoleProtocol, conf.PIHoleHostname, conf.PIHolePassword, conf.PIHoleApiToken, conf.Interval) initPiHoleClient(conf.PIHoleProtocol, conf.PIHoleHostname, conf.PIHolePort, conf.PIHolePassword, conf.PIHoleApiToken, conf.Interval)
initHttpServer(conf.Port) initHttpServer(conf.Port)
handleExitSignal() handleExitSignal()
} }
func initPiHoleClient(protocol, hostname, password, apiToken string, interval time.Duration) { func initPiHoleClient(protocol, hostname string, port uint16, password, apiToken string, interval time.Duration) {
client := pihole.NewClient(protocol, hostname, password, apiToken, interval) client := pihole.NewClient(protocol, hostname, port, password, apiToken, interval)
go client.Scrape() go client.Scrape()
} }