Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Reorganize layout #39

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
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
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; https://editorconfig.org/

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{Makefile,go.mod,go.sum,*.go,.gitmodules}]
indent_style = tab
indent_size = 4

[*.md]
indent_size = 4
eclint_indent_style = unset

[Dockerfile]
indent_size = 4
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
85 changes: 83 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,90 @@
package main

import (
"kvm"
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/jetkvm/kvm/internal/config"
"github.com/jetkvm/kvm/internal/kvm"
"github.com/jetkvm/kvm/internal/logging"

"github.com/gwatts/rootcerts"
)

var ctx context.Context

func main() {
kvm.Main()
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(context.Background())
defer cancel()

logging.Logger.Info("Starting JetKvm")
go kvm.RunWatchdog(ctx)
go kvm.ConfirmCurrentSystem()

http.DefaultClient.Timeout = 1 * time.Minute
cfg := config.LoadConfig()
logging.Logger.Debug("config loaded")

err := rootcerts.UpdateDefaultTransport()
if err != nil {
logging.Logger.Errorf("failed to load CA certs: %v", err)
}

go kvm.TimeSyncLoop()

kvm.StartNativeCtrlSocketServer()
kvm.StartNativeVideoSocketServer()

go func() {
err = kvm.ExtractAndRunNativeBin(ctx)
if err != nil {
logging.Logger.Errorf("failed to extract and run native bin: %v", err)
//TODO: prepare an error message screen buffer to show on kvm screen
}
}()

go func() {
time.Sleep(15 * time.Minute)
for {
logging.Logger.Debugf("UPDATING - Auto update enabled: %v", cfg.AutoUpdateEnabled)
if cfg.AutoUpdateEnabled == false {
return
}
if kvm.CurrentSession != nil {
logging.Logger.Debugf("skipping update since a session is active")
time.Sleep(1 * time.Minute)
continue
}
includePreRelease := cfg.IncludePreRelease
err = kvm.TryUpdate(context.Background(), kvm.GetDeviceID(), includePreRelease)
if err != nil {
logging.Logger.Errorf("failed to auto update: %v", err)
}
time.Sleep(1 * time.Hour)
}
}()
//go RunFuseServer()
go kvm.RunWebServer()
go kvm.RunWebsocketClient()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
log.Println("JetKVM Shutting Down")
//if fuseServer != nil {
// err := setMassStorageImage(" ")
// if err != nil {
// log.Printf("Failed to unmount mass storage image: %v", err)
// }
// err = fuseServer.Unmount()
// if err != nil {
// log.Printf("Failed to unmount fuse: %v", err)
// }

// os.Exit(0)
}
Empty file modified dev_deploy.sh
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module kvm
module github.com/jetkvm/kvm

go 1.21.0

Expand Down
21 changes: 12 additions & 9 deletions config.go → internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package kvm
package config

import (
"encoding/json"
"fmt"
"os"

"github.com/jetkvm/kvm/internal/logging"
)

type WakeOnLanDevice struct {
Expand Down Expand Up @@ -33,30 +35,31 @@ var defaultConfig = &Config{

var config *Config

func LoadConfig() {
func LoadConfig() *Config {
if config != nil {
return
return config
}

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

var loadedConfig Config
if err := json.NewDecoder(file).Decode(&loadedConfig); err != nil {
logger.Errorf("config file JSON parsing failed, %v", err)
logging.Logger.Errorf("config file JSON parsing failed, %v", err)
config = defaultConfig
return
return config
}

config = &loadedConfig
return config
}

func SaveConfig() error {
func SaveConfig(cfg *Config) error {
file, err := os.Create(configPath)
if err != nil {
return fmt.Errorf("failed to create config file: %w", err)
Expand All @@ -65,7 +68,7 @@ func SaveConfig() error {

encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(config); err != nil {
if err := encoder.Encode(cfg); err != nil {
return fmt.Errorf("failed to encode config: %w", err)
}

Expand Down
27 changes: 14 additions & 13 deletions block_device.go → internal/kvm/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"time"

"github.com/jetkvm/kvm/internal/logging"
"github.com/pojntfx/go-nbd/pkg/client"
"github.com/pojntfx/go-nbd/pkg/server"
)
Expand All @@ -16,15 +17,15 @@ type remoteImageBackend struct {
}

func (r remoteImageBackend) ReadAt(p []byte, off int64) (n int, err error) {
virtualMediaStateMutex.RLock()
logger.Debugf("currentVirtualMediaState is %v", currentVirtualMediaState)
logger.Debugf("read size: %d, off: %d", len(p), off)
if currentVirtualMediaState == nil {
VirtualMediaStateMutex.RLock()
logging.Logger.Debugf("currentVirtualMediaState is %v", CurrentVirtualMediaState)
logging.Logger.Debugf("read size: %d, off: %d", len(p), off)
if CurrentVirtualMediaState == nil {
return 0, errors.New("image not mounted")
}
source := currentVirtualMediaState.Source
mountedImageSize := currentVirtualMediaState.Size
virtualMediaStateMutex.RUnlock()
source := CurrentVirtualMediaState.Source
mountedImageSize := CurrentVirtualMediaState.Size
VirtualMediaStateMutex.RUnlock()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand All @@ -35,14 +36,14 @@ func (r remoteImageBackend) ReadAt(p []byte, off int64) (n int, err error) {
}
var data []byte
if source == WebRTC {
data, err = webRTCDiskReader.Read(ctx, off, readLen)
data, err = WebRTCDiskReader.Read(ctx, off, readLen)
if err != nil {
return 0, err
}
n = copy(p, data)
return n, nil
} else if source == HTTP {
return httpRangeReader.ReadAt(p, off)
return HttpRangeReader.ReadAt(p, off)
} else {
return 0, errors.New("unknown image source")
}
Expand All @@ -53,12 +54,12 @@ func (r remoteImageBackend) WriteAt(p []byte, off int64) (n int, err error) {
}

func (r remoteImageBackend) Size() (int64, error) {
virtualMediaStateMutex.Lock()
defer virtualMediaStateMutex.Unlock()
if currentVirtualMediaState == nil {
VirtualMediaStateMutex.Lock()
defer VirtualMediaStateMutex.Unlock()
if CurrentVirtualMediaState == nil {
return 0, errors.New("no virtual media state")
}
return currentVirtualMediaState.Size, nil
return CurrentVirtualMediaState.Size, nil
}

func (r remoteImageBackend) Sync() error {
Expand Down
Loading