mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
ipset: avoid adding twice
This commit is contained in:
parent
bebfa0e48e
commit
e91d4bcb6e
8
dns.go
8
dns.go
@ -270,7 +270,7 @@ func (s *DNS) ServeTCP(c net.Conn) {
|
|||||||
|
|
||||||
// Exchange handles request msg and return response msg
|
// Exchange handles request msg and return response msg
|
||||||
func (s *DNS) Exchange(reqLen uint16, reqMsg []byte) (respLen uint16, respMsg []byte) {
|
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)
|
query, err := parseQuestion(reqMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("proxy-dns error in parseQuestion reqMsg %s", err)
|
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
|
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
|
var ip string
|
||||||
if respLen > 0 {
|
if respLen > 0 {
|
||||||
@ -380,7 +380,7 @@ func parseQuestion(p []byte) (*DNSQuestion, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if lenP <= i+l+1 {
|
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]...)
|
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])
|
q.QNAME = string(domain[:len(domain)-1])
|
||||||
|
|
||||||
if len(p) < i+4 {
|
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:])
|
q.QTYPE = binary.BigEndian.Uint16(p[i:])
|
||||||
|
@ -83,11 +83,9 @@ func NewIPSetManager(mainSet string, rules []*RuleConf) (*IPSetManager, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
m := &IPSetManager{fd: fd, lsa: lsa, mainSet: mainSet}
|
m := &IPSetManager{fd: fd, lsa: lsa, mainSet: mainSet}
|
||||||
|
|
||||||
CreateSet(fd, lsa, mainSet)
|
CreateSet(fd, lsa, mainSet)
|
||||||
|
|
||||||
for _, r := range rules {
|
for _, r := range rules {
|
||||||
|
|
||||||
set := r.IPSet
|
set := r.IPSet
|
||||||
|
|
||||||
if set != "" && set != m.mainSet {
|
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
|
// AddDomainIP implements the DNSAnswerHandler function, used to update ipset according to domainSet rule
|
||||||
func (m *IPSetManager) AddDomainIP(domain, ip string) error {
|
func (m *IPSetManager) AddDomainIP(domain, ip string) error {
|
||||||
|
|
||||||
if ip != "" {
|
if ip != "" {
|
||||||
domainParts := strings.Split(domain, ".")
|
domainParts := strings.Split(domain, ".")
|
||||||
length := len(domainParts)
|
length := len(domainParts)
|
||||||
@ -127,7 +124,9 @@ func (m *IPSetManager) AddDomainIP(domain, ip string) error {
|
|||||||
// find in domainMap
|
// find in domainMap
|
||||||
if ipset, ok := m.domainSet.Load(domain); ok {
|
if ipset, ok := m.domainSet.Load(domain); ok {
|
||||||
AddToSet(m.fd, m.lsa, m.mainSet, ip)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user