summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/unbound/config/config.go
blob: 69dc5c219237cb98c236ee9d3d8f7d7791b624ff (plain)
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 config

import (
	"fmt"
	"strings"
)

// UnboundConfig represents Unbound configuration file.
type UnboundConfig struct {
	cumulative string // statistics-cumulative
	enable     string // control-enable
	iface      string // control-interface
	port       string // control-port
	useCert    string // control-use-cert
	keyFile    string // control-key-file
	certFile   string // control-cert-file
}

func (c UnboundConfig) String() string {
	format := strings.Join([]string{
		"[",
		`"statistics-cumulative": '%s', `,
		`"control-enable": '%s', `,
		`"control-interface": '%s', `,
		`"control-port": '%s', `,
		`"control-user-cert": '%s', `,
		`"control-key-file": '%s', `,
		`"control-cert-file": '%s'`,
		"]",
	}, "")
	return fmt.Sprintf(format, c.cumulative, c.enable, c.iface, c.port, c.useCert, c.keyFile, c.certFile)
}

func (c UnboundConfig) Empty() bool                      { return c == UnboundConfig{} }
func (c UnboundConfig) Cumulative() (bool, bool)         { return c.cumulative == "yes", c.cumulative != "" }
func (c UnboundConfig) ControlEnabled() (bool, bool)     { return c.enable == "yes", c.enable != "" }
func (c UnboundConfig) ControlInterface() (string, bool) { return c.iface, c.iface != "" }
func (c UnboundConfig) ControlPort() (string, bool)      { return c.port, c.port != "" }
func (c UnboundConfig) ControlUseCert() (bool, bool)     { return c.useCert == "yes", c.useCert != "" }
func (c UnboundConfig) ControlKeyFile() (string, bool)   { return c.keyFile, c.keyFile != "" }
func (c UnboundConfig) ControlCertFile() (string, bool)  { return c.certFile, c.certFile != "" }

func fromOptions(options []option) *UnboundConfig {
	cfg := &UnboundConfig{}
	for _, opt := range options {
		switch opt.name {
		default:
		case optInterface:
			applyControlInterface(cfg, opt.value)
		case optCumulative:
			cfg.cumulative = opt.value
		case optEnable:
			cfg.enable = opt.value
		case optPort:
			cfg.port = opt.value
		case optUseCert:
			cfg.useCert = opt.value
		case optKeyFile:
			cfg.keyFile = opt.value
		case optCertFile:
			cfg.certFile = opt.value
		}
	}
	return cfg
}

// Unbound doesn't allow to query stats from unix socket when control-interface is enabled on ip interface.
func applyControlInterface(cfg *UnboundConfig, value string) {
	if cfg.iface == "" || !isUnixSocket(value) || isUnixSocket(cfg.iface) {
		cfg.iface = value
	}
}

func isUnixSocket(address string) bool {
	return strings.HasPrefix(address, "/")
}