mirror of https://github.com/jetkvm/kvm.git
Compare commits
4 Commits
88f69f1fa5
...
255f18a2ad
| Author | SHA1 | Date |
|---|---|---|
|
|
255f18a2ad | |
|
|
36f06a064a | |
|
|
dafebacdfd | |
|
|
f0595fff40 |
|
|
@ -177,7 +177,7 @@ func (u *UsbGadget) Init() error {
|
|||
|
||||
u.udc = udcs[0]
|
||||
|
||||
err := u.configureUsbGadget(false)
|
||||
err := u.configureUsbGadget(false, true)
|
||||
if err != nil {
|
||||
return u.logError("unable to initialize USB stack", err)
|
||||
}
|
||||
|
|
@ -185,13 +185,13 @@ func (u *UsbGadget) Init() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (u *UsbGadget) UpdateGadgetConfig() error {
|
||||
func (u *UsbGadget) UpdateGadgetConfig(resetUsbIfNeeded bool) error {
|
||||
u.configLock.Lock()
|
||||
defer u.configLock.Unlock()
|
||||
|
||||
u.loadGadgetConfig()
|
||||
|
||||
err := u.configureUsbGadget(true)
|
||||
err := u.configureUsbGadget(true, resetUsbIfNeeded)
|
||||
if err != nil {
|
||||
return u.logError("unable to update gadget config", err)
|
||||
}
|
||||
|
|
@ -199,14 +199,28 @@ func (u *UsbGadget) UpdateGadgetConfig() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (u *UsbGadget) configureUsbGadget(resetUsb bool) error {
|
||||
return u.WithTransaction(func() error {
|
||||
func (u *UsbGadget) configureUsbGadget(resetUsb bool, resetUsbIfNeeded bool) error {
|
||||
f := func(resetUsbBefore bool, resetUsbAfter bool) func() error {
|
||||
return func() error {
|
||||
if resetUsbBefore {
|
||||
u.tx.RebindUsb(true)
|
||||
}
|
||||
u.tx.MountConfigFS()
|
||||
u.tx.CreateConfigPath()
|
||||
u.tx.WriteGadgetConfig()
|
||||
if resetUsb {
|
||||
if resetUsbAfter {
|
||||
u.tx.RebindUsb(true)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// initial attempt to configure the gadget
|
||||
err := u.WithTransaction(f(false, resetUsb))
|
||||
if err != nil && !resetUsbIfNeeded {
|
||||
return err
|
||||
}
|
||||
|
||||
// if the initial attempt failed, try to configure the gadget again with the resetUsb flag
|
||||
return u.WithTransaction(f(true, resetUsb))
|
||||
}
|
||||
|
|
|
|||
10
jsonrpc.go
10
jsonrpc.go
|
|
@ -678,7 +678,7 @@ func rpcSetUsbConfig(usbConfig usbgadget.Config) error {
|
|||
LoadConfig()
|
||||
config.UsbConfig = &usbConfig
|
||||
gadget.SetGadgetConfig(config.UsbConfig)
|
||||
return updateUsbRelatedConfig()
|
||||
return updateUsbRelatedConfig(false)
|
||||
}
|
||||
|
||||
func rpcGetWakeOnLanDevices() ([]WakeOnLanDevice, error) {
|
||||
|
|
@ -890,8 +890,8 @@ func rpcGetUsbDevices() (usbgadget.Devices, error) {
|
|||
return *config.UsbDevices, nil
|
||||
}
|
||||
|
||||
func updateUsbRelatedConfig() error {
|
||||
if err := gadget.UpdateGadgetConfig(); err != nil {
|
||||
func updateUsbRelatedConfig(resetUsbIfNeeded bool) error {
|
||||
if err := gadget.UpdateGadgetConfig(resetUsbIfNeeded); err != nil {
|
||||
return fmt.Errorf("failed to write gadget config: %w", err)
|
||||
}
|
||||
if err := SaveConfig(); err != nil {
|
||||
|
|
@ -903,7 +903,7 @@ func updateUsbRelatedConfig() error {
|
|||
func rpcSetUsbDevices(usbDevices usbgadget.Devices) error {
|
||||
config.UsbDevices = &usbDevices
|
||||
gadget.SetGadgetDevices(config.UsbDevices)
|
||||
return updateUsbRelatedConfig()
|
||||
return updateUsbRelatedConfig(false)
|
||||
}
|
||||
|
||||
func rpcSetUsbDeviceState(device string, enabled bool) error {
|
||||
|
|
@ -920,7 +920,7 @@ func rpcSetUsbDeviceState(device string, enabled bool) error {
|
|||
return fmt.Errorf("invalid device: %s", device)
|
||||
}
|
||||
gadget.SetGadgetDevices(config.UsbDevices)
|
||||
return updateUsbRelatedConfig()
|
||||
return updateUsbRelatedConfig(false)
|
||||
}
|
||||
|
||||
func rpcSetCloudUrl(apiUrl string, appUrl string) error {
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ func setMassStorageMode(cdrom bool) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
return gadget.UpdateGadgetConfig()
|
||||
return gadget.UpdateGadgetConfig(true)
|
||||
}
|
||||
|
||||
func mountImage(imagePath string) error {
|
||||
|
|
|
|||
24
web.go
24
web.go
|
|
@ -8,6 +8,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"path/filepath"
|
||||
|
|
@ -184,6 +185,8 @@ func setupRouter() *gin.Engine {
|
|||
protected.PUT("/auth/password-local", handleUpdatePassword)
|
||||
protected.DELETE("/auth/local-password", handleDeletePassword)
|
||||
protected.POST("/storage/upload", handleUploadHttp)
|
||||
|
||||
protected.POST("/device/send-wol/:mac-addr", handleSendWOLMagicPacket)
|
||||
}
|
||||
|
||||
// Catch-all route for SPA
|
||||
|
|
@ -341,7 +344,6 @@ func handleWebRTCSignalWsMessages(
|
|||
|
||||
l.Trace().Msg("sending ping frame")
|
||||
err := wsCon.Ping(runCtx)
|
||||
|
||||
if err != nil {
|
||||
l.Warn().Str("error", err.Error()).Msg("websocket ping error")
|
||||
cancelRun()
|
||||
|
|
@ -807,3 +809,23 @@ func handleSetup(c *gin.Context) {
|
|||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Device setup completed successfully"})
|
||||
}
|
||||
|
||||
func handleSendWOLMagicPacket(c *gin.Context) {
|
||||
inputMacAddr := c.Param("mac-addr")
|
||||
macAddr, err := net.ParseMAC(inputMacAddr)
|
||||
if err != nil {
|
||||
logger.Warn().Err(err).Str("sendWol", inputMacAddr).Msg("Invalid mac address provided")
|
||||
c.String(http.StatusBadRequest, "Invalid mac address provided")
|
||||
return
|
||||
}
|
||||
|
||||
macAddrString := macAddr.String()
|
||||
err = rpcSendWOLMagicPacket(macAddrString)
|
||||
if err != nil {
|
||||
logger.Warn().Err(err).Str("sendWOL", macAddrString).Msg("Failed to send WOL magic packet")
|
||||
c.String(http.StatusInternalServerError, "Failed to send WOL to %s: %v", macAddrString, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.String(http.StatusOK, "WOL sent to %s ", macAddr)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue