|
| 1 | +package proxmox |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | + |
| 12 | + "github.com/gorilla/websocket" |
| 13 | + "github.com/sp-yduck/proxmox-go/api" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + finMessage = "done with status: " |
| 18 | + finMessageFormat = finMessage + `[0-9]+` |
| 19 | +) |
| 20 | + |
| 21 | +type VNCWebSocketClient struct { |
| 22 | + conn *websocket.Conn |
| 23 | +} |
| 24 | + |
| 25 | +func (s *Service) NewNodeVNCWebSocketConnection(ctx context.Context, nodeName string) (*VNCWebSocketClient, error) { |
| 26 | + termProxy, err := s.restclient.CreateNodeTermProxy(ctx, nodeName, api.TermProxyOption{}) |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + conn, err := s.restclient.DialNodeVNCWebSocket(ctx, nodeName, *termProxy) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + |
| 35 | + return &VNCWebSocketClient{conn: conn}, nil |
| 36 | +} |
| 37 | + |
| 38 | +func (c *VNCWebSocketClient) Close() { |
| 39 | + c.conn.Close() |
| 40 | +} |
| 41 | + |
| 42 | +func (c *VNCWebSocketClient) Write(cmd string) error { |
| 43 | + b := []byte(fmt.Sprintf("%s\n", cmd)) |
| 44 | + bheader := []byte(fmt.Sprintf("0:%d:", len(b))) |
| 45 | + bmsg := append(bheader, b...) |
| 46 | + if err := c.conn.WriteMessage(websocket.BinaryMessage, bmsg); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + return c.sendFinMessage() |
| 50 | +} |
| 51 | + |
| 52 | +func (c *VNCWebSocketClient) sendFinMessage() error { |
| 53 | + b := []byte(fmt.Sprintf(`echo "%s$?"%s`, finMessage, "\n")) |
| 54 | + bheader := []byte(fmt.Sprintf("0:%d:", len(b))) |
| 55 | + bmsg := append(bheader, b...) |
| 56 | + if err := c.conn.WriteMessage(websocket.BinaryMessage, bmsg); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +// Read() reads message until find fin message |
| 63 | +// then returns whole message and status code |
| 64 | +func (c *VNCWebSocketClient) Read(ctx context.Context) (outputs string, code int, err error) { |
| 65 | + done := make(chan error, 1) |
| 66 | + go func() { |
| 67 | + defer close(done) |
| 68 | + for { |
| 69 | + _, msg, err := c.conn.ReadMessage() |
| 70 | + if err != nil { |
| 71 | + done <- err |
| 72 | + return |
| 73 | + } |
| 74 | + outputs += string(msg) |
| 75 | + finMsg := parseFinMessage(string(msg)) |
| 76 | + if finMsg != "" { |
| 77 | + code, err = parseStatusFromFinMessage(finMsg) |
| 78 | + done <- err |
| 79 | + return |
| 80 | + } |
| 81 | + } |
| 82 | + }() |
| 83 | + select { |
| 84 | + case err = <-done: |
| 85 | + return outputs, code, err |
| 86 | + case <-ctx.Done(): |
| 87 | + return outputs, -1, errors.New("context deadline exceeded") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Exec executes a command and return error if code is not 0 |
| 92 | +// usually out contains many extra messages that is just useless |
| 93 | +func (c *VNCWebSocketClient) Exec(ctx context.Context, cmd string) (out string, code int, err error) { |
| 94 | + if err := c.Write(cmd); err != nil { |
| 95 | + return "", 0, err |
| 96 | + } |
| 97 | + out, code, err = c.Read(ctx) |
| 98 | + if err != nil { |
| 99 | + return out, code, err |
| 100 | + } |
| 101 | + if code != 0 { |
| 102 | + return out, code, errors.Errorf("exit with non zero code: %d", code) |
| 103 | + } |
| 104 | + return out, 0, nil |
| 105 | +} |
| 106 | + |
| 107 | +func parseFinMessage(message string) string { |
| 108 | + re := regexp.MustCompile(finMessageFormat) |
| 109 | + return re.FindString(message) |
| 110 | +} |
| 111 | + |
| 112 | +func parseStatusFromFinMessage(message string) (int, error) { |
| 113 | + re := regexp.MustCompile(finMessageFormat) |
| 114 | + match := re.FindString(message) |
| 115 | + if match == "" { |
| 116 | + return 0, errors.Errorf("failed to find status code from %s", message) |
| 117 | + } |
| 118 | + statusCode := strings.Split(match, ": ")[1] |
| 119 | + return strconv.Atoi(statusCode) |
| 120 | +} |
0 commit comments