wifi.go 6.9 KB

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