ghostlan

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0, MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ghostlan

ghostlan turns a single Linux machine into 1..N isolated virtual PCs ("ghosts") on a named virtual LAN—with live packet/network fault injection (tc netem), optional internet access (--nat), isolated per-ghost home/temp directories, headless virtual X11 displays (Xvfb + xdotool), and a built-in MCP server for AI agents.


Why ghostlan?

Testing distributed, P2P, media-transport, or desktop apps normally forces you into either:

  • Docker/VMs — painful for native GUI apps, GPU/audio, X11/Wayland, and fast iteration.
  • Manual ip netns bash scripts — fragile, require sudo on every command, and leak namespaces/temp dirs on failure.

ghostlan runs native host binaries inside isolated Linux network namespaces (netns) connected via a Linux bridge, with:

  • Normal unprivileged user UID inside every ghost (no root GUI warnings).
  • Isolated HOME, TMPDIR, and XDG_* directories per ghost (no /tmp lockfile collisions).
  • One-time setcap setup so both humans and LLM agents run without sudo.

Requirements & One-Time Setup

Runtime Dependencies
  • Core: Linux kernel with netns + nftables + tc netem
  • GUI mode (--gui): Xvfb, xdotool, import (ImageMagick), plus optional openbox (auto-started per display when installed for full window-manager focus/dialog support)
  • Isolated audio (--audio-null): pipewire, pipewire-pulse, pactl (each ghost gets its own XDG_RUNTIME_DIR + virtual null sink/mic, zero host audio bleed)
Quick Install (Pre-built Static Binary + setcap + MCP)
curl -fsSL https://raw.githubusercontent.com/saifmukhtar/ghostlan/main/install.sh | sh
Build & Grant Capabilities (From Source)
go build -o ghostlan ./cmd/ghostlan
./ghostlan setup --mcp

ghostlan setup --mcp installs /usr/local/bin/ghostlan (0750, owned by root:<your-group>), prompts for sudo once to apply cap_net_admin,cap_sys_admin,cap_dac_override+ep, and registers ghostlan in detected MCP clients. After this, no sudo is needed at runtime.


Quick Start

# Create a named virtual LAN "openavx-test" with 2 ghosts (tx, rx),
# headless X11 displays (--gui), and 20ms latency + 5% packet loss defaults:
./ghostlan up openavx-test tx rx --gui --delay 20 --loss 5

# List active LANs
./ghostlan ls

# Check IPs, displays, link state, netem rules, and stats
./ghostlan status openavx-test --json

# Run a one-shot command inside tx
./ghostlan exec openavx-test tx -- ping -c 2 10.99.x.12

# Start a background server in rx and read its logs
./ghostlan start openavx-test rx -- python3 -m http.server 8080
./ghostlan logs openavx-test rx --tail 20

# Inject or clear live network faults
./ghostlan netem set openavx-test tx --loss 15 --delay 80 --jitter 10 --rate 2000
./ghostlan netem clear openavx-test tx

# Simulate unplugging/replugging an Ethernet cable
./ghostlan link down openavx-test rx
./ghostlan link up openavx-test rx

# Tear down everything cleanly (processes, Xvfb, netns, bridge, temp dirs)
./ghostlan down openavx-test

Shorthand: Any ghost-targeted command accepts either <lan> <ghost> or <lan>/<ghost> (e.g., ghostlan exec openavx-test/tx -- ...).


Command Reference

