mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Merge branch 'master' of https://github.com/senorprogrammer/wtf
fixed conflict. Signed-off-by: Mike Lloyd <mike@reboot3times.org>
This commit is contained in:
@@ -2,13 +2,54 @@ package security
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
const dnsCmd = "networksetup"
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func DnsServers() string {
|
||||
cmd := exec.Command(dnsCmd, "-getdnsservers", "Wi-Fi")
|
||||
return wtf.ExecuteCommand(cmd)
|
||||
func DnsServers() []string {
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
return dnsLinux()
|
||||
case "darwin":
|
||||
return dnsMacOS()
|
||||
default:
|
||||
return []string{runtime.GOOS}
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
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)
|
||||
|
||||
lines := strings.Split(out, "\n")
|
||||
|
||||
if len(lines) > 0 {
|
||||
return lines
|
||||
} else {
|
||||
return []string{}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package security
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
@@ -12,27 +13,57 @@ const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw"
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func FirewallState() string {
|
||||
cmd := exec.Command(osxFirewallCmd, "--getglobalstate")
|
||||
str := wtf.ExecuteCommand(cmd)
|
||||
|
||||
return status(str)
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
return firewallStateLinux()
|
||||
case "darwin":
|
||||
return firewallStateMacOS()
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func FirewallStealthState() string {
|
||||
cmd := exec.Command(osxFirewallCmd, "--getstealthmode")
|
||||
str := wtf.ExecuteCommand(cmd)
|
||||
|
||||
return status(str)
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
return firewallStealthStateLinux()
|
||||
case "darwin":
|
||||
return firewallStealthStateMacOS()
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func status(str string) string {
|
||||
icon := "[red]off[white]"
|
||||
func firewallStateLinux() string {
|
||||
return "[red]NA[white]"
|
||||
}
|
||||
|
||||
func firewallStateMacOS() string {
|
||||
cmd := exec.Command(osxFirewallCmd, "--getglobalstate")
|
||||
str := wtf.ExecuteCommand(cmd)
|
||||
|
||||
return statusLabel(str)
|
||||
}
|
||||
|
||||
func firewallStealthStateLinux() string {
|
||||
return "[red]NA[white]"
|
||||
}
|
||||
|
||||
func firewallStealthStateMacOS() string {
|
||||
cmd := exec.Command(osxFirewallCmd, "--getstealthmode")
|
||||
str := wtf.ExecuteCommand(cmd)
|
||||
|
||||
return statusLabel(str)
|
||||
}
|
||||
|
||||
func statusLabel(str string) string {
|
||||
label := "off"
|
||||
|
||||
if strings.Contains(str, "enabled") {
|
||||
icon = "[green]on[white]"
|
||||
label = "on"
|
||||
}
|
||||
|
||||
return icon
|
||||
return label
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -1,21 +1,30 @@
|
||||
package security
|
||||
|
||||
// http://applehelpwriter.com/2017/05/21/how-to-reveal-hidden-users/
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
|
||||
// http://applehelpwriter.com/2017/05/21/how-to-reveal-hidden-users/
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func LoggedInUsers() []string {
|
||||
cmd := exec.Command("dscl", []string{".", "-list", "/Users"}...)
|
||||
users := wtf.ExecuteCommand(cmd)
|
||||
|
||||
return cleanUsers(strings.Split(users, "\n"))
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
return loggedInUsersLinux()
|
||||
case "darwin":
|
||||
return loggedInUsersMacOs()
|
||||
default:
|
||||
return []string{}
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func cleanUsers(users []string) []string {
|
||||
rejects := []string{"_", "root", "nobody", "daemon", "Guest"}
|
||||
cleaned := []string{}
|
||||
@@ -37,3 +46,38 @@ func cleanUsers(users []string) []string {
|
||||
|
||||
return cleaned
|
||||
}
|
||||
|
||||
func loggedInUsersLinux() []string {
|
||||
cmd := exec.Command("who", "-us")
|
||||
users := wtf.ExecuteCommand(cmd)
|
||||
|
||||
cleaned := []string{}
|
||||
|
||||
for _, user := range strings.Split(users, "\n") {
|
||||
clean := true
|
||||
col := strings.Split(user, " ")
|
||||
|
||||
if len(col) > 0 {
|
||||
for _, cleanedU := range cleaned {
|
||||
u := strings.TrimSpace(col[0])
|
||||
if len(u) == 0 || 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"))
|
||||
}
|
||||
|
||||
@@ -34,9 +34,8 @@ func (widget *Widget) Refresh() {
|
||||
data.Fetch()
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.View.Clear()
|
||||
|
||||
fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
|
||||
widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(data)))
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
@@ -46,12 +45,23 @@ func (widget *Widget) contentFrom(data *SecurityData) string {
|
||||
str = str + fmt.Sprintf(" %8s: %s\n", "Network", data.WifiName)
|
||||
str = str + fmt.Sprintf(" %8s: %s\n", "Crypto", data.WifiEncryption)
|
||||
str = str + "\n"
|
||||
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 + " [red]Firewall[white] [red]DNS[white]\n"
|
||||
str = str + fmt.Sprintf(" %8s: [%s]%-3s[white] %-16s\n", "Enabled", widget.labelColor(data.FirewallEnabled), data.FirewallEnabled, data.DnsAt(0))
|
||||
str = str + fmt.Sprintf(" %8s: [%s]%-3s[white] %-16s\n", "Stealth", widget.labelColor(data.FirewallStealth), 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
|
||||
}
|
||||
|
||||
func (widget *Widget) labelColor(label string) string {
|
||||
switch label {
|
||||
case "on":
|
||||
return "green"
|
||||
case "off":
|
||||
return "red"
|
||||
default:
|
||||
return "white"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package security
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"runtime"
|
||||
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
)
|
||||
@@ -13,22 +14,67 @@ const osxWifiArg = "-I"
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func WifiEncryption() string {
|
||||
name := wtf.FindMatch(`s*auth: (.+)s*`, wifiInfo())
|
||||
return matchStr(name)
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
return wifiEncryptionLinux()
|
||||
case "darwin":
|
||||
return wifiEncryptionMacOS()
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func WifiName() string {
|
||||
name := wtf.FindMatch(`s*SSID: (.+)s*`, wifiInfo())
|
||||
return matchStr(name)
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
return wifiNameLinux()
|
||||
case "darwin":
|
||||
return wifiNameMacOS()
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
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 wifiEncryptionMacOS() string {
|
||||
name := wtf.FindMatch(`s*auth: (.+)s*`, wifiInfo())
|
||||
return matchStr(name)
|
||||
}
|
||||
|
||||
func wifiInfo() string {
|
||||
cmd := exec.Command(osxWifiCmd, osxWifiArg)
|
||||
return wtf.ExecuteCommand(cmd)
|
||||
}
|
||||
|
||||
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 wifiNameMacOS() string {
|
||||
name := wtf.FindMatch(`s*SSID: (.+)s*`, wifiInfo())
|
||||
return matchStr(name)
|
||||
}
|
||||
|
||||
func matchStr(data [][]string) string {
|
||||
if len(data) <= 1 {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user