func TestSimpleRouter(t *testing.T) { v2testing.Current(t) router := NewRouter().AddRule( &Rule{ Tag: "test", Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()), }) tag, err := router.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80)) assert.Error(err).IsNil() assert.StringLiteral(tag).Equals("test") }
func (this *FieldRule) Apply(dest v2net.Destination) bool { address := dest.Address() if len(this.Domain) > 0 { if !address.IsDomain() { return false } foundMatch := false for _, domain := range this.Domain { if domain.Match(address.Domain()) { foundMatch = true break } } if !foundMatch { return false } } if this.IP != nil && len(this.IP) > 0 { if !(address.IsIPv4() || address.IsIPv6()) { return false } foundMatch := false for _, ipnet := range this.IP { if ipnet.Contains(address.IP()) { foundMatch = true break } } if !foundMatch { return false } } if this.Port != nil { port := dest.Port() if port.Value() < this.Port.From().Value() || port.Value() > this.Port.To().Value() { return false } } if this.Network != nil { if !this.Network.HasNetwork(v2net.Network(dest.Network())) { return false } } return true }
func TestSimpleRouter(t *testing.T) { assert := assert.On(t) config := &RouterRuleConfig{ Rules: []*Rule{ { Tag: "test", Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()), }, }, } space := app.NewSpace() space.BindApp(dns.APP_ID, dns.NewCacheServer(space, &dns.Config{})) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager()) r := NewRouter(config, space) space.BindApp(router.APP_ID, r) assert.Error(space.Initialize()).IsNil() tag, err := r.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80)) assert.Error(err).IsNil() assert.String(tag).Equals("test") }
func (this *NetworkMatcher) Apply(dest v2net.Destination) bool { return this.network.HasNetwork(v2net.Network(dest.Network())) }