-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathiproute.go
39 lines (32 loc) · 1.21 KB
/
iproute.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
package main
import (
"fmt"
"os/exec"
"strings"
"github.com/sirupsen/logrus"
)
func applyRoute() error {
logrus.Info("[route] add ip rules...")
bs, err := exec.Command("ip", "rule", "add", "fwmark", tproxyMark, "lookup", tproxyMark).CombinedOutput()
if err != nil && !strings.Contains(string(bs), "File exists") {
return fmt.Errorf("failed to create ip rule: %s, %v", bs, err)
}
logrus.Info("[route] add ip routes...")
bs, err = exec.Command("ip", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", tproxyMark).CombinedOutput()
if err != nil && !strings.Contains(string(bs), "File exists") {
return fmt.Errorf("failed to create ip route: %s, %v", bs, err)
}
return nil
}
func cleanRoute() {
logrus.Info("[route] clean ip rules...")
bs, err := exec.Command("ip", "rule", "del", "fwmark", tproxyMark, "table", tproxyMark).CombinedOutput()
if err != nil {
logrus.Warnf("failed to clean route: %s, %v", strings.TrimSpace(string(bs)), err)
}
logrus.Info("[route] clean ip routes...")
bs, err = exec.Command("ip", "route", "del", "local", "0.0.0.0/0", "dev", "lo", "table", tproxyMark).CombinedOutput()
if err != nil {
logrus.Warnf("failed to clean route: %s, %v", strings.TrimSpace(string(bs)), err)
}
}