diff options
| author | Paul Buetow <paul@buetow.org> | 2023-05-21 23:49:03 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-05-21 23:49:03 +0300 |
| commit | e958dc957a2339228bfe74865b4e1728484f4fd0 (patch) | |
| tree | 186bdf561be5a122e122f94106fd2d82aece1fe3 /internal/config | |
| parent | 6cd559398b5b1183c19ac0ab2b5840b02132489c (diff) | |
refactor config to separate package
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/config.go | 82 | ||||
| -rw-r--r-- | internal/config/config_test.go | 71 |
2 files changed, 153 insertions, 0 deletions
diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..1e2705c --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,82 @@ +package config + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net" + "os" + + "codeberg.org/snonux/gorum/internal/utils" +) + +type Config struct { + StateDir string + Address string + Participants []string +} + +func New(configFile string) (Config, error) { + var c Config + + file, err := os.Open(configFile) + if err != nil { + return c, err + } + defer file.Close() + + bytes, err := ioutil.ReadAll(file) + if err != nil { + return c, err + } + + err = json.Unmarshal(bytes, &c) + if err != nil { + return c, err + } + + return c, nil +} + +func (c Config) ParticipantNumber(participant string) (int, error) { + for i, participant_ := range c.Participants { + if participant == utils.StripPort(participant_) { + return i, nil + } + } + + return 0, fmt.Errorf("participant %s not found", participant) +} + +func (c Config) IsParticipant(remoteAddr string) bool { + remoteAddr = utils.StripPort(remoteAddr) + + for _, participant := range c.Participants { + if remoteAddr == utils.StripPort(participant) { + return true + } + } + + return false +} + +func (c Config) IsParticipantWithLookup(remoteAddr string, lookupIP func(string) ([]net.IP, error)) bool { + remoteAddr = utils.StripPort(remoteAddr) + + for _, participant := range c.Participants { + ips, err := lookupIP(utils.StripPort(participant)) + if err != nil { + log.Println(err) + continue + } + + for _, ip := range ips { + if remoteAddr == ip.String() { + return true + } + } + } + + return false +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000..bccfb2e --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,71 @@ +package config + +import ( + "fmt" + "net" + "testing" +) + +func TestParticipantNumber(t *testing.T) { + conf := Config{Participants: []string{"localhost:1234", "hamburger:4321"}} + + num, err := conf.ParticipantNumber("localhost") + if err != nil { + t.Errorf(err.Error()) + } + if num != 0 { + t.Errorf("localhost should be participant number 0 but is %d", num) + } + + num, err = conf.ParticipantNumber("hamburger") + if err != nil { + t.Errorf(err.Error()) + } + if num != 1 { + t.Errorf("hamburger should be participant number 1 but is %d", num) + } + + _, err = conf.ParticipantNumber("doener") + if err == nil { + t.Errorf("doener is not a participant") + } +} + +func TestIsParticipant(t *testing.T) { + conf := Config{Participants: []string{"localhost:1234", "hamburger:4321"}} + + remoteAddr := "localhost:323232" + if !conf.IsParticipant(remoteAddr) { + t.Errorf("%s should be participant of %v", remoteAddr, conf.Participants) + } + + remoteAddr = "foo.zone:2345" + if conf.IsParticipant(remoteAddr) { + t.Errorf("%s should not be participant of %v", remoteAddr, conf.Participants) + } +} + +func TestIsParticipantWithLookup(t *testing.T) { + conf := Config{Participants: []string{"localhost:1234", "hamburger:4321"}} + + lookupIP := func(addr string) ([]net.IP, error) { + switch addr { + case "localhost": + return []net.IP{{127, 0, 0, 1}}, nil + case "hamburger": + return []net.IP{{8, 8, 8, 8}}, nil + default: + return []net.IP{}, fmt.Errorf("Can't resolve %s", addr) + } + } + + remoteAddr := "127.0.0.1:323232" + if !conf.IsParticipantWithLookup(remoteAddr, lookupIP) { + t.Errorf("%s should be participant of %v", remoteAddr, conf.Participants) + } + + remoteAddr = "9.9.9.9:2345" + if conf.IsParticipantWithLookup(remoteAddr, lookupIP) { + t.Errorf("%s should not be participant of %v", remoteAddr, conf.Participants) + } +} |
