-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_type.go
74 lines (64 loc) · 1.24 KB
/
in_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package common
import (
"fmt"
C "github.com/devsolux/clash-meta/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 couldn't 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
}