protocol

package
v0.1.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package protocol defines the vsock wire protocol between host API server and in-VM agent. Messages are JSON-encoded with a 4-byte big-endian length prefix.

Message flow:

Host                          Guest Agent
  │                               │
  │──── PingRequest ─────────────▶│
  │◀─── PingResponse ────────────│
  │                               │
  │──── ExecRequest ─────────────▶│
  │◀─── ExecResponse ────────────│
  │                               │
  │──── FileWriteRequest ────────▶│
  │◀─── FileWriteResponse ───────│
  │                               │
  │──── FileReadRequest ─────────▶│
  │◀─── FileReadResponse ────────│

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodePayload

func DecodePayload[T any](env *Envelope) (*T, error)

DecodePayload extracts a typed payload from a raw Envelope. The Envelope.Payload is initially decoded as a json.RawMessage-like map, so we re-marshal and unmarshal into the target type.

func WriteMessage

func WriteMessage(w io.Writer, msg *Envelope) error

WriteMessage sends a length-prefixed JSON message to w.

Types

type Envelope

type Envelope struct {
	Type    MessageType `json:"type"`
	Payload any         `json:"payload"`
}

Envelope wraps every vsock message with a type discriminator.

func ReadMessage

func ReadMessage(r io.Reader) (*Envelope, error)

ReadMessage reads a length-prefixed JSON message from r.

type ErrorResponse

type ErrorResponse struct {
	Message string `json:"message"`
}

ErrorResponse is returned when the agent encounters an error.

type ExecRequest

type ExecRequest struct {
	Command []string          `json:"command"`
	Env     map[string]string `json:"env,omitempty"`
	WorkDir string            `json:"workdir,omitempty"`
	Timeout int               `json:"timeout,omitempty"` // seconds, 0 = no timeout
}

ExecRequest asks the agent to run a command.

type ExecResponse

type ExecResponse struct {
	ExitCode int    `json:"exit_code"`
	Stdout   string `json:"stdout"`
	Stderr   string `json:"stderr"`
}

ExecResponse returns the result of a command execution.

type FileReadRequest

type FileReadRequest struct {
	Path string `json:"path"`
}

FileReadRequest asks the agent to read a file from the VM.

type FileReadResponse

type FileReadResponse struct {
	Content string `json:"content"` // base64-encoded
	Size    int64  `json:"size"`
	Mode    int    `json:"mode"`
}

FileReadResponse returns the file contents.

type FileWriteRequest

type FileWriteRequest struct {
	Path    string `json:"path"`
	Content string `json:"content"` // base64-encoded for binary, raw for text
	Mode    int    `json:"mode"`    // file permissions (e.g., 0644)
	Binary  bool   `json:"binary"`  // if true, Content is base64-encoded
}

FileWriteRequest asks the agent to write a file inside the VM.

type FileWriteResponse

type FileWriteResponse struct {
	BytesWritten int `json:"bytes_written"`
}

FileWriteResponse confirms the file was written.

type MessageType

type MessageType string

MessageType identifies the kind of vsock message.

const (
	TypePingRequest       MessageType = "ping_request"
	TypePingResponse      MessageType = "ping_response"
	TypeExecRequest       MessageType = "exec_request"
	TypeExecResponse      MessageType = "exec_response"
	TypeFileWriteRequest  MessageType = "file_write_request"
	TypeFileWriteResponse MessageType = "file_write_response"
	TypeFileReadRequest   MessageType = "file_read_request"
	TypeFileReadResponse  MessageType = "file_read_response"
	TypeError             MessageType = "error"
)

type PingRequest

type PingRequest struct{}

PingRequest is a health check from host to agent.

type PingResponse

type PingResponse struct {
	Version string `json:"version"`
}

PingResponse confirms the agent is alive.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL