This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iptable.go
178 lines (159 loc) · 4.33 KB
/
iptable.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"log"
"os/exec"
"strconv"
"strings"
)
// TODO investigate if we are making repeate entries in iptable
// TODO what the behvaiour of iptables if there are multiple entries. first match? should we be adding? are later entries replacing already read values.
// Netfilter - hold device ip mac and bandwidth
type Netfilter struct {
Mac string
IP string
In uint64
Out uint64
}
func setupIptable() {
// Create tables (it doesn't matter if it already exists).
err := exec.Command("iptables", "-N", "WRTLINK").Run()
if err != nil {
// Add the WRTLINK CHAIN to the FORWARD chain (if non existing).
err = exec.Command("/bin/sh", "-c", "iptables -L FORWARD --line-numbers -n | grep WRTLINK | grep 1").Run()
if err != nil {
// if that command errors the chain is not where is should be
log.Println(err)
log.Println("iptables chain out of place")
err = exec.Command("/bin/sh", "-c", "iptables -L FORWARD -n | grep WRTLINK").Run()
if err == nil {
// the chain exists but is in the wrong spot
log.Println("iptables chain misplaced, deleting it...")
// delete the chain
//TODO This only deletes one
err = exec.Command("iptables", "-D", "FORWARD", "-j", "WRTLINK").Run()
}
log.Println("creating iptables chain")
_ = exec.Command("iptables", "-I", "FORWARD", "-j", "WRTLINK").Run()
}
out, err := exec.Command("grep", lanInterface, "/proc/net/arp").Output()
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
if len(line) >= 1 {
if line[0] == 'I' {
continue
}
fields := strings.Split(line, " ")
if len(fields) >= 1 {
_, err = exec.Command("sh", "-c", "'iptables -nL WRTLINK | grep \""+fields[0]+"\"' | echo $?").Output()
if err != nil {
log.Println("Adding Ip rules for " + fields[0])
_ = exec.Command("iptables", "-I", "WRTLINK", "-d", fields[0], "-j", "RETURN").Run()
_ = exec.Command("iptables", "-I", "WRTLINK", "-s", fields[0], "-j", "RETURN").Run()
}
}
}
}
} else {
log.Println(err)
}
}
func readArp() map[string]Netfilter {
var arpData = make(map[string]Netfilter)
out, err := exec.Command("grep", "-v", "\"0x0\"", "/proc/net/arp").Output()
if err != nil {
log.Fatal(err)
}
arpLines := strings.Split(string(out), "\n")
for _, line := range arpLines {
if len(line) >= 1 {
if line[0] == 'I' {
continue
}
fields := strings.Fields(line)
if len(fields) >= 6 {
dev, ok := arpData[fields[0]]
if !ok {
arpData[fields[0]] = Netfilter{
IP: fields[0],
Mac: fields[3],
Out: 0,
In: 0,
}
} else {
dev.IP = fields[0]
dev.Mac = fields[3]
dev.Out = 0
dev.In = 0
}
}
}
}
return arpData
}
func readIptable(conntrackResult []Conntrack) []Netfilter {
arpData := readArp()
out, err := exec.Command("iptables", "-L", "WRTLINK", "-vnxZ").Output()
if err != nil {
log.Fatal(err)
}
iptableLines := strings.Split(string(out), "\n")
for _, line := range iptableLines {
fields := strings.Fields(line)
if len(fields) == 9 && fields[0] != "pkts" {
if fields[0] != "0" && fields[1] != "0" {
if fields[7] == "0.0.0.0/0" {
// Download
dev, ok := arpData[fields[8]]
if !ok {
log.Println("Error looking up device 7")
} else {
tmp, _ := strconv.ParseUint(fields[1], 10, 32)
dev.In = dev.In + tmp
arpData[fields[8]] = dev
}
} else if fields[8] == "0.0.0.0/0" {
// Upload
dev, ok := arpData[fields[7]]
if !ok {
log.Println("Error looking up device 8")
} else {
tmp, _ := strconv.ParseUint(fields[1], 10, 32)
dev.Out = dev.Out + tmp
arpData[fields[7]] = dev
}
} else {
log.Println("iptable line missing 0.0.0.0/0")
}
}
}
}
if sfe {
for _, cr := range conntrackResult {
dev, ok := arpData[cr.Src]
if !ok {
dev, ok := arpData[cr.Dst]
if !ok {
// log.Println("No match conntrack result in arp data", dev)
} else {
dev.In += cr.Out
dev.Out += cr.In
arpData[cr.Dst] = dev
}
} else {
dev.In += cr.In
dev.Out += cr.Out
arpData[cr.Src] = dev
}
}
}
// Turn map into a array and return
var iptableResult []Netfilter
for _, value := range arpData {
vCopy := value
iptableResult = append(iptableResult, vCopy)
}
return iptableResult
}