put ip to ipset

This commit is contained in:
Vladimir Avtsenov 2024-08-30 05:27:52 +03:00
parent d940897a55
commit a77906410c
2 changed files with 34 additions and 10 deletions

View File

@ -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

View File

@ -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)
}
}
}