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

Security module now displays logged-in users

This commit is contained in:
Chris Cummer 2018-05-22 17:51:59 -07:00
parent efaccc83ae
commit 38cf1d5632
10 changed files with 84 additions and 52 deletions

View File

@ -5,7 +5,7 @@ draft: false
---
Displays some general information about the state of the machine's wifi
connection, firewall, and DNS settings.
connection, firewall, DNS settings, and logged-in users.
<img src="/imgs/modules/security.png" width="320" height="192" alt="security screenshot" />
@ -29,6 +29,13 @@ connection, firewall, and DNS settings.
<li>Which <a hre="https://developers.cloudflare.com/1.1.1.1/what-is-1.1.1.1/">DNS resolvers</a> (servers) the machine is configured to use</li>
</ul>
#### Users
<ul class="list-ornate">
<li> Which users are logged into the machine. Note: Does not yet
show hidden users.</li>
</ul>
## Source Code
```bash

View File

@ -23,12 +23,14 @@ type Widget struct {
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" 🏃 Runner ", "cmdrunner", true),
TextWidget: wtf.NewTextWidget(" 🏃 Runner ", "cmdrunner", false),
args: wtf.ToStrs(Config.UList("wtf.mods.cmdrunner.args")),
cmd: Config.UString("wtf.mods.cmdrunner.cmd"),
}
widget.View.SetWrap(true)
return &widget
}

View File

@ -197,8 +197,8 @@ position Where in the grid this module&amp;rsquo;s widget will be displayed.</de
<pubDate>Tue, 08 May 2018 20:33:28 -0700</pubDate>
<guid>http://wtfutil.com/posts/modules/security/</guid>
<description>Displays some general information about the state of the machine&amp;rsquo;s wifi connection, firewall, and DNS settings.
Wifi Network The name of the current network Whether or not the network uses encryption and if so, what flavour Firewall Whether or not the firewall is enabled Whether or not Stealth Mode is enabled DNS Which DNS resolvers (servers) the machine is configured to use Source Code wtf/security Required ENV Variables None.</description>
<description>Displays some general information about the state of the machine&amp;rsquo;s wifi connection, firewall, DNS settings, and logged-in users.
Wifi Network The name of the current network Whether or not the network uses encryption and if so, what flavour Firewall Whether or not the firewall is enabled Whether or not Stealth Mode is enabled DNS Which DNS resolvers (servers) the machine is configured to use Users Which users are logged into the machine.</description>
</item>
<item>

View File

@ -197,8 +197,8 @@ position Where in the grid this module&amp;rsquo;s widget will be displayed.</de
<pubDate>Tue, 08 May 2018 20:33:28 -0700</pubDate>
<guid>http://wtfutil.com/posts/modules/security/</guid>
<description>Displays some general information about the state of the machine&amp;rsquo;s wifi connection, firewall, and DNS settings.
Wifi Network The name of the current network Whether or not the network uses encryption and if so, what flavour Firewall Whether or not the firewall is enabled Whether or not Stealth Mode is enabled DNS Which DNS resolvers (servers) the machine is configured to use Source Code wtf/security Required ENV Variables None.</description>
<description>Displays some general information about the state of the machine&amp;rsquo;s wifi connection, firewall, DNS settings, and logged-in users.
Wifi Network The name of the current network Whether or not the network uses encryption and if so, what flavour Firewall Whether or not the firewall is enabled Whether or not Stealth Mode is enabled DNS Which DNS resolvers (servers) the machine is configured to use Users Which users are logged into the machine.</description>
</item>
<item>

View File

@ -108,7 +108,7 @@
<p>Displays some general information about the state of the machine&rsquo;s wifi
connection, firewall, and DNS settings.</p>
connection, firewall, DNS settings, and logged-in users.</p>
<p><img src="/imgs/modules/security.png" width="320" height="192" alt="security screenshot" /></p>
@ -132,6 +132,13 @@ connection, firewall, and DNS settings.</p>
<li>Which <a hre="https://developers.cloudflare.com/1.1.1.1/what-is-1.1.1.1/">DNS resolvers</a> (servers) the machine is configured to use</li>
</ul>
<h4 id="users">Users</h4>
<ul class="list-ornate">
<li> Which users are logged into the machine. Note: Does not yet
show hidden users.</li>
</ul>
<h2 id="source-code">Source Code</h2>
<div class="highlight"><pre class="chroma"><code class="language-bash" data-lang="bash">wtf/security</code></pre></div>
<h2 id="required-env-variables">Required ENV Variables</h2>

View File

@ -8,6 +8,7 @@ type SecurityData struct {
Dns string
FirewallEnabled string
FirewallStealth string
LoggedInUsers []string
WifiEncryption string
WifiName string
}
@ -30,6 +31,7 @@ func (data *SecurityData) Fetch() {
data.Dns = DnsServers()
data.FirewallEnabled = FirewallState()
data.FirewallStealth = FirewallStealthState()
data.LoggedInUsers = LoggedInUsers()
data.WifiName = WifiName()
data.WifiEncryption = WifiEncryption()
}

39
security/users.go Normal file
View File

@ -0,0 +1,39 @@
package security
import (
"os/exec"
"strings"
"github.com/senorprogrammer/wtf/wtf"
)
// http://applehelpwriter.com/2017/05/21/how-to-reveal-hidden-users/
func LoggedInUsers() []string {
cmd := exec.Command("dscl", []string{".", "-list", "/Users"}...)
users := wtf.ExecuteCommand(cmd)
return cleanUsers(strings.Split(users, "\n"))
}
func cleanUsers(users []string) []string {
rejects := []string{"_", "root", "nobody", "daemon", "Guest"}
cleaned := []string{}
for _, user := range users {
clean := true
for _, reject := range rejects {
if strings.HasPrefix(user, reject) {
clean = false
continue
}
}
if clean && user != "" {
cleaned = append(cleaned, user)
}
}
return cleaned
}

View File

@ -2,6 +2,7 @@ package security
import (
"fmt"
"strings"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
@ -41,7 +42,6 @@ func (widget *Widget) Refresh() {
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) contentFrom(data *SecurityData) string {
str := " [red]WiFi[white]\n"
str = str + fmt.Sprintf(" %8s: %s\n", "Network", data.WifiName)
str = str + fmt.Sprintf(" %8s: %s\n", "Crypto", data.WifiEncryption)
@ -49,6 +49,9 @@ func (widget *Widget) contentFrom(data *SecurityData) string {
str = str + " [red]Firewall[white] [red]DNS[white]\n"
str = str + fmt.Sprintf(" %8s: %4s %12s\n", "Enabled", data.FirewallEnabled, data.DnsAt(0))
str = str + fmt.Sprintf(" %8s: %4s %12s\n", "Stealth", data.FirewallStealth, data.DnsAt(1))
str = str + "\n"
str = str + " [red]Users[white]\n"
str = str + fmt.Sprintf(" %s", strings.Join(data.LoggedInUsers, ", "))
return str
}

View File

@ -1,65 +1,32 @@
package security
import (
"io/ioutil"
"os/exec"
"regexp"
"github.com/senorprogrammer/wtf/wtf"
)
// https://github.com/yelinaung/wifi-name/blob/master/wifi-name.go
const osxWifiCmd = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
const osxWifiArg = "-I"
/* -------------------- 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)
name := wtf.FindMatch(`s*auth: (.+)s*`, wifiInfo())
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)
name := wtf.FindMatch(`s*SSID: (.+)s*`, wifiInfo())
return matchStr(name)
}
/* -------------------- Unexported Functions -------------------- */
func findMatch(pattern string, data string) [][]string {
r := regexp.MustCompile(pattern)
name := r.FindAllStringSubmatch(data, -1)
return name
func wifiInfo() string {
cmd := exec.Command(osxWifiCmd, osxWifiArg)
return wtf.ExecuteCommand(cmd)
}
func matchStr(data [][]string) string {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os/exec"
"regexp"
"strings"
"time"
@ -51,6 +52,11 @@ func Exclude(strs []string, val string) bool {
return true
}
func FindMatch(pattern string, data string) [][]string {
r := regexp.MustCompile(pattern)
return r.FindAllStringSubmatch(data, -1)
}
func NameFromEmail(email string) string {
parts := strings.Split(email, "@")
return strings.Title(strings.Replace(parts[0], ".", " ", -1))
@ -68,10 +74,9 @@ func NamesFromEmails(emails []string) []string {
// OpenFile opens the file defined in `path` via the operating system
func OpenFile(path string) {
confDir, _ := ConfigDir()
filePath := fmt.Sprintf("%s/%s", confDir, path)
filePath, _ := ExpandHomeDir(path)
cmd := exec.Command("open", filePath)
ExecuteCommand(cmd)
}