From a9876d6803f281db299ba64f5c7d377303ee8bdb Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Wed, 9 Jan 2019 12:35:19 +0100 Subject: [feat] add /firewall/isup - Related: bitmask-systray#97 --- helper/darwin.go | 10 ++++++++++ helper/helper.go | 11 +++++++++++ helper/linux.go | 5 +++++ helper/windows.go | 5 +++++ 4 files changed, 31 insertions(+) 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 +} -- cgit v1.2.3