Documentation
¶
Overview ¶
Package vps connects to a user's server over SSH and reads its state on demand — no agent installed on the box, just standard commands whose output is parsed into metrics. It also runs arbitrary commands and transfers files for the VPS-manager tools.
Index ¶
- Constants
- Variables
- func Download(ctx context.Context, t Target, remotePath, localPath string) (n int64, seen string, err error)
- func NormalizeTunnelHost(s string) string
- func Ping(ctx context.Context, t Target) (who string, seenHostKey string, err error)
- func Run(ctx context.Context, t Target, command string) (string, string, error)
- func Upload(ctx context.Context, t Target, localPath, remotePath string) (n int64, seen string, err error)
- type Metrics
- type Process
- type Target
Constants ¶
const MaxTransferBytes = 256 << 20
MaxTransferBytes caps a single upload or download so a runaway path cannot exhaust memory or fill the workspace. 256 MiB is enough for configs, modest builds, and logs; larger payloads should use a remote pull (curl/wget) or rsync via vps_run.
const TunnelCloudflared = "cloudflared"
TunnelCloudflared is the Target.Tunnel value for hosts whose SSH port is only reachable through a Cloudflare Tunnel. Empty means direct SSH.
Variables ¶
var ErrAuthFailed = errors.New("the server rejected the login")
ErrAuthFailed is returned when the server rejects the login. x/crypto/ssh reports this only as text ("ssh: unable to authenticate ..."), so dial recognises it by that phrase and rewords it with the username tried.
var ErrHostKeyChanged = errors.New("host key changed since it was first trusted — possible MITM, or the server was rebuilt; remove and re-add it if you trust the change")
ErrHostKeyChanged is returned when a host presents a key different from the pinned one — a possible man-in-the-middle, or a legitimately rebuilt server.
var ErrTimeout = errors.New("vps operation timed out")
ErrTimeout is returned when a remote command or transfer exceeds its deadline. The error text includes the duration; partial output may accompany it from Run.
var ErrTunnelRefused = errors.New("cloudflare refused the tunnel connection")
ErrTunnelRefused is returned when Cloudflare rejects the WebSocket upgrade.
Functions ¶
func Download ¶ added in v0.4.0
func Download(ctx context.Context, t Target, remotePath, localPath string) (n int64, seen string, err error)
Download copies remotePath from the VPS into localPath over SFTP. Parent directories on the local side are created as needed. Returns bytes written and the host key seen.
func NormalizeTunnelHost ¶ added in v0.8.0
NormalizeTunnelHost reduces a pasted URL or hostname to a bare host: scheme, path and userinfo are dropped; an explicit :port is kept.
func Ping ¶
Ping confirms a host is reachable and authenticates, returning the remote user@hostname it landed on (proof it really connected) plus the host key seen.
func Run ¶
Run opens a connection, runs one command, and returns its combined output plus the host key the server presented (for TOFU pinning). A non-zero exit is returned with the output so the caller sees stderr rather than a bare error.
func Upload ¶ added in v0.4.0
func Upload(ctx context.Context, t Target, localPath, remotePath string) (n int64, seen string, err error)
Upload copies a local file to remotePath on the VPS over SFTP. Parent directories on the remote side are created as needed. Returns bytes written and the host key seen (for TOFU pinning).
Types ¶
type Metrics ¶
type Metrics struct {
Reachable bool `json:"reachable"`
Error string `json:"error,omitempty"`
Hostname string `json:"hostname"`
OS string `json:"os"`
Kernel string `json:"kernel"`
Uptime string `json:"uptime"`
CPUCores int `json:"cpu_cores"`
CPUPercent float64 `json:"cpu_percent"` // 0..100, from load1/cores as a rough gauge
Load1 float64 `json:"load1"`
Load5 float64 `json:"load5"`
Load15 float64 `json:"load15"`
MemTotalMB int `json:"mem_total_mb"`
MemUsedMB int `json:"mem_used_mb"`
MemPercent float64 `json:"mem_percent"`
SwapTotalMB int `json:"swap_total_mb"`
SwapUsedMB int `json:"swap_used_mb"`
SwapPercent float64 `json:"swap_percent"`
DiskTotalGB float64 `json:"disk_total_gb"`
DiskUsedGB float64 `json:"disk_used_gb"`
DiskPercent float64 `json:"disk_percent"`
Processes int `json:"processes"`
TopProc []string `json:"top_proc"` // "pid cpu% mem% command", a few rows
}
Metrics is a point-in-time snapshot of a host, read on demand.
type Process ¶
type Process struct {
PID int `json:"pid"`
User string `json:"user"`
CPU float64 `json:"cpu"`
Mem float64 `json:"mem"`
Command string `json:"command"`
}
Process is one row from the remote process table.
type Target ¶
type Target struct {
Host string
Port int
Username string
AuthMethod string // password|key
Password string
PrivateKey string
Passphrase string
// KnownHostKey is the server's pinned SSH public key (authorized_keys format,
// e.g. "ssh-ed25519 AAAA..."). Empty means first use: any key is accepted and
// returned via the connection's SeenHostKey for the caller to pin. Non-empty
// means the presented key MUST match, or the dial fails — this is what stops
// a MITM from harvesting the credentials.
KnownHostKey string
// Tunnel selects the transport: "" is direct SSH, TunnelCloudflared reaches
// Host through a Cloudflare Tunnel (Port is then ignored). CFClientID and
// CFClientSecret are an optional Cloudflare Access service token.
Tunnel string
CFClientID string
CFClientSecret string
}
Target is everything needed to dial one host.