diff --git a/group.go b/group.go index 5e40932..8db90fb 100644 --- a/group.go +++ b/group.go @@ -4,7 +4,9 @@ import ( "errors" "fmt" "github.com/nadoo/ipset" + "net" "strconv" + "time" "kvas2-go/models" "kvas2-go/pkg/ip-helper" @@ -22,6 +24,32 @@ type Group struct { options GroupOptions } +func (g *Group) HandleIPv4(names []string, address net.IP, ttl time.Duration) error { + if !g.options.Enabled { + return nil + } + +DomainSearch: + for _, domain := range g.Domains { + if !domain.IsEnabled() { + continue + } + for _, name := range names { + if domain.IsMatch(name) { + // TODO: Looks like I need patch this module :\ + //err := ipset.Add(g.ipsetName, address.String(), ipset.OptTimeout(uint32(ttl.Seconds()))) + err := ipset.Add(g.ipsetName, address.String()) + if err != nil { + return fmt.Errorf("failed to assign address %s with %s ipset", address, g.ipsetName) + } + break DomainSearch + } + } + } + + return nil +} + func (g *Group) Enable() error { if g.options.Enabled { return nil diff --git a/kvas2.go b/kvas2.go index 1b77898..5e646f0 100644 --- a/kvas2.go +++ b/kvas2.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "log" "net" "sync" "time" @@ -158,17 +159,12 @@ func (a *App) processARecord(aRecord dnsProxy.Address) { a.Records.PutARecord(aRecord.Name.String(), aRecord.Address, ttlDuration) - cNames := append([]string{aRecord.Name.String()}, a.Records.GetCNameRecords(aRecord.Name.String(), true, true)...) + names := append([]string{aRecord.Name.String()}, a.Records.GetCNameRecords(aRecord.Name.String(), true, true)...) for _, group := range a.Groups { - for _, domain := range group.Domains { - if !domain.IsEnabled() { - continue - } - for _, cName := range cNames { - if domain.IsMatch(cName) { - fmt.Printf("Matched %s (%s) for %s in %s group!\n", cName, aRecord.Name, domain.Domain, group.Name) - } - } + err := group.HandleIPv4(names, aRecord.Address, ttlDuration) + if err != nil { + // TODO: Error log level + log.Printf("failed to handle address: %v", err) } } }