diff options
author | Ruben Pollan <meskio@sindominio.net> | 2019-01-09 12:35:19 +0100 |
---|---|---|
committer | Kali Kaneko (leap communications) <kali@leap.se> | 2019-01-10 00:25:55 +0100 |
commit | a9876d6803f281db299ba64f5c7d377303ee8bdb (patch) | |
tree | a90e7baf3e3f81641a3dc4c7f3999d7164b1e2b7 | |
parent | 18f313693543132e8c1e627a3da9facd6f984c40 (diff) |
[feat] add /firewall/isup
- Related: bitmask-systray#97
-rw-r--r-- | helper/darwin.go | 10 | ||||
-rw-r--r-- | helper/helper.go | 11 | ||||
-rw-r--r-- | helper/linux.go | 5 | ||||
-rw-r--r-- | helper/windows.go | 5 |
4 files changed, 31 insertions, 0 deletions
diff --git a/helper/darwin.go b/helper/darwin.go index 10bab31..99e0d46 100644 --- a/helper/darwin.go +++ b/helper/darwin.go @@ -27,6 +27,7 @@ To inspect the rules in the firewall manually, use the bitmask anchor: package main import ( + "bytes" "errors" "fmt" "log" @@ -103,6 +104,15 @@ func firewallStop() error { return exec.Command(pfctl, "-a", bitmask_anchor, "-F", "all").Run() } +func firewallIsUp() bool { + out, err := exec.Command(pfctl, "-a", bitmask_anchor, "-sr").Output() + if err != nil { + log.Printf("An error ocurred getting the status of the firewall: %v", err) + return false + } + return bytes.Contains(out, []byte("block out proto udp to any port 53")) +} + func enablePf() { cmd := exec.Command(pfctl, "-e") cmd.Run() diff --git a/helper/helper.go b/helper/helper.go index 9d6d327..198e2c0 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -51,6 +51,7 @@ func serveHTTP() { http.HandleFunc("/openvpn/stop", openvpn.stop) http.HandleFunc("/firewall/start", firewallStartHandler) http.HandleFunc("/firewall/stop", firewallStopHandler) + http.HandleFunc("/firewall/isup", firewallIsUpHandler) log.Fatal(http.ListenAndServe(bindAddr, nil)) } @@ -138,6 +139,16 @@ func firewallStopHandler(w http.ResponseWriter, r *http.Request) { log.Println("Stop firewall: firewall stopped") } +func firewallIsUpHandler(w http.ResponseWriter, r *http.Request) { + if firewallIsUp() { + w.Write([]byte("true")) + w.WriteHeader(http.StatusOK) + } else { + w.Write([]byte("false")) + w.WriteHeader(http.StatusNoContent) + } +} + func getArgs(r *http.Request) ([]string, error) { args := []string{} decoder := json.NewDecoder(r.Body) diff --git a/helper/linux.go b/helper/linux.go index 4273578..1216e9e 100644 --- a/helper/linux.go +++ b/helper/linux.go @@ -60,3 +60,8 @@ func firewallStop() error { log.Println("Stop firewall: do nothing, not implemented") return nil } + +func firewallIsUp() bool { + log.Println("IsUp firewall: do nothing, not implemented") + return false +} diff --git a/helper/windows.go b/helper/windows.go index 88c96e9..ba812c1 100644 --- a/helper/windows.go +++ b/helper/windows.go @@ -59,3 +59,8 @@ func firewallStop() error { log.Println("Stop firewall: do nothing, not implemented") return nil } + +func firewallIsUp() bool { + log.Println("IsUp firewall: do nothing, not implemented") + return false +} |