chore: dscp support range too

This commit is contained in:
wwqgtxx 2024-01-20 11:00:29 +08:00
parent 0e1bdb07d4
commit c1f0ed18ef

View File

@ -2,14 +2,14 @@ package common
import ( import (
"fmt" "fmt"
"strconv"
"github.com/metacubex/mihomo/common/utils"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
) )
type DSCP struct { type DSCP struct {
*Base *Base
dscp uint8 ranges utils.IntRanges[uint8]
payload string payload string
adapter string adapter string
} }
@ -19,7 +19,7 @@ func (d *DSCP) RuleType() C.RuleType {
} }
func (d *DSCP) Match(metadata *C.Metadata) (bool, string) { func (d *DSCP) Match(metadata *C.Metadata) (bool, string) {
return metadata.DSCP == d.dscp, d.adapter return d.ranges.Check(metadata.DSCP), d.adapter
} }
func (d *DSCP) Adapter() string { func (d *DSCP) Adapter() string {
@ -31,17 +31,19 @@ func (d *DSCP) Payload() string {
} }
func NewDSCP(dscp string, adapter string) (*DSCP, error) { func NewDSCP(dscp string, adapter string) (*DSCP, error) {
dscpi, err := strconv.Atoi(dscp) ranges, err := utils.NewUnsignedRanges[uint8](dscp)
if err != nil { if err != nil {
return nil, fmt.Errorf("parse DSCP rule fail: %w", err) return nil, fmt.Errorf("parse DSCP rule fail: %w", err)
} }
if dscpi < 0 || dscpi > 63 { for _, r := range ranges {
if r.End() > 63 {
return nil, fmt.Errorf("DSCP couldn't be negative or exceed 63") return nil, fmt.Errorf("DSCP couldn't be negative or exceed 63")
} }
}
return &DSCP{ return &DSCP{
Base: &Base{}, Base: &Base{},
payload: dscp, payload: dscp,
dscp: uint8(dscpi), ranges: ranges,
adapter: adapter, adapter: adapter,
}, nil }, nil
} }