summaryrefslogtreecommitdiff
path: root/internal/config/config.go
blob: 988315a7f91f032403ec8557f3226f8ddcf3b9bd (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
package config

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

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

type Config struct {
	StateDir        string
	Address         string
	Nodes           []string
	LoopIntervalS   int64  `json:"LoopIntervalS,omitempty"`
	MyID            string `json:"MyID,omitempty"`
	RelaxedMode     bool   `json:"RelaxedMode,omitempty"`
	nodeNumberCache map[string]int
	EmailTo         string `json:"EmailTo,omitempty"`
	EmailFrom       string `json:"EmailFrom,omitempty"`
	SMTPServer      string `json:"SMTPServer,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
	}

	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 ...string) func(*Config) {
	return func(conf *Config) {
		conf.Nodes = append(conf.Nodes, nodes...)
	}
}

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.LoopIntervalS == 0 {
		conf.LoopIntervalS = 10
	}

	if conf.MyID == "" {
		hostname, err := os.Hostname()
		if err != nil {
			return conf, err
		}
		conf.MyID = hostname
	}

	conf.nodeNumberCache = make(map[string]int, len(conf.Nodes))
	for i, node := range conf.Nodes {
		conf.nodeNumberCache[utils.StripPort(node)] = i
	}

	return conf, nil
}

func (conf Config) NodeNumber(node string) int {
	node = utils.StripPort(node)
	nodeNumber, ok := conf.nodeNumberCache[node]
	if ok {
		return nodeNumber
	}

	log.Println("config:",
		fmt.Errorf("node %s not found - it will affect it's score!", node))
	return 0
}

func (conf Config) IsNode(remoteAddr string) bool {
	remoteAddr = utils.StripPort(remoteAddr)

	for _, node := range conf.Nodes {
		if remoteAddr == utils.StripPort(node) {
			return true
		}
	}

	return false
}

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

	remoteAddr = utils.StripPort(remoteAddr)

	for _, node := range conf.Nodes {
		ips, err := lookupIP(utils.StripPort(node))
		if err != nil {
			log.Println("config:", err)
			continue
		}

		for _, ip := range ips {
			if remoteAddr == ip.String() {
				return true
			}
		}
	}

	return false
}