LAN Lifecycle
Command What it does
ghostlan up <lan> [count | g1 g2 ...] Create a named LAN (defaults to ghost1, ghost2). Flags: --nat, --uplink, --gui, --audio-null, --private, --dir <path>, --screen WxH, --loss, --delay, --jitter, --corrupt, --duplicate, --rate.
ghostlan ls [--json] List all active virtual LANs.
ghostlan status <lan> [--json] Show ghost IPs, virtual displays, link state, active netem, live interface rx/tx/drop counters, and running process states.
ghostlan down <lan> Stop all tracked processes and Xvfb displays, delete namespaces/veths/bridge/nftables rules, and remove /tmp/ghostlan/<lan>.
Process & Command Execution
Command What it does
ghostlan exec <lan> <ghost> -- <cmd...> Run a one-shot command inside the ghost's netns and isolated env, returning its exit code.
ghostlan start <lan> <ghost> -- <cmd...> [--json] Launch a detached background process inside the ghost; returns proc_id (e.g., proc-1), PID, and log path.
ghostlan stop <lan> <ghost> <proc-id> Terminate a tracked background process (SIGTERM, then SIGKILL).
ghostlan logs <lan> <ghost> [--proc id] [--tail N] [--since line] [--filter text] Read trailing or filtered stdout/stderr log lines of a background process.
ghostlan wait-log <lan> <ghost> <pattern> [--proc id] [--timeout 10s] Poll a ghost's process log until a line matches <pattern>.
Network Fault Injection
Command What it does
ghostlan netem set <lan> <ghost> [flags] Apply live tc netem on the ghost's eth0 (--loss, --delay, --jitter, --corrupt, --duplicate, --rate).
ghostlan netem clear <lan> <ghost> Remove active netem impairments from the ghost.
ghostlan link up <lan> <ghost> Bring the ghost's eth0 interface up.
ghostlan link down <lan> <ghost> Bring the ghost's eth0 interface down (100% disconnect without killing processes).
Virtual X11 Display (--gui)
Command What it does
ghostlan gui windows <lan> <ghost> [--json] List visible X11 windows, PIDs, geometries, and titles inside the ghost's Xvfb.
ghostlan gui wait-window <lan> <ghost> <title> [--timeout 10s] Poll until a window matching <title> appears.
ghostlan gui screenshot <lan> <ghost> [out.png] [--window id|title] Capture the ghost's virtual screen (or a specific window) to a PNG file.
ghostlan gui click <lan> <ghost> <x> <y> [--button 1] [--double] [--window id|title] Click at coordinates (screen-relative or window-relative).
ghostlan gui type <lan> <ghost> <text> [--window id|title] Type text into the ghost's display or target window.
ghostlan gui key <lan> <ghost> <key> [--window id|title] Send a key or combo (e.g., Return, ctrl+s, Escape).

MCP Server (For AI Agents)

ghostlan includes a built-in Model Context Protocol (MCP) server exposing all operations as structured tools.

Stdio Transport (Default — Auto-Started by Any MCP Client)

Add to your MCP client config (Claude Desktop, Claude Code, Cursor, Antigravity, Zed, etc.):

{
  "mcpServers": {
    "ghostlan": {
      "command": "/home/saif/ghostlan/ghostlan",
      "args": ["mcp"]
    }
  }
}
Streamable HTTP Transport (Optional)
./ghostlan mcp --http 127.0.0.1:8999

(Always bind to 127.0.0.1, never 0.0.0.0, since ghostlan holds network capabilities.)

Available MCP Tools (18)
  • LAN: ghostlan_ls, ghostlan_up, ghostlan_status, ghostlan_down
  • Processes: ghostlan_exec, ghostlan_start, ghostlan_stop, ghostlan_logs, ghostlan_wait_log
  • Network: ghostlan_netem_set, ghostlan_netem_clear, ghostlan_link
  • GUI: ghostlan_gui_windows, ghostlan_gui_wait_window, ghostlan_gui_screenshot (returns native PNG ImageContent + path, optional out_path), ghostlan_gui_click, ghostlan_gui_type, ghostlan_gui_key

