clash/rules/common/in_type.go
adlyq 9b89ff9f2d feat: support sub-rule, eg.
rules:
  - SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
  - SUB-RULE,(GEOIP,!CN),TEST1
  - MATCH,DIRECT

sub-rules:
  TEST2:
    - MATCH,Proxy
  TEST1:
    - RULE-SET,Local,DIRECT,no-resolve
    - GEOSITE,CN,Domestic
    - GEOIP,CN,Domestic
    - MATCH,Proxy
2022-09-06 17:30:35 +08:00

75 lines
1.2 KiB
Go

package common
import (
"fmt"
C "github.com/Dreamacro/clash/constant"
"strings"
)
type InType struct {
*Base
types []C.Type
adapter string
payload string
}
func (u *InType) Match(metadata *C.Metadata) (bool, string) {
for _, tp := range u.types {
if metadata.Type == tp {
return true, u.adapter
}
}
return false, ""
}
func (u *InType) RuleType() C.RuleType {
return C.INTYPE
}
func (u *InType) Adapter() string {
return u.adapter
}
func (u *InType) Payload() string {
return u.payload
}
func NewInType(iTypes, adapter string) (*InType, error) {
types := strings.Split(iTypes, "/")
if len(types) == 0 {
return nil, fmt.Errorf("in type could be empty")
}
tps, err := parseInTypes(types)
if err != nil {
return nil, err
}
return &InType{
Base: &Base{},
types: tps,
adapter: adapter,
payload: strings.ToUpper(iTypes),
}, nil
}
func parseInTypes(tps []string) (res []C.Type, err error) {
for _, tp := range tps {
utp := strings.ToUpper(tp)
var r *C.Type
if utp == "SOCKS" {
r, _ = C.ParseType("SOCKS4")
res = append(res, *r)
r, _ = C.ParseType("SOCKS5")
res = append(res, *r)
} else {
r, err = C.ParseType(utp)
if err != nil {
return
}
res = append(res, *r)
}
}
return
}