Skip to content
Open
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
2 changes: 2 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func initialize() {
log.Println("NOTICE: Authentication is disabled! Please ensure your service is secure!")
}

instance.Tokens = make(map[string]*User, 10)

log.Println("config loaded successfully")
}

Expand Down
11 changes: 10 additions & 1 deletion server/config/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import "time"

type Config struct {
Proto string `yaml:"proto"`
Port Port `yaml:"port"`
Expand All @@ -10,7 +12,8 @@ type Config struct {
Stun string `yaml:"stun"`
Turn Turn `yaml:"turn"`

Hardware Hardware `yaml:"-"`
Hardware Hardware `yaml:"-"`
Tokens map[string]*User `yaml:"-"`
}

type Logger struct {
Expand Down Expand Up @@ -47,3 +50,9 @@ type Hardware struct {
GPIOPowerLED string `yaml:"-"`
GPIOHDDLed string `yaml:"-"`
}

type User struct {
Username string `yaml:"-"`
Group string `yaml:"-"`
ExpiresAt time.Time `yaml:"-"`
}
3 changes: 3 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"NanoKVM-Server/logger"
"NanoKVM-Server/middleware"
"NanoKVM-Server/router"
"NanoKVM-Server/service/auth"
"NanoKVM-Server/service/vm/jiggler"

"github.com/gin-gonic/gin"
Expand All @@ -31,6 +32,8 @@ func main() {
func initialize() {
logger.Init()

auth.CheckAccountsFile()

// init screen parameters
_ = common.GetScreen()

Expand Down
30 changes: 30 additions & 0 deletions server/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,36 @@ func CheckToken() gin.HandlerFunc {
}
}

func CheckAdminToken() gin.HandlerFunc {
return func(c *gin.Context) {
conf := config.GetInstance()

if conf.Authentication == "disable" {
c.Next()
return
}

cookie, err := c.Cookie("nano-kvm-token")
if err == nil {
_, err = ParseJWT(cookie)
if err == nil {
user, ok := config.GetInstance().Tokens[cookie]
if ok && user.Group != "admin" {
c.JSON(http.StatusForbidden, "unauthorized")
c.Abort()
return
}

c.Next()
return
}
}

c.JSON(http.StatusUnauthorized, "unauthorized")
c.Abort()
}
}

func GenerateJWT(username string) (string, error) {
conf := config.GetInstance()

Expand Down
3 changes: 2 additions & 1 deletion server/proto/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type LoginReq struct {
}

type LoginRsp struct {
Token string `json:"token"`
Token string `json:"token"`
IsAdmin bool `json:"is_admin"`
}

type GetAccountRsp struct {
Expand Down
1 change: 1 addition & 0 deletions server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func server(r *gin.Engine) {
authRouter(r)
applicationRouter(r)
vmRouter(r)
vmAdminRouter(r)
streamRouter(r)
storageRouter(r)
networkRouter(r)
Expand Down
65 changes: 51 additions & 14 deletions server/router/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,69 @@ func vmRouter(r *gin.Engine) {
api.GET("/vm/gpio", service.GetGpio) // get gpio
api.POST("/vm/screen", service.SetScreen) // update screen

// api.GET("/vm/terminal", service.Terminal) // web terminal

api.GET("/vm/script", service.GetScripts) // get script
// api.POST("/vm/script/upload", service.UploadScript) // upload script
// api.POST("/vm/script/run", service.RunScript) // run script
// api.DELETE("/vm/script", service.DeleteScript) // delete script

api.GET("/vm/device/virtual", service.GetVirtualDevice) // get virtual device
// api.POST("/vm/device/virtual", service.UpdateVirtualDevice) // update virtual device

api.GET("/vm/memory/limit", service.GetMemoryLimit) // get memory limit
// api.POST("/vm/memory/limit", service.SetMemoryLimit) // set memory limit

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

// Only supported by PCIe version
api.GET("/vm/hdmi", service.GetHdmiState) // get HDMI state
// api.POST("/vm/hdmi/reset", service.ResetHdmi) // reset hdmi
// api.POST("/vm/hdmi/enable", service.EnableHdmi) // enable hdmi
// api.POST("/vm/hdmi/disable", service.DisableHdmi) // disable hdmi

// api.GET("/vm/ssh", service.GetSSHState) // get SSH state
// api.POST("/vm/ssh/enable", service.EnableSSH) // enable SSH
// api.POST("/vm/ssh/disable", service.DisableSSH) // disable SSH

api.GET("/vm/swap", service.GetSwap) // get swap file size
// api.POST("/vm/swap", service.SetSwap) // set swap file size

api.GET("/vm/mouse-jiggler", service.GetMouseJiggler) // get mouse jiggler
//api.POST("/vm/mouse-jiggler/", service.SetMouseJiggler) // set mouse jiggler

api.GET("/vm/hostname", service.GetHostname) // Get Hostname
// api.POST("/vm/hostname", service.SetHostname) // Set Hostname

api.GET("/vm/web-title", service.GetWebTitle) // Get web title
// api.POST("/vm/web-title", service.SetWebTitle) // Set web title

// api.GET("/vm/mdns", service.GetMdnsState) // get mDNS state
// api.POST("/vm/mdns/enable", service.EnableMdns) // enable mDNS
// api.POST("/vm/mdns/disable", service.DisableMdns) // disable mDNS

api.POST("/vm/tls", service.SetTls) // enable/disable TLS

api.POST("/vm/system/reboot", service.Reboot) // reboot system
}

func vmAdminRouter(r *gin.Engine) {
service := vm.NewService()
api := r.Group("/api").Use(middleware.CheckAdminToken())

api.GET("/vm/terminal", service.Terminal) // web terminal

api.GET("/vm/script", service.GetScripts) // get script
api.POST("/vm/script/upload", service.UploadScript) // upload script
api.POST("/vm/script/run", service.RunScript) // run script
api.DELETE("/vm/script", service.DeleteScript) // delete script

api.GET("/vm/device/virtual", service.GetVirtualDevice) // get virtual device
api.POST("/vm/device/virtual", service.UpdateVirtualDevice) // update virtual device

api.GET("/vm/memory/limit", service.GetMemoryLimit) // get memory limit
api.POST("/vm/memory/limit", service.SetMemoryLimit) // set memory limit

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

// Only supported by PCIe version
api.GET("/vm/hdmi", service.GetHdmiState) // get HDMI state
api.POST("/vm/hdmi/reset", service.ResetHdmi) // reset hdmi
api.POST("/vm/hdmi/enable", service.EnableHdmi) // enable hdmi
api.POST("/vm/hdmi/disable", service.DisableHdmi) // disable hdmi
Expand All @@ -45,23 +90,15 @@ func vmRouter(r *gin.Engine) {
api.POST("/vm/ssh/enable", service.EnableSSH) // enable SSH
api.POST("/vm/ssh/disable", service.DisableSSH) // disable SSH

api.GET("/vm/swap", service.GetSwap) // get swap file size
api.POST("/vm/swap", service.SetSwap) // set swap file size

api.GET("/vm/mouse-jiggler", service.GetMouseJiggler) // get mouse jiggler
api.POST("/vm/mouse-jiggler/", service.SetMouseJiggler) // set mouse jiggler

api.GET("/vm/hostname", service.GetHostname) // Get Hostname
api.POST("/vm/hostname", service.SetHostname) // Set Hostname

api.GET("/vm/web-title", service.GetWebTitle) // Get web title
api.POST("/vm/web-title", service.SetWebTitle) // Set web title

api.GET("/vm/mdns", service.GetMdnsState) // get mDNS state
api.POST("/vm/mdns/enable", service.EnableMdns) // enable mDNS
api.POST("/vm/mdns/disable", service.DisableMdns) // disable mDNS

api.POST("/vm/tls", service.SetTls) // enable/disable TLS

api.POST("/vm/system/reboot", service.Reboot) // reboot system
}
Loading