|
@@ -1,30 +1,106 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
-import "net/http"
|
|
|
|
-
|
|
|
|
-// Aux APIs for USB KVM mode
|
|
|
|
-func registerLocalAuxRoutes() {
|
|
|
|
- http.HandleFunc("/aux/switchusbkvm", auxMCU.HandleSwitchUSBToKVM)
|
|
|
|
- http.HandleFunc("/aux/switchusbremote", auxMCU.HandleSwitchUSBToRemote)
|
|
|
|
- http.HandleFunc("/aux/presspower", auxMCU.HandlePressPowerButton)
|
|
|
|
- http.HandleFunc("/aux/releasepower", auxMCU.HandleReleasePowerButton)
|
|
|
|
- http.HandleFunc("/aux/pressreset", auxMCU.HandlePressResetButton)
|
|
|
|
- http.HandleFunc("/aux/releasereset", auxMCU.HandleReleaseResetButton)
|
|
|
|
- http.HandleFunc("/aux/getuuid", auxMCU.HandleGetUUID)
|
|
|
|
|
|
+import (
|
|
|
|
+ "fmt"
|
|
|
|
+ "net/http"
|
|
|
|
+
|
|
|
|
+ "imuslab.com/dezukvm/dezukvmd/mod/utils"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+func register_auth_apis(mux *http.ServeMux) {
|
|
|
|
+ // Check API for session validation
|
|
|
|
+ mux.HandleFunc("/api/v1/check", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ if r.Method != http.MethodPost {
|
|
|
|
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ ok := authManager.UserIsLoggedIn(r)
|
|
|
|
+ if !ok {
|
|
|
|
+ http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
|
+ w.Write([]byte("{\"status\":\"ok\"}"))
|
|
|
|
+ })
|
|
|
|
+ // Login API
|
|
|
|
+ mux.HandleFunc("/api/v1/login", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ if r.Method != http.MethodPost {
|
|
|
|
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ err := authManager.LoginUser(w, r)
|
|
|
|
+ if err != nil {
|
|
|
|
+ http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
|
+ w.Write([]byte("{\"status\":\"success\"}"))
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ // Logout API
|
|
|
|
+ mux.HandleFunc("/api/v1/logout", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ if r.Method != http.MethodPost {
|
|
|
|
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ err := authManager.LogoutUser(w, r)
|
|
|
|
+ if err != nil {
|
|
|
|
+ http.Error(w, "Logout failed", http.StatusInternalServerError)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
|
+ w.Write([]byte("{\"status\":\"logged out\"}"))
|
|
|
|
+ })
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
-// Dummy Aux APIs for setups that do not have an aux MCU
|
|
|
|
-func registerDummyLocalAuxRoutes() {
|
|
|
|
- dummyHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
- w.WriteHeader(http.StatusNotImplemented)
|
|
|
|
- w.Write([]byte("Not implemented"))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- http.HandleFunc("/aux/switchusbkvm", dummyHandler)
|
|
|
|
- http.HandleFunc("/aux/switchusbremote", dummyHandler)
|
|
|
|
- http.HandleFunc("/aux/presspower", dummyHandler)
|
|
|
|
- http.HandleFunc("/aux/releasepower", dummyHandler)
|
|
|
|
- http.HandleFunc("/aux/pressreset", dummyHandler)
|
|
|
|
- http.HandleFunc("/aux/releasereset", dummyHandler)
|
|
|
|
- http.HandleFunc("/aux/getuuid", dummyHandler)
|
|
|
|
|
|
+func register_ipkvm_apis(mux *http.ServeMux) {
|
|
|
|
+ authManager.HandleFunc("/api/v1/stream/{uuid}/video", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ instanceUUID := r.PathValue("uuid")
|
|
|
|
+ fmt.Println("Requested video stream for instance UUID:", instanceUUID)
|
|
|
|
+ dezukvmManager.HandleVideoStreams(w, r, instanceUUID)
|
|
|
|
+ }, mux)
|
|
|
|
+
|
|
|
|
+ authManager.HandleFunc("/api/v1/stream/{uuid}/audio", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ instanceUUID := r.PathValue("uuid")
|
|
|
|
+ dezukvmManager.HandleAudioStreams(w, r, instanceUUID)
|
|
|
|
+ }, mux)
|
|
|
|
+
|
|
|
|
+ authManager.HandleFunc("/api/v1/hid/{uuid}/events", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ instanceUUID := r.PathValue("uuid")
|
|
|
|
+ dezukvmManager.HandleHIDEvents(w, r, instanceUUID)
|
|
|
|
+ }, mux)
|
|
|
|
+
|
|
|
|
+ authManager.HandleFunc("/api/v1/mass_storage/switch", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ if r.Method != http.MethodPost {
|
|
|
|
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ instanceUUID, err := utils.PostPara(r, "uuid")
|
|
|
|
+ if err != nil {
|
|
|
|
+ http.Error(w, "Missing or invalid uuid parameter", http.StatusBadRequest)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ side, err := utils.PostPara(r, "side")
|
|
|
|
+ if err != nil {
|
|
|
|
+ http.Error(w, "Missing or invalid side parameter", http.StatusBadRequest)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ switch side {
|
|
|
|
+ case "kvm":
|
|
|
|
+ dezukvmManager.HandleMassStorageSideSwitch(w, r, instanceUUID, true)
|
|
|
|
+ case "remote":
|
|
|
|
+ dezukvmManager.HandleMassStorageSideSwitch(w, r, instanceUUID, false)
|
|
|
|
+ default:
|
|
|
|
+ http.Error(w, "Invalid side parameter", http.StatusBadRequest)
|
|
|
|
+ }
|
|
|
|
+ }, mux)
|
|
|
|
+
|
|
|
|
+ authManager.HandleFunc("/api/v1/instances", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ if r.Method == http.MethodGet {
|
|
|
|
+ dezukvmManager.HandleListInstances(w, r)
|
|
|
|
+ } else {
|
|
|
|
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
+ }
|
|
|
|
+ }, mux)
|
|
}
|
|
}
|