dns, ipset, rule: change dns AnswerHandler to use netip instead of string

This commit is contained in:
nadoo committed 2022-02-03 00:04:17 +08:00
1 parent fdb32370e9
commit 7f925fb711
8 files changed
+53 -48

No files matched your search

+10 -7
View File
@@ -1,7 +1,7 @@
package ipset
import (
"errors"
"net/netip"
"strings"
"sync"
@@ -22,6 +22,13 @@ func addToSet(s, item string) error {
return ipset.Add(s, item)
}
func addAddrToSet(s string, ip netip.Addr) error {
if ip.Is4() {
return ipset.AddAddr(s, ip)
}
return ipset.AddAddr(s+"6", ip)
}
// NewManager returns a Manager
func NewManager(rules []*rule.Config) (*Manager, error) {
if err := ipset.Init(); err != nil {
@@ -59,16 +66,12 @@ func NewManager(rules []*rule.Config) (*Manager, error) {
}
// AddDomainIP implements the dns AnswerHandler function, used to update ipset according to domainSet rule.
func (m *Manager) AddDomainIP(domain, ip string) error {
if domain == "" || ip == "" {
return errors.New("please specify the domain and ip address")
}
func (m *Manager) AddDomainIP(domain string, ip netip.Addr) error {
domain = strings.ToLower(domain)
for i := len(domain); i != -1; {
i = strings.LastIndexByte(domain[:i], '.')
if setName, ok := m.domainSet.Load(domain[i+1:]); ok {
addToSet(setName.(string), ip)
addAddrToSet(setName.(string), ip)
}
}
return nil
+2 -1
View File
@@ -5,6 +5,7 @@ package ipset
import (
"errors"
"net/netip"
"github.com/nadoo/glider/rule"
)
@@ -18,6 +19,6 @@ func NewManager(rules []*rule.Config) (*Manager, error) {
}
// AddDomainIP implements the DNSAnswerHandler function
func (m *Manager) AddDomainIP(domain, ip string) error {
func (m *Manager) AddDomainIP(domain string, ip netip.Addr) error {
return errors.New("ipset not supported on this os")
}