/
type.changeContent.go
57 lines (44 loc) · 1.71 KB
/
type.changeContent.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
package server
import (
"errors"
"math/rand"
"time"
)
type changeContent struct {
ChangeRateMin float64 `json:"changeRateMin"`
ChangeRateMax float64 `json:"changeRateMax"`
ChangeBytesMin int `json:"changeBytesMin"`
ChangeBytesMax int `json:"changeBytesMax"`
Rate float64 `json:"rate"`
}
func (el *changeContent) prepare() error {
if el.Rate == 0.0 {
return nil
}
if el.ChangeRateMin == el.ChangeRateMax && el.ChangeBytesMin == el.ChangeBytesMax && el.ChangeRateMin == 0.0 {
el.Rate = 0.0
return errors.New("pygocentrus attack > changeContent > rate set to zero")
}
if el.ChangeRateMin > el.ChangeRateMax {
return errors.New("pygocentrus attack > changeContent > rate > the minimum value is greater than the maximum value")
}
if el.ChangeBytesMin > el.ChangeBytesMax {
return errors.New("pygocentrus attack > changeContent > bytes > the minimum value is greater than the maximum value")
}
if (el.ChangeRateMin != 0.0 || el.ChangeRateMax != 0.0) && (el.ChangeBytesMin != 0.0 || el.ChangeBytesMax != 0.0) {
return errors.New("pygocentrus attack > changeContent > you must choose option rate change or option bytes change")
}
return nil
}
func (el *changeContent) GetRandomByMaxMin(length int) int {
r1 := rand.New(rand.NewSource(time.Now().UnixNano()))
if el.ChangeRateMin != 0.0 || el.ChangeRateMax != 0.0 {
var changeMin = int(float64(length) * el.ChangeRateMin)
var changeMax = int(float64(length) * el.ChangeRateMax)
return r1.Intn(changeMax-changeMin) + changeMin
}
return r1.Intn(el.ChangeBytesMax-el.ChangeBytesMin) + el.ChangeBytesMin
}
func (el *changeContent) GetRandomByLength(length int) int {
return rand.New(rand.NewSource(time.Now().UnixNano())).Intn(length)
}