summaryrefslogtreecommitdiff
path: root/internal/config/config.go
blob: 2753ac9a363957f77cffd0bc6c09822c934ade0e (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package config

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net"
	"os"

	"codeberg.org/snonux/gorum/internal/utils"
)

type Node struct {
	Port       int
	Priority   int
	Alias      string `json:",omitempty"`
	Hostname   string
	originalId string
}

func (n Node) Address() string {
	return fmt.Sprintf("%s:%d", n.Hostname, n.Port)
}

type Config struct {
	StateDir      string
	LogToSyslog   bool `json:"LogToSyslog,omitempty"`
	ScoreFile     string
	Address       string
	Nodes         map[string]Node
	LoopIntervalS int64  `json:"LoopIntervalS,omitempty"`
	MyID          string `json:"MyID,omitempty"`
	RelaxedMode   bool   `json:"RelaxedMode,omitempty"`
	EmailTo       string `json:"EmailTo,omitempty"`
	EmailFrom     string `json:"EmailFrom,omitempty"`
	SMTPServer    string `json:"SMTPServer,omitempty"`
	MailThrottle  int    `json:"MailThrottle,omitempty"`
}

func NewFromConfigFile(configFile string) (Config, error) {
	var conf Config

	file, err := os.Open(configFile)
	if err != nil {
		return conf, err
	}
	defer file.Close()

	bytes, err := io.ReadAll(file)
	if err != nil {
		return conf, err
	}

	err = json.Unmarshal(bytes, &conf)
	if err != nil {
		return conf, err
	}

	// Make it so that the key is the Hostname for internal lookup.
	newNodes := make(map[string]Node, len(conf.Nodes))
	for id, node := range conf.Nodes {
		log.Printf("adding node %s: %v", id, node)
		node.originalId = id
		if node.Alias != "" {
			newNodes[node.Alias] = node
			continue
		}
		newNodes[node.Hostname] = node
	}
	conf.Nodes = newNodes

	if conf.SMTPServer == "" {
		hostname, err := os.Hostname()
		if err != nil {
			log.Fatal(err)
		}
		conf.SMTPServer = fmt.Sprintf("%s:25", hostname)
		log.Println("Set SMTPServer to " + conf.SMTPServer)
	}

	if conf.MailThrottle == 0 {
		// By default, send out an E-Mail once every minute max. This
		// is to avoid E-Mail bursts.
		conf.MailThrottle = 60
	}

	return conf.setDefaults()
}

func New(options ...func(*Config)) (Config, error) {
	var conf Config

	for _, opt := range options {
		opt(&conf)
	}

	return conf.setDefaults()
}

func WithStateDir(stateDir string) func(*Config) {
	return func(conf *Config) {
		conf.StateDir = stateDir
	}
}

func WithAddress(address string) func(*Config) {
	return func(conf *Config) {
		conf.Address = address
	}
}

func WithNodes(nodes ...Node) func(*Config) {
	return func(conf *Config) {
		if conf.Nodes == nil {
			conf.Nodes = make(map[string]Node)
		}
		for _, node := range nodes {
			conf.Nodes[node.Hostname] = node
		}
	}
}

func WithMyID(id string) func(*Config) {
	return func(conf *Config) {
		conf.MyID = id
	}
}

func WithRelaxedMode() func(*Config) {
	return func(conf *Config) {
		conf.RelaxedMode = true
	}
}

func (conf Config) setDefaults() (Config, error) {
	if conf.ScoreFile == "" {
		conf.ScoreFile = "scores"
	}
	if conf.LoopIntervalS == 0 {
		conf.LoopIntervalS = 10
	}
	if conf.MyID == "" {
		hostname, err := os.Hostname()
		if err != nil {
			return conf, err
		}
		conf.MyID = hostname
	}

	return conf, nil
}

func (conf Config) NodePriority(id string) (int, error) {
	node, err := conf.findNode(id)
	if err != nil {
		return 0, err
	}
	return node.Priority, nil
}

func (conf Config) IsNode(remoteAddr string) bool {
	_, err := conf.findNode(utils.StripPort(remoteAddr))
	return err == nil
}

func (conf Config) IsNodeWithLookup(remoteAddr string,
	lookupIP func(string) ([]net.IP, error)) bool {

	remoteAddr = utils.StripPort(remoteAddr)

	compare := func(hostname string) bool {
		ips, err := lookupIP(hostname)
		if err != nil {
			log.Println("config:", err)
			return false
		}
		for _, ip := range ips {
			if remoteAddr == ip.String() {
				return true
			}
		}
		return false
	}

	for _, node := range conf.Nodes {
		if compare(node.Hostname) || compare(node.Alias) {
			return true
		}
	}
	return false
}

func (conf Config) findNode(hostname string) (Node, error) {
	if node, ok := conf.Nodes[hostname]; ok {
		return node, nil
	}

	for _, node := range conf.Nodes {
		if hostname == node.Alias {
			return node, nil
		}
	}

	return Node{}, fmt.Errorf("node %s not found in %v", hostname, conf.Nodes)
}

func (conf Config) EmailNotifycationEnabled() bool {
	return conf.EmailFrom != "" && conf.EmailTo != "" && conf.SMTPServer != ""
}