refactor IPSet
This commit is contained in:
parent
066eeb0ab7
commit
5fd28ae005
10
group.go
10
group.go
@ -21,17 +21,17 @@ type Group struct {
|
||||
ipsetToLink *netfilterHelper.IPSetToLink
|
||||
}
|
||||
|
||||
func (g *Group) AddIPv4(address net.IP, ttl time.Duration) error {
|
||||
func (g *Group) AddIP(address net.IP, ttl time.Duration) error {
|
||||
ttlSeconds := uint32(ttl.Seconds())
|
||||
return g.ipset.AddIP(address, &ttlSeconds)
|
||||
}
|
||||
|
||||
func (g *Group) DelIPv4(address net.IP) error {
|
||||
return g.ipset.Del(address)
|
||||
func (g *Group) DelIP(address net.IP) error {
|
||||
return g.ipset.DelIP(address)
|
||||
}
|
||||
|
||||
func (g *Group) ListIPv4() (map[string]*uint32, error) {
|
||||
return g.ipset.List()
|
||||
func (g *Group) ListIP() (map[string]*uint32, error) {
|
||||
return g.ipset.ListIPs()
|
||||
}
|
||||
|
||||
func (g *Group) Enable() error {
|
||||
|
10
kvas2.go
10
kvas2.go
@ -326,7 +326,7 @@ func (a *App) SyncGroup(group *Group) error {
|
||||
}
|
||||
}
|
||||
|
||||
currentAddresses, err := group.ListIPv4()
|
||||
currentAddresses, err := group.ListIP()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get old ipset list: %w", err)
|
||||
}
|
||||
@ -337,7 +337,7 @@ func (a *App) SyncGroup(group *Group) error {
|
||||
continue
|
||||
}
|
||||
ip := net.IP(addr)
|
||||
err = group.AddIPv4(ip, ttl)
|
||||
err = group.AddIP(ip, ttl)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Str("address", ip.String()).
|
||||
@ -356,7 +356,7 @@ func (a *App) SyncGroup(group *Group) error {
|
||||
continue
|
||||
}
|
||||
ip := net.IP(addr)
|
||||
err = group.DelIPv4(ip)
|
||||
err = group.DelIP(ip)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Str("address", ip.String()).
|
||||
@ -418,7 +418,7 @@ func (a *App) processARecord(aRecord dns.A) {
|
||||
continue
|
||||
}
|
||||
// TODO: Check already existed
|
||||
err := group.AddIPv4(aRecord.A, ttlDuration)
|
||||
err := group.AddIP(aRecord.A, ttlDuration)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Str("address", aRecord.A.String()).
|
||||
@ -467,7 +467,7 @@ func (a *App) processCNameRecord(cNameRecord dns.CNAME) {
|
||||
continue
|
||||
}
|
||||
for _, aRecord := range aRecords {
|
||||
err := group.AddIPv4(aRecord.Address, now.Sub(aRecord.Deadline))
|
||||
err := group.AddIP(aRecord.Address, now.Sub(aRecord.Deadline))
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Str("address", aRecord.Address.String()).
|
||||
|
@ -2,9 +2,10 @@ package netfilterHelper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/vishvananda/netlink"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
)
|
||||
|
||||
type IPSet struct {
|
||||
@ -23,7 +24,7 @@ func (r *IPSet) AddIP(addr net.IP, timeout *uint32) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *IPSet) Del(addr net.IP) error {
|
||||
func (r *IPSet) DelIP(addr net.IP) error {
|
||||
err := netlink.IpsetDel(r.SetName, &netlink.IPSetEntry{
|
||||
IP: addr,
|
||||
})
|
||||
@ -33,7 +34,7 @@ func (r *IPSet) Del(addr net.IP) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *IPSet) List() (map[string]*uint32, error) {
|
||||
func (r *IPSet) ListIPs() (map[string]*uint32, error) {
|
||||
list, err := netlink.IpsetList(r.SetName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -62,9 +63,8 @@ func (nh *NetfilterHelper) IPSet(name string) (*IPSet, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defaultTimeout := uint32(300)
|
||||
err = netlink.IpsetCreate(ipset.SetName, "hash:net", netlink.IpsetCreateOptions{
|
||||
Timeout: &defaultTimeout,
|
||||
Timeout: func(i uint32) *uint32 { return &i }(300),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create ipset: %w", err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user