|
1 | 1 | # Copilot Instructions for Dev Proxy |
2 | 2 |
|
3 | 3 | ## Overview |
4 | | -Dev Proxy is a cross-platform .NET API simulator and proxy for testing how applications handle API failures, rate limits, latency, and more. It is highly extensible via a plugin architecture and is configured using JSON files. The project is organized as a multi-project .NET solution. |
5 | | - |
6 | | -**Key technologies:** |
7 | | -- Uses a fork of Titanium.Web.Proxy: [svrooij/unobtanium-web-proxy](https://github.com/svrooij/unobtanium-web-proxy) |
8 | | -- CLI built with System.CommandLine |
9 | | -- .NET Dependency Injection is used throughout |
10 | | - |
11 | | -## Architecture |
12 | | -- **Main entry point:** `DevProxy/Program.cs` sets up the web host, loads configuration, logging, and plugins, and starts the proxy engine. |
13 | | -- **Proxy Engine:** `DevProxy/Proxy/ProxyEngine.cs` manages network interception, request/response handling, plugin invocation, and system proxy integration. |
14 | | -- **Plugins:** Implement the `IPlugin` interface (`DevProxy.Abstractions/Plugins/IPlugin.cs`). For most plugins, inherit from `BasePlugin` or `BaseReportingPlugin` in `DevProxy.Abstractions/Plugins/`. Plugins are loaded dynamically based on configuration in `devproxyrc.json` or similar config files. Plugins can: |
15 | | - - Intercept and modify requests/responses |
16 | | - - Add custom commands/options to the CLI (via System.CommandLine) |
17 | | - - React to events (recording, logging, etc.) |
18 | | -- **Configuration:** |
19 | | - - Main config: `DevProxy/config/devproxyrc.json` (or other JSON files in `config/`) |
20 | | - - Plugins and URLs to watch are defined here. See `m365.json` for examples. |
21 | | -- **Commands:** |
22 | | - - CLI commands are defined in `DevProxy/Commands/` and registered via `DevProxyCommand.cs` (using System.CommandLine). |
23 | | - - Certificate management: `cert ensure` and `cert remove` (see `CertCommand.cs`). |
24 | | -- **Cross-platform support:** |
25 | | - - macOS-specific scripts: `trust-cert.sh`, `remove-cert.sh`, `toggle-proxy.sh` in `DevProxy/`. |
26 | | - |
27 | | -## Dev Proxy MCP Server |
28 | | -- The Dev Proxy MCP server exposes access to Dev Proxy documentation and JSON schemas, making it invaluable for both contributors and users. |
29 | | -- Use the MCP server to programmatically retrieve up-to-date docs and schema definitions for config and plugins. |
30 | | -- See: [@devproxy/mcp on npm](https://www.npmjs.com/package/@devproxy/mcp) |
31 | | - |
32 | | -## Developer Workflows |
33 | | -- **Build:** Use the VS Code task `build` or run `dotnet build DevProxy.sln`. |
34 | | -- **Run:** Use the VS Code task `watch` or run `dotnet watch run --project DevProxy.sln`. |
35 | | -- **Publish:** Use the VS Code task `publish` or run `dotnet publish DevProxy.sln`. |
36 | | -- **Debug:** Attach to the running process or use `dotnet watch run` for hot reload. |
37 | | -- **Test:** (No test project detected; add tests in `tests/` if needed.) |
38 | | - |
39 | | -## Project Conventions |
40 | | -- **Plugin loading:** Plugins must be registered in the config file and implement `IPlugin` (preferably via `BasePlugin` or `BaseReportingPlugin`). Use the `PluginServiceExtensions` for registration logic. |
41 | | -- **URL matching:** URLs to watch are defined as wildcards in config and converted to regexes at runtime. |
42 | | -- **Logging:** Uses Microsoft.Extensions.Logging. Log levels and output are configurable. |
43 | | -- **Configuration tokens:** Paths in config can use `~appFolder` for resolution. |
44 | | -- **Hotkeys:** When running interactively, hotkeys are available (see console output for details). |
45 | | -- **Schema validation:** Config files reference a `$schema` for validation and versioning. |
46 | | -- **CLI:** All CLI commands and options are built using System.CommandLine. |
47 | | -- **Dependency Injection:** All services, plugins, and commands are registered and resolved via .NET DI. |
48 | | - |
49 | | -## Key Files & Directories |
50 | | -- `DevProxy/Program.cs` — Main entry |
51 | | -- `DevProxy/Proxy/ProxyEngine.cs` — Core proxy logic |
52 | | -- `DevProxy/Commands/` — CLI commands (System.CommandLine) |
53 | | -- `DevProxy/Plugins/` — Plugin loader and helpers |
54 | | -- `DevProxy.Abstractions/Plugins/IPlugin.cs` — Plugin interface |
55 | | -- `DevProxy.Abstractions/Plugins/BasePlugin.cs`, `BaseReportingPlugin.cs` — Plugin base classes |
56 | | -- `DevProxy/config/` — Example and default configs |
57 | | -- `media/` — Branding assets |
58 | | -- `scripts/` — Local build/setup scripts |
59 | | - |
60 | | -## External Dependencies |
61 | | -- [svrooij/unobtanium-web-proxy](https://github.com/svrooij/unobtanium-web-proxy) — Core proxy engine (fork of Titanium.Web.Proxy) |
62 | | -- Microsoft.Extensions.* — Logging, configuration, DI |
63 | | -- System.CommandLine — CLI framework |
64 | | - |
65 | | -## Examples |
66 | | -- To add a new plugin, inherit from `BasePlugin` or `BaseReportingPlugin`, implement `IPlugin`, and register it in the config file under `plugins`. |
67 | | -- To add a new CLI command, implement a `Command` in `DevProxy/Commands/` and register it in `DevProxyCommand.cs`. |
| 4 | + |
| 5 | +Dev Proxy is a cross-platform .NET API simulator and proxy for testing how applications handle API failures, rate limits, latency, and more. |
| 6 | + |
| 7 | +## Critical (Non-Obvious) |
| 8 | + |
| 9 | +- **Proxy engine:** Uses [svrooij/unobtanium-web-proxy](https://github.com/svrooij/unobtanium-web-proxy) (fork of Titanium.Web.Proxy) |
| 10 | +- **Config tokens:** Paths in config files can use `~appFolder` for resolution |
| 11 | +- **MCP Server:** Use [@devproxy/mcp](https://www.npmjs.com/package/@devproxy/mcp) to programmatically retrieve up-to-date docs and JSON schemas |
| 12 | + |
| 13 | +## Best Practices |
| 14 | + |
| 15 | +See the comprehensive best practices guide: https://raw.githubusercontent.com/dev-proxy-tools/mcp/refs/heads/main/assets/best-practices.md |
| 16 | + |
| 17 | +Key points: |
| 18 | +- Store Dev Proxy files in `.devproxy` folder in the workspace |
| 19 | +- Schema version in `$schema` must match installed Dev Proxy version |
| 20 | +- In config files: `plugins` first, then `urlsToWatch`, then plugin configs |
| 21 | +- Plugin order matters: latency plugins first, response simulators before reporters, reporters last |
| 22 | +- URL matching is order-dependent: most specific URLs first, use `!` prefix to exclude |
| 23 | +- File paths are relative to the config file where they're defined |
| 24 | +- Hot reload (v2.1.0+): config changes are detected and the proxy restarts automatically to apply them |
| 25 | + |
| 26 | +## Plugin Development |
| 27 | + |
| 28 | +To create a new plugin: |
| 29 | +1. Implement the `IPlugin` interface (`DevProxy.Abstractions/Plugins/IPlugin.cs`) either directly or by inheriting from `BasePlugin` / `BaseReportingPlugin`. Optionally implement `IStdioPlugin` for stdio-based communication. |
| 30 | +2. Implement it as a **public class** in a class library project. |
| 31 | +3. Implement the required interface members and override any relevant base class methods/events for your use case. |
| 32 | + |
| 33 | +## Commits & Releases |
| 34 | + |
| 35 | +- **Commit messages:** Clear, succinct, and reference the issue they close when applicable |
| 36 | +- **Versioning:** We follow semver. Use the `upgrade-devproxy-version` skill for version bumps |
| 37 | +- **Release:** After release builds, homebrew and winget are updated manually |
| 38 | + |
| 39 | +## Testing Code Changes |
| 40 | + |
| 41 | +When testing code, features, or changes, **always launch the proxy in detached mode** with these settings to avoid conflicts: |
| 42 | + |
| 43 | +```bash |
| 44 | +dotnet run --project DevProxy -- --detach --as-system-proxy false --port 0 --api-port 0 |
| 45 | +``` |
| 46 | + |
| 47 | +- `--as-system-proxy false` — prevents modifying system-wide network settings |
| 48 | +- `--port 0` — lets the OS assign a random available port for the proxy |
| 49 | +- `--api-port 0` — lets the OS assign a random available port for the API |
| 50 | + |
| 51 | +**Auth for M365/Azure:** No automated auth. You can try `m365 util accesstoken get` or `az account get-access-token` (use `-h` for options) but the user may not be signed in. |
| 52 | + |
| 53 | +**Cleanup:** After testing, stop the proxy using `devproxy stop` (preferred, cross-platform). If you started it with `dotnet run`, you can also stop it with `Ctrl+C` in that terminal; as a last resort, terminate the process using OS-specific tools (for example, `kill <PID>` on Unix-like systems or Task Manager on Windows). Clean up any generated log files (`devproxy-*.log`). |
| 54 | + |
| 55 | +## Reference (Architecture) |
| 56 | + |
| 57 | +- **Main entry point:** `DevProxy/Program.cs` — sets up web host, configuration, logging, plugins, and starts proxy engine |
| 58 | +- **Proxy Engine:** `DevProxy/Proxy/ProxyEngine.cs` — network interception, request/response handling, plugin invocation |
| 59 | +- **Plugins:** Implement `IPlugin` interface (`DevProxy.Abstractions/Plugins/IPlugin.cs`). Base classes: `BasePlugin`, `BaseReportingPlugin` in `DevProxy.Abstractions/Plugins/` |
| 60 | +- **CLI Commands:** `DevProxy/Commands/` — built with System.CommandLine, registered via `DevProxyCommand.cs` |
| 61 | +- **Configuration:** `DevProxy/config/devproxyrc.json` and other JSON files in `config/` |
| 62 | +- **macOS scripts:** `trust-cert.sh`, `remove-cert.sh`, `toggle-proxy.sh` in `DevProxy/` |
68 | 63 |
|
69 | 64 | ## Resources |
| 65 | + |
70 | 66 | - [Official documentation](https://aka.ms/devproxy) |
71 | 67 | - [YouTube channel](https://youtube.com/@devproxy) |
72 | | - |
73 | | ---- |
74 | | - |
75 | | -*Update this file as project conventions evolve. For questions, see the README or open an issue.* |
0 commit comments