1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Display firewall status

This commit is contained in:
Chris Cummer 2018-03-31 09:16:40 -07:00 committed by Chris Cummer
parent 42ce7f4353
commit a365c1df96
5 changed files with 151 additions and 56 deletions

View File

@ -1,58 +1,14 @@
package security
import (
"io/ioutil"
"os/exec"
"regexp"
//"runtime"
//"strings"
)
const fwGlobalState = "/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate"
const fwStealthMode = "/usr/libexec/ApplicationFirewall/socketfilterfw --getstealthmode"
// https://github.com/yelinaung/wifi-name/blob/master/wifi-name.go
const osxCmd = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
const osxArgs = "-I"
import ()
func Fetch() map[string]string {
data := make(map[string]string)
data["Wifi"] = WifiName()
data["Wifi Network"] = WifiName()
data["Wifi Encryption"] = WifiEncryption()
data["Firewall Enabled"] = FirewallState()
data["Firewall Stealth"] = FirewallStealthState()
return data
}
func WifiName() string {
cmd := exec.Command(osxCmd, osxArgs)
stdout, err := cmd.StdoutPipe()
panicIf(err)
// start the command after having set up the pipe
if err := cmd.Start(); err != nil {
panic(err)
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += (string(b) + "\n")
}
r := regexp.MustCompile(`s*SSID: (.+)s*`)
name := r.FindAllStringSubmatch(str, -1)
if len(name) <= 1 {
return ""
} else {
return name[1][1]
}
}
func panicIf(err error) {
if err != nil {
panic(err)
}
}

59
security/firewall.go Normal file
View File

@ -0,0 +1,59 @@
package security
import (
"io/ioutil"
"os/exec"
"strings"
)
const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw"
func FirewallState() string {
cmd := exec.Command(osxFirewallCmd, "--getglobalstate")
stdout, err := cmd.StdoutPipe()
if err != nil {
return firewallIcon("err")
}
if err := cmd.Start(); err != nil {
return firewallIcon("err")
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += string(b)
}
return firewallIcon(str)
}
func FirewallStealthState() string {
cmd := exec.Command(osxFirewallCmd, "--getstealthmode")
stdout, err := cmd.StdoutPipe()
if err != nil {
return firewallIcon("err")
}
if err := cmd.Start(); err != nil {
return firewallIcon("err")
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += string(b)
}
return firewallIcon(str)
}
func firewallIcon(str string) string {
icon := "[red]off[white]"
if strings.Contains(str, "enabled") {
icon = "[green]on[white]"
}
return icon
}

View File

@ -2,6 +2,7 @@ package security
import (
"fmt"
"sort"
"time"
"github.com/rivo/tview"
@ -16,9 +17,9 @@ type Widget struct {
func NewWidget() *Widget {
widget := Widget{
BaseWidget: wtf.BaseWidget{
Name: "Weather",
Name: "Security",
RefreshedAt: time.Now(),
RefreshInterval: 5,
RefreshInterval: 300,
},
}
@ -33,7 +34,7 @@ func NewWidget() *Widget {
func (widget *Widget) Refresh() {
data := Fetch()
widget.View.SetTitle(" 🐼 Security")
widget.View.SetTitle(" 🦂 Security ")
widget.RefreshedAt = time.Now()
widget.View.Clear()
@ -56,8 +57,16 @@ func (widget *Widget) addView() {
func (widget *Widget) contentFrom(data map[string]string) string {
str := "\n"
for key, val := range data {
str = str + fmt.Sprintf("%8s: %8s\n", key, val)
// Sort the map keys in alphabetical order
var keys []string
for key, _ := range data {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
val := data[key]
str = str + fmt.Sprintf(" %16s: %s\n", key, val)
}
return str

71
security/wifi.go Normal file
View File

@ -0,0 +1,71 @@
package security
import (
"io/ioutil"
"os/exec"
"regexp"
)
// https://github.com/yelinaung/wifi-name/blob/master/wifi-name.go
const osxWifiCmd = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
/* -------------------- Exported Functions -------------------- */
func WifiEncryption() string {
cmd := exec.Command(osxWifiCmd, "-I")
stdout, err := cmd.StdoutPipe()
if err != nil {
return ""
}
if err := cmd.Start(); err != nil {
return ""
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += (string(b) + "\n")
}
name := findMatch(`s*auth: (.+)s*`, str)
return matchStr(name)
}
func WifiName() string {
cmd := exec.Command(osxWifiCmd, "-I")
stdout, err := cmd.StdoutPipe()
if err != nil {
return ""
}
if err := cmd.Start(); err != nil {
return ""
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += (string(b) + "\n")
}
name := findMatch(`s*SSID: (.+)s*`, str)
return matchStr(name)
}
/* -------------------- Unexported Functions -------------------- */
func findMatch(pattern string, data string) [][]string {
r := regexp.MustCompile(pattern)
name := r.FindAllStringSubmatch(data, -1)
return name
}
func matchStr(data [][]string) string {
if len(data) <= 1 {
return ""
} else {
return data[1][1]
}
}

View File

@ -68,7 +68,7 @@ func (widget *Widget) contentFrom(data *owm.CurrentWeatherData) string {
str = str + strings.Join(descs, ",") + "\n\n"
str = str + fmt.Sprintf("%10s: %4.1f° C\n\n", "Current", data.Main.Temp)
str = str + fmt.Sprintf("%10s: %4.1f° C\n", "Current", data.Main.Temp)
str = str + fmt.Sprintf("%10s: %4.1f° C\n", "High", data.Main.TempMax)
str = str + fmt.Sprintf("%10s: %4.1f° C\n", "Low", data.Main.TempMin)
str = str + "\n\n\n\n"