Skip to content

Commit

Permalink
update version to 2.1.5
Browse files Browse the repository at this point in the history
- feat: add HDMI reset for NanoKVM-PCIe
- fix: HID reset
- attempt to fix Tailscale OOM
  • Loading branch information
wj-xiao committed Jan 14, 2025
1 parent d7ca7c4 commit 85f6447
Show file tree
Hide file tree
Showing 42 changed files with 598 additions and 196 deletions.
15 changes: 15 additions & 0 deletions server/common/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ func (k *KvmVision) ReadH264PPS() ([]byte, int) {
return data, result
}

func (k *KvmVision) EnableHdmi(enable bool) int {
hdmiEnable := C.uint8_t(0)
if enable {
hdmiEnable = C.uint8_t(1)
}

result := int(C.kvmv_hdmi_control(hdmiEnable))
if result < 0 {
log.Errorf("failed to set hdmi to %t", enable)
return result
}

return result
}

func (k *KvmVision) Close() {
k.mutex.Lock()
defer k.mutex.Unlock()
Expand Down
1 change: 1 addition & 0 deletions server/proto/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type TailscaleState string

const (
TailscaleNotInstall TailscaleState = "notInstall"
TailscaleNotRunning TailscaleState = "notRunning"
TailscaleNotLogin TailscaleState = "notLogin"
TailscaleStopped TailscaleState = "stopped"
TailscaleRunning TailscaleState = "running"
Expand Down
4 changes: 4 additions & 0 deletions server/proto/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ type GetInfoRsp struct {
DeviceKey string `json:"deviceKey"`
}

type GetHardwareRsp struct {
Version string `json:"version"`
}

type SetGpioReq struct {
Type string `validate:"required"` // reset / power
Duration uint `validate:"omitempty"` // press time (unit: milliseconds)
Expand Down
2 changes: 2 additions & 0 deletions server/router/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func networkRouter(r *gin.Engine) {
api.POST("/network/tailscale/down", service.TsDown) // run tailscale down
api.POST("/network/tailscale/login", service.TsLogin) // tailscale login
api.POST("/network/tailscale/logout", service.TsLogout) // tailscale logout
api.POST("/network/tailscale/stop", service.TsStop) // tailscale stop
api.POST("/network/tailscale/restart", service.TsRestart) // tailscale restart

api.GET("/network/wifi", service.GetWifi) // get Wi-Fi information
}
5 changes: 4 additions & 1 deletion server/router/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ func vmRouter(r *gin.Engine) {
service := vm.NewService()
api := r.Group("/api").Use(middleware.CheckToken())

api.GET("/vm/info", service.GetInfo) // get device information
api.GET("/vm/info", service.GetInfo) // get device information
api.GET("/vm/hardware", service.GetHardware) // get hardware version

api.POST("/vm/gpio", service.SetGpio) // update gpio
api.GET("/vm/gpio", service.GetGpio) // get gpio
Expand All @@ -32,4 +33,6 @@ func vmRouter(r *gin.Engine) {

api.GET("/vm/oled", service.GetOLED) // get OLED configuration
api.POST("/vm/oled", service.SetOLED) // set OLED configuration

api.POST("/vm/hdmi/reset", service.ResetHdmi) // reset hdmi (pcie only)
}
3 changes: 0 additions & 3 deletions server/service/hid/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (

func (s *Service) Reset(c *gin.Context) {
var rsp proto.Response
s.hid.kbMutex.Lock()
defer s.hid.kbMutex.Unlock()

// reset USB
f, err := os.OpenFile("/sys/kernel/config/usb_gadget/g0/UDC", os.O_WRONLY, 0644)
Expand Down Expand Up @@ -67,7 +65,6 @@ func (s *Service) Reset(c *gin.Context) {
rsp.ErrRsp(c, -1, "write to usb gadget file failed")
return
}

}

_ = f.Close()
Expand Down
8 changes: 8 additions & 0 deletions server/service/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ func (s *Service) TsLogin(c *gin.Context) {
func (s *Service) TsLogout(c *gin.Context) {
tailscale.Logout(c)
}

func (s *Service) TsStop(c *gin.Context) {
tailscale.Stop(c)
}

func (s *Service) TsRestart(c *gin.Context) {
tailscale.Restart(c)
}
37 changes: 28 additions & 9 deletions server/service/network/tailscale/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tailscale

import (
"NanoKVM-Server/proto"
"NanoKVM-Server/utils"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -38,11 +37,6 @@ func Login(c *gin.Context) {
return
}

// set GOMEMLIMIT = 50M
if !utils.IsGoMemLimitExist() {
_ = utils.SetGoMemLimit(50)
}

rsp.OkRspWithData(c, &proto.LoginTailscaleRsp{
Url: url,
})
Expand All @@ -60,9 +54,34 @@ func Logout(c *gin.Context) {
return
}

// delete GOMEMLIMIT
_ = utils.DelGoMemLimit()

rsp.OkRsp(c)
log.Debugf("tailscale logout successfully")
}

func Restart(c *gin.Context) {
var rsp proto.Response

err := NewCli().Restart()
if err != nil {
rsp.ErrRsp(c, -1, "restart failed")
log.Errorf("failed to run tailscale restart: %s", err)
return
}

rsp.OkRsp(c)
log.Debugf("tailscale restart successfully")
}

func Stop(c *gin.Context) {
var rsp proto.Response

err := NewCli().Stop()
if err != nil {
rsp.ErrRsp(c, -1, "stop failed")
log.Errorf("failed to run tailscale stop: %s", err)
return
}

rsp.OkRsp(c)
log.Debugf("tailscale stop successfully")
}
33 changes: 30 additions & 3 deletions server/service/network/tailscale/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)

const (
ScriptPath = "/etc/init.d/S98tailscaled"
ScriptBackupPath = "/kvmapp/system/init.d/S98tailscaled"
)

type Cli struct{}

type TsStatus struct {
Expand All @@ -36,15 +43,35 @@ func (c *Cli) Start() error {
}
}

command := "/etc/init.d/S98tailscaled start"
commands := []string{
fmt.Sprintf("cp -f %s %s", ScriptBackupPath, ScriptPath),
fmt.Sprintf("%s start", ScriptPath),
}

command := strings.Join(commands, " && ")
return exec.Command("sh", "-c", command).Run()
}

func (c *Cli) Stop() error {
command := "/etc/init.d/S98tailscaled stop"
func (c *Cli) Restart() error {
commands := []string{
fmt.Sprintf("cp -f %s %s", ScriptBackupPath, ScriptPath),
fmt.Sprintf("%s restart", ScriptPath),
}

command := strings.Join(commands, " && ")
return exec.Command("sh", "-c", command).Run()
}

func (c *Cli) Stop() error {
command := fmt.Sprintf("%s stop", ScriptPath)
err := exec.Command("sh", "-c", command).Run()
if err != nil {
return err
}

return os.Remove(ScriptPath)
}

func (c *Cli) Up() error {
command := "tailscale up"
return exec.Command("sh", "-c", command).Run()
Expand Down
4 changes: 3 additions & 1 deletion server/service/network/tailscale/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
)

var StateMap = map[string]proto.TailscaleState{
"NoState": proto.TailscaleNotRunning,
"Starting": proto.TailscaleNotRunning,
"NeedsLogin": proto.TailscaleNotLogin,
"Running": proto.TailscaleRunning,
"Stopped": proto.TailscaleStopped,
Expand All @@ -28,7 +30,7 @@ func GetStatus(c *gin.Context) {
if err != nil {
log.Debugf("failed to get tailscale status: %s", err)
rsp.OkRspWithData(c, &proto.GetTailscaleStatusRsp{
State: proto.TailscaleNotLogin,
State: proto.TailscaleNotRunning,
})
return
}
Expand Down
23 changes: 23 additions & 0 deletions server/service/vm/hdmi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package vm

import (
"NanoKVM-Server/common"
"NanoKVM-Server/proto"
"time"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

func (s *Service) ResetHdmi(c *gin.Context) {
var rsp proto.Response

vision := common.GetKvmVision()

vision.EnableHdmi(false)
time.Sleep(1 * time.Second)
vision.EnableHdmi(true)

rsp.OkRsp(c)
log.Debug("reset hdmi")
}
12 changes: 12 additions & 0 deletions server/service/vm/info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vm

import (
"NanoKVM-Server/config"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -92,3 +93,14 @@ func getDeviceKey() string {

return strings.ReplaceAll(string(content), "\n", "")
}

func (s *Service) GetHardware(c *gin.Context) {
var rsp proto.Response

conf := config.GetInstance()
version := conf.Hardware.Version.String()

rsp.OkRspWithData(c, &proto.GetHardwareRsp{
Version: version,
})
}
10 changes: 10 additions & 0 deletions web/src/api/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ export function logoutTailscale() {
return http.post('/api/network/tailscale/logout');
}

// stop tailscale
export function stopTailscale() {
return http.post('/api/network/tailscale/stop');
}

// restart tailscale
export function restartTailscale() {
return http.post('/api/network/tailscale/restart');
}

// get wifi information
export function getWiFi() {
return http.get('/api/network/wifi');
Expand Down
10 changes: 10 additions & 0 deletions web/src/api/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export function getInfo() {
return http.get('/api/vm/info');
}

// get hardware information
export function getHardware() {
return http.get('/api/vm/hardware');
}

// set gpio value
export function setGpio(type: string, duration: number) {
const data = {
Expand Down Expand Up @@ -51,3 +56,8 @@ export function getOLED() {
export function setOLED(sleep: number) {
return http.post('/api/vm/oled', { sleep });
}

// reset HDMI
export function resetHdmi() {
return http.post('/api/vm/hdmi/reset');
}
22 changes: 14 additions & 8 deletions web/src/i18n/locales/cz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ const cz = {
qualityLow: 'Nízký',
frameDetect: 'Detekce snímků',
frameDetectTip:
'Vypočítá rozdíl mezi snímky. Přenos video streamu se zastaví, pokud nejsou detekovány změny na obrazovce vzdáleného hostitele.'
'Vypočítá rozdíl mezi snímky. Přenos video streamu se zastaví, pokud nejsou detekovány změny na obrazovce vzdáleného hostitele.',
resetHdmi: 'Reset HDMI'
},
keyboard: {
paste: 'Vložit',
Expand Down Expand Up @@ -189,15 +190,18 @@ const cz = {
disk: 'Virtual Disk',
diskDesc: 'Mount virtual U-disk on the remote host',
network: 'Virtual Network',
networkDesc: 'Mount virtual network card on the remote host',
memory: {
title: 'Memory optimization',
tip: 'When memory usage exceeds the limit, garbage collection is performed more aggressively to attempt to free up memory.',
disable: 'Disable'
}
networkDesc: 'Mount virtual network card on the remote host'
},
tailscale: {
title: 'Tailscale',
memory: {
title: 'Memory optimization',
tip: "When memory usage exceeds the limit, garbage collection is performed more aggressively to attempt to free up memory. it's recommended to set to 50MB if using Tailscale. A Tailscale restart is required for the change to take effect.",
disable: 'Disable'
},
restart: 'Are you sure to restart Tailscale?',
stop: 'Are you sure to stop Tailscale?',
stopDesc: 'Log out Tailscale and disable its automatic startup on boot.',
loading: 'Načítání...',
notInstall: 'Tailscale nebyl nalezen! Prosím nainstalujte.',
install: 'Nainstalovat',
Expand All @@ -220,7 +224,9 @@ const cz = {
deviceIP: 'IP zařízení',
account: 'Účet',
logout: 'Odhlásit se',
logout2: 'Opravdu se chcete odhlásit?'
logout2: 'Opravdu se chcete odhlásit?',
okBtn: 'Yes',
cancelBtn: 'No'
},
update: {
title: 'Zkontrolovat aktualizaci',
Expand Down
Loading

0 comments on commit 85f6447

Please sign in to comment.