feat: add DSCP rule for Tproxy UDP packets (#996)

* feat: add `DSCP` rule for Tproxy UDP packets

* fix: fix compatibility issue with non_linux platform

* chore: remove redundant lines for DSCP
This commit is contained in:
pretze
2024-01-20 10:19:42 +08:00
committed by GitHub
parent 90ea6ab278
commit 25d6ad220d
9 changed files with 102 additions and 1 deletions

47
rules/common/dscp.go Normal file
View File

@@ -0,0 +1,47 @@
package common
import (
"fmt"
"strconv"
C "github.com/metacubex/mihomo/constant"
)
type DSCP struct {
*Base
dscp uint8
payload string
adapter string
}
func (d *DSCP) RuleType() C.RuleType {
return C.DSCP
}
func (d *DSCP) Match(metadata *C.Metadata) (bool, string) {
return metadata.DSCP == d.dscp, d.adapter
}
func (d *DSCP) Adapter() string {
return d.adapter
}
func (d *DSCP) Payload() string {
return d.payload
}
func NewDSCP(dscp string, adapter string) (*DSCP, error) {
dscpi, err := strconv.Atoi(dscp)
if err != nil {
return nil, fmt.Errorf("parse DSCP rule fail: %w", err)
}
if dscpi < 0 || dscpi > 63 {
return nil, fmt.Errorf("DSCP couldn't be negative or exceed 63")
}
return &DSCP{
Base: &Base{},
payload: dscp,
dscp: uint8(dscpi),
adapter: adapter,
}, nil
}

View File

@@ -38,6 +38,8 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
parsed, parseErr = RC.NewPort(payload, target, C.DstPort)
case "IN-PORT":
parsed, parseErr = RC.NewPort(payload, target, C.InPort)
case "DSCP":
parsed, parseErr = RC.NewDSCP(payload, target)
case "PROCESS-NAME":
parsed, parseErr = RC.NewProcess(payload, target, true)
case "PROCESS-PATH":