feat: add cross-platform support via platform detection module

Introduces src/platform.rs with OS detection and env var overrides.
All hardcoded Linux paths replaced with Platform::detect() across
8 source files. Key changes:

- New Platform struct with 11 fields (all overridable via env vars)
- /proc/ access gated to Linux (#[cfg(target_os = "linux")])
- pgrep/pkill patterns broadened for cross-platform LS discovery
- sec-ch-ua-platform header now dynamic per OS
- Token, traces, config, CA cert paths use platform module
- LD_PRELOAD DNS redirect gated to Linux only
- Setup scripts for Linux (systemd) and macOS (launchd)
- find_ls_binary_path has cross-platform stubs

All 46 tests pass, cargo check clean.
This commit is contained in:
Nikketryhard
2026-02-18 02:13:23 -06:00
parent 7136c0e53c
commit 8a9662edea
10 changed files with 587 additions and 108 deletions

70
scripts/setup-linux.sh Executable file
View File

@@ -0,0 +1,70 @@
#!/usr/bin/env bash
# ZeroGravity — Linux setup
# Creates the zerogravity-ls system user for UID-scoped iptables isolation,
# installs the systemd user service, and builds the dns_redirect.so preload lib.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
# ── 1. System user for UID isolation ──
echo "→ Creating zerogravity-ls system user…"
if id -u zerogravity-ls &>/dev/null; then
echo " Already exists."
else
sudo useradd --system --no-create-home --shell /usr/sbin/nologin zerogravity-ls
echo " Created."
fi
# ── 2. Sudoers rule (run commands as zerogravity-ls without password) ──
SUDOERS="/etc/sudoers.d/zerogravity"
echo "→ Installing sudoers rule…"
if [ -f "$SUDOERS" ]; then
echo " Already exists."
else
echo "$USER ALL=(zerogravity-ls) NOPASSWD: ALL" | sudo tee "$SUDOERS" > /dev/null
sudo chmod 0440 "$SUDOERS"
echo " Installed: $SUDOERS"
fi
# ── 3. Data directory permissions ──
echo "→ Setting up /tmp/zerogravity-standalone…"
sudo mkdir -p /tmp/zerogravity-standalone
sudo chmod 1777 /tmp/zerogravity-standalone
# ── 4. Config directory ──
echo "→ Setting up ~/.config/zerogravity…"
mkdir -p "$HOME/.config/zerogravity"
# ── 5. Systemd user service ──
echo "→ Installing systemd user service…"
UNIT_DIR="$HOME/.config/systemd/user"
mkdir -p "$UNIT_DIR"
cat > "$UNIT_DIR/zerogravity.service" << EOF
[Unit]
Description=ZeroGravity Proxy
After=network.target
[Service]
Type=simple
ExecStart=$PROJECT_DIR/target/release/zerogravity
WorkingDirectory=$PROJECT_DIR
Environment=RUST_LOG=info
Restart=on-failure
RestartSec=3
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
echo " Installed: $UNIT_DIR/zerogravity.service"
echo " Enable with: systemctl --user enable zerogravity"
# ── 6. Build ──
echo "→ Building release binary…"
cd "$PROJECT_DIR"
cargo build --release 2>&1 | tail -1
echo ""
echo "✓ Setup complete. Start with: zg start"

65
scripts/setup-macos.sh Executable file
View File

@@ -0,0 +1,65 @@
#!/usr/bin/env bash
# ZeroGravity — macOS setup
# Installs a launchd plist for automatic startup and sets up config directories.
# No UID isolation on macOS — runs in headless/HTTPS_PROXY mode only.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
CONFIG_DIR="$HOME/Library/Application Support/zerogravity"
# ── 1. Config directory ──
echo "→ Setting up config directory…"
mkdir -p "$CONFIG_DIR"
# ── 2. Data directory ──
echo "→ Setting up /tmp/zerogravity-standalone…"
mkdir -p /tmp/zerogravity-standalone
# ── 3. Launchd plist ──
echo "→ Installing launchd plist…"
PLIST_DIR="$HOME/Library/LaunchAgents"
PLIST="$PLIST_DIR/com.zerogravity.proxy.plist"
mkdir -p "$PLIST_DIR"
cat > "$PLIST" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.zerogravity.proxy</string>
<key>ProgramArguments</key>
<array>
<string>$PROJECT_DIR/target/release/zerogravity</string>
</array>
<key>WorkingDirectory</key>
<string>$PROJECT_DIR</string>
<key>EnvironmentVariables</key>
<dict>
<key>RUST_LOG</key>
<string>info</string>
</dict>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>StandardOutPath</key>
<string>$HOME/Library/Logs/zerogravity.log</string>
<key>StandardErrorPath</key>
<string>$HOME/Library/Logs/zerogravity.log</string>
</dict>
</plist>
EOF
echo " Installed: $PLIST"
echo " Start with: launchctl load $PLIST"
echo " Stop with: launchctl unload $PLIST"
# ── 4. Build ──
echo "→ Building release binary…"
cd "$PROJECT_DIR"
cargo build --release 2>&1 | tail -1
echo ""
echo "✓ Setup complete."
echo " Start with: launchctl load $PLIST"
echo " Or manually: zg start"