@@ -2,10 +2,12 @@ package proxmox
22
33import (
44 "context"
5+ "encoding/base64"
56 "fmt"
67 "regexp"
78 "strconv"
89 "strings"
10+ "time"
911
1012 "github.com/pkg/errors"
1113
@@ -19,7 +21,8 @@ const (
1921)
2022
2123type VNCWebSocketClient struct {
22- conn * websocket.Conn
24+ conn * websocket.Conn
25+ ticker * time.Ticker
2326}
2427
2528func (s * Service ) NewNodeVNCWebSocketConnection (ctx context.Context , nodeName string ) (* VNCWebSocketClient , error ) {
@@ -32,11 +35,22 @@ func (s *Service) NewNodeVNCWebSocketConnection(ctx context.Context, nodeName st
3235 return nil , err
3336 }
3437
35- return & VNCWebSocketClient {conn : conn }, nil
38+ ticker := time .NewTicker (30 * time .Second )
39+ go func () {
40+ for {
41+ select {
42+ case <- ticker .C :
43+ conn .WriteMessage (websocket .BinaryMessage , []byte ("2" ))
44+ }
45+ }
46+ }()
47+
48+ return & VNCWebSocketClient {conn : conn , ticker : ticker }, nil
3649}
3750
3851func (c * VNCWebSocketClient ) Close () {
3952 c .conn .Close ()
53+ c .ticker .Stop ()
4054}
4155
4256func (c * VNCWebSocketClient ) Write (cmd string ) error {
@@ -49,6 +63,38 @@ func (c *VNCWebSocketClient) Write(cmd string) error {
4963 return c .sendFinMessage ()
5064}
5165
66+ func (c * VNCWebSocketClient ) WriteFile (ctx context.Context , content , path string ) error {
67+ c .Exec (ctx , fmt .Sprintf ("rm %s" , path ))
68+ if _ , _ , err := c .Exec (ctx , fmt .Sprintf ("touch %s" , path )); err != nil {
69+ return err
70+ }
71+ chunks := chunkString (content , 3000 )
72+ for _ , chunk := range chunks {
73+ b64chunk := base64 .StdEncoding .EncodeToString ([]byte (chunk ))
74+ _ , _ , err := c .Exec (ctx , fmt .Sprintf ("echo %s | base64 -d >> %s" , b64chunk , path ))
75+ if err != nil {
76+ return err
77+ }
78+ }
79+ return nil
80+ }
81+
82+ func chunkString (s string , chunkSize int ) []string {
83+ var chunks []string
84+ runes := []rune (s )
85+ if len (runes ) == 0 {
86+ return []string {s }
87+ }
88+ for i := 0 ; i < len (runes ); i += chunkSize {
89+ nn := i + chunkSize
90+ if nn > len (runes ) {
91+ nn = len (runes )
92+ }
93+ chunks = append (chunks , string (runes [i :nn ]))
94+ }
95+ return chunks
96+ }
97+
5298func (c * VNCWebSocketClient ) sendFinMessage () error {
5399 b := []byte (fmt .Sprintf (`echo "%s$?"%s` , finMessage , "\n " ))
54100 bheader := []byte (fmt .Sprintf ("0:%d:" , len (b )))
0 commit comments