83 lines
2.5 KiB
Go
83 lines
2.5 KiB
Go
package development
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
|
|
"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"
|
|
}
|
|
domain := strings.Split(ctx.Request.Host, ":")[0]
|
|
|
|
ctx.Data(http.StatusOK, "text/html; charset=utf-8", []byte(fmt.Sprintf(`<iframe
|
|
src="https://novnc.com/noVNC/vnc.html?host=%s&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>
|
|
`, domain, url.QueryEscape("?payload="+jwt), encrypt)))
|
|
|
|
})
|
|
r.GET("/dump", func(ctx *gin.Context) {
|
|
|
|
payload := ctx.Query("payload")
|
|
|
|
if payload == "" {
|
|
log.Log.Info("Received Request with missing Payload parameter")
|
|
ctx.String(http.StatusBadRequest, "Missing payload parameter")
|
|
return
|
|
}
|
|
jwtClain, err := encryption.DecodePayload(payload)
|
|
if err != nil {
|
|
log.Log.Info("Received Request with invalid Payload parameter")
|
|
ctx.String(http.StatusBadRequest, "Invalid Payload provided")
|
|
fmt.Println(err.Error())
|
|
return
|
|
}
|
|
|
|
configuration := encryption.DecryptMessage(jwtClain.EncryptedPayload)
|
|
log.Log.Info(string(configuration))
|
|
ctx.String(http.StatusOK, "The decoded payload has been dumped to the console")
|
|
})
|
|
|
|
}
|