github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/route/mange.go (about)

     1  package route
     2  
     3  import (
     4  	"errors"
     5  	"github.com/inazumav/sing-box/adapter"
     6  	"github.com/inazumav/sing-box/option"
     7  	E "github.com/sagernet/sing/common/exceptions"
     8  )
     9  
    10  func (r *Router) AddInbound(inbound adapter.Inbound) error {
    11  	r.actionLock.Lock()
    12  	defer r.actionLock.Unlock()
    13  	if _, ok := r.inboundByTag[inbound.Tag()]; ok {
    14  		return errors.New("the inbound is exist")
    15  	}
    16  	r.inboundByTag[inbound.Tag()] = inbound
    17  	return nil
    18  }
    19  
    20  func (r *Router) DelInbound(tag string) error {
    21  	r.actionLock.Lock()
    22  	defer r.actionLock.Unlock()
    23  	if _, ok := r.inboundByTag[tag]; ok {
    24  		delete(r.inboundByTag, tag)
    25  	} else {
    26  		return errors.New("the inbound not have")
    27  	}
    28  	return nil
    29  }
    30  
    31  func (r *Router) UpdateDnsRules(rules []option.DNSRule) error {
    32  	dnsRules := make([]adapter.DNSRule, 0, len(rules))
    33  	for i, rule := range rules {
    34  		dnsRule, err := NewDNSRule(r, r.logger, rule)
    35  		if err != nil {
    36  			return E.Cause(err, "parse dns rule[", i, "]")
    37  		}
    38  		err = dnsRule.Start()
    39  		if err != nil {
    40  			return E.Cause(err, "initialize DNS rule[", i, "]")
    41  		}
    42  		dnsRules = append(dnsRules, dnsRule)
    43  	}
    44  	var tempRules []adapter.DNSRule
    45  	r.actionLock.Lock()
    46  	r.dnsRules = tempRules
    47  	r.dnsRules = dnsRules
    48  	r.actionLock.Unlock()
    49  	for i, rule := range tempRules {
    50  		err := rule.Close()
    51  		if err != nil {
    52  			return E.Cause(err, "closing DNS rule[", i, "]")
    53  		}
    54  	}
    55  	return nil
    56  }