feat: add backend for configuring WiFi STA

This commit is contained in:
2025-05-13 13:44:26 +02:00
parent 48122cb61d
commit 35c72b1e39
6 changed files with 202 additions and 55 deletions

View File

@@ -6,15 +6,23 @@ import (
"log"
"net/http"
"github.com/0x1d/rcond/pkg/network"
network "github.com/0x1d/rcond/pkg/network"
"github.com/0x1d/rcond/pkg/user"
"github.com/gorilla/mux"
)
type configureAPRequest struct {
Interface string `json:"interface"`
SSID string `json:"ssid"`
Password string `json:"password"`
Interface string `json:"interface"`
SSID string `json:"ssid"`
Password string `json:"password"`
Autoconnect bool `json:"autoconnect"`
}
type configureSTARequest struct {
Interface string `json:"interface"`
SSID string `json:"ssid"`
Password string `json:"password"`
Autoconnect bool `json:"autoconnect"`
}
type networkUpRequest struct {
@@ -40,6 +48,30 @@ func writeError(w http.ResponseWriter, message string, code int) {
json.NewEncoder(w).Encode(errorResponse{Error: message})
}
func HandleConfigureSTA(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeError(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req configureSTARequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, err.Error(), http.StatusBadRequest)
return
}
log.Printf("Configuring station on interface %s", req.Interface)
uuid, err := network.ConfigureSTA(req.Interface, req.SSID, req.Password, req.Autoconnect)
if err != nil {
log.Printf("Failed to configure station on interface %s: %v", req.Interface, err)
writeError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"uuid": uuid})
}
func HandleConfigureAP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeError(w, "Method not allowed", http.StatusMethodNotAllowed)
@@ -53,7 +85,7 @@ func HandleConfigureAP(w http.ResponseWriter, r *http.Request) {
}
log.Printf("Configuring access point on interface %s", req.Interface)
uuid, err := network.ConfigureAP(req.Interface, req.SSID, req.Password)
uuid, err := network.ConfigureAP(req.Interface, req.SSID, req.Password, req.Autoconnect)
if err != nil {
log.Printf("Failed to configure access point on interface %s: %v", req.Interface, err)
writeError(w, err.Error(), http.StatusInternalServerError)

View File

@@ -59,6 +59,7 @@ func (s *Server) verifyToken(next http.HandlerFunc) http.HandlerFunc {
func (s *Server) RegisterRoutes() {
s.router.HandleFunc("/health", s.healthHandler).Methods(http.MethodGet)
s.router.HandleFunc("/network/ap", s.verifyToken(HandleConfigureAP)).Methods(http.MethodPost)
s.router.HandleFunc("/network/sta", s.verifyToken(HandleConfigureSTA)).Methods(http.MethodPost)
s.router.HandleFunc("/network/interface/{interface}", s.verifyToken(HandleNetworkUp)).Methods(http.MethodPut)
s.router.HandleFunc("/network/interface/{interface}", s.verifyToken(HandleNetworkDown)).Methods(http.MethodDelete)
s.router.HandleFunc("/network/connection/{uuid}", s.verifyToken(HandleNetworkRemove)).Methods(http.MethodDelete)