2022-05-20 15:17:16 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InType struct {
|
|
|
|
*Base
|
|
|
|
types []C.Type
|
|
|
|
adapter string
|
|
|
|
payload string
|
|
|
|
}
|
|
|
|
|
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 09:30:35 +00:00
|
|
|
func (u *InType) Match(metadata *C.Metadata) (bool, string) {
|
2022-05-20 15:17:16 +00:00
|
|
|
for _, tp := range u.types {
|
|
|
|
if metadata.Type == tp {
|
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 09:30:35 +00:00
|
|
|
return true, u.adapter
|
2022-05-20 15:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
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 09:30:35 +00:00
|
|
|
return false, ""
|
2022-05-20 15:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *InType) RuleType() C.RuleType {
|
2023-05-28 09:19:57 +00:00
|
|
|
return C.InType
|
2022-05-20 15:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-05-28 09:19:57 +00:00
|
|
|
return nil, fmt.Errorf("in type couldn't be empty")
|
2022-05-20 15:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|