mirror of
				https://github.com/nadoo/glider.git
				synced 2025-11-04 07:42:38 +08:00 
			
		
		
		
	ipset: avoid adding twice
This commit is contained in:
		
							parent
							
								
									7c30f1001a
								
							
						
					
					
						commit
						0e9f6721b4
					
				
							
								
								
									
										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
 | 
			
		||||
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:])
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user