mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-26 16:57:08 +00:00
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:
47
rules/common/dscp.go
Normal file
47
rules/common/dscp.go
Normal 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
|
||||
}
|
||||
@@ -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":
|
||||
|
||||
Reference in New Issue
Block a user