wifi_windows.go 4.8 KB

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