github.com/metacubex/mihomo@v1.18.5/rules/common/dscp.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/metacubex/mihomo/common/utils"
     7  	C "github.com/metacubex/mihomo/constant"
     8  )
     9  
    10  type DSCP struct {
    11  	*Base
    12  	ranges  utils.IntRanges[uint8]
    13  	payload string
    14  	adapter string
    15  }
    16  
    17  func (d *DSCP) RuleType() C.RuleType {
    18  	return C.DSCP
    19  }
    20  
    21  func (d *DSCP) Match(metadata *C.Metadata) (bool, string) {
    22  	return d.ranges.Check(metadata.DSCP), d.adapter
    23  }
    24  
    25  func (d *DSCP) Adapter() string {
    26  	return d.adapter
    27  }
    28  
    29  func (d *DSCP) Payload() string {
    30  	return d.payload
    31  }
    32  
    33  func NewDSCP(dscp string, adapter string) (*DSCP, error) {
    34  	ranges, err := utils.NewUnsignedRanges[uint8](dscp)
    35  	if err != nil {
    36  		return nil, fmt.Errorf("parse DSCP rule fail: %w", err)
    37  	}
    38  	for _, r := range ranges {
    39  		if r.End() > 63 {
    40  			return nil, fmt.Errorf("DSCP couldn't be negative or exceed 63")
    41  		}
    42  	}
    43  	return &DSCP{
    44  		Base:    &Base{},
    45  		payload: dscp,
    46  		ranges:  ranges,
    47  		adapter: adapter,
    48  	}, nil
    49  }