[Feature] Proxy stores delay data of different URLs. And supports specifying different test URLs and expected statue by group (#588)
Co-authored-by: Larvan2 <78135608+Larvan2@users.noreply.github.com> Co-authored-by: wwqgtxx <wwqgtxx@gmail.com>
This commit is contained in:
@ -3,7 +3,6 @@ package common
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Dreamacro/clash/common/utils"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
@ -11,10 +10,10 @@ import (
|
||||
|
||||
type Port struct {
|
||||
*Base
|
||||
adapter string
|
||||
port string
|
||||
ruleType C.RuleType
|
||||
portList []utils.Range[uint16]
|
||||
adapter string
|
||||
port string
|
||||
ruleType C.RuleType
|
||||
portRanges utils.IntRanges[uint16]
|
||||
}
|
||||
|
||||
func (p *Port) RuleType() C.RuleType {
|
||||
@ -43,61 +42,25 @@ func (p *Port) Payload() string {
|
||||
func (p *Port) matchPortReal(portRef string) bool {
|
||||
port, _ := strconv.Atoi(portRef)
|
||||
|
||||
for _, pr := range p.portList {
|
||||
if pr.Contains(uint16(port)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return p.portRanges.Check(uint16(port))
|
||||
}
|
||||
|
||||
func NewPort(port string, adapter string, ruleType C.RuleType) (*Port, error) {
|
||||
ports := strings.Split(port, "/")
|
||||
if len(ports) > 28 {
|
||||
return nil, fmt.Errorf("%s, too many ports to use, maximum support 28 ports", errPayload.Error())
|
||||
portRanges, err := utils.NewIntRanges[uint16](port)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w, %s", errPayload, err.Error())
|
||||
}
|
||||
|
||||
var portRange []utils.Range[uint16]
|
||||
for _, p := range ports {
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
subPorts := strings.Split(p, "-")
|
||||
subPortsLen := len(subPorts)
|
||||
if subPortsLen > 2 {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
portStart, err := strconv.ParseUint(strings.Trim(subPorts[0], "[ ]"), 10, 16)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
switch subPortsLen {
|
||||
case 1:
|
||||
portRange = append(portRange, *utils.NewRange(uint16(portStart), uint16(portStart)))
|
||||
case 2:
|
||||
portEnd, err := strconv.ParseUint(strings.Trim(subPorts[1], "[ ]"), 10, 16)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
portRange = append(portRange, *utils.NewRange(uint16(portStart), uint16(portEnd)))
|
||||
}
|
||||
}
|
||||
|
||||
if len(portRange) == 0 {
|
||||
if len(portRanges) == 0 {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
return &Port{
|
||||
Base: &Base{},
|
||||
adapter: adapter,
|
||||
port: port,
|
||||
ruleType: ruleType,
|
||||
portList: portRange,
|
||||
Base: &Base{},
|
||||
adapter: adapter,
|
||||
port: port,
|
||||
ruleType: ruleType,
|
||||
portRanges: portRanges,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -2,57 +2,28 @@ package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/Dreamacro/clash/common/utils"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Uid struct {
|
||||
*Base
|
||||
uids []utils.Range[uint32]
|
||||
uids utils.IntRanges[uint32]
|
||||
oUid string
|
||||
adapter string
|
||||
}
|
||||
|
||||
func NewUid(oUid, adapter string) (*Uid, error) {
|
||||
//if len(_uids) > 28 {
|
||||
// return nil, fmt.Errorf("%s, too many uid to use, maximum support 28 uid", errPayload.Error())
|
||||
//}
|
||||
if !(runtime.GOOS == "linux" || runtime.GOOS == "android") {
|
||||
return nil, fmt.Errorf("uid rule not support this platform")
|
||||
}
|
||||
|
||||
var uidRange []utils.Range[uint32]
|
||||
for _, u := range strings.Split(oUid, "/") {
|
||||
if u == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
subUids := strings.Split(u, "-")
|
||||
subUidsLen := len(subUids)
|
||||
if subUidsLen > 2 {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
uidStart, err := strconv.ParseUint(strings.Trim(subUids[0], "[ ]"), 10, 32)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
switch subUidsLen {
|
||||
case 1:
|
||||
uidRange = append(uidRange, *utils.NewRange(uint32(uidStart), uint32(uidStart)))
|
||||
case 2:
|
||||
uidEnd, err := strconv.ParseUint(strings.Trim(subUids[1], "[ ]"), 10, 32)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
uidRange = append(uidRange, *utils.NewRange(uint32(uidStart), uint32(uidEnd)))
|
||||
}
|
||||
uidRange, err := utils.NewIntRanges[uint32](oUid)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w, %s", errPayload, err.Error())
|
||||
}
|
||||
|
||||
if len(uidRange) == 0 {
|
||||
@ -72,10 +43,8 @@ func (u *Uid) RuleType() C.RuleType {
|
||||
|
||||
func (u *Uid) Match(metadata *C.Metadata) (bool, string) {
|
||||
if metadata.Uid != 0 {
|
||||
for _, uid := range u.uids {
|
||||
if uid.Contains(metadata.Uid) {
|
||||
return true, u.adapter
|
||||
}
|
||||
if u.uids.Check(metadata.Uid) {
|
||||
return true, u.adapter
|
||||
}
|
||||
}
|
||||
log.Warnln("[UID] could not get uid from %s", metadata.String())
|
||||
|
Reference in New Issue
Block a user