github.com/wtfutil/wtf@v0.43.0/modules/security/wifi.go (about)

     1  package security
     2  
     3  import (
     4  	"os/exec"
     5  	"runtime"
     6  	"strings"
     7  
     8  	"github.com/wtfutil/wtf/utils"
     9  )
    10  
    11  // https://github.com/yelinaung/wifi-name/blob/master/wifi-name.go
    12  const osxWifiCmd = "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
    13  const osxWifiArg = "-I"
    14  
    15  /* -------------------- Exported Functions -------------------- */
    16  
    17  func WifiEncryption() string {
    18  	switch runtime.GOOS {
    19  	case "linux":
    20  		return wifiEncryptionLinux()
    21  	case "darwin":
    22  		return wifiEncryptionMacOS()
    23  	case "windows":
    24  		return wifiEncryptionWindows()
    25  	default:
    26  		return ""
    27  	}
    28  }
    29  
    30  func WifiName() string {
    31  	switch runtime.GOOS {
    32  	case "linux":
    33  		return wifiNameLinux()
    34  	case "darwin":
    35  		return wifiNameMacOS()
    36  	case "windows":
    37  		return wifiNameWindows()
    38  	default:
    39  		return ""
    40  	}
    41  }
    42  
    43  /* -------------------- Unexported Functions -------------------- */
    44  
    45  func wifiEncryptionLinux() string {
    46  	cmd := exec.Command("nmcli", "-t", "-f", "in-use,security", "dev", "wifi")
    47  	out := utils.ExecuteCommand(cmd)
    48  
    49  	name := utils.FindMatch(`\*:(.+)`, out)
    50  
    51  	if len(name) > 0 {
    52  		return name[0][1]
    53  	}
    54  
    55  	return ""
    56  }
    57  
    58  func wifiEncryptionMacOS() string {
    59  	name := utils.FindMatch(`s*auth: (.+)s*`, wifiInfo())
    60  	return matchStr(name)
    61  }
    62  
    63  func wifiInfo() string {
    64  	cmd := exec.Command(osxWifiCmd, osxWifiArg)
    65  	return utils.ExecuteCommand(cmd)
    66  }
    67  
    68  func wifiNameLinux() string {
    69  	cmd, _ := exec.Command("iwgetid", "-r").Output()
    70  	return string(cmd)
    71  }
    72  
    73  func wifiNameMacOS() string {
    74  	name := utils.FindMatch(`s*SSID: (.+)s*`, wifiInfo())
    75  	return matchStr(name)
    76  }
    77  
    78  func matchStr(data [][]string) string {
    79  	if len(data) <= 1 {
    80  		return ""
    81  	}
    82  
    83  	return data[1][1]
    84  }
    85  
    86  // Windows
    87  func wifiEncryptionWindows() string {
    88  	return parseWlanNetsh("Authentication")
    89  }
    90  
    91  func wifiNameWindows() string {
    92  	return parseWlanNetsh("SSID")
    93  }
    94  
    95  func parseWlanNetsh(target string) string {
    96  	cmd := exec.Command("netsh.exe", "wlan", "show", "interfaces")
    97  	out, err := cmd.Output()
    98  	if err != nil {
    99  		return ""
   100  	}
   101  	splits := strings.Split(string(out), "\n")
   102  	var words []string
   103  	for _, line := range splits {
   104  		token := strings.Split(line, ":")
   105  		for _, word := range token {
   106  			words = append(words, strings.TrimSpace(word))
   107  		}
   108  	}
   109  	for i, token := range words {
   110  		if token == target {
   111  			return words[i+1]
   112  		}
   113  	}
   114  	return "N/A"
   115  }