Esempio n. 1
0
func BenchmarkProxyHandlerBlockWithMatchingRegexRule(b *testing.B) {
	var handler http.Handler = &testServer{}
	s := httptest.NewServer(handler)
	defer s.Close()
	rs := make([]*rule.Rule, 0)
	r := &rule.Rule{}

	r.Filters = make([]*selector.Selector, 0, 1)
	sel, _ := selector.Parse("IP=.*")
	r.Filters = append(r.Filters, sel)

	r.Actions = make([]action.Action, 0, 1)
	a, _ := action.Create("block", make(action.ActionParams))
	r.Actions = append(r.Actions, a)

	rs = append(rs, r)
	p := &Proxy{
		NumberOfRequests: 0,
		target:           []byte(s.URL),
		Rules:            &rs,
	}
	b.ResetTimer()
	ctx := &fasthttp.RequestCtx{}
	ctx.Request = *fasthttp.AcquireRequest()
	ctx.Init(&ctx.Request, nil, nil)
	defer fasthttp.ReleaseRequest(&ctx.Request)
	for i := 0; i < b.N; i++ {
		p.Handler(ctx)
	}
}
Esempio n. 2
0
func (s *shellAction) SetParams(params ActionParams) error {
	if val, found := params["cmd"]; found {
		cmd, found := val.(string)
		if !found {
			return errors.New("String type expected as shell action cmd param")
		}
		s.cmd = cmd
	} else {
		return errors.New("Missing \"cmd\" argument in shell action")
	}
	if val, found := params["args"]; found {
		args, found := val.([]interface{})
		if !found {
			return errors.New("Array of selector strings expected as shell action args")
		}
		s.args = make([]*selector.Selector, 0, len(args))
		for _, val := range args {
			arg, found := val.(string)
			if !found {
				return errors.New("Selector string expected as shell argument")
			}
			sel, err := selector.Parse(arg)
			if err != nil {
				return errors.New("Invalid selector in shell argument")
			}
			s.args = append(s.args, sel)
		}
	}
	return nil
}
Esempio n. 3
0
func (r *Rule) ParseFilters(filters []string) error {
	r.Filters = make([]*selector.Selector, 0, len(filters))
	for _, t := range filters {
		s, err := selector.Parse(t)
		if err != nil {
			return errors.New(fmt.Sprintf("Cannot parse selector '%v': %v", t, err))
		}
		r.Filters = append(r.Filters, s)
	}
	return nil
}
Esempio n. 4
0
func (r *Rule) ParseAggregations(aggregations []string) error {
	r.Aggregations = make([]*Aggregation, 0, len(aggregations))
	for _, t := range aggregations {
		s, err := selector.Parse(t)
		if err != nil {
			return errors.New(fmt.Sprintf("Cannot parse selector '%v': %v", t, err))
		}
		a := &Aggregation{
			Values:   make(map[string]uint64),
			Selector: s,
		}
		r.Aggregations = append(r.Aggregations, a)
	}
	return nil
}