wifi.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. wifi "imuslab.com/arozos/mod/network/wifi"
  7. prout "imuslab.com/arozos/mod/prouter"
  8. "imuslab.com/arozos/mod/utils"
  9. )
  10. /*
  11. Network WiFi Module
  12. This module handle wifi connections and scanning on the devices that support wpa_supplicant like the Raspberry Pi
  13. Require service launch with Dbus (Work well on stock Raspberry Pi OS)
  14. */
  15. var (
  16. wifiManager *wifi.WiFiManager
  17. )
  18. func WiFiInit() {
  19. //Start the Wifi Manager
  20. wifiManager = wifi.NewWiFiManager(sysdb, sudo_mode, *wpa_supplicant_path, *wan_interface_name)
  21. //Only activate script on linux and if hardware management is enabled
  22. router := prout.NewModuleRouter(prout.RouterOption{
  23. ModuleName: "System Setting",
  24. AdminOnly: true,
  25. UserHandler: userHandler,
  26. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  27. utils.SendErrorResponse(w, "Permission Denied")
  28. },
  29. })
  30. //Allow hardware management. Generate the endpoint for WiFi Control
  31. if *allow_hardware_management {
  32. //Register endpoints
  33. router.HandleFunc("/system/network/scanWifi", network_wifi_handleScan)
  34. router.HandleFunc("/system/network/connectWifi", network_wifi_handleConnect)
  35. router.HandleFunc("/system/network/removeWifi", network_wifi_handleWiFiRemove)
  36. router.HandleFunc("/system/network/wifiinfo", network_wifi_handleWiFiInfo)
  37. //Sudo mode only for wifi toggle
  38. if sudo_mode {
  39. router.HandleFunc("/system/network/power", network_wifi_handleWiFiPower)
  40. }
  41. //Register WiFi Settings if system have WiFi interface
  42. wlanInterfaces, _ := wifiManager.GetWirelessInterfaces()
  43. if len(wlanInterfaces) > 0 {
  44. //Contain at least 1 wireless interface Register System Settings
  45. registerSetting(settingModule{
  46. Name: "WiFi Info",
  47. Desc: "Current Connected WiFi Information",
  48. IconPath: "SystemAO/network/img/WiFi.png",
  49. Group: "Network",
  50. StartDir: "SystemAO/network/wifiinfo.html",
  51. })
  52. registerSetting(settingModule{
  53. Name: "WiFi Settings",
  54. Desc: "Setup WiFi Conenctions",
  55. IconPath: "SystemAO/network/img/WiFi.png",
  56. Group: "Network",
  57. StartDir: "SystemAO/network/wifi.html",
  58. RequireAdmin: true,
  59. })
  60. }
  61. }
  62. }
  63. func network_wifi_handleWiFiPower(w http.ResponseWriter, r *http.Request) {
  64. //Require admin permission to scan and connect wifi
  65. user, err := userHandler.GetUserInfoFromRequest(w, r)
  66. if err != nil {
  67. utils.SendErrorResponse(w, "Internal Server Error")
  68. return
  69. }
  70. if !user.IsAdmin() {
  71. utils.SendErrorResponse(w, "Permission Denied")
  72. return
  73. }
  74. status, _ := utils.PostPara(r, "status")
  75. if status == "" {
  76. //Show current power status
  77. infs, err := wifiManager.GetWirelessInterfaces()
  78. if err != nil {
  79. utils.SendErrorResponse(w, err.Error())
  80. return
  81. }
  82. type WlanInterfaceStatus struct {
  83. Name string
  84. Running bool
  85. }
  86. results := []WlanInterfaceStatus{}
  87. for _, inf := range infs {
  88. status, _ := wifiManager.GetInterfacePowerStatuts(strings.TrimSpace(inf))
  89. results = append(results, WlanInterfaceStatus{
  90. Name: inf,
  91. Running: status,
  92. })
  93. }
  94. js, _ := json.Marshal(results)
  95. utils.SendJSONResponse(w, string(js))
  96. } else {
  97. //Change current power status
  98. wlaninterface, err := utils.PostPara(r, "interface")
  99. if err != nil {
  100. utils.SendErrorResponse(w, "Invalid interface")
  101. return
  102. }
  103. if status == "on" {
  104. err := wifiManager.SetInterfacePower(wlaninterface, true)
  105. if err != nil {
  106. utils.SendErrorResponse(w, err.Error())
  107. } else {
  108. utils.SendOK(w)
  109. }
  110. } else if status == "off" {
  111. err := wifiManager.SetInterfacePower(wlaninterface, false)
  112. if err != nil {
  113. utils.SendErrorResponse(w, err.Error())
  114. } else {
  115. utils.SendOK(w)
  116. }
  117. } else {
  118. utils.SendErrorResponse(w, "Invalid status")
  119. }
  120. }
  121. }
  122. func network_wifi_handleScan(w http.ResponseWriter, r *http.Request) {
  123. //Require admin permission to scan and connect wifi
  124. user, err := userHandler.GetUserInfoFromRequest(w, r)
  125. if err != nil {
  126. utils.SendErrorResponse(w, "Internal Server Error")
  127. return
  128. }
  129. if !user.IsAdmin() {
  130. utils.SendErrorResponse(w, "Permission Denied")
  131. return
  132. }
  133. //Get a list of current on system wireless interface
  134. wirelessInterfaces, err := wifiManager.GetWirelessInterfaces()
  135. if err != nil {
  136. utils.SendErrorResponse(w, err.Error())
  137. return
  138. }
  139. if len(wirelessInterfaces) == 0 {
  140. //No wireless interface
  141. utils.SendErrorResponse(w, "Wireless Interface Not Found")
  142. return
  143. }
  144. //Get the first ethernet interface and use it to scan nearby wifi
  145. scannedWiFiInfo, err := wifiManager.ScanNearbyWiFi(wirelessInterfaces[0])
  146. if err != nil {
  147. utils.SendErrorResponse(w, err.Error())
  148. return
  149. }
  150. jsonString, _ := json.Marshal(scannedWiFiInfo)
  151. utils.SendJSONResponse(w, string(jsonString))
  152. }
  153. func network_wifi_handleConnect(w http.ResponseWriter, r *http.Request) {
  154. user, err := userHandler.GetUserInfoFromRequest(w, r)
  155. if err != nil {
  156. utils.SendErrorResponse(w, "Internal Server Error")
  157. return
  158. }
  159. //Get information from client and create a new network config file
  160. if !user.IsAdmin() {
  161. utils.SendErrorResponse(w, "Permission denied")
  162. return
  163. }
  164. ssid, err := utils.PostPara(r, "ESSID")
  165. if err != nil {
  166. utils.SendErrorResponse(w, "ESSID not given")
  167. return
  168. }
  169. connType, _ := utils.PostPara(r, "ConnType")
  170. password, _ := utils.PostPara(r, "pwd")
  171. systemWideLogger.PrintAndLog("WiFi", "WiFi Switch Request Received. Genering Network Configuration...", nil)
  172. identity, err := utils.PostPara(r, "identity")
  173. if err != nil {
  174. identity = ""
  175. }
  176. result, err := wifiManager.ConnectWiFi(ssid, password, connType, identity)
  177. if err != nil {
  178. utils.SendErrorResponse(w, err.Error())
  179. return
  180. }
  181. jsonString, err := json.Marshal(result)
  182. if err != nil {
  183. utils.SendErrorResponse(w, err.Error())
  184. return
  185. }
  186. utils.SendJSONResponse(w, string(jsonString))
  187. systemWideLogger.PrintAndLog("WiFi", "WiFi Connected", nil)
  188. }
  189. func network_wifi_handleWiFiRemove(w http.ResponseWriter, r *http.Request) {
  190. //Require admin permission to scan and connect wifi
  191. user, err := userHandler.GetUserInfoFromRequest(w, r)
  192. if err != nil {
  193. utils.SendErrorResponse(w, "Internal Server Error")
  194. return
  195. }
  196. if !user.IsAdmin() {
  197. utils.SendErrorResponse(w, "Permission Denied")
  198. return
  199. }
  200. //Get ESSID from post request
  201. ESSID, err := utils.PostPara(r, "ESSID")
  202. if err != nil {
  203. utils.SendErrorResponse(w, "ESSID not given")
  204. return
  205. }
  206. err = wifiManager.RemoveWifi(ESSID)
  207. if err != nil {
  208. utils.SendErrorResponse(w, err.Error())
  209. }
  210. utils.SendOK(w)
  211. }
  212. func network_wifi_handleWiFiInfo(w http.ResponseWriter, r *http.Request) {
  213. //Get and return the current conencted WiFi Information
  214. _, err := authAgent.GetUserName(w, r)
  215. if err != nil {
  216. utils.SendErrorResponse(w, "User not logged in")
  217. return
  218. }
  219. ESSID, interfaceName, err := wifiManager.GetConnectedWiFi()
  220. if err != nil {
  221. utils.SendErrorResponse(w, "Failed to retrieve WiFi Information")
  222. return
  223. }
  224. jsonString, _ := json.Marshal(map[string]string{
  225. "ESSID": ESSID,
  226. "Interface": interfaceName,
  227. })
  228. utils.SendJSONResponse(w, string(jsonString))
  229. }