From 2109157edcff66bbab4ab467e6fcd9c98dff304f Mon Sep 17 00:00:00 2001 From: James Sapara Date: Thu, 31 May 2018 12:29:32 -0700 Subject: [PATCH 1/6] linux/ubuntu wraps for security --- Makefile | 2 +- security/dns.go | 43 ++++++++++++++++++++++++++++++----- security/firewall.go | 36 ++++++++++++++++++++++++++++-- security/security_data.go | 17 +++++--------- security/users.go | 40 +++++++++++++++++++++++++++++++-- security/wifi.go | 47 +++++++++++++++++++++++++++++++++++++-- 6 files changed, 161 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 60ef00cf..0828bf38 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ dependencies: go get -v ./... install: - which wtf | xargs rm + which wtf | xargs rm || true go install -ldflags="-X main.version=$(shell git describe --always --abbrev=6)_$(BRANCH) -X main.date=$(shell date +%FT%T%z)" which wtf diff --git a/security/dns.go b/security/dns.go index c57a202b..93eadf43 100644 --- a/security/dns.go +++ b/security/dns.go @@ -2,13 +2,46 @@ package security import ( "os/exec" + "runtime" + "strings" "github.com/senorprogrammer/wtf/wtf" ) -const dnsCmd = "networksetup" - -func DnsServers() string { - cmd := exec.Command(dnsCmd, "-getdnsservers", "Wi-Fi") - return wtf.ExecuteCommand(cmd) +func dnsLinux() []string { + // This may be very Ubuntu specific + cmd := exec.Command("nmcli", "device", "show") + out := wtf.ExecuteCommand(cmd) + lines := strings.Split(out, "\n") + dns := []string{} + for _, l := range lines { + if strings.HasPrefix(l, "IP4.DNS") { + parts := strings.Split(l, ":") + dns = append(dns, strings.TrimSpace(parts[1])) + } + } + return dns +} + +func dnsMacOS() []string { + cmd := exec.Command("networksetup", "-getdnsservers", "Wi-Fi") + out := wtf.ExecuteCommand(cmd) + records := strings.Split(out, "\n") + + if len(records) > 0 { + return records + } else { + return []string{} + } +} + +func DnsServers() []string { + switch runtime.GOOS { + case "linux": + return dnsLinux() + case "macos": + return dnsMacOS() + default: + return []string{runtime.GOOS} + } } diff --git a/security/firewall.go b/security/firewall.go index 5d5cb95a..5a7f4c75 100644 --- a/security/firewall.go +++ b/security/firewall.go @@ -2,6 +2,7 @@ package security import ( "os/exec" + "runtime" "strings" "github.com/senorprogrammer/wtf/wtf" @@ -11,20 +12,51 @@ const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw" /* -------------------- Exported Functions -------------------- */ -func FirewallState() string { +func firewallStateLinux() string { + return "[red]NA[white]" +} + +func firewallStateMacOS() string { cmd := exec.Command(osxFirewallCmd, "--getglobalstate") str := wtf.ExecuteCommand(cmd) return status(str) } -func FirewallStealthState() string { +func FirewallState() string { + switch runtime.GOOS { + case "linux": + return firewallStateLinux() + case "macos": + return firewallStateMacOS() + default: + return "" + } +} + +func firewallStealthStateLinux() string { + return "[red]NA[white]" +} + +func firewallStealthStateMacOS() string { cmd := exec.Command(osxFirewallCmd, "--getstealthmode") str := wtf.ExecuteCommand(cmd) return status(str) } +func FirewallStealthState() string { + q + switch runtime.GOOS { + case "linux": + return firewallStealthStateLinux() + case "macos": + return firewallStealthStateMacOS() + default: + return "" + } +} + /* -------------------- Unexported Functions -------------------- */ func status(str string) string { diff --git a/security/security_data.go b/security/security_data.go index 1959b8f2..3a339340 100644 --- a/security/security_data.go +++ b/security/security_data.go @@ -1,11 +1,7 @@ package security -import ( - "strings" -) - type SecurityData struct { - Dns string + Dns []string FirewallEnabled string FirewallStealth string LoggedInUsers []string @@ -17,14 +13,11 @@ func NewSecurityData() *SecurityData { return &SecurityData{} } -func (data *SecurityData) DnsAt(idx int) string { - records := strings.Split(data.Dns, "\n") - - if len(records) > 0 && len(records) > idx { - return records[idx] - } else { - return "" +func (data SecurityData) DnsAt(idx int) string { + if len(data.Dns) > idx { + return data.Dns[idx] } + return "" } func (data *SecurityData) Fetch() { diff --git a/security/users.go b/security/users.go index 47e9f4c1..82479950 100644 --- a/security/users.go +++ b/security/users.go @@ -2,20 +2,56 @@ package security import ( "os/exec" + "runtime" "strings" "github.com/senorprogrammer/wtf/wtf" ) -// http://applehelpwriter.com/2017/05/21/how-to-reveal-hidden-users/ +func loggedInUsersLinux() []string { + cmd := exec.Command("who", "-us") + users := wtf.ExecuteCommand(cmd) -func LoggedInUsers() []string { + cleaned := []string{} + for _, u := range strings.Split(users, "\n") { + clean := true + col := strings.Split(u, " ") + if len(col) > 0 { + for _, cleanedU := range cleaned { + if strings.Compare(cleanedU, col[0]) == 0 { + clean = false + } + } + if clean { + cleaned = append(cleaned, col[0]) + } + } + + } + + return cleaned +} + +func loggedInUsersMacOs() []string { cmd := exec.Command("dscl", []string{".", "-list", "/Users"}...) users := wtf.ExecuteCommand(cmd) return cleanUsers(strings.Split(users, "\n")) } +// http://applehelpwriter.com/2017/05/21/how-to-reveal-hidden-users/ + +func LoggedInUsers() []string { + switch runtime.GOOS { + case "linux": + return loggedInUsersLinux() + case "macos": + return loggedInUsersMacOs() + default: + return []string{} + } +} + func cleanUsers(users []string) []string { rejects := []string{"_", "root", "nobody", "daemon", "Guest"} cleaned := []string{} diff --git a/security/wifi.go b/security/wifi.go index d84b808d..335a44f8 100644 --- a/security/wifi.go +++ b/security/wifi.go @@ -2,6 +2,7 @@ package security import ( "os/exec" + "runtime" "github.com/senorprogrammer/wtf/wtf" ) @@ -12,16 +13,58 @@ const osxWifiArg = "-I" /* -------------------- Exported Functions -------------------- */ -func WifiEncryption() string { +func wifiEncryptionLinux() string { + cmd := exec.Command("nmcli", "-t", "-f", "active,security", "dev", "wifi") + out := wtf.ExecuteCommand(cmd) + name := wtf.FindMatch(`yes:(.+)`, out) + if len(name) > 0 { + return name[0][1] + } + return "" +} + +func wifkEncryptionMacOS() string { name := wtf.FindMatch(`s*auth: (.+)s*`, wifiInfo()) return matchStr(name) } -func WifiName() string { +func WifiEncryption() string { + switch runtime.GOOS { + case "linux": + return wifiEncryptionLinux() + case "macos": + return wifkEncryptionMacOS() + default: + return "" + } +} + +func wifiNameMacOS() string { name := wtf.FindMatch(`s*SSID: (.+)s*`, wifiInfo()) return matchStr(name) } +func wifiNameLinux() string { + cmd := exec.Command("nmcli", "-t", "-f", "active,ssid", "dev", "wifi") + out := wtf.ExecuteCommand(cmd) + name := wtf.FindMatch(`yes:(.+)`, out) + if len(name) > 0 { + return name[0][1] + } + return "" +} + +func WifiName() string { + switch runtime.GOOS { + case "linux": + return wifiNameLinux() + case "macos": + return wifiNameMacOS() + default: + return "" + } +} + /* -------------------- Unexported Functions -------------------- */ func wifiInfo() string { From 2e126c3d239f935ee930d5e49c5b10b2ab5247ea Mon Sep 17 00:00:00 2001 From: James Sapara Date: Thu, 31 May 2018 12:30:52 -0700 Subject: [PATCH 2/6] removed q mistype --- security/firewall.go | 1 - 1 file changed, 1 deletion(-) diff --git a/security/firewall.go b/security/firewall.go index 5a7f4c75..33e0bfc8 100644 --- a/security/firewall.go +++ b/security/firewall.go @@ -46,7 +46,6 @@ func firewallStealthStateMacOS() string { } func FirewallStealthState() string { - q switch runtime.GOOS { case "linux": return firewallStealthStateLinux() From 9adc0ea98d840e24ccee4d4e9fb6c27fd97ed475 Mon Sep 17 00:00:00 2001 From: James Sapara Date: Thu, 31 May 2018 12:40:00 -0700 Subject: [PATCH 3/6] golang platform is darwin not macos --- security/dns.go | 2 +- security/firewall.go | 4 ++-- security/users.go | 2 +- security/wifi.go | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/security/dns.go b/security/dns.go index 93eadf43..343ddd94 100644 --- a/security/dns.go +++ b/security/dns.go @@ -39,7 +39,7 @@ func DnsServers() []string { switch runtime.GOOS { case "linux": return dnsLinux() - case "macos": + case "darwin": return dnsMacOS() default: return []string{runtime.GOOS} diff --git a/security/firewall.go b/security/firewall.go index 33e0bfc8..507103cc 100644 --- a/security/firewall.go +++ b/security/firewall.go @@ -27,7 +27,7 @@ func FirewallState() string { switch runtime.GOOS { case "linux": return firewallStateLinux() - case "macos": + case "darwin": return firewallStateMacOS() default: return "" @@ -49,7 +49,7 @@ func FirewallStealthState() string { switch runtime.GOOS { case "linux": return firewallStealthStateLinux() - case "macos": + case "darwin": return firewallStealthStateMacOS() default: return "" diff --git a/security/users.go b/security/users.go index 82479950..75b3cc2c 100644 --- a/security/users.go +++ b/security/users.go @@ -45,7 +45,7 @@ func LoggedInUsers() []string { switch runtime.GOOS { case "linux": return loggedInUsersLinux() - case "macos": + case "darwin": return loggedInUsersMacOs() default: return []string{} diff --git a/security/wifi.go b/security/wifi.go index 335a44f8..47c31fae 100644 --- a/security/wifi.go +++ b/security/wifi.go @@ -32,7 +32,7 @@ func WifiEncryption() string { switch runtime.GOOS { case "linux": return wifiEncryptionLinux() - case "macos": + case "darwin": return wifkEncryptionMacOS() default: return "" @@ -58,7 +58,7 @@ func WifiName() string { switch runtime.GOOS { case "linux": return wifiNameLinux() - case "macos": + case "darwin": return wifiNameMacOS() default: return "" From 1dae2456cc668566e3705e786c1d16afaae1f1a6 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 31 May 2018 08:48:10 -0700 Subject: [PATCH 4/6] Close #84. Add wtf.colors.background config option --- _sample_configs/complex_config.yml | 6 ++++++ _sample_configs/simple_config.yml | 1 + _site/content/posts/configuration/attributes.md | 8 ++++++++ docs/index.xml | 2 +- docs/posts/configuration/attributes/index.html | 8 ++++++++ docs/posts/index.xml | 2 +- todo/display.go | 2 +- wtf.go | 2 ++ wtf/text_widget.go | 1 + 9 files changed, 29 insertions(+), 3 deletions(-) diff --git a/_sample_configs/complex_config.yml b/_sample_configs/complex_config.yml index 8f443238..c21bdb72 100644 --- a/_sample_configs/complex_config.yml +++ b/_sample_configs/complex_config.yml @@ -1,4 +1,10 @@ wtf: + colors: + background: gray + border: + focusable: darkslateblue + focused: orange + normal: gray grid: # How _wide_ the columns are, in terminal characters. In this case we have # five columns, each of which are 37 characters wide diff --git a/_sample_configs/simple_config.yml b/_sample_configs/simple_config.yml index f0fe3be9..3d08666d 100644 --- a/_sample_configs/simple_config.yml +++ b/_sample_configs/simple_config.yml @@ -1,5 +1,6 @@ wtf: colors: + background: gray border: focusable: darkslateblue focused: orange diff --git a/_site/content/posts/configuration/attributes.md b/_site/content/posts/configuration/attributes.md index 1228a2a3..d709a8fb 100644 --- a/_site/content/posts/configuration/attributes.md +++ b/_site/content/posts/configuration/attributes.md @@ -10,6 +10,7 @@ See this +The color to draw the background of the app in. Use this to match your +terminal colors. May be over-written by individual module +configurations.
+Values: Any
X11 +color name. + `colors.border.focusable`
The color in which to draw the border of widgets that can accept keyboard focus.
diff --git a/docs/index.xml b/docs/index.xml index aa614d56..22a96bce 100644 --- a/docs/index.xml +++ b/docs/index.xml @@ -82,7 +82,7 @@ cmd The terminal command to be run, withouth the arguments. Ie: ping, whoami, cu https://wtfutil.com/posts/configuration/attributes/ The following top-level attributes are configurable in config.yml. See this example config file for more details. -wtf:colors:border:Focusable:"darkslateblue"focused:"orange"normal:"gray"grid:# How _wide_ the columns are, in terminal characters. In this case we have# six columns, each of which are 35 characters widecolumns:[35,35,35,35,35,35]# How _high_ the rows are, in terminal lines. In this case we have five rows# that support ten line of text, one of three lines, and one of fourrows:[10,10,10,10,10,3,4]# The app redraws itself once a secondrefreshInterval:1 Attributes colors. +wtf:colors:background:"red"border:Focusable:"darkslateblue"focused:"orange"normal:"gray"grid:# How _wide_ the columns are, in terminal characters. In this case we have# six columns, each of which are 35 characters widecolumns:[35,35,35,35,35,35]# How _high_ the rows are, in terminal lines. In this case we have five rows# that support ten line of text, one of three lines, and one of fourrows:[10,10,10,10,10,3,4]# The app redraws itself once a secondrefreshInterval:1 Attributes colors. diff --git a/docs/posts/configuration/attributes/index.html b/docs/posts/configuration/attributes/index.html index 33d24908..e06cedcc 100644 --- a/docs/posts/configuration/attributes/index.html +++ b/docs/posts/configuration/attributes/index.html @@ -113,6 +113,7 @@ See this example config file for more details.

wtf:
   colors:
+    background: "red"
     border:
       Focusable: "darkslateblue"
       focused: "orange"
@@ -129,6 +130,13 @@ See this   refreshInterval: 1

Attributes

+

colors.background
+The color to draw the background of the app in. Use this to match your +terminal colors. May be over-written by individual module +configurations.
+Values: Any
X11 +color name.

+

colors.border.focusable
The color in which to draw the border of widgets that can accept keyboard focus.
diff --git a/docs/posts/index.xml b/docs/posts/index.xml index 1be94e36..b867dd86 100644 --- a/docs/posts/index.xml +++ b/docs/posts/index.xml @@ -82,7 +82,7 @@ cmd The terminal command to be run, withouth the arguments. Ie: ping, whoami, cu https://wtfutil.com/posts/configuration/attributes/ The following top-level attributes are configurable in config.yml. See this example config file for more details. -wtf:colors:border:Focusable:&#34;darkslateblue&#34;focused:&#34;orange&#34;normal:&#34;gray&#34;grid:# How _wide_ the columns are, in terminal characters. In this case we have# six columns, each of which are 35 characters widecolumns:[35,35,35,35,35,35]# How _high_ the rows are, in terminal lines. In this case we have five rows# that support ten line of text, one of three lines, and one of fourrows:[10,10,10,10,10,3,4]# The app redraws itself once a secondrefreshInterval:1 Attributes colors. +wtf:colors:background:&#34;red&#34;border:Focusable:&#34;darkslateblue&#34;focused:&#34;orange&#34;normal:&#34;gray&#34;grid:# How _wide_ the columns are, in terminal characters. In this case we have# six columns, each of which are 35 characters widecolumns:[35,35,35,35,35,35]# How _high_ the rows are, in terminal lines. In this case we have five rows# that support ten line of text, one of three lines, and one of fourrows:[10,10,10,10,10,3,4]# The app redraws itself once a secondrefreshInterval:1 Attributes colors. diff --git a/todo/display.go b/todo/display.go index f66f578c..98f3eb72 100644 --- a/todo/display.go +++ b/todo/display.go @@ -33,7 +33,7 @@ func (widget *Widget) display() { } func (widget *Widget) formattedItemLine(item *Item, selectedItem *Item, maxLen int) string { - foreColor, backColor := "white", "black" + foreColor, backColor := "white", Config.UString("wtf.colors.background", "black") if item.Checked { foreColor = Config.UString("wtf.mods.todo.colors.checked", "white") diff --git a/wtf.go b/wtf.go index 63819b53..1cac8af9 100644 --- a/wtf.go +++ b/wtf.go @@ -225,6 +225,8 @@ func main() { pages.AddPage("grid", grid, true, true) app.SetInputCapture(keyboardIntercept) + grid.SetBackgroundColor(wtf.ColorFor(Config.UString("wtf.colors.background", "black"))) + // Loop in a routine to redraw the screen go redrawApp(app) go watchForConfigChanges(app, cmdFlags.Config, grid, pages) diff --git a/wtf/text_widget.go b/wtf/text_widget.go index 6acb1572..17342f0c 100644 --- a/wtf/text_widget.go +++ b/wtf/text_widget.go @@ -78,6 +78,7 @@ func (widget *TextWidget) TextView() *tview.TextView { func (widget *TextWidget) addView() { view := tview.NewTextView() + view.SetBackgroundColor(ColorFor(Config.UString("wtf.colors.background", "black"))) view.SetBorder(true) view.SetBorderColor(ColorFor(widget.BorderColor())) view.SetDynamicColors(true) From 0a1b1376be80bb4df563754b72f6bc04b5c21dc4 Mon Sep 17 00:00:00 2001 From: Lasantha Kularatne Date: Thu, 31 May 2018 13:35:47 -0500 Subject: [PATCH 5/6] NewRelic Module - Fixing out of bound error --- newrelic/widget.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/newrelic/widget.go b/newrelic/widget.go index 70466174..c4076af9 100644 --- a/newrelic/widget.go +++ b/newrelic/widget.go @@ -68,9 +68,14 @@ func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string { lineColor = "lightblue" } + var revLen = 8 + if revLen > len(deploy.Revision) { + revLen = len(deploy.Revision) + } + str = str + fmt.Sprintf( " [green]%s[%s] %s %-.16s[white]\n", - deploy.Revision[0:8], + deploy.Revision[0:revLen], lineColor, deploy.Timestamp.Format("Jan 02, 15:04 MST"), wtf.NameFromEmail(deploy.User), From f1548bb10fe4bf9a597010fffd385ba82db2c5a5 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Thu, 31 May 2018 13:57:33 -0700 Subject: [PATCH 6/6] Update readme with Go version warning --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7e5a84f0..5148713e 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ displaying infrequently-needed, but very important, daily data. ### Installation from Source -*Note:* Requires `go v1.7` or later (because it uses the `context` -package). +*Note:* This has only been tested to build against Go 1.9.2. It won't +work with Go versions < 1.7, and only _may_ work on other versions. ```bash go get github.com/senorprogrammer/wtf