diff --git a/config.go b/config.go index e4e27d7..005efc8 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,11 @@ type WakeOnLanDevice struct { MacAddress string `json:"macAddress"` } +type NameConfig struct { + Name string `json:"name"` + DNS string `json:"dns"` +} + type UsbConfig struct { VendorId string `json:"vendor_id"` ProductId string `json:"product_id"` @@ -34,6 +39,7 @@ type Config struct { WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"` EdidString string `json:"hdmi_edid_string"` ActiveExtension string `json:"active_extension"` + NameConfig NameConfig `json:"name_config"` DisplayMaxBrightness int `json:"display_max_brightness"` DisplayDimAfterSec int `json:"display_dim_after_sec"` DisplayOffAfterSec int `json:"display_off_after_sec"` @@ -57,6 +63,10 @@ var defaultConfig = &Config{ Manufacturer: "JetKVM", Product: "USB Emulation Device", }, + NameConfig: NameConfig{ + Name: "JetKVM", + DNS: "jetkvm.local", + }, } var ( diff --git a/go.mod b/go.mod index 5748e64..68408af 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/creack/pty v1.1.23 github.com/gin-gonic/gin v1.9.1 github.com/google/uuid v1.6.0 + github.com/gosimple/slug v1.15.0 github.com/gwatts/rootcerts v0.0.0-20240401182218-3ab9db955caf github.com/hanwen/go-fuse/v2 v2.5.1 github.com/hashicorp/go-envparse v0.1.0 @@ -46,6 +47,7 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.20.0 // indirect github.com/goccy/go-json v0.10.2 // indirect + github.com/gosimple/unidecode v1.0.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.11 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect diff --git a/go.sum b/go.sum index a5ce4cd..8f6f732 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,10 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gosimple/slug v1.15.0 h1:wRZHsRrRcs6b0XnxMUBM6WK1U1Vg5B0R7VkIf1Xzobo= +github.com/gosimple/slug v1.15.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ= +github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o= +github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc= github.com/gwatts/rootcerts v0.0.0-20240401182218-3ab9db955caf h1:JO6ISZIvEUitto5zjQ3/VEnDM5rPbqIFuOhS0U0ByeA= github.com/gwatts/rootcerts v0.0.0-20240401182218-3ab9db955caf/go.mod h1:5Kt9XkWvkGi2OHOq0QsGxebHmhCcqJ8KCbNg/a6+n+g= github.com/hanwen/go-fuse/v2 v2.5.1 h1:OQBE8zVemSocRxA4OaFJbjJ5hlpCmIWbGr7r0M4uoQQ= diff --git a/jsonrpc.go b/jsonrpc.go index 40be85c..de3c692 100644 --- a/jsonrpc.go +++ b/jsonrpc.go @@ -13,6 +13,7 @@ import ( "strconv" "time" + "github.com/gosimple/slug" "github.com/pion/webrtc/v4" "go.bug.st/serial" ) @@ -43,6 +44,11 @@ type BacklightSettings struct { OffAfter int `json:"off_after"` } +type NameSettings struct { + Name string `json:"name"` + DNS string `json:"dns"` +} + func writeJSONRPCResponse(response JSONRPCResponse, session *Session) { responseBytes, err := json.Marshal(response) if err != nil { @@ -293,6 +299,28 @@ type SSHKeyState struct { SSHKey string `json:"sshKey"` } +func rpcGetNameConfig() (NameConfig, error) { + return config.NameConfig, nil +} + +func rpcSetNameConfig(deviceName string) (NameConfig, error) { + config.NameConfig = NameConfig{ + Name: deviceName, + DNS: slug.Make(deviceName) + ".local", + } + + RestartMDNS() + + err := SaveConfig() + if err != nil { + return NameConfig{}, fmt.Errorf("failed to save device name: %w", err) + } + + nameConfig := config.NameConfig + log.Printf("[jsonrpc.go:rpcSetNameConfig] device name set to %s, dns name set to %s", nameConfig.Name, nameConfig.DNS) + return nameConfig, nil +} + func rpcGetDevModeState() (DevModeState, error) { devModeEnabled := false if _, err := os.Stat(devModeFile); err != nil { @@ -798,6 +826,8 @@ var rpcHandlers = map[string]RPCHandler{ "setDevChannelState": {Func: rpcSetDevChannelState, Params: []string{"enabled"}}, "getUpdateStatus": {Func: rpcGetUpdateStatus}, "tryUpdate": {Func: rpcTryUpdate}, + "setNameConfig": {Func: rpcSetNameConfig, Params: []string{"deviceName"}}, + "getNameConfig": {Func: rpcGetNameConfig}, "getDevModeState": {Func: rpcGetDevModeState}, "setDevModeState": {Func: rpcSetDevModeState, Params: []string{"enabled"}}, "getSSHKeyState": {Func: rpcGetSSHKeyState}, diff --git a/network.go b/network.go index 3553015..474dab8 100644 --- a/network.go +++ b/network.go @@ -113,6 +113,13 @@ func checkNetworkState() { } } +func RestartMDNS() { + err := startMDNS() + if err != nil { + return + } +} + func startMDNS() error { // If server was previously running, stop it if mDNSConn != nil { @@ -124,7 +131,7 @@ func startMDNS() error { } // Start a new server - fmt.Printf("Starting mDNS server on jetkvm.local\n") + fmt.Printf("Starting mDNS server on %v\n", config.NameConfig.DNS) addr4, err := net.ResolveUDPAddr("udp4", mdns.DefaultAddressIPv4) if err != nil { return err @@ -146,7 +153,7 @@ func startMDNS() error { } mDNSConn, err = mdns.Server(ipv4.NewPacketConn(l4), ipv6.NewPacketConn(l6), &mdns.Config{ - LocalNames: []string{"jetkvm.local"}, //TODO: make it configurable + LocalNames: []string{config.NameConfig.DNS}, }) if err != nil { mDNSConn = nil diff --git a/ui/src/hooks/stores.ts b/ui/src/hooks/stores.ts index ac8ad7d..3d1aae9 100644 --- a/ui/src/hooks/stores.ts +++ b/ui/src/hooks/stores.ts @@ -23,6 +23,11 @@ const appendStatToMap = ( export type AvailableSidebarViews = "connection-stats"; export type AvailableTerminalTypes = "kvm" | "serial" | "none"; +export interface NameConfig { + name: string; + dns: string; +} + export interface User { sub: string; email?: string; @@ -273,6 +278,9 @@ interface SettingsState { backlightSettings: BacklightSettings; setBacklightSettings: (settings: BacklightSettings) => void; + + nameConfig: NameConfig; + setNameConfig: (config: NameConfig) => void; } export const useSettingsStore = create( @@ -298,6 +306,12 @@ export const useSettingsStore = create( }, setBacklightSettings: (settings: BacklightSettings) => set({ backlightSettings: settings }), + + nameConfig: { + name: "JetKVM", + dns: "jetkvm.local" + }, + setNameConfig: (config: NameConfig) => set({ nameConfig: config }), }), { name: "settings", diff --git a/ui/src/routes/devices.$id.settings.appearance.tsx b/ui/src/routes/devices.$id.settings.appearance.tsx index 11a9536..b5a0796 100644 --- a/ui/src/routes/devices.$id.settings.appearance.tsx +++ b/ui/src/routes/devices.$id.settings.appearance.tsx @@ -1,12 +1,22 @@ import { useCallback, useState } from "react"; +import { Button } from "@components/Button"; +import { InputFieldWithLabel } from "@components/InputField"; import { SettingsPageHeader } from "../components/SettingsPageheader"; import { SelectMenuBasic } from "../components/SelectMenuBasic"; import { SettingsItem } from "./devices.$id.settings"; +import {NameConfig, useSettingsStore} from "@/hooks/stores"; +import { useJsonRpc } from "@/hooks/useJsonRpc"; +import notifications from "@/notifications"; export default function SettingsAppearanceRoute() { const [currentTheme, setCurrentTheme] = useState(() => { return localStorage.theme || "system"; }); + const [send] = useJsonRpc(); + const [name, setName] = useState(""); + + const nameConfigSettings = useSettingsStore(state => state.nameConfig); + const setNameConfigSettings = useSettingsStore(state => state.setNameConfig); const handleThemeChange = useCallback((value: string) => { const root = document.documentElement; @@ -26,7 +36,26 @@ export default function SettingsAppearanceRoute() { } }, []); - return ( + const handleNameChange = (value: string) => { + setName(value); + }; + + const handleNameSave = useCallback(() => { + send("setNameConfig", { deviceName: name }, resp => { + if ("error" in resp) { + notifications.error(`Failed to set name config: ${resp.error.data || "Unknown error"}`); + return; + } + const nameConfig = resp.result as NameConfig; + setNameConfigSettings(nameConfig); + document.title = nameConfig.name; + notifications.success( + `Device name set to "${nameConfig.name}" successfully.\nDNS Name set to "${nameConfig.dns}"` + ); + }); + }, [send, name, setNameConfigSettings]); + + return (
+ + handleNameChange(e.target.value)} + /> + +
+
); } diff --git a/ui/src/routes/devices.$id.tsx b/ui/src/routes/devices.$id.tsx index 24a6428..e48bbb8 100644 --- a/ui/src/routes/devices.$id.tsx +++ b/ui/src/routes/devices.$id.tsx @@ -3,6 +3,7 @@ import { cx } from "@/cva.config"; import { DeviceSettingsState, HidState, + NameConfig, UpdateState, useDeviceSettingsStore, useDeviceStore, @@ -10,6 +11,7 @@ import { useMountMediaStore, User, useRTCStore, + useSettingsStore, useUiStore, useUpdateStore, useVideoStore, @@ -131,6 +133,7 @@ export default function KvmIdRoute() { const setRpcDataChannel = useRTCStore(state => state.setRpcDataChannel); const setTransceiver = useRTCStore(state => state.setTransceiver); + const navigate = useNavigate(); const { otaState, setOtaState, setModalView } = useUpdateStore(); @@ -380,6 +383,17 @@ export default function KvmIdRoute() { }); }, [rpcDataChannel?.readyState, send, setHdmiState]); + const setNameConfig = useSettingsStore(state => state.setNameConfig); + + useEffect(() => { + send("getNameConfig", {}, resp => { + if ("error" in resp) return; + const results = resp.result as NameConfig; + setNameConfig(results) + document.title = results.name; + }); + }, [send, setNameConfig]) + // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error window.send = send;