github.com/metacubex/mihomo@v1.18.5/rules/common/in_type.go (about) 1 package common 2 3 import ( 4 "fmt" 5 C "github.com/metacubex/mihomo/constant" 6 "strings" 7 ) 8 9 type InType struct { 10 *Base 11 types []C.Type 12 adapter string 13 payload string 14 } 15 16 func (u *InType) Match(metadata *C.Metadata) (bool, string) { 17 for _, tp := range u.types { 18 if metadata.Type == tp { 19 return true, u.adapter 20 } 21 } 22 return false, "" 23 } 24 25 func (u *InType) RuleType() C.RuleType { 26 return C.InType 27 } 28 29 func (u *InType) Adapter() string { 30 return u.adapter 31 } 32 33 func (u *InType) Payload() string { 34 return u.payload 35 } 36 37 func NewInType(iTypes, adapter string) (*InType, error) { 38 types := strings.Split(iTypes, "/") 39 if len(types) == 0 { 40 return nil, fmt.Errorf("in type couldn't be empty") 41 } 42 43 tps, err := parseInTypes(types) 44 if err != nil { 45 return nil, err 46 } 47 48 return &InType{ 49 Base: &Base{}, 50 types: tps, 51 adapter: adapter, 52 payload: strings.ToUpper(iTypes), 53 }, nil 54 } 55 56 func parseInTypes(tps []string) (res []C.Type, err error) { 57 for _, tp := range tps { 58 utp := strings.ToUpper(tp) 59 var r *C.Type 60 if utp == "SOCKS" { 61 r, _ = C.ParseType("SOCKS4") 62 res = append(res, *r) 63 r, _ = C.ParseType("SOCKS5") 64 res = append(res, *r) 65 } else { 66 r, err = C.ParseType(utp) 67 if err != nil { 68 return 69 } 70 res = append(res, *r) 71 } 72 } 73 return 74 }