forked from tylertreat/comcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipfw.go
62 lines (49 loc) · 1.16 KB
/
ipfw.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
package throttler
import (
"strconv"
)
const (
ipfwAddPipe = `sudo ipfw add 1 pipe 1 ip from any to any via `
ipfwTeardown = `sudo ipfw delete 1`
ipfwConfig = `sudo ipfw pipe 1 config`
ipfwExists = `sudo ipfw list | grep "pipe 1"`
ipfwCheck = `sudo ipfw list`
)
type ipfwThrottler struct{}
func (i *ipfwThrottler) setup(c *Config) error {
cmd := ipfwAddPipe + c.Device
err := runCommand(cmd)
if err != nil {
return err
}
configCmd := i.buildConfigCommand(c)
err = runCommand(configCmd)
return err
}
func (i *ipfwThrottler) teardown(_ *Config) error {
err := runCommand(ipfwTeardown)
return err
}
func (i *ipfwThrottler) exists() bool {
if dry {
return false
}
err := runCommand(ipfwExists)
return err == nil
}
func (i *ipfwThrottler) check() string {
return ipfwCheck
}
func (i *ipfwThrottler) buildConfigCommand(c *Config) string {
cmd := ipfwConfig
if c.Latency > 0 {
cmd = cmd + " delay " + strconv.Itoa(c.Latency) + "ms"
}
if c.TargetBandwidth > 0 {
cmd = cmd + " bw " + strconv.Itoa(c.TargetBandwidth) + "Kbit/s"
}
if c.PacketLoss > 0 {
cmd = cmd + " plr " + strconv.FormatFloat(c.PacketLoss/100, 'f', 4, 64)
}
return cmd
}