ipset: avoid adding twice

This commit is contained in:
nadoo 2018-01-10 13:11:13 +08:00
parent bebfa0e48e
commit e91d4bcb6e
2 changed files with 7 additions and 8 deletions

8
dns.go
View File

@ -270,7 +270,7 @@ func (s *DNS) ServeTCP(c net.Conn) {
// Exchange handles request msg and return response msg
func (s *DNS) Exchange(reqLen uint16, reqMsg []byte) (respLen uint16, respMsg []byte) {
// fmt.Printf("dns req len %d:\n%s\n\n", reqLen, hex.Dump(reqMsg[:]))
// fmt.Printf("\ndns req len %d:\n%s\n", reqLen, hex.Dump(reqMsg[:]))
query, err := parseQuestion(reqMsg)
if err != nil {
logf("proxy-dns error in parseQuestion reqMsg %s", err)
@ -310,7 +310,7 @@ func (s *DNS) Exchange(reqLen uint16, reqMsg []byte) (respLen uint16, respMsg []
return
}
// fmt.Printf("dns resp len %d:\n%s\n\n", respLen, hex.Dump(respMsg[:]))
// fmt.Printf("\ndns resp len %d:\n%s\n", respLen, hex.Dump(respMsg[:]))
var ip string
if respLen > 0 {
@ -380,7 +380,7 @@ func parseQuestion(p []byte) (*DNSQuestion, error) {
}
if lenP <= i+l+1 {
return nil, errors.New("parseQuestion error, not enough data for QNAME")
return nil, errors.New("not enough data for QNAME")
}
domain = append(domain, p[i+1:i+l+1]...)
@ -392,7 +392,7 @@ func parseQuestion(p []byte) (*DNSQuestion, error) {
q.QNAME = string(domain[:len(domain)-1])
if len(p) < i+4 {
return nil, errors.New("parseQuestion error, not enough data")
return nil, errors.New("not enough data")
}
q.QTYPE = binary.BigEndian.Uint16(p[i:])

View File

@ -83,11 +83,9 @@ func NewIPSetManager(mainSet string, rules []*RuleConf) (*IPSetManager, error) {
}
m := &IPSetManager{fd: fd, lsa: lsa, mainSet: mainSet}
CreateSet(fd, lsa, mainSet)
for _, r := range rules {
set := r.IPSet
if set != "" && set != m.mainSet {
@ -117,7 +115,6 @@ func NewIPSetManager(mainSet string, rules []*RuleConf) (*IPSetManager, error) {
// AddDomainIP implements the DNSAnswerHandler function, used to update ipset according to domainSet rule
func (m *IPSetManager) AddDomainIP(domain, ip string) error {
if ip != "" {
domainParts := strings.Split(domain, ".")
length := len(domainParts)
@ -127,7 +124,9 @@ func (m *IPSetManager) AddDomainIP(domain, ip string) error {
// find in domainMap
if ipset, ok := m.domainSet.Load(domain); ok {
AddToSet(m.fd, m.lsa, m.mainSet, ip)
AddToSet(m.fd, m.lsa, ipset.(string), ip)
if ipset.(string) != m.mainSet {
AddToSet(m.fd, m.lsa, ipset.(string), ip)
}
}
}