This commit is contained in:
jackislanding 2025-02-11 14:53:07 +01:00 committed by GitHub
commit af972914aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 907 additions and 560 deletions

View File

@ -11,24 +11,42 @@ type WakeOnLanDevice struct {
MacAddress string `json:"macAddress"` MacAddress string `json:"macAddress"`
} }
type UsbConfig struct {
VendorId string `json:"vendor_id"`
ProductId string `json:"product_id"`
SerialNumber string `json:"serial_number"`
Manufacturer string `json:"manufacturer"`
Product string `json:"product"`
}
type Config struct { type Config struct {
CloudURL string `json:"cloud_url"` CloudURL string `json:"cloud_url"`
CloudToken string `json:"cloud_token"` CloudToken string `json:"cloud_token"`
GoogleIdentity string `json:"google_identity"` GoogleIdentity string `json:"google_identity"`
JigglerEnabled bool `json:"jiggler_enabled"` JigglerEnabled bool `json:"jiggler_enabled"`
AutoUpdateEnabled bool `json:"auto_update_enabled"` AutoUpdateEnabled bool `json:"auto_update_enabled"`
IncludePreRelease bool `json:"include_pre_release"` IncludePreRelease bool `json:"include_pre_release"`
HashedPassword string `json:"hashed_password"` HashedPassword string `json:"hashed_password"`
LocalAuthToken string `json:"local_auth_token"` LocalAuthToken string `json:"local_auth_token"`
LocalAuthMode string `json:"localAuthMode"` //TODO: fix it with migration LocalAuthMode string `json:"localAuthMode"` //TODO: fix it with migration
WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"` WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"`
UsbConfig UsbConfig `json:"usb_config"`
VirtualMediaEnabled bool `json:"virtual_media_enabled"`
} }
const configPath = "/userdata/kvm_config.json" const configPath = "/userdata/kvm_config.json"
var defaultConfig = &Config{ var defaultConfig = &Config{
CloudURL: "https://api.jetkvm.com", CloudURL: "https://api.jetkvm.com",
AutoUpdateEnabled: true, // Set a default value AutoUpdateEnabled: true, // Set a default value
VirtualMediaEnabled: true,
UsbConfig: UsbConfig{
VendorId: "0x1d6b", //The Linux Foundation
ProductId: "0x0104", //Multifunction Composite Gadget
SerialNumber: "",
Manufacturer: "JetKVM",
Product: "JetKVM USB Emulation Device",
},
} }
var config *Config var config *Config

View File

@ -478,6 +478,29 @@ func rpcSetUsbEmulationState(enabled bool) error {
} }
} }
func rpcGetUsbConfig() (UsbConfig, error) {
LoadConfig()
return config.UsbConfig, nil
}
func rpcSetUsbConfig(usbConfig UsbConfig) error {
LoadConfig()
config.UsbConfig = usbConfig
err := UpdateGadgetConfig()
if err != nil {
return fmt.Errorf("failed to write gadget config: %w", err)
}
err = SaveConfig()
if err != nil {
return fmt.Errorf("failed to save usb config: %w", err)
}
log.Printf("[jsonrpc.go:rpcSetUsbConfig] usb config set to %s", usbConfig)
return nil
}
func rpcGetWakeOnLanDevices() ([]WakeOnLanDevice, error) { func rpcGetWakeOnLanDevices() ([]WakeOnLanDevice, error) {
LoadConfig() LoadConfig()
if config.WakeOnLanDevices == nil { if config.WakeOnLanDevices == nil {
@ -542,6 +565,8 @@ var rpcHandlers = map[string]RPCHandler{
"isUpdatePending": {Func: rpcIsUpdatePending}, "isUpdatePending": {Func: rpcIsUpdatePending},
"getUsbEmulationState": {Func: rpcGetUsbEmulationState}, "getUsbEmulationState": {Func: rpcGetUsbEmulationState},
"setUsbEmulationState": {Func: rpcSetUsbEmulationState, Params: []string{"enabled"}}, "setUsbEmulationState": {Func: rpcSetUsbEmulationState, Params: []string{"enabled"}},
"getUsbConfig": {Func: rpcGetUsbConfig},
"setUsbConfig": {Func: rpcSetUsbConfig, Params: []string{"usbConfig"}},
"checkMountUrl": {Func: rpcCheckMountUrl, Params: []string{"url"}}, "checkMountUrl": {Func: rpcCheckMountUrl, Params: []string{"url"}},
"getVirtualMediaState": {Func: rpcGetVirtualMediaState}, "getVirtualMediaState": {Func: rpcGetVirtualMediaState},
"getStorageSpace": {Func: rpcGetStorageSpace}, "getStorageSpace": {Func: rpcGetStorageSpace},

View File

@ -0,0 +1,215 @@
import { GridCard } from "@/components/Card";
import {useCallback, useEffect, useState} from "react";
import { Button } from "@components/Button";
import LogoBlueIcon from "@/assets/logo-blue.svg";
import LogoWhiteIcon from "@/assets/logo-white.svg";
import Modal from "@components/Modal";
import { InputFieldWithLabel } from "./InputField";
import { useJsonRpc } from "@/hooks/useJsonRpc";
import { useUsbConfigModalStore } from "@/hooks/stores";
export interface UsbConfigState {
vendor_id: string;
product_id: string;
serial_number: string;
manufacturer: string;
product: string;
}
export default function USBConfigDialog({
open,
setOpen,
}: {
open: boolean;
setOpen: (open: boolean) => void;
}) {
return (
<Modal open={open} onClose={() => setOpen(false)}>
<Dialog setOpen={setOpen} />
</Modal>
);
}
export function Dialog({ setOpen }: { setOpen: (open: boolean) => void }) {
const { modalView, setModalView } = useUsbConfigModalStore();
const [error, setError] = useState<string | null>(null);
const [send] = useJsonRpc();
const handleUsbConfigChange = useCallback((usbConfig: object) => {
send("setUsbConfig", { usbConfig }, resp => {
if ("error" in resp) {
setError(`Failed to update USB Config: ${resp.error.data || "Unknown error"}`);
return;
}
setModalView("updateUsbConfigSuccess");
});
}, [send, setModalView]);
return (
<GridCard cardClassName="relative max-w-lg mx-auto text-left pointer-events-auto dark:bg-slate-800">
<div className="p-10">
{modalView === "updateUsbConfig" && (
<UpdateUsbConfigModal
onSetUsbConfig={handleUsbConfigChange}
onCancel={() => setOpen(false)}
error={error}
/>
)}
{modalView === "updateUsbConfigSuccess" && (
<SuccessModal
headline="USB Configuration Updated Successfully"
description="You've successfully updated the USB Configuration"
onClose={() => setOpen(false)}
/>
)}
</div>
</GridCard>
);
}
function UpdateUsbConfigModal({
onSetUsbConfig,
onCancel,
error,
}: {
onSetUsbConfig: (usb_config: object) => void;
onCancel: () => void;
error: string | null;
}) {
const [usbConfigState, setUsbConfigState] = useState<UsbConfigState>({
vendor_id: '',
product_id: '',
serial_number: '',
manufacturer: '',
product: ''
});
const [send] = useJsonRpc();
const syncUsbConfig = useCallback(() => {
send("getUsbConfig", {}, resp => {
if ("error" in resp) {
console.error("Failed to load USB Config:", resp.error);
} else {
setUsbConfigState(resp.result as UsbConfigState);
}
});
}, [send, setUsbConfigState]);
// Load stored usb config from the backend
useEffect(() => {
syncUsbConfig();
}, [syncUsbConfig]);
const handleUsbVendorIdChange = (value: string) => {
setUsbConfigState({... usbConfigState, vendor_id: value})
};
const handleUsbProductIdChange = (value: string) => {
setUsbConfigState({... usbConfigState, product_id: value})
};
const handleUsbSerialChange = (value: string) => {
setUsbConfigState({... usbConfigState, serial_number: value})
};
const handleUsbManufacturer = (value: string) => {
setUsbConfigState({... usbConfigState, manufacturer: value})
};
const handleUsbProduct = (value: string) => {
setUsbConfigState({... usbConfigState, product: value})
};
return (
<div className="flex flex-col items-start justify-start space-y-4 text-left">
<div>
<img src={LogoWhiteIcon} alt="" className="h-[24px] hidden dark:block" />
<img src={LogoBlueIcon} alt="" className="h-[24px] dark:hidden" />
</div>
<div className="space-y-4">
<div>
<h2 className="text-lg font-semibold dark:text-white">USB Emulation Configuration</h2>
<p className="text-sm text-slate-600 dark:text-slate-400">
Set custom USB parameters to control how the USB device is emulated.
The device will rebind once the parameters are updated.
</p>
</div>
<InputFieldWithLabel
required
label="Vendor ID"
placeholder="Enter Vendor ID"
pattern="^0[xX][\da-fA-F]{4}$"
defaultValue={usbConfigState?.vendor_id}
onChange={e => handleUsbVendorIdChange(e.target.value)}
/>
<InputFieldWithLabel
required
label="Product ID"
placeholder="Enter Product ID"
pattern="^0[xX][\da-fA-F]{4}$"
defaultValue={usbConfigState?.product_id}
onChange={e => handleUsbProductIdChange(e.target.value)}
/>
<InputFieldWithLabel
required
label="Serial Number"
placeholder="Enter Serial Number"
defaultValue={usbConfigState?.serial_number}
onChange={e => handleUsbSerialChange(e.target.value)}
/>
<InputFieldWithLabel
required
label="Manufacturer"
placeholder="Enter Manufacturer"
defaultValue={usbConfigState?.manufacturer}
onChange={e => handleUsbManufacturer(e.target.value)}
/>
<InputFieldWithLabel
required
label="Product Name"
placeholder="Enter Product Name"
defaultValue={usbConfigState?.product}
onChange={e => handleUsbProduct(e.target.value)}
/>
<div className="flex gap-x-2">
<Button
size="SM"
theme="primary"
text="Update USB Config"
onClick={() => onSetUsbConfig(usbConfigState)}
/>
<Button size="SM" theme="light" text="Not Now" onClick={onCancel} />
</div>
{error && <p className="text-sm text-red-500">{error}</p>}
</div>
</div>
);
}
function SuccessModal({
headline,
description,
onClose,
}: {
headline: string;
description: string;
onClose: () => void;
}) {
return (
<div className="flex flex-col items-start justify-start w-full max-w-lg space-y-4 text-left">
<div>
<img src={LogoWhiteIcon} alt="" className="h-[24px] hidden dark:block" />
<img src={LogoBlueIcon} alt="" className="h-[24px] dark:hidden" />
</div>
<div className="space-y-4">
<div>
<h2 className="text-lg font-semibold dark:text-white">{headline}</h2>
<p className="text-sm text-slate-600 dark:text-slate-400">{description}</p>
</div>
<Button size="SM" theme="primary" text="Close" onClick={onClose} />
</div>
</div>
);
}

File diff suppressed because it is too large Load Diff

View File

@ -528,3 +528,19 @@ export const useLocalAuthModalStore = create<LocalAuthModalState>(set => ({
setModalView: view => set({ modalView: view }), setModalView: view => set({ modalView: view }),
setErrorMessage: message => set({ errorMessage: message }), setErrorMessage: message => set({ errorMessage: message }),
})); }));
interface UsbConfigModalState {
modalView:
| "updateUsbConfig"
| "updateUsbConfigSuccess";
errorMessage: string | null;
setModalView: (view: UsbConfigModalState["modalView"]) => void;
setErrorMessage: (message: string | null) => void;
}
export const useUsbConfigModalStore = create<UsbConfigModalState>(set => ({
modalView: "updateUsbConfig",
errorMessage: null,
setModalView: view => set({ modalView: view }),
setErrorMessage: message => set({ errorMessage: message }),
}));

49
usb.go
View File

@ -58,6 +58,44 @@ func init() {
//TODO: read hid reports(capslock, numlock, etc) from keyboardHidFile //TODO: read hid reports(capslock, numlock, etc) from keyboardHidFile
} }
func UpdateGadgetConfig() error {
LoadConfig()
gadgetAttrs := [][]string{
{"idVendor", config.UsbConfig.VendorId},
{"idProduct", config.UsbConfig.ProductId},
}
err := writeGadgetAttrs(kvmGadgetPath, gadgetAttrs)
if err != nil {
return err
}
log.Printf("Successfully updated usb gadget attributes: %v", gadgetAttrs)
strAttrs := [][]string{
{"serialnumber", config.UsbConfig.SerialNumber},
{"manufacturer", config.UsbConfig.Manufacturer},
{"product", config.UsbConfig.Product},
}
gadgetStringsPath := filepath.Join(kvmGadgetPath, "strings", "0x409")
err = os.MkdirAll(gadgetStringsPath, 0755)
if err != nil {
return err
}
err = writeGadgetAttrs(gadgetStringsPath, strAttrs)
if err != nil {
return err
}
log.Printf("Successfully updated usb string attributes: %s", strAttrs)
err = rebindUsb()
if err != nil {
return err
}
return nil
}
func writeGadgetAttrs(basePath string, attrs [][]string) error { func writeGadgetAttrs(basePath string, attrs [][]string) error {
for _, item := range attrs { for _, item := range attrs {
filePath := filepath.Join(basePath, item[0]) filePath := filepath.Join(basePath, item[0])
@ -79,10 +117,11 @@ func writeGadgetConfig() error {
return err return err
} }
LoadConfig()
err = writeGadgetAttrs(kvmGadgetPath, [][]string{ err = writeGadgetAttrs(kvmGadgetPath, [][]string{
{"bcdUSB", "0x0200"}, //USB 2.0 {"bcdUSB", "0x0200"}, //USB 2.0
{"idVendor", "0x1d6b"}, //The Linux Foundation {"idVendor", config.UsbConfig.VendorId},
{"idProduct", "0104"}, //Multifunction Composite Gadget¬ {"idProduct", config.UsbConfig.ProductId},
{"bcdDevice", "0100"}, {"bcdDevice", "0100"},
}) })
if err != nil { if err != nil {
@ -97,8 +136,8 @@ func writeGadgetConfig() error {
err = writeGadgetAttrs(gadgetStringsPath, [][]string{ err = writeGadgetAttrs(gadgetStringsPath, [][]string{
{"serialnumber", GetDeviceID()}, {"serialnumber", GetDeviceID()},
{"manufacturer", "JetKVM"}, {"manufacturer", config.UsbConfig.Manufacturer},
{"product", "JetKVM USB Emulation Device"}, {"product", config.UsbConfig.Product},
}) })
if err != nil { if err != nil {
return err return err