add dev mode for testing vnc proxy
This commit is contained in:
parent
6919f65a77
commit
2b53cedbc5
7 changed files with 178 additions and 4 deletions
16
Readme.md
16
Readme.md
|
|
@ -46,4 +46,18 @@ systemctl daemon-reload
|
|||
systemctl enable vncproxy
|
||||
systemctl start vncproxy
|
||||
```
|
||||
When using a dedicated VPS, use the smalles one you can get.
|
||||
When using a dedicated VPS, use the smalles one you can get.
|
||||
|
||||
## Dev Mode
|
||||
|
||||
You can enable the dev mode by setting `DEV_MODE` to true. With this mode you get a new HTTP endpoint at `/vnc` to directly start a VNC session. It will create the VNC Ticket using the PVE API, encrypts it and passes it to a noVNC iframe. It can be called with the following parameters:
|
||||
```
|
||||
http://localhost:8080/vnc
|
||||
?host=1.2.3.4
|
||||
&node=pve
|
||||
&user=root@pam
|
||||
&password=myPveUserPassword
|
||||
&vm=100
|
||||
&type=qemu # optional parameter
|
||||
&port=8006 # optional parameter
|
||||
```
|
||||
60
development/main.go
Normal file
60
development/main.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package development
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"jonasled.dev/jonasled/hostware-vnc-proxy/encryption"
|
||||
"jonasled.dev/jonasled/hostware-vnc-proxy/log"
|
||||
"jonasled.dev/jonasled/hostware-vnc-proxy/types"
|
||||
)
|
||||
|
||||
func RegisterDevSetup(r *gin.Engine) {
|
||||
if os.Getenv("DEV_MODE") != "true" {
|
||||
return
|
||||
}
|
||||
log.Log.Warn("Running in dev mode, for production use unset DEV_MODE")
|
||||
r.GET("/vnc", func(ctx *gin.Context) {
|
||||
pveHost := ctx.Query("host")
|
||||
pveNode := ctx.Query("node")
|
||||
pveUser := ctx.Query("user")
|
||||
pvePassword := ctx.Query("password")
|
||||
pveVmid := ctx.Query("vm")
|
||||
pvePort := ctx.DefaultQuery("port", "8006")
|
||||
vmType := ctx.DefaultQuery("type", "qemu")
|
||||
log.Log.Info("Generating VNC URL for " + pveVmid + " on " + pveHost)
|
||||
|
||||
vncConfig := types.VncConfig{
|
||||
Virtualization: vmType,
|
||||
VmID: pveVmid,
|
||||
NodeName: pveNode,
|
||||
NodePort: pvePort,
|
||||
NodeUrl: pveHost,
|
||||
NodeUser: pveUser,
|
||||
NodePassword: pvePassword,
|
||||
}
|
||||
vncConfig.GenerateVncTicket()
|
||||
vncConfigJson, _ := json.Marshal(vncConfig)
|
||||
jwtPayload := types.PayloadClaim{
|
||||
EncryptedPayload: encryption.EncryptMessage(string(vncConfigJson)),
|
||||
}
|
||||
jwt, _ := encryption.EncodePayload(&jwtPayload)
|
||||
|
||||
encrypt := "false"
|
||||
if os.Getenv("TLS_CERT") != "" {
|
||||
encrypt = "true"
|
||||
}
|
||||
|
||||
ctx.Data(http.StatusOK, "text/html; charset=utf-8", []byte(fmt.Sprintf(`<iframe
|
||||
src="https://novnc.com/noVNC/vnc.html?host=localhost&port=8080&password=PVEVNC%%3A675379E3&path=%s&encrypt=%s&logging=debug&scale=true&autoconnect=true"
|
||||
style="width: 100%%; height: 100%%;border: none;position: absolute;left: 0; top: 0;">
|
||||
</iframe>
|
||||
`, url.QueryEscape("?payload="+jwt), encrypt)))
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package encryption
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"jonasled.dev/jonasled/hostware-vnc-proxy/log"
|
||||
|
|
@ -25,3 +26,31 @@ func DecodePayload(payload string) (*types.PayloadClaim, error) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
func EncodePayload(payload *types.PayloadClaim) (string, error) {
|
||||
// Retrieve the signing key
|
||||
signingKey := os.Getenv("JWT_SIGN_KEY")
|
||||
if signingKey == "" {
|
||||
log.Log.Error("JWT_SIGN_KEY environment variable not set")
|
||||
return "", fmt.Errorf("signing key not available")
|
||||
}
|
||||
|
||||
// Set standard claims (e.g., issued at, expiration)
|
||||
now := time.Now()
|
||||
payload.RegisteredClaims.IssuedAt = jwt.NewNumericDate(now)
|
||||
if payload.RegisteredClaims.ExpiresAt == nil {
|
||||
payload.RegisteredClaims.ExpiresAt = jwt.NewNumericDate(now.Add(24 * time.Hour)) // Default to 24 hours expiration
|
||||
}
|
||||
|
||||
// Create the token
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, payload)
|
||||
|
||||
// Sign the token
|
||||
tokenString, err := token.SignedString([]byte(signingKey))
|
||||
if err != nil {
|
||||
log.Log.Error("Failed signing the JWT: ", err.Error())
|
||||
return "", err
|
||||
}
|
||||
|
||||
return tokenString, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package encryption
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
|
@ -43,3 +46,36 @@ func DecryptMessage(text string) []byte {
|
|||
// Unpad if necessary
|
||||
return cleanedData
|
||||
}
|
||||
|
||||
func EncryptMessage(plainText string) string {
|
||||
ivLength := 16
|
||||
|
||||
// Generate a random IV
|
||||
iv := make([]byte, ivLength)
|
||||
if _, err := rand.Read(iv); err != nil {
|
||||
log.Log.Error("Failed to generate IV: ", err.Error())
|
||||
return ""
|
||||
}
|
||||
|
||||
// Create AES cipher block
|
||||
block, err := aes.NewCipher([]byte(os.Getenv("JWT_SIGN_KEY")))
|
||||
if err != nil {
|
||||
log.Log.Error("Failed creating AES Cipher: ", err.Error())
|
||||
return ""
|
||||
}
|
||||
|
||||
// Pad the plaintext to be a multiple of the block size
|
||||
blockSize := block.BlockSize()
|
||||
padding := blockSize - len(plainText)%blockSize
|
||||
paddedText := append([]byte(plainText), bytes.Repeat([]byte{byte(padding)}, padding)...)
|
||||
|
||||
// Create AES encryption stream
|
||||
encrypted := make([]byte, len(paddedText))
|
||||
stream := cipher.NewCBCEncrypter(block, iv)
|
||||
stream.CryptBlocks(encrypted, paddedText)
|
||||
|
||||
// Combine IV and encrypted text
|
||||
encryptedMessage := fmt.Sprintf("%s:%s", hex.EncodeToString(iv), hex.EncodeToString(encrypted))
|
||||
|
||||
return encryptedMessage
|
||||
}
|
||||
|
|
|
|||
2
main.go
2
main.go
|
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/gorilla/websocket"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
ginlogrus "github.com/toorop/gin-logrus"
|
||||
"jonasled.dev/jonasled/hostware-vnc-proxy/development"
|
||||
"jonasled.dev/jonasled/hostware-vnc-proxy/encryption"
|
||||
"jonasled.dev/jonasled/hostware-vnc-proxy/log"
|
||||
"jonasled.dev/jonasled/hostware-vnc-proxy/metrics"
|
||||
|
|
@ -155,6 +156,7 @@ func main() {
|
|||
})
|
||||
|
||||
metrics.InitPrometheus(r)
|
||||
development.RegisterDevSetup(r)
|
||||
|
||||
log.Log.Info("Start listening on port 8080")
|
||||
if os.Getenv("TLS_CERT") == "" {
|
||||
|
|
|
|||
14
types/pveVncTicketResponse.go
Normal file
14
types/pveVncTicketResponse.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package types
|
||||
|
||||
type PveVncTicketResponseData struct {
|
||||
Cert string `json:"cert"`
|
||||
Port any `json:"port"`
|
||||
Ticket string `json:"ticket"`
|
||||
Upid string `json:"upid"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password,omitempty"`
|
||||
}
|
||||
|
||||
type PveVncTicketResponse struct {
|
||||
Data PveVncTicketResponseData `json:"data"`
|
||||
}
|
||||
|
|
@ -20,12 +20,31 @@ type VncConfig struct {
|
|||
NodePort any `json:"NodePort"`
|
||||
NodeUrl string `json:"nodeUrl"`
|
||||
NodePassword string `json:"nodePassword"`
|
||||
ApiTicket string
|
||||
CSRFPreventionToken string
|
||||
ApiTicket string `json:"-"`
|
||||
CSRFPreventionToken string `json:"-"`
|
||||
}
|
||||
|
||||
func (v *VncConfig) GetVncUrl() string {
|
||||
return fmt.Sprintf("wss://%s:%s/api2/json/nodes/%s/qemu/%s/vncwebsocket?port=%s&vncticket=%s", v.NodeUrl, convertToString(v.NodePort), v.NodeName, v.VmID, v.VncProxyPort, url.QueryEscape(v.VncProxyTicket))
|
||||
return fmt.Sprintf("wss://%s:%s/api2/json/nodes/%s/%s/%s/vncwebsocket?port=%s&vncticket=%s", v.NodeUrl, convertToString(v.NodePort), v.NodeName, v.Virtualization, v.VmID, v.VncProxyPort, url.QueryEscape(v.VncProxyTicket))
|
||||
}
|
||||
|
||||
func (v *VncConfig) GenerateVncTicket() {
|
||||
v.GenerateApiTicket()
|
||||
client := resty.New()
|
||||
if os.Getenv("PVE_IGNORE_SSL") == "true" {
|
||||
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
|
||||
}
|
||||
client.SetBaseURL(fmt.Sprintf("https://%s:%s/", v.NodeUrl, convertToString(v.NodePort)))
|
||||
resp, err := client.R().SetResult(PveVncTicketResponse{}).SetHeaders(map[string]string{
|
||||
"Cookie": "PVEAuthCookie=" + url.QueryEscape(v.ApiTicket) + ";",
|
||||
"CSRFPreventionToken": v.CSRFPreventionToken,
|
||||
}).Post("/api2/json/nodes/" + v.NodeName + "/" + v.Virtualization + "/" + v.VmID + "/vncproxy")
|
||||
if err != nil {
|
||||
log.Log.Error("Failed generating vnc ticket to PVE: ", err.Error())
|
||||
return
|
||||
}
|
||||
v.VncProxyTicket = resp.Result().(*PveVncTicketResponse).Data.Ticket
|
||||
v.VncProxyPort = convertToString(resp.Result().(*PveVncTicketResponse).Data.Port)
|
||||
}
|
||||
|
||||
func (v *VncConfig) GenerateApiTicket() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue