v2fly/app/router/condition.go

335 lines
6.8 KiB
Go
Raw Normal View History

2016-10-12 14:11:13 +00:00
package router
2016-01-17 15:20:49 +00:00
import (
2018-07-16 11:47:00 +00:00
"strings"
2016-01-17 15:20:49 +00:00
"go.starlark.net/starlark"
"go.starlark.net/syntax"
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/strmatcher"
"github.com/v2fly/v2ray-core/v4/features/routing"
2016-01-17 15:20:49 +00:00
)
type Condition interface {
Apply(ctx routing.Context) bool
2016-01-17 15:20:49 +00:00
}
type ConditionChan []Condition
func NewConditionChan() *ConditionChan {
var condChan ConditionChan = make([]Condition, 0, 8)
return &condChan
}
2016-11-27 20:39:09 +00:00
func (v *ConditionChan) Add(cond Condition) *ConditionChan {
*v = append(*v, cond)
return v
2016-01-17 15:20:49 +00:00
}
// Apply applies all conditions registered in this chan.
func (v *ConditionChan) Apply(ctx routing.Context) bool {
2016-11-27 20:39:09 +00:00
for _, cond := range *v {
if !cond.Apply(ctx) {
2016-01-17 15:20:49 +00:00
return false
}
}
return true
}
2016-11-27 20:39:09 +00:00
func (v *ConditionChan) Len() int {
return len(*v)
2016-01-17 15:20:49 +00:00
}
2018-06-26 19:57:41 +00:00
var matcherTypeMap = map[Domain_Type]strmatcher.Type{
Domain_Plain: strmatcher.Substr,
Domain_Regex: strmatcher.Regex,
Domain_Domain: strmatcher.Domain,
2018-08-21 19:30:32 +00:00
Domain_Full: strmatcher.Full,
2018-06-26 19:57:41 +00:00
}
2018-08-19 19:04:15 +00:00
func domainToMatcher(domain *Domain) (strmatcher.Matcher, error) {
2018-06-26 19:57:41 +00:00
matcherType, f := matcherTypeMap[domain.Type]
if !f {
2018-08-19 19:04:15 +00:00
return nil, newError("unsupported domain type", domain.Type)
2017-11-06 20:12:28 +00:00
}
2018-06-26 19:57:41 +00:00
matcher, err := matcherType.New(domain.Value)
if err != nil {
2018-08-19 19:04:15 +00:00
return nil, newError("failed to create domain matcher").Base(err)
2018-06-26 19:57:41 +00:00
}
2018-08-19 19:04:15 +00:00
return matcher, nil
2017-11-06 21:30:56 +00:00
}
2018-08-19 19:04:15 +00:00
type DomainMatcher struct {
matchers strmatcher.IndexMatcher
2017-11-06 21:30:56 +00:00
}
func NewMphMatcherGroup(domains []*Domain) (*DomainMatcher, error) {
g := strmatcher.NewMphMatcherGroup()
for _, d := range domains {
matcherType, f := matcherTypeMap[d.Type]
if !f {
return nil, newError("unsupported domain type", d.Type)
}
_, err := g.AddPattern(d.Value, matcherType)
if err != nil {
return nil, err
}
}
g.Build()
return &DomainMatcher{
matchers: g,
}, nil
}
2018-08-19 19:04:15 +00:00
func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
2018-08-20 13:39:58 +00:00
g := new(strmatcher.MatcherGroup)
2018-08-19 19:04:15 +00:00
for _, d := range domains {
m, err := domainToMatcher(d)
if err != nil {
return nil, err
}
g.Add(m)
2017-11-06 21:30:56 +00:00
}
2018-08-19 19:04:15 +00:00
return &DomainMatcher{
matchers: g,
2018-08-19 19:04:15 +00:00
}, nil
}
2017-11-06 21:30:56 +00:00
2018-08-19 19:04:15 +00:00
func (m *DomainMatcher) ApplyDomain(domain string) bool {
return len(m.matchers.Match(strings.ToLower(domain))) > 0
2017-11-06 21:30:56 +00:00
}
// Apply implements Condition.
func (m *DomainMatcher) Apply(ctx routing.Context) bool {
domain := ctx.GetTargetDomain()
if len(domain) == 0 {
2017-02-09 21:49:38 +00:00
return false
}
return m.ApplyDomain(domain)
2018-12-04 19:36:51 +00:00
}
type MultiGeoIPMatcher struct {
matchers []*GeoIPMatcher
onSource bool
}
func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
var matchers []*GeoIPMatcher
for _, geoip := range geoips {
matcher, err := globalGeoIPContainer.Add(geoip)
if err != nil {
return nil, err
}
matchers = append(matchers, matcher)
}
2018-12-04 19:36:51 +00:00
matcher := &MultiGeoIPMatcher{
matchers: matchers,
onSource: onSource,
2018-11-07 22:57:06 +00:00
}
2018-12-04 19:36:51 +00:00
return matcher, nil
}
// Apply implements Condition.
func (m *MultiGeoIPMatcher) Apply(ctx routing.Context) bool {
var ips []net.IP
if m.onSource {
ips = ctx.GetSourceIPs()
} else {
ips = ctx.GetTargetIPs()
}
for _, ip := range ips {
for _, matcher := range m.matchers {
if matcher.Match(ip) {
return true
}
}
}
return false
}
2016-01-17 15:20:49 +00:00
type PortMatcher struct {
port net.MemoryPortList
onSource bool
2016-01-17 15:20:49 +00:00
}
// NewPortMatcher create a new port matcher that can match source or destination port
func NewPortMatcher(list *net.PortList, onSource bool) *PortMatcher {
2016-01-17 15:20:49 +00:00
return &PortMatcher{
port: net.PortListFromProto(list),
onSource: onSource,
2016-01-17 15:20:49 +00:00
}
}
// Apply implements Condition.
func (v *PortMatcher) Apply(ctx routing.Context) bool {
if v.onSource {
return v.port.Contains(ctx.GetSourcePort())
2017-02-09 21:49:38 +00:00
}
return v.port.Contains(ctx.GetTargetPort())
2016-01-17 15:20:49 +00:00
}
type NetworkMatcher struct {
2018-11-20 11:25:56 +00:00
list [8]bool
2016-01-17 15:20:49 +00:00
}
func NewNetworkMatcher(network []net.Network) NetworkMatcher {
2018-11-20 11:25:56 +00:00
var matcher NetworkMatcher
for _, n := range network {
2018-11-20 11:25:56 +00:00
matcher.list[int(n)] = true
2016-01-17 15:20:49 +00:00
}
2018-11-20 11:25:56 +00:00
return matcher
2016-01-17 15:20:49 +00:00
}
// Apply implements Condition.
func (v NetworkMatcher) Apply(ctx routing.Context) bool {
return v.list[int(ctx.GetNetwork())]
2016-10-18 21:01:39 +00:00
}
type UserMatcher struct {
user []string
}
func NewUserMatcher(users []string) *UserMatcher {
2017-05-08 09:48:41 +00:00
usersCopy := make([]string, 0, len(users))
for _, user := range users {
if len(user) > 0 {
usersCopy = append(usersCopy, user)
}
}
2016-10-18 21:01:39 +00:00
return &UserMatcher{
2017-05-08 09:48:41 +00:00
user: usersCopy,
2016-10-18 21:01:39 +00:00
}
}
// Apply implements Condition.
func (v *UserMatcher) Apply(ctx routing.Context) bool {
user := ctx.GetUser()
if len(user) == 0 {
2016-10-18 21:01:39 +00:00
return false
}
2016-11-27 20:39:09 +00:00
for _, u := range v.user {
if u == user {
2016-10-18 21:01:39 +00:00
return true
}
}
return false
2016-01-17 15:20:49 +00:00
}
2016-11-13 20:23:34 +00:00
type InboundTagMatcher struct {
tags []string
}
func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
2017-05-08 09:48:41 +00:00
tagsCopy := make([]string, 0, len(tags))
for _, tag := range tags {
if len(tag) > 0 {
tagsCopy = append(tagsCopy, tag)
}
}
2016-11-13 20:23:34 +00:00
return &InboundTagMatcher{
2017-05-08 09:48:41 +00:00
tags: tagsCopy,
2016-11-13 20:23:34 +00:00
}
}
// Apply implements Condition.
func (v *InboundTagMatcher) Apply(ctx routing.Context) bool {
tag := ctx.GetInboundTag()
if len(tag) == 0 {
2016-11-13 20:23:34 +00:00
return false
}
2016-11-27 20:39:09 +00:00
for _, t := range v.tags {
if t == tag {
2016-11-13 20:23:34 +00:00
return true
}
}
return false
}
2018-07-16 11:47:00 +00:00
type ProtocolMatcher struct {
protocols []string
}
func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
pCopy := make([]string, 0, len(protocols))
for _, p := range protocols {
if len(p) > 0 {
pCopy = append(pCopy, p)
}
}
return &ProtocolMatcher{
protocols: pCopy,
}
}
// Apply implements Condition.
func (m *ProtocolMatcher) Apply(ctx routing.Context) bool {
protocol := ctx.GetProtocol()
if len(protocol) == 0 {
2018-07-16 11:47:00 +00:00
return false
}
for _, p := range m.protocols {
if strings.HasPrefix(protocol, p) {
return true
}
}
return false
}
type AttributeMatcher struct {
program *starlark.Program
}
func NewAttributeMatcher(code string) (*AttributeMatcher, error) {
starFile, err := syntax.Parse("attr.star", "satisfied=("+code+")", 0)
if err != nil {
return nil, newError("attr rule").Base(err)
}
p, err := starlark.FileProgram(starFile, func(name string) bool {
2020-04-15 01:19:52 +00:00
return name == "attrs"
})
if err != nil {
return nil, err
}
return &AttributeMatcher{
program: p,
}, nil
}
// Match implements attributes matching.
func (m *AttributeMatcher) Match(attrs map[string]string) bool {
attrsDict := new(starlark.Dict)
for key, value := range attrs {
attrsDict.SetKey(starlark.String(key), starlark.String(value))
}
predefined := make(starlark.StringDict)
predefined["attrs"] = attrsDict
thread := &starlark.Thread{
Name: "matcher",
}
results, err := m.program.Init(thread, predefined)
if err != nil {
newError("attr matcher").Base(err).WriteToLog()
}
satisfied := results["satisfied"]
return satisfied != nil && bool(satisfied.Truth())
}
// Apply implements Condition.
func (m *AttributeMatcher) Apply(ctx routing.Context) bool {
attributes := ctx.GetAttributes()
if attributes == nil {
return false
}
return m.Match(attributes)
}