wifi_windows.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //go:build windows
  2. // +build windows
  3. package wifi
  4. /*
  5. WiFi connection module for Windows
  6. author: tobychui
  7. */
  8. import (
  9. "errors"
  10. "os/exec"
  11. "strconv"
  12. "strings"
  13. "imuslab.com/arozos/mod/info/logger"
  14. )
  15. // Toggle WiFi On Off. Only allow on sudo mode
  16. func (w *WiFiManager) SetInterfacePower(wlanInterface string, on bool) error {
  17. return errors.New("Windows WiFi function is currently readonly")
  18. }
  19. func (w *WiFiManager) GetInterfacePowerStatuts(wlanInterface string) (bool, error) {
  20. return false, errors.New("Platform not supported")
  21. }
  22. func (w *WiFiManager) ScanNearbyWiFi(interfaceName string) ([]WiFiInfo, error) {
  23. cmd := exec.Command("cmd", "/c", "chcp 65001 && netsh WLAN show networks mode=bssid")
  24. out, err := cmd.CombinedOutput()
  25. if err != nil {
  26. //No interface found on the system
  27. logger.PrintAndLog("Wifi", string(out), nil)
  28. return []WiFiInfo{}, errors.New(string(out))
  29. }
  30. //Filter the output
  31. output := string(out)
  32. results := []WiFiInfo{}
  33. var currentWiFiInfo *WiFiInfo = nil
  34. for _, line := range strings.Split(output, "\r\n") {
  35. line = strings.TrimSpace(line)
  36. for strings.Contains(line, " ") {
  37. line = strings.ReplaceAll(line, " ", "")
  38. }
  39. line = strings.TrimSpace(line)
  40. if len(line) == 0 {
  41. //This is an empty line
  42. continue
  43. }
  44. if line[:4] == "SSID" {
  45. //Starting a new WiFi Info
  46. if currentWiFiInfo != nil {
  47. currentWiFiInfo.Quality = "-"
  48. results = append(results, *currentWiFiInfo)
  49. }
  50. essid := ""
  51. tmp := strings.Split(line, ":")
  52. if len(tmp) > 1 {
  53. essid = tmp[1]
  54. }
  55. currentWiFiInfo = &WiFiInfo{
  56. ESSID: strings.TrimSpace(essid),
  57. }
  58. } else if line[:5] == "BSSID" {
  59. bssid := ""
  60. tmp := strings.Split(line, ":")
  61. if len(tmp) > 1 {
  62. tmp = tmp[1:]
  63. bssid = strings.Join(tmp, ":")
  64. }
  65. currentWiFiInfo.Address = strings.TrimSpace(bssid)
  66. } else if line[:7] == "Channel" {
  67. channel := ""
  68. tmp := strings.Split(line, ":")
  69. if len(tmp) > 1 {
  70. channel = tmp[1]
  71. }
  72. channel = strings.TrimSpace(channel)
  73. channelInt, err := strconv.Atoi(channel)
  74. if err != nil {
  75. channelInt = -1
  76. }
  77. currentWiFiInfo.Channel = channelInt
  78. } else if line[:6] == "Signal" {
  79. signal := ""
  80. tmp := strings.Split(line, ":")
  81. if len(tmp) > 1 {
  82. signal = tmp[1]
  83. }
  84. signal = strings.TrimSpace(signal)
  85. currentWiFiInfo.SignalLevel = signal
  86. } else if line[:10] == "Encryption" {
  87. encryp := ""
  88. tmp := strings.Split(line, ":")
  89. if len(tmp) > 1 {
  90. encryp = tmp[1]
  91. }
  92. encryp = strings.TrimSpace(encryp)
  93. if encryp == "CCMO" || encryp == "TKIP" {
  94. currentWiFiInfo.EncryptionKey = true
  95. } else {
  96. currentWiFiInfo.EncryptionKey = false
  97. }
  98. } else if line[:10] == "Radio type" {
  99. radtype := ""
  100. tmp := strings.Split(line, ":")
  101. if len(tmp) > 1 {
  102. radtype = tmp[1]
  103. }
  104. radtype = strings.TrimSpace(radtype)
  105. currentWiFiInfo.Frequency = radtype
  106. }
  107. }
  108. return results, nil
  109. }
  110. func (w *WiFiManager) GetWirelessInterfaces() ([]string, error) {
  111. //Try to get wireless interface info from cmd
  112. cmd := exec.Command("cmd", "/c", "chcp 65001 && netsh WLAN show drivers")
  113. out, err := cmd.CombinedOutput()
  114. if err != nil {
  115. //No interface found on the system
  116. logger.PrintAndLog("Wifi", string(out), nil)
  117. return []string{}, err
  118. }
  119. output := string(out)
  120. wlanInterfaces := []string{}
  121. for _, line := range strings.Split(output, "\r\n") {
  122. line = strings.TrimSpace(line)
  123. for strings.Contains(line, " ") {
  124. line = strings.ReplaceAll(line, " ", "")
  125. }
  126. if strings.Contains(line, "Interface name: ") {
  127. tmp := strings.Split(line, ":")
  128. if len(tmp) > 1 {
  129. thisInterfaceName := tmp[1]
  130. wlanInterfaces = append(wlanInterfaces, thisInterfaceName)
  131. }
  132. }
  133. }
  134. return wlanInterfaces, nil
  135. }
  136. func (w *WiFiManager) ConnectWiFi(ssid string, password string, connType string, identity string) (*WiFiConnectionResult, error) {
  137. return &WiFiConnectionResult{}, errors.New("Windows WiFi function is currently readonly")
  138. }
  139. // Get connected wifi ssid, interface name and error if any
  140. func (w *WiFiManager) GetConnectedWiFi() (string, string, error) {
  141. cmd := exec.Command("cmd", "/c", "chcp 65001 && netsh WLAN show interface")
  142. out, err := cmd.CombinedOutput()
  143. if err != nil {
  144. //No interface found on the system
  145. logger.PrintAndLog("Wifi", string(out), nil)
  146. return "", "", nil
  147. }
  148. output := string(out)
  149. //Things to be returned
  150. interfaceName := ""
  151. connectedSSID := ""
  152. for _, line := range strings.Split(output, "\r\n") {
  153. line = strings.TrimSpace(line)
  154. for strings.Contains(line, " ") {
  155. line = strings.ReplaceAll(line, " ", "")
  156. }
  157. if len(line) > 4 && line[:4] == "Name" {
  158. tmp := strings.Split(line, ":")
  159. if len(tmp) > 1 {
  160. interfaceName = tmp[1]
  161. }
  162. } else if len(line) > 4 && line[:4] == "SSID" {
  163. tmp := strings.Split(line, ":")
  164. if len(tmp) > 1 {
  165. connectedSSID = tmp[1]
  166. }
  167. }
  168. }
  169. return connectedSSID, interfaceName, nil
  170. }
  171. func (w *WiFiManager) RemoveWifi(ssid string) error {
  172. return errors.New("Windows WiFi function is currently readonly")
  173. }