2017-05-19 19:29:57 +02:00
|
|
|
package server
|
2017-05-19 19:31:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct{}
|
|
|
|
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
toggle := r.URL.Query().Get("toggle")
|
|
|
|
switch toggle {
|
|
|
|
case "on":
|
|
|
|
if !h.WifiEnabled(w, r) {
|
|
|
|
if e := exec.Command("nmcli", "radio", "wifi", "on").Run(); e != nil {
|
|
|
|
http.Error(w, e.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e := exec.Command("nmcli", "connection", "up", "Hotspot", "ifname", "wlp2s0").Run(); e != nil {
|
|
|
|
http.Error(w, e.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case "off":
|
|
|
|
if h.WifiEnabled(w, r) {
|
|
|
|
if e := exec.Command("nmcli", "connection", "down", "Hotspot").Run(); e != nil {
|
|
|
|
http.Error(w, e.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e := exec.Command("nmcli", "radio", "wifi", "off").Run(); e != nil {
|
|
|
|
http.Error(w, e.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) WifiEnabled(w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
status, e := exec.Command("nmcli", "radio", "wifi").Output()
|
|
|
|
if e != nil {
|
|
|
|
http.Error(w, e.Error(), http.StatusInternalServerError)
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
if string(status) == "enabled" {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|