-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptables.go
68 lines (53 loc) · 1.09 KB
/
iptables.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
63
64
65
66
67
68
package main
import (
"fmt"
"os/exec"
"strconv"
"strings"
)
func setupIptables() (func(), error) {
if err := iptablesCleanup(); err != nil {
return func() {}, err
}
cu := func() {
_ = iptablesCleanup
}
if err := iptablesAddTap("80"); err != nil {
return cu, err
}
if err := iptablesAddTap("443"); err != nil {
return cu, err
}
return cu, nil
}
func iptables(args ...string) error {
args = append([]string{"-t", "nat"}, args...)
cmd := exec.Command("iptables", args...)
_, err := cmd.Output()
if err != nil {
if eerr, ok := err.(*exec.ExitError); ok {
errStr := strings.TrimSpace(string(eerr.Stderr))
errStr = strings.Replace(errStr, "\n", " ", -1)
return fmt.Errorf(errStr)
}
return err
}
return nil
}
func iptablesCleanup() error {
return iptables("-F")
}
func iptablesAddTap(port string) error {
if _, err := strconv.Atoi(port); err != nil {
return fmt.Errorf("provided port %q is not a number", port)
}
return iptables(
"-A", "PREROUTING",
"-i", "wlan0",
"-p", "tcp",
"--dport", port,
"-m", "tcp",
"-j", "REDIRECT",
"--to-ports", port,
)
}