mirror of https://github.com/subgraph/fw-daemon
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
9 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
9 years ago
|
const iptablesRule = "OUTPUT -t mangle -m conntrack --ctstate NEW -j NFQUEUE --queue-num 0 --queue-bypass"
|
||
|
const dnsRule = "INPUT --protocol udp --sport 53 -j NFQUEUE --queue-num 0 --queue-bypass"
|
||
8 years ago
|
|
||
|
//const logRule = "OUTPUT --protocol tcp -m mark --mark 1 -j LOG"
|
||
9 years ago
|
const blockRule = "OUTPUT --protocol tcp -m mark --mark 1 -j REJECT"
|
||
9 years ago
|
|
||
|
func setupIPTables() {
|
||
8 years ago
|
// addIPTRules(iptablesRule, dnsRule, logRule, blockRule)
|
||
9 years ago
|
addIPTRules(iptablesRule, dnsRule, blockRule)
|
||
9 years ago
|
}
|
||
|
|
||
|
func addIPTRules(rules ...string) {
|
||
|
for _, r := range rules {
|
||
9 years ago
|
if iptables('C', r) {
|
||
8 years ago
|
log.Infof("IPTables rule already present: %s", r)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
log.Infof("Installing IPTables rule: %s", r)
|
||
9 years ago
|
iptables('I', r)
|
||
|
}
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func iptables(verb rune, rule string) bool {
|
||
9 years ago
|
iptablesPath, err := exec.LookPath("iptables")
|
||
|
if err != nil {
|
||
|
log.Warning("Could not find iptables binary in path")
|
||
|
os.Exit(1)
|
||
|
}
|
||
9 years ago
|
argLine := fmt.Sprintf("-%c %s", verb, rule)
|
||
9 years ago
|
args := strings.Fields(argLine)
|
||
|
cmd := exec.Command(iptablesPath, args...)
|
||
9 years ago
|
_, err = cmd.CombinedOutput()
|
||
9 years ago
|
_, exitErr := err.(*exec.ExitError)
|
||
|
if err != nil && !exitErr {
|
||
8 years ago
|
log.Warningf("Error running iptables: %v", err)
|
||
9 years ago
|
}
|
||
9 years ago
|
return !exitErr
|
||
9 years ago
|
}
|