Limitations, Quirks & Workarounds (For Agents)

  • No virtual webcam (/dev/video*): --gui provides an isolated X11 display (Xvfb) and --audio-null provides isolated PipeWire virtual audio sinks (ghostlan_sink, ghostlan_mic), not a V4L2 camera device. For synthetic video, use OBS color_source/xshm_input or ffmpeg -f lavfi -i testsrc.
  • Window Manager in --gui: If openbox or fluxbox is installed, ghostlan auto-starts it inside each ghost's Xvfb. If neither is installed, _NET_ACTIVE_WINDOW is unavailable and ghostlan falls back to xdotool windowfocus; if Escape/dialog shortcuts don't work on a popup, take a screenshot and click the dialog button directly.
  • No wait_ms on start/exec: Use ghostlan_wait_log (or ghostlan wait-log) and ghostlan_gui_wait_window instead of fixed sleep calls.
  • Raw socket tools (tcpdump, etc.): Child processes run with PR_SET_NO_NEW_PRIVS as your unprivileged user, so cap_net_raw binaries like tcpdump won't capture inside a ghost. Unprivileged ICMP ping does work (via per-netns ping_group_range), and ghostlan status reports live interface rx_bytes/tx_bytes/rx_dropped.
  • netem units & direction: --delay and --jitter take integer milliseconds (e.g. --delay 50, not 50ms), and netem applies to egress on that ghost's eth0.

FAQ

1. Why setcap instead of sudo?
So AI agents and scripts can run ghostlan without password prompts, while apps launched inside ghosts still run as your normal unprivileged user ($UID)—not root.

2. Do --gui windows appear on my real desktop?
No. Each ghost gets its own headless Xvfb display (:99, :100, …) with WAYLAND_DISPLAY unset and X11 backends forced (QT_QPA_PLATFORM=xcb, GDK_BACKEND=x11, SDL_VIDEODRIVER=x11). Your real mouse, keyboard, and desktop are never touched. Use ghostlan gui screenshot (or ghostlan_gui_screenshot in MCP) to inspect the screen.

3. Does --gui work if my Linux desktop uses Wayland?
Yes. Xvfb is a standalone virtual X11 server, so xdotool and import work identically whether your host runs Wayland, X11, or a headless SSH session.

4. Can multiple virtual LANs run at the same time?
Yes. Each named LAN gets its own bridge (glbr-<hash>), unique veth names, non-overlapping /24 subnet (10.99.<n>.0/24), and isolated /tmp/ghostlan/<lan>/ state directory.

5. How does --nat work?
--nat detects your default IPv4 uplink (or uses --uplink <iface>), configures a dedicated ghostlan_<lan> nftables masquerade/forward table, adds a default route inside each ghost, and bind-mounts a per-netns /etc/resolv.conf (1.1.1.1, 8.8.8.8) when processes run.

6. Where do apps store configs inside a ghost?
Each ghost isolates HOME, XDG_CONFIG_HOME (<home_dir>/config), XDG_DATA_HOME (<home_dir>/data), and TMPDIR (<home_dir>/tmp). Check xdg_config_home in ghostlan_up / ghostlan_status rather than writing to <home_dir>/.config.

7. Storage location choices (Default vs --private vs --dir):

  • Default (/tmp/ghostlan/<lan>): Fast tmpfs (RAM-backed) with maximum temp space—best for normal single-user dev testing.
  • --private ($XDG_RUNTIME_DIR/ghostlan/<lan>, 0700): Also tmpfs (RAM-backed), locked to your user (0700)—best on shared/multi-user hosts or when handling sensitive tokens/configs.
  • --dir <path>: Stores ghost directories under a custom path (e.g., SSD/disk)—best if a test writes multi-GB recordings or artifacts that won't fit in tmpfs.
  • Note for AI Agents / LLMs: Before calling ghostlan_up for a new test environment, ask the user which storage mode they prefer if they haven't specified one (default /tmp for normal speed/space, --private for strict isolation, or --dir <path> for large files).

License

Dual-licensed under either of:

at your option.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Readme string

Readme contains the embedded README.md documentation for ghostlan.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
ghostlan command
internal
gui
mcp
net

Jump to

Keyboard shortcuts

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