Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"sync"
)

type WakeOnLanDevice struct {
Expand Down Expand Up @@ -40,32 +41,38 @@ var defaultConfig = &Config{
DisplayOffAfterSec: 1800, // 30 minutes
}

var config *Config
var (
config *Config
configLock = &sync.Mutex{}
)

func LoadConfig() {
if config != nil {
logger.Info("config already loaded, skipping")
return
}

file, err := os.Open(configPath)
if err != nil {
logger.Debug("default config file doesn't exist, using default")
config = defaultConfig
return
}
defer file.Close()

var loadedConfig Config
// load and merge the default config with the user config
loadedConfig := *defaultConfig
if err := json.NewDecoder(file).Decode(&loadedConfig); err != nil {
logger.Errorf("config file JSON parsing failed, %v", err)
config = defaultConfig
return
}

config = &loadedConfig
}

func SaveConfig() error {
configLock.Lock()
defer configLock.Unlock()

file, err := os.Create(configPath)
if err != nil {
return fmt.Errorf("failed to create config file: %w", err)
Expand Down
1 change: 0 additions & 1 deletion display.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func watchTsEvents() {
// if they're not already set. This is done separately to the init routine as the "never dim"
// option has the value set to zero, but time.NewTicker only accept positive values.
func startBacklightTickers() {
LoadConfig()
// Don't start the tickers if the display is switched off.
// Set the display to off if that's the case.
if config.DisplayMaxBrightness == 0 {
Expand Down
8 changes: 0 additions & 8 deletions jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ func rpcSetEDID(edid string) error {
}

// Save EDID to config, allowing it to be restored on reboot.
LoadConfig()
config.EdidString = edid
SaveConfig()

Expand Down Expand Up @@ -235,8 +234,6 @@ func rpcTryUpdate() error {
}

func rpcSetBacklightSettings(params BacklightSettings) error {
LoadConfig()

blConfig := params

// NOTE: by default, the frontend limits the brightness to 64, as that's what the device originally shipped with.
Expand Down Expand Up @@ -275,8 +272,6 @@ func rpcSetBacklightSettings(params BacklightSettings) error {
}

func rpcGetBacklightSettings() (*BacklightSettings, error) {
LoadConfig()

return &BacklightSettings{
MaxBrightness: config.DisplayMaxBrightness,
DimAfter: int(config.DisplayDimAfterSec),
Expand Down Expand Up @@ -544,7 +539,6 @@ func rpcSetUsbEmulationState(enabled bool) error {
}

func rpcGetWakeOnLanDevices() ([]WakeOnLanDevice, error) {
LoadConfig()
if config.WakeOnLanDevices == nil {
return []WakeOnLanDevice{}, nil
}
Expand All @@ -556,13 +550,11 @@ type SetWakeOnLanDevicesParams struct {
}

func rpcSetWakeOnLanDevices(params SetWakeOnLanDevicesParams) error {
LoadConfig()
config.WakeOnLanDevices = params.Devices
return SaveConfig()
}

func rpcResetConfig() error {
LoadConfig()
config = defaultConfig
if err := SaveConfig(); err != nil {
return fmt.Errorf("failed to reset config: %w", err)
Expand Down
1 change: 0 additions & 1 deletion native.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ func ensureBinaryUpdated(destPath string) error {
// Restore the HDMI EDID value from the config.
// Called after successful connection to jetkvm_native.
func restoreHdmiEdid() {
LoadConfig()
if config.EdidString != "" {
logger.Infof("Restoring HDMI EDID to %v", config.EdidString)
_, err := CallCtrlAction("set_edid", map[string]interface{}{"edid": config.EdidString})
Expand Down
20 changes: 0 additions & 20 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ func handleWebRTCSession(c *gin.Context) {
}

func handleLogin(c *gin.Context) {
LoadConfig()

if config.LocalAuthMode == "noPassword" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Login is disabled in noPassword mode"})
return
Expand All @@ -161,7 +159,6 @@ func handleLogin(c *gin.Context) {
return
}

LoadConfig()
err := bcrypt.CompareHashAndPassword([]byte(config.HashedPassword), []byte(req.Password))
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid password"})
Expand All @@ -177,7 +174,6 @@ func handleLogin(c *gin.Context) {
}

func handleLogout(c *gin.Context) {
LoadConfig()
config.LocalAuthToken = ""
if err := SaveConfig(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save configuration"})
Expand All @@ -191,8 +187,6 @@ func handleLogout(c *gin.Context) {

func protectedMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
LoadConfig()

if config.LocalAuthMode == "noPassword" {
c.Next()
return
Expand Down Expand Up @@ -221,8 +215,6 @@ func RunWebServer() {
}

func handleDevice(c *gin.Context) {
LoadConfig()

response := LocalDevice{
AuthMode: &config.LocalAuthMode,
DeviceID: GetDeviceID(),
Expand All @@ -232,8 +224,6 @@ func handleDevice(c *gin.Context) {
}

func handleCreatePassword(c *gin.Context) {
LoadConfig()

if config.HashedPassword != "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Password already set"})
return
Expand Down Expand Up @@ -274,8 +264,6 @@ func handleCreatePassword(c *gin.Context) {
}

func handleUpdatePassword(c *gin.Context) {
LoadConfig()

if config.HashedPassword == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Password is not set"})
return
Expand Down Expand Up @@ -319,8 +307,6 @@ func handleUpdatePassword(c *gin.Context) {
}

func handleDeletePassword(c *gin.Context) {
LoadConfig()

if config.HashedPassword == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Password is not set"})
return
Expand Down Expand Up @@ -357,8 +343,6 @@ func handleDeletePassword(c *gin.Context) {
}

func handleDeviceStatus(c *gin.Context) {
LoadConfig()

response := DeviceStatus{
IsSetup: config.LocalAuthMode != "",
}
Expand All @@ -367,8 +351,6 @@ func handleDeviceStatus(c *gin.Context) {
}

func handleDeviceUIConfig(c *gin.Context) {
LoadConfig()

config, _ := json.Marshal(gin.H{
"CLOUD_API": config.CloudURL,
"DEVICE_VERSION": builtAppVersion,
Expand All @@ -384,8 +366,6 @@ func handleDeviceUIConfig(c *gin.Context) {
}

func handleSetup(c *gin.Context) {
LoadConfig()

// Check if the device is already set up
if config.LocalAuthMode != "" || config.HashedPassword != "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Device is already set up"})
Expand Down