feat(proxy): add persistent proxy server with start/stop/env/status commands

Adds pmg proxy command group for running a long-lived MITM proxy that
intercepts all package manager traffic without requiring PMG shims or
wrappers. Targets CI/CD pipelines where env vars can be set globally.

- pmg proxy start: starts proxy with npm+pypi interceptors, writes
  state file (pid/addr/ca-cert-path), auto-blocks suspicious packages
- pmg proxy stop: sends SIGTERM to the running proxy
- pmg proxy env: emits HTTP_PROXY/HTTPS_PROXY/SSL_CERT_FILE etc. as
  shell exports, or writes directly to $GITHUB_ENV with --gha
- pmg proxy status: shows running/stopped status

GHA usage:
  pmg proxy start &
  pmg proxy env --gha   # populates env for all subsequent steps
  npm install           # intercepted automatically, no wrapper needed

Also adds .github/workflows/persistent-proxy-e2e.yml to validate the
persistent proxy mode end-to-end in CI.
This commit is contained in:
Sahilb315
2026-06-23 15:36:41 +05:30
parent 5bd756e3fb
commit 39debb474d
7 changed files with 432 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
package proxy
import (
"fmt"
"os"
"syscall"
"github.com/safedep/pmg/config"
"github.com/safedep/pmg/internal/proxystate"
"github.com/spf13/cobra"
)
func newStopCommand() *cobra.Command {
return &cobra.Command{
Use: "stop",
Short: "Stop the running persistent PMG proxy server",
RunE: runStop,
}
}
func runStop(_ *cobra.Command, _ []string) error {
cfg := config.Get()
statePath := proxystate.StatePath(cfg.ConfigDir())
state, err := proxystate.Read(statePath)
if err != nil {
return fmt.Errorf("no proxy state found — is the proxy running? (%w)", err)
}
if !state.IsRunning() {
_ = proxystate.Remove(statePath)
return fmt.Errorf("proxy process (pid %d) is not running; state file cleaned up", state.PID)
}
proc, err := os.FindProcess(state.PID)
if err != nil {
return fmt.Errorf("find proxy process (pid %d): %w", state.PID, err)
}
if err := proc.Signal(syscall.SIGTERM); err != nil {
return fmt.Errorf("send SIGTERM to proxy (pid %d): %w", state.PID, err)
}
if _, err := fmt.Fprintf(os.Stdout, "Sent SIGTERM to PMG proxy (pid %d, addr %s)\n", state.PID, state.Addr); err != nil {
return fmt.Errorf("write stop message: %w", err)
}
return nil